Exemplo n.º 1
0
def load_ool_module():
    """
    Compile an out-of-line module, return the corresponding ffi and
    module objects.
    """
    from cffi import FFI

    defs = """
    double sin(double x);
    double cos(double x);
    void vsSin(int n, float* x, float* y);
    void vdSin(int n, double* x, double* y);
    int foo(int a, int b, int c);
    """

    source = """
    static int foo(int a, int b, int c)
    {
        return a + b * c;
    }

    void vsSin(int n, float* x, float* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }
    """

    ffi = FFI()
    ffi.set_source('cffi_usecases_ool', source)
    ffi.cdef(defs, override=True)
    tmpdir = static_temp_directory('test_cffi')
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        import cffi_usecases_ool as mod
        cffi_support.register_module(mod)
        return mod.ffi, mod
    finally:
        sys.path.remove(tmpdir)
Exemplo n.º 2
0
def load_ool_module():
    """
    Compile an out-of-line module, return the corresponding ffi and
    module objects.
    """
    from cffi import FFI

    defs = """
    double sin(double x);
    double cos(double x);
    void vsSin(int n, float* x, float* y);
    void vdSin(int n, double* x, double* y);
    int foo(int a, int b, int c);
    """

    source = """
    static int foo(int a, int b, int c)
    {
        return a + b * c;
    }

    void vsSin(int n, float* x, float* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }
    """

    ffi = FFI()
    ffi.set_source('cffi_usecases_ool', source)
    ffi.cdef(defs, override=True)
    tmpdir = static_temp_directory('test_cffi')
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        import cffi_usecases_ool as mod
        cffi_support.register_module(mod)
        return mod.ffi, mod
    finally:
        sys.path.remove(tmpdir)
Exemplo n.º 3
0
import numpy as np
from numba import njit, cffi_support

import _ndtri
from _ndtri import lib, ffi

cffi_support.register_module(_ndtri)

_ndtri = lib.ndtri


@njit
def ndtri(q):
    return _ndtri(q)
Exemplo n.º 4
0
from pywigxjpf_ffi import ffi, lib
import pywigxjpf_ffi
import time

import numba
from numba import cffi_support
cffi_support.register_module(pywigxjpf_ffi)
nb_wig3jj = pywigxjpf_ffi.lib.wig3jj

lib.wig_table_init(100, 9)
lib.wig_temp_init(100)

print(lib.wig3jj(5, 6, 7, 3, 4, -7))
print(lib.wig6jj(6, 6, 6, 6, 6, 6))
print(lib.wig9jj(6, 6, 6, 7, 7, 6, 7, 7, 8))
print(lib.wig9jj(6, 6, 6, 7, 7, 6, 7, 7, 8))
print(lib.wig6jj(6, 6, 6, 6, 6, int(4 + 0.5 * 4)))


# Benchmark 1, make calls for a lot of trivially-0 symbols.
@numba.jit(nopython=True)
def benchmark(jjmax):
    sum = 0.0
    calls = 0
    for jj1 in range(0, jjmax + 1):
        for jj2 in range(0, jjmax + 1):
            for jj3 in range(0, jjmax + 1):
                for mm1 in range(-jjmax, jjmax + 1):
                    for mm2 in range(-jjmax, jjmax + 1):
                        for mm3 in range(-jjmax, jjmax + 1):
                            w = nb_wig3jj(jj1, jj2, jj3, \
Exemplo n.º 5
0
# Import our Rmath module
import _rmath_ffi

dnorm = _rmath_ffi.lib.dnorm
pnorm = _rmath_ffi.lib.pnorm

dunif = _rmath_ffi.lib.dunif
punif = _rmath_ffi.lib.punif

dgamma = _rmath_ffi.lib.dgamma
pgamma = _rmath_ffi.lib.pgamma

from numba import cffi_support

cffi_support.register_module(_rmath_ffi)

@nb.jit(nopython=True)
def pnorm_nb(x):
    y = np.empty_like(x)
    for k in range(x.shape[0]):
        y[k] = pnorm(x[k], 0.0, 1.0, 1, 0)

    return y

@nb.vectorize(nopython=True)
def pnorm_nb_vec(x):
    return pnorm(x, 0.0, 1.0, 1, 0)

x = np.random.normal(size=(100,))
Exemplo n.º 6
0
from numba import jit, prange, types, cffi_support
import numpy as np
import time
import sys
from _writer import ffi, lib
import _writer
# from ._writer import ffi, lib
# from . import _writer

cffi_support.register_module(_writer)
write = _writer.lib.write_data


@jit((types.Array(types.float32, 2, "A"), types.int32),
     nopython=True,
     nogil=True,
     parallel=True,
     fastmath=True,
     cache=False)
def activity_score(a, buffer_size):
    """Calculate activity score for column pairs. If
    
    Args:
        a (np.array[np.float32]): 2d array with data
        buffer_size (int32): size of buffer per thread. If buffer_size>a.shape[1]**2, "small data" mode is used
    
    Returns:
        if "small data" mode:
            np.array[np.float32]: 1D array with maximum interquantile range for each column pair
            np.array[np.float32]: 1D array with (1-spearman correlation) score for each column pair
        else:
Exemplo n.º 7
0
from __future__ import unicode_literals

from . import _cprob_ffi
from ._cprob_ffi.lib import *
try:
    from numba import cffi_support as _cffi_support
    _cffi_support.register_module(_cprob_ffi)
except ImportError:
    pass
Exemplo n.º 8
0
from numba import njit

import chess
import itertools
import functools

from chess.polyglot import POLYGLOT_RANDOM_ARRAY, zobrist_hash

from numba import cffi_support
from cffi import FFI

ffi = FFI()

import khash_numba._khash_ffi as khash_ffi

cffi_support.register_module(khash_ffi)

khash_init = khash_ffi.lib.khash_int2int_init
khash_get = khash_ffi.lib.khash_int2int_get
khash_set = khash_ffi.lib.khash_int2int_set
khash_destroy = khash_ffi.lib.khash_int2int_destroy


@njit
def create_index_table(ids):
    table = khash_init()
    for j in range(len(ids)):
        khash_set(table, ids[j], j)
    return table

Exemplo n.º 9
0
import numpy as np
from numba import njit, cffi_support

import _hyper
from _hyper import lib, ffi

cffi_support.register_module(_hyper)

_hyp2f1 = lib.hyp2f1
_vd_hyp2f1 = lib.vd_hyp2f1


@njit
def hyp2f1(a, b, c, x):
    """Gauss hypergeometric function 2F1.

    Parameters
    ----------
    a, b, c, x : float

    """
    if x == 1.0:
        return np.inf

    return _hyp2f1(a, b, c, x)


@njit
def vd_hyp2f1(a, b, c, x):
    """
    Gauss hypergeometric function 2F1 vectorized for float x.
Exemplo n.º 10
0
    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }
    """

    ffi_ool = FFI()
    ffi.set_source('cffi_usecases_ool', source)
    ffi.cdef(defs, override=True)
    tmpdir = static_temp_directory('test_cffi')
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        import cffi_usecases_ool
        cffi_support.register_module(cffi_usecases_ool)
        cffi_sin_ool = cffi_usecases_ool.lib.sin
        cffi_cos_ool = cffi_usecases_ool.lib.cos
        cffi_foo = cffi_usecases_ool.lib.foo
        vsSin = cffi_usecases_ool.lib.vsSin
        vdSin = cffi_usecases_ool.lib.vdSin
    finally:
        sys.path.remove(tmpdir)


def use_cffi_sin(x):
    return cffi_sin(x) * 2

def use_two_funcs(x):
    return cffi_sin(x) - cffi_cos(x)
Exemplo n.º 11
0
import numpy as np
from numba import njit, cffi_support

import _hyper
from _hyper import lib, ffi

cffi_support.register_module(_hyper)

_hyp2f1 = lib.hyp2f1
_vd_hyp2f1 = lib.vd_hyp2f1


@njit
def hyp2f1(a, b, c, x):
    """Gauss hypergeometric function 2F1.

    Parameters
    ----------
    a, b, c, x : float

    """
    if x == 1.0:
        return np.inf

    return _hyp2f1(a, b, c, x)


@njit
def vd_hyp2f1(a, b, c, x):
    """
    Gauss hypergeometric function 2F1 vectorized for float x.
Exemplo n.º 12
0
import importlib

import numba
import numpy as np
from numba import types
from numba.typed import Dict
from numba.cffi_support import register_module

loaded = importlib.import_module('_utils')
register_module(loaded)

get_seconds_since_epoch = loaded.lib.get_seconds_since_epoch
print_char_int = loaded.lib.print_char
print_float = loaded.lib.print_float

print_bar = loaded.lib.print_bar
print_info_bar = loaded.lib.print_info_bar
print_remaining = loaded.lib.print_remaining


@numba.jit(nopython=True)
def numba_ord(chr):
    if chr == '=':
        return 61
    elif chr == '.':
        return 46
    elif chr == '>':
        return 62
    elif chr == '[':
        return 91
    elif chr == ']':
Exemplo n.º 13
0
import numpy as np
from numba import cffi_support, float32, float64, vectorize

import _chi2
from _chi2 import lib, ffi

cffi_support.register_module(_chi2)
_chdtr = lib.chdtr
_chdtrc = lib.chdtrc
_chdtri = lib.chdtri

@vectorize([float64(float64, float64),
            float32(float32, float32)])
def cdf(x, df):
    return _chdtr(df, x)

@vectorize([float64(float64, float64),
            float32(float32, float32)])
def ppf(p, df):
    return _chdtri(df, 1.0-p)

@vectorize([float64(float64, float64),
            float32(float32, float32)])
def sf(p, df):
    return _chdtrc(df, p)

@vectorize([float64(float64, float64),
            float32(float32, float32)])
def isf(p, df):
    return _chdtri(df, p)
Exemplo n.º 14
0
import numpy as np
from numba import jit, cffi_support
from . import _cnufft

cffi_support.register_module(_cnufft)

nufft3d1 = _cnufft.lib.nufft3d1


@jit(nopython=True)
def njit_nufft3d1(x,
                  y,
                  z,
                  c,
                  ms,
                  mt,
                  mu,
                  df=1,
                  eps=1e-15,
                  iflag=1,
                  direct=False):
    nj = x.shape[0]
    x = np.ascontiguousarray(x)
    y = np.ascontiguousarray(y)
    z = np.ascontiguousarray(z)
    c = np.ascontiguousarray(c)
    fk = np.ascontiguousarray(np.zeros((ms * mt * mu * 2)))

    return_code = nufft3d1(np.int32(nj),
                           cffi_support.ffi.from_buffer(x.view(np.float64)),
                           cffi_support.ffi.from_buffer(y.view(np.float64)),
Exemplo n.º 15
0
from __future__ import unicode_literals

from . import _ellf_ffi
from ._ellf_ffi.lib import *

try:
    from numba import cffi_support as _cffi_support
    _cffi_support.register_module(_ellf_ffi)
except ImportError:
    pass
Exemplo n.º 16
0
from __future__ import print_function

import numpy as np
import numba as nb

from numba import cffi_support

import _rmath_ffi

cffi_support.register_module(_rmath_ffi)


pnorm = _rmath_ffi.lib.pnorm
qnorm = _rmath_ffi.lib.qnorm
runif = _rmath_ffi.lib.runif
set_seed = _rmath_ffi.lib.set_seed

set_seed(123, 456)

@nb.njit
def f():
    zlo = np.random.random()
    zhi = np.random.random()
    if zlo > 0.0:
        plo = pnorm(-zhi, 0.0, 1.0, 1, 0)
        phi = pnorm(-zlo, 0.0, 1.0, 1, 0)
    else:
        plo = pnorm(zlo, 0.0, 1.0, 1, 0)
        phi = pnorm(zhi, 0.0, 1.0, 1, 0)

    p = runif(plo, phi)
Exemplo n.º 17
0
import cffi

ffi = cffi.FFI()

import numpy as np

from numba import cffi_support

import numbaflow._tensorflow_ffi as _tensorflow_ffi



cffi_support.register_module(_tensorflow_ffi)

get_info_struct = _tensorflow_ffi.lib.create_run_info
close_info_struct = _tensorflow_ffi.lib.close_run_info
sess_run = _tensorflow_ffi.lib.sess_run


def create_eval_fn():
    info_struct = get_info_struct()

    def evaluator(pieces, masks):
        returned_pointer = sess_run(
            info_struct,
            ffi.cast("uint8_t *", ffi.from_buffer(pieces)),
            ffi.cast("uint64_t *", ffi.from_buffer(masks)),
            len(masks),
            len(pieces))

        result_buff = ffi.buffer(returned_pointer)
Exemplo n.º 18
0
"""Wrappers to the functions in the extension module built by `build_ssht_cffi.py`

The function signatures are defined in the SSHT docs.
"""
import cffi
import numba as nb
import numpy as np
from numba import cffi_support

from . import _ssht_cffi

ffi = cffi.FFI()

# allows numba to use functions from this module in nopython mode
cffi_support.register_module(_ssht_cffi)

# tells numba how to map numba types to the types defined in the cffi function signatures
cffi_support.register_type(ffi.typeof("double _Complex"), nb.types.complex128)
cffi_support.register_type(ffi.typeof("double _Complex *"),
                           nb.types.CPointer(nb.types.complex128))

__all__ = [
    "mw_forward_sov_conv_sym",
    "mw_inverse_sov_sym",
    "mw_forward_sov_conv_sym_real",
    "mw_inverse_sov_sym_real",
    "mw_forward_sov_conv_sym_ss",
    "mw_inverse_sov_sym_ss",
    "mw_forward_sov_conv_sym_ss_real",
    "mw_inverse_sov_sym_ss_real",
    "ind2elm",
Exemplo n.º 19
0
def load_ool_module():
    """
    Compile an out-of-line module, return the corresponding ffi and
    module objects.
    """
    from cffi import FFI

    numba_complex = """
    typedef struct _numba_complex {
        double real;
        double imag;
    } numba_complex;
    """

    defs = numba_complex + """
    double sin(double x);
    double cos(double x);
    int foo(int a, int b, int c);
    void vsSin(int n, float* x, float* y);
    void vdSin(int n, double* x, double* y);
    void vector_real(numba_complex *c, double *real, int n);
    void vector_imag(numba_complex *c, double *imag, int n);
    """

    source = numba_complex + """
    static int foo(int a, int b, int c)
    {
        return a + b * c;
    }

    void vsSin(int n, float* x, float* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    static void vector_real(numba_complex *c, double *real, int n) {
        int i;
        for (i = 0; i < n; i++)
            real[i] = c[i].real;
    }

    static void vector_imag(numba_complex *c, double *imag, int n) {
        int i;
        for (i = 0; i < n; i++)
            imag[i] = c[i].imag;
    }
    """

    ffi = FFI()
    ffi.set_source('cffi_usecases_ool', source)
    ffi.cdef(defs, override=True)
    tmpdir = temp_directory('test_cffi')
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        mod = import_dynamic('cffi_usecases_ool')
        cffi_support.register_module(mod)
        cffi_support.register_type(mod.ffi.typeof('struct _numba_complex'),
                                   complex128)
        return mod.ffi, mod
    finally:
        sys.path.remove(tmpdir)
Exemplo n.º 20
0
import numpy as np
from numba import cffi_support, float32, float64, vectorize

import _igam
from _igam import lib, ffi

cffi_support.register_module(_igam)
_igam = lib.igam
_igamc = lib.igamc


@vectorize([float64(float64, float64), float32(float32, float32)])
def lower(s, x):
    return _igam(s, x)


@vectorize([float64(float64, float64), float32(float32, float32)])
def upper(s, x):
    return _igamc(s, x)
Exemplo n.º 21
0
from __future__ import unicode_literals

from . import _misc_ffi
from ._misc_ffi.lib import *

try:
    from numba import cffi_support as _cffi_support
    _cffi_support.register_module(_misc_ffi)
except ImportError:
    pass
Exemplo n.º 22
0
from __future__ import absolute_import, print_function, division

import numpy as np
import numba as nb
from numba.targets.registry import CPUDispatcher
from numba.cffi_support import register_module

from . import _wrapper
from ._wrapper import lib, ffi

register_module(_wrapper)

start_instrumentation = lib.start_instrumentation
stop_instrumentation = lib.stop_instrumentation
zero_stats = lib.zero_stats
dump_stats = lib.dump_stats
dump_stats_at = lib.dump_stats_at


def _wrap_numba(func, tag=None):
    if not isinstance(func, CPUDispatcher):
        raise TypeError("Not a numba dispatch function")

    if tag is None:

        @nb.jit(**func.targetoptions)
        def inner(*args):
            zero_stats()
            start_instrumentation()
            res = func(*args)
            stop_instrumentation()