Beispiel #1
0
def resize_overload(A_t, resize_shape_t):
    n_channels = 3
    if not (isinstance(A_t, types.Array) and A_t.ndim == n_channels
            and A_t.dtype == types.uint8
            and resize_shape_t == types.UniTuple(types.int64, 2)):
        raise ValueError("Unsupported cv2.resize() with types {} {}".format(
            A_t, resize_shape_t))

    dtype = A_t.dtype
    sig = types.void(
        types.intp,  # new num rows
        types.intp,  # new num cols
        types.CPointer(dtype),  # output data
        types.CPointer(dtype),  # input data
        types.intp,  # num rows
        types.intp,  # num cols
    )
    cv_resize = types.ExternalFunction("cv_resize", sig)

    def resize_imp(in_arr, resize_shape):
        A = np.ascontiguousarray(in_arr)
        n_channels = A.shape[2]
        # cv Size object has column first
        B = np.empty((resize_shape[1], resize_shape[0], n_channels), A.dtype)

        cv_resize(resize_shape[1], resize_shape[0], B.ctypes, A.ctypes,
                  A.shape[0], A.shape[1])
        return B

    return resize_imp
Beispiel #2
0
def hypot_float_impl(context, builder, sig, args):
    xty, yty = sig.args
    assert xty == yty == sig.return_type
    x, y = args

    # Windows has alternate names for hypot/hypotf, see
    # https://msdn.microsoft.com/fr-fr/library/a9yb3dbt%28v=vs.80%29.aspx
    fname = {
        types.float32: "_hypotf" if sys.platform == 'win32' else "hypotf",
        types.float64: "_hypot" if sys.platform == 'win32' else "hypot",
    }[xty]
    plat_hypot = types.ExternalFunction(fname, sig)

    if sys.platform == 'win32' and config.MACHINE_BITS == 32:
        inf = xty(float('inf'))

        def hypot_impl(x, y):
            if math.isinf(x) or math.isinf(y):
                return inf
            return plat_hypot(x, y)
    else:
        def hypot_impl(x, y):
            return plat_hypot(x, y)

    res = context.compile_internal(builder, hypot_impl, sig, args)
    return impl_ret_untracked(context, builder, sig.return_type, res)
Beispiel #3
0
def char_getitem_overload(_str, ind):
    if _str == string_type and isinstance(ind, types.Integer):
        sig = char_type(
                    string_type,   # string
                    types.intp,    # index
                    )
        get_char_from_string = types.ExternalFunction("get_char_from_string", sig)
        def impl(s, i):
            return get_char_from_string(s, i)

        return impl
Beispiel #4
0
    def cho_impl(a):
        ensure_lapack()

        _check_linalg_matrix(a, "cholesky")

        xxpotrf_sig = types.intc(types.int8, types.int8, types.intp,
                                 types.CPointer(a.dtype), types.intp)
        xxpotrf = types.ExternalFunction("numba_xxpotrf", xxpotrf_sig)

        kind = ord(get_blas_kind(a.dtype, "cholesky"))
        UP = ord('U')
        LO = ord('L')

        def cho_impl(a):
            n = a.shape[-1]
            if a.shape[-2] != n:
                msg = "Last 2 dimensions of the array must be square."
                raise np.linalg.LinAlgError(msg)

            # The output is allocated in C order
            out = a.copy()
            # Pass UP since xxpotrf() operates in F order
            # The semantics ensure this works fine
            # (out is really its Hermitian in F order, but UP instructs
            #  xxpotrf to compute the Hermitian of the upper triangle
            #  => they cancel each other)
            r = xxpotrf(kind, UP, n, out.ctypes, n)
            if r < 0:
                fatal_error_func()
                assert 0   # unreachable
            if r > 0:
                raise np.linalg.LinAlgError(
                    "Matrix is not positive definite.")
            # Zero out upper triangle, in F order
            for col in range(n):
                out[:col, col] = 0
            return out

        return cho_impl
Beispiel #5
0
def imdecode_overload(A_t, flags_t):

    if (isinstance(A_t, types.Array) and A_t.ndim == 1
            and A_t.dtype == types.uint8 and flags_t == types.intp):
        in_dtype = A_t.dtype
        out_dtype = A_t.dtype

        sig = types.CPointer(out_dtype)(
            types.CPointer(types.intp),  # output shape
            types.CPointer(in_dtype),  # input array
            types.intp,  # input size (num_bytes)
            types.intp,  # flags
        )
        cv_imdecode = types.ExternalFunction("cv_imdecode", sig)

        def imdecode_imp(A, flags):
            out_shape = np.empty(2, dtype=np.int64)
            data = cv_imdecode(out_shape.ctypes, A.ctypes, len(A), flags)
            n_channels = 3
            out_shape_tup = (out_shape[0], out_shape[1], n_channels)
            img = wrap_array(data, out_shape_tup)
            return img

        return imdecode_imp
Beispiel #6
0
# func_parse_iso = tslib_cdll.parse_iso_8601_datetime
# func_parse_iso.restype = ctypes.c_int32
# func_parse_iso.argtypes = [ctypes.c_void_p, ctypes.c_int32, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
# func_dts_to_dt = tslib_cdll.pandas_datetimestruct_to_datetime
# func_dts_to_dt.restype = ctypes.c_int64
# func_dts_to_dt.argtypes = [ctypes.c_int, ctypes.c_void_p]


sig = types.intp(
    types.voidptr,             # C str
    types.intp,             # len(str)
    types.voidptr,          # struct ptr
    types.voidptr,  # int ptr
    types.voidptr,  # int ptr
)
parse_iso_8601_datetime = types.ExternalFunction("parse_iso_8601_datetime", sig)
sig = types.intp(
    types.intp,             # fr magic number
    types.voidptr,          # struct ptr
    types.voidptr,  # out int ptr
)
convert_datetimestruct_to_datetime = types.ExternalFunction("convert_datetimestruct_to_datetime", sig)


@numba.njit(locals={'arg1': numba.int32, 'arg3': numba.int32, 'arg4': numba.int32})
def parse_datetime_str(str):
    arg0 = hpat.str_ext.unicode_to_char_ptr(str)
    arg1 = len(str)
    arg2 = PANDAS_DATETIMESTRUCT()
    arg3 = np.int32(13)
    arg4 = np.int32(13)

@overload(get_type_enum)
def get_type_enum_overload(arr):
    dtype = arr.dtype
    if isinstance(dtype, hpat.hiframes.pd_categorical_ext.PDCategoricalDtype):
        dtype = hpat.hiframes.pd_categorical_ext.get_categories_int_type(dtype)

    typ_val = _numba_to_c_type_map[dtype]
    return lambda arr: np.int32(typ_val)


INT_MAX = np.iinfo(np.int32).max

_send = types.ExternalFunction(
    "c_send",
    types.void(types.voidptr, types.int32, types.int32, types.int32,
               types.int32))


@numba.njit
def send(val, rank, tag):
    # dummy array for val
    send_arr = np.full(1, val)
    type_enum = get_type_enum(send_arr)
    _send(send_arr.ctypes, 1, type_enum, rank, tag)


_recv = types.ExternalFunction(
    "c_recv",
    types.void(types.voidptr, types.int32, types.int32, types.int32,
               types.int32))
Beispiel #8
0

for key_typ in elem_types:
    for val_typ in elem_types:
        k_obj = typ_str_to_obj(key_typ)
        v_obj = typ_str_to_obj(val_typ)
        key_str = str(key_typ)
        val_str = str(val_typ)
        _add_dict_symbols(key_str, val_str)
        # create types
        exec("dict_{}_{}_type = DictType({}, {})".format(key_str, val_str, k_obj, v_obj))
        exec_format_line = "dict_{0}_{1}_init = types.ExternalFunction('dict_{0}_{1}_init', dict_{0}_{1}_type())"
        exec(exec_format_line.format(key_str, val_str))

dict_byte_vec_int64_type = DictType(byte_vec_type, types.int64)
dict_byte_vec_int64_init = types.ExternalFunction('dict_byte_vec_int64_init', dict_byte_vec_int64_type())
_add_dict_symbols('byte_vec', 'int64')

ll.add_symbol('byte_vec_init', hdict_ext.byte_vec_init)
ll.add_symbol('byte_vec_set', hdict_ext.byte_vec_set)
ll.add_symbol('byte_vec_free', hdict_ext.byte_vec_free)
ll.add_symbol('byte_vec_resize', hdict_ext.byte_vec_resize)

byte_vec_init = types.ExternalFunction('byte_vec_init', byte_vec_type(types.int64, types.voidptr))
byte_vec_set = types.ExternalFunction(
    'byte_vec_set',
    types.void(
        byte_vec_type,
        types.int64,
        types.voidptr,
        types.int64))
Beispiel #9
0
xe_connect_type = XeConnectType()

register_model(XeConnectType)(models.OpaqueModel)


class XeDSetType(types.Opaque):
    def __init__(self):
        super(XeDSetType, self).__init__(name='XeDSetType')


xe_dset_type = XeDSetType()

register_model(XeDSetType)(models.OpaqueModel)

get_column_size_xenon = types.ExternalFunction(
    "get_column_size_xenon", types.int64(xe_connect_type, xe_dset_type, types.intp))
# read_xenon_col = types.ExternalFunction(
#     "c_read_xenon",
#     types.void(
#         string_type,
#         types.intp,
#         types.voidptr,
#         types.CPointer(
#             types.int64)))
xe_connect = types.ExternalFunction("c_xe_connect", xe_connect_type(types.voidptr))
xe_open = types.ExternalFunction("c_xe_open", xe_dset_type(xe_connect_type, types.voidptr))
xe_close = types.ExternalFunction("c_xe_close", types.void(xe_connect_type, xe_dset_type))


# TODO: fix liveness/alias in Numba to be able to use arr.ctypes directly
@intrinsic
Beispiel #10
0
def dpctl_event_wait():
    ret_type = types.voidptr
    sig = signature(ret_type, types.voidptr)
    return types.ExternalFunction("DPCTLEvent_Wait", sig)
Beispiel #11
0
def qr_impl(a):
    ensure_lapack()

    _check_linalg_matrix(a, "qr")

    # Need two functions, the first computes R, storing it in the upper
    # triangle of A with the below diagonal part of A containing elementary
    # reflectors needed to construct Q. The second turns the below diagonal
    # entries of A into Q, storing Q in A (creates orthonormal columns from
    # the elementary reflectors).

    numba_ez_geqrf_sig = types.intc(
        types.char,  # kind
        types.intp,  # m
        types.intp,  # n
        types.CPointer(a.dtype),  # a
        types.intp,  # lda
        types.CPointer(a.dtype),  # tau
    )

    numba_ez_geqrf = types.ExternalFunction("numba_ez_geqrf",
                                            numba_ez_geqrf_sig)

    numba_ez_xxgqr_sig = types.intc(
        types.char,  # kind
        types.intp,  # m
        types.intp,  # n
        types.intp,  # k
        types.CPointer(a.dtype),  # a
        types.intp,  # lda
        types.CPointer(a.dtype),  # tau
    )

    numba_ez_xxgqr = types.ExternalFunction("numba_ez_xxgqr",
                                            numba_ez_xxgqr_sig)

    kind = ord(get_blas_kind(a.dtype, "qr"))

    F_layout = a.layout == 'F'

    def qr_impl(a):
        n = a.shape[-1]
        m = a.shape[-2]

        _check_finite_matrix(a)

        # copy A as it will be destroyed
        if F_layout:
            q = np.copy(a)
        else:
            q = np.asfortranarray(a)

        lda = m

        minmn = min(m, n)
        tau = np.empty((minmn), dtype=a.dtype)

        ret = numba_ez_geqrf(
            kind,  # kind
            m,  # m
            n,  # n
            q.ctypes,  # a
            m,  # lda
            tau.ctypes  # tau
        )
        if ret < 0:
            fatal_error_func()
            assert 0   # unreachable

        # pull out R, this is transposed because of Fortran
        r = np.zeros((n, minmn), dtype=a.dtype).T

        # the triangle in R
        for i in range(minmn):
            for j in range(i + 1):
                r[j, i] = q[j, i]

        # and the possible square in R
        for i in range(minmn, n):
            for j in range(minmn):
                r[j, i] = q[j, i]

        ret = numba_ez_xxgqr(
            kind,  # kind
            m,  # m
            minmn,  # n
            minmn,  # k
            q.ctypes,  # a
            m,  # lda
            tau.ctypes  # tau
        )
        if ret < 0:
            fatal_error_func()
            assert 0   # unreachable

        # help liveness analysis
        tau.size
        q.size

        return (q[:, :minmn], r)

    return qr_impl
Beispiel #12
0
def dpctl_get_current_queue():
    ret_type = types.voidptr
    sig = signature(ret_type)
    return types.ExternalFunction("DPCTLQueueMgr_GetCurrentQueue", sig)
Beispiel #13
0
for key_typ in elem_types:
    for val_typ in elem_types:
        k_obj = typ_str_to_obj(key_typ)
        v_obj = typ_str_to_obj(val_typ)
        key_str = str(key_typ)
        val_str = str(val_typ)
        _add_dict_symbols(key_str, val_str)
        # create types
        exec("dict_{}_{}_type = DictType({}, {})".format(
            key_str, val_str, k_obj, v_obj))
        exec(
            "dict_{0}_{1}_init = types.ExternalFunction('dict_{0}_{1}_init', dict_{0}_{1}_type())"
            .format(key_str, val_str))

dict_byte_vec_int64_type = DictType(byte_vec_type, types.int64)
dict_byte_vec_int64_init = types.ExternalFunction('dict_byte_vec_int64_init',
                                                  dict_byte_vec_int64_type())
_add_dict_symbols('byte_vec', 'int64')

ll.add_symbol('byte_vec_init', hdict_ext.byte_vec_init)
ll.add_symbol('byte_vec_set', hdict_ext.byte_vec_set)
ll.add_symbol('byte_vec_free', hdict_ext.byte_vec_free)
ll.add_symbol('byte_vec_resize', hdict_ext.byte_vec_resize)

byte_vec_init = types.ExternalFunction(
    'byte_vec_init', byte_vec_type(types.int64, types.voidptr))
byte_vec_set = types.ExternalFunction(
    'byte_vec_set',
    types.void(byte_vec_type, types.int64, types.voidptr, types.int64))
byte_vec_resize = types.ExternalFunction(
    'byte_vec_resize', types.void(byte_vec_type, types.int64))
byte_vec_free = types.ExternalFunction('byte_vec_free',
Beispiel #14
0
class StreamReaderType(types.Opaque):
    def __init__(self):
        super(StreamReaderType, self).__init__(name='StreamReaderType')


stream_reader_type = StreamReaderType()
register_model(StreamReaderType)(models.OpaqueModel)


@box(StreamReaderType)
def box_stream_reader(typ, val, c):
    return val


csv_file_chunk_reader = types.ExternalFunction(
    "csv_file_chunk_reader",
    stream_reader_type(types.voidptr, types.bool_, types.int64, types.int64))


def _get_dtype_str(t):
    dtype = t.dtype
    if isinstance(dtype, PDCategoricalDtype):
        cat_arr = CategoricalArray(dtype)
        # HACK: add cat type to numba.types
        # FIXME: fix after Numba #3372 is resolved
        cat_arr_name = 'CategoricalArray' + str(ir_utils.next_label())
        setattr(types, cat_arr_name, cat_arr)
        return cat_arr_name

    if dtype == types.NPDatetime('ns'):
        dtype = 'NPDatetime("ns")'
Beispiel #15
0
def dpctl_malloc_shared():
    ret_type = types.voidptr
    sig = signature(ret_type, types.int64, types.voidptr)
    return types.ExternalFunction("DPCTLmalloc_shared", sig)
Beispiel #16
0
def dpctl_queue_wait():
    ret_type = types.void
    sig = signature(ret_type, types.voidptr)
    return types.ExternalFunction("DPCTLQueue_Wait", sig)
Beispiel #17
0
def dpctl_free_with_queue():
    ret_type = types.void
    sig = signature(ret_type, types.voidptr, types.voidptr)
    return types.ExternalFunction("DPCTLfree_with_queue", sig)
Beispiel #18
0
def dpctl_event_delete():
    ret_type = types.void
    sig = signature(ret_type, types.voidptr)
    return types.ExternalFunction("DPCTLEvent_Delete", sig)
Beispiel #19
0

@box(CharType)
def box_char(typ, val, c):
    """
    """
    fnty = lir.FunctionType(lir.IntType(8).as_pointer(), [lir.IntType(8)])
    fn = c.builder.module.get_or_insert_function(fnty, name="get_char_ptr")
    c_str = c.builder.call(fn, [val])
    pystr = c.pyapi.string_from_string_and_size(
        c_str, c.context.get_constant(types.intp, 1))
    # TODO: delete ptr
    return pystr


del_str = types.ExternalFunction("del_str", types.void(string_type))
_hash_str = types.ExternalFunction("_hash_str", types.int64(string_type))
get_c_str = types.ExternalFunction("get_c_str", types.voidptr(string_type))


@overload_method(StringType, 'c_str')
def str_c_str(str_typ):
    return lambda s: get_c_str(s)


@overload_method(StringType, 'join')
def str_join(str_typ, iterable_typ):
    # TODO: more efficient implementation (e.g. C++ string buffer)
    def str_join_impl(sep_str, str_container):
        res = ""
        counter = 0
Beispiel #20
0
    def eig_impl(a):
        ensure_lapack()

        _check_linalg_matrix(a, "eig")

        numba_ez_rgeev_sig = types.intc(types.char,  # kind
                                        types.char,  # jobvl
                                        types.char,  # jobvr
                                        types.intp,  # n
                                        types.CPointer(a.dtype),  # a
                                        types.intp,  # lda
                                        types.CPointer(a.dtype),  # wr
                                        types.CPointer(a.dtype),  # wi
                                        types.CPointer(a.dtype),  # vl
                                        types.intp,  # ldvl
                                        types.CPointer(a.dtype),  # vr
                                        types.intp  # ldvr
                                        )

        numba_ez_rgeev = types.ExternalFunction("numba_ez_rgeev",
                                                numba_ez_rgeev_sig)

        numba_ez_cgeev_sig = types.intc(types.char,  # kind
                                        types.char,  # jobvl
                                        types.char,  # jobvr
                                        types.intp,  # n
                                        types.CPointer(a.dtype),  # a
                                        types.intp,  # lda
                                        types.CPointer(a.dtype),  # w
                                        types.CPointer(a.dtype),  # vl
                                        types.intp,  # ldvl
                                        types.CPointer(a.dtype),  # vr
                                        types.intp  # ldvr
                                        )

        numba_ez_cgeev = types.ExternalFunction("numba_ez_cgeev",
                                                numba_ez_cgeev_sig)

        kind = ord(get_blas_kind(a.dtype, "eig"))

        JOBVL = ord('N')
        JOBVR = ord('V')

        F_layout = a.layout == 'F'

        def real_eig_impl(a):
            """
            eig() implementation for real arrays.
            """
            n = a.shape[-1]
            if a.shape[-2] != n:
                msg = "Last 2 dimensions of the array must be square."
                raise np.linalg.LinAlgError(msg)

            _check_finite_matrix(a)

            if F_layout:
                acpy = np.copy(a)
            else:
                acpy = np.asfortranarray(a)

            ldvl = 1
            ldvr = n
            wr = np.empty(n, dtype=a.dtype)
            wi = np.empty(n, dtype=a.dtype)
            vl = np.empty((n, ldvl), dtype=a.dtype)
            vr = np.empty((n, ldvr), dtype=a.dtype)

            r = numba_ez_rgeev(kind,
                               JOBVL,
                               JOBVR,
                               n,
                               acpy.ctypes,
                               n,
                               wr.ctypes,
                               wi.ctypes,
                               vl.ctypes,
                               ldvl,
                               vr.ctypes,
                               ldvr)
            if r < 0:
                fatal_error_func()
                assert 0   # unreachable

            # By design numba does not support dynamic return types, however,
            # Numpy does. Numpy uses this ability in the case of returning
            # eigenvalues/vectors of a real matrix. The return type of
            # np.linalg.eig(), when operating on a matrix in real space
            # depends on the values present in the matrix itself (recalling
            # that eigenvalues are the roots of the characteristic polynomial
            # of the system matrix, which will by construction depend on the
            # values present in the system matrix). As numba cannot handle
            # the case of a runtime decision based domain change relative to
            # the input type, if it is required numba raises as below.
            if np.any(wi):
                raise ValueError(
                    "eig() argument must not cause a domain change.")

            # put these in to help with liveness analysis,
            # `.ctypes` doesn't keep the vars alive
            acpy.size
            vl.size
            vr.size
            wr.size
            wi.size
            return (wr, vr.T)

        def cmplx_eig_impl(a):
            """
            eig() implementation for complex arrays.
            """
            n = a.shape[-1]
            if a.shape[-2] != n:
                msg = "Last 2 dimensions of the array must be square."
                raise np.linalg.LinAlgError(msg)

            _check_finite_matrix(a)

            if F_layout:
                acpy = np.copy(a)
            else:
                acpy = np.asfortranarray(a)

            ldvl = 1
            ldvr = n
            w = np.empty(n, dtype=a.dtype)
            vl = np.empty((n, ldvl), dtype=a.dtype)
            vr = np.empty((n, ldvr), dtype=a.dtype)

            r = numba_ez_cgeev(kind,
                               JOBVL,
                               JOBVR,
                               n,
                               acpy.ctypes,
                               n,
                               w.ctypes,
                               vl.ctypes,
                               ldvl,
                               vr.ctypes,
                               ldvr)
            if r < 0:
                fatal_error_func()
                assert 0   # unreachable

            # put these in to help with liveness analysis,
            # `.ctypes` doesn't keep the vars alive
            acpy.size
            vl.size
            vr.size
            w.size
            return (w, vr.T)

        if isinstance(a.dtype, types.scalars.Complex):
            return cmplx_eig_impl
        else:
            return real_eig_impl
Beispiel #21
0
    inds = hpat.distributed_api.gatherv(indices)
    if rank == 0:
        all_indices = inds
    else:
        all_indices = np.empty(n, indices.dtype)
    hpat.distributed_api.bcast(all_indices)

    start = hpat.distributed_api.get_start(n, n_pes, rank)
    end = hpat.distributed_api.get_end(n, n_pes, rank)
    return all_indices[start:end]

@intrinsic
def tuple_to_ptr(typingctx, tuple_tp=None):
    def codegen(context, builder, sig, args):
        ptr = cgutils.alloca_once(builder, args[0].type)
        builder.store(args[0], ptr)
        return builder.bitcast(ptr, lir.IntType(8).as_pointer())
    return signature(types.voidptr, tuple_tp), codegen

_h5read_filter = types.ExternalFunction("hpat_h5_read_filter",
    types.int32(h5dataset_or_group_type, types.int32, types.voidptr,
    types.voidptr, types.intp, types.voidptr, types.int32, types.voidptr, types.int32))

@numba.njit
def h5read_filter(dset_id, ndim, starts, counts, is_parallel, out_arr, read_indices):
    starts_ptr = tuple_to_ptr(starts)
    counts_ptr = tuple_to_ptr(counts)
    type_enum = hpat.distributed_api.get_type_enum(out_arr)
    return _h5read_filter(dset_id, ndim, starts_ptr, counts_ptr, is_parallel,
                   out_arr.ctypes, type_enum, read_indices.ctypes, len(read_indices))
Beispiel #22
0
    # TODO: close file?
    return col_names, col_types


def _rm_pd_index(col_names, col_types):
    """remove pandas index if found in columns
    """
    try:
        pd_index_loc = col_names.index('__index_level_0__')
        del col_names[pd_index_loc]
        del col_types[pd_index_loc]
    except ValueError:
        pass


_get_arrow_readers = types.ExternalFunction("get_arrow_readers", types.Opaque('arrow_reader')(types.voidptr))
_del_arrow_readers = types.ExternalFunction("del_arrow_readers", types.void(types.Opaque('arrow_reader')))


@infer_global(get_column_size_parquet)
class SizeParquetInfer(AbstractTemplate):
    def generic(self, args, kws):
        assert not kws
        assert len(args) == 2
        return signature(types.intp, args[0], types.unliteral(args[1]))


@infer_global(read_parquet)
class ReadParquetInfer(AbstractTemplate):
    def generic(self, args, kws):
        assert not kws
Beispiel #23
0
@lower_builtin(np.linalg.inv, types.Array)
def inv(context, builder, sig, args):
    """
    np.linalg.inv(a)
    """
    ensure_lapack()

    ndims = sig.args[0].ndim
    if ndims == 2:
        return mat_inv(context, builder, sig, args)
    else:
        assert 0


fatal_error_sig = types.intc()
fatal_error_func = types.ExternalFunction("numba_fatal_error", fatal_error_sig)


@jit(nopython=True)
def _check_finite_matrix(a):
    for v in np.nditer(a):
        if not np.isfinite(v.item()):
            raise np.linalg.LinAlgError(
                "Array must not contain infs or NaNs.")


def _check_linalg_matrix(a, func_name):
    if not isinstance(a, types.Array):
        raise TypingError("np.linalg.%s() only supported for array types"
                          % func_name)
    if not a.ndim == 2:
Beispiel #24
0
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************

import numpy as np
import numba
import sdc
from numba import types, cgutils
from numba.targets.arrayobj import make_array
from numba.extending import overload, intrinsic, overload_method
from sdc.str_ext import string_type

from numba.ir_utils import (compile_to_numba_ir, replace_arg_nodes,
                            find_callname, guard)

_get_file_size = types.ExternalFunction("get_file_size",
                                        types.int64(types.voidptr))
_file_read = types.ExternalFunction(
    "file_read", types.void(types.voidptr, types.voidptr, types.intp))
_file_read_parallel = types.ExternalFunction(
    "file_read_parallel",
    types.void(types.voidptr, types.voidptr, types.intp, types.intp))

file_write = types.ExternalFunction(
    "file_write", types.void(types.voidptr, types.voidptr, types.intp))

_file_write_parallel = types.ExternalFunction(
    "file_write_parallel",
    types.void(types.voidptr, types.voidptr, types.intp, types.intp,
               types.intp))

Beispiel #25
0
    def is_precise(self):
        return self.dtype.is_precise()


set_string_type = SetType(string_type)


class SetIterType(types.BaseContainerIterator):
    container_class = SetType


register_model(SetType)(models.OpaqueModel)


_init_set_string = types.ExternalFunction("init_set_string",
                                          set_string_type())


def init_set_string():
    return set()


@overload(init_set_string)
def init_set_overload():
    return lambda: _init_set_string()


add_set_string = types.ExternalFunction("insert_set_string",
                                        types.void(set_string_type, types.voidptr))

len_set_string = types.ExternalFunction("len_set_string",
Beispiel #26
0
def dpctl_queue_memcpy():
    ret_type = types.voidptr
    sig = signature(ret_type, types.voidptr, types.voidptr, types.voidptr,
                    types.int64)
    return types.ExternalFunction("DPCTLQueue_Memcpy", sig)
Beispiel #27
0
    def svd_impl(a, full_matrices=1):
        ensure_lapack()

        _check_linalg_matrix(a, "svd")

        F_layout = a.layout == 'F'

        # convert typing floats to numpy floats for use in the impl
        s_type = getattr(a.dtype, "underlying_float", a.dtype)
        if s_type.bitwidth == 32:
            s_dtype = np.float32
        else:
            s_dtype = np.float64

        numba_ez_gesdd_sig = types.intc(
            types.char,  # kind
            types.char,  # jobz
            types.intp,  # m
            types.intp,  # n
            types.CPointer(a.dtype),  # a
            types.intp,  # lda
            types.CPointer(s_type),  # s
            types.CPointer(a.dtype),  # u
            types.intp,  # ldu
            types.CPointer(a.dtype),  # vt
            types.intp  # ldvt
        )

        numba_ez_gesdd = types.ExternalFunction("numba_ez_gesdd",
                                                numba_ez_gesdd_sig)

        kind = ord(get_blas_kind(a.dtype, "svd"))

        JOBZ_A = ord('A')
        JOBZ_S = ord('S')

        def svd_impl(a, full_matrices=1):
            n = a.shape[-1]
            m = a.shape[-2]

            _check_finite_matrix(a)

            if F_layout:
                acpy = np.copy(a)
            else:
                acpy = np.asfortranarray(a)

            ldu = m
            minmn = min(m, n)

            if full_matrices:
                JOBZ = JOBZ_A
                ucol = m
                ldvt = n
            else:
                JOBZ = JOBZ_S
                ucol = minmn
                ldvt = minmn

            u = np.empty((ucol, ldu), dtype=a.dtype)
            s = np.empty(minmn, dtype=s_dtype)
            vt = np.empty((n, ldvt), dtype=a.dtype)

            r = numba_ez_gesdd(
                kind,  # kind
                JOBZ,  # jobz
                m,  # m
                n,  # n
                acpy.ctypes,  # a
                m,  # lda
                s.ctypes,  # s
                u.ctypes,  # u
                ldu,  # ldu
                vt.ctypes,  # vt
                ldvt          # ldvt
            )
            if r < 0:
                fatal_error_func()
                assert 0   # unreachable

            # help liveness analysis
            acpy.size
            vt.size
            u.size
            s.size

            return (u.T, s, vt.T)

        return svd_impl
Beispiel #28
0

@box(CharType)
def box_char(typ, val, c):
    """
    """
    fnty = lir.FunctionType(lir.IntType(8).as_pointer(),
                            [lir.IntType(8)])
    fn = c.builder.module.get_or_insert_function(fnty, name="get_char_ptr")
    c_str = c.builder.call(fn, [val])
    pystr = c.pyapi.string_from_string_and_size(c_str, c.context.get_constant(types.intp, 1))
    # TODO: delete ptr
    return pystr


del_str = types.ExternalFunction("del_str", types.void(std_str_type))
_hash_str = types.ExternalFunction("_hash_str", types.int64(std_str_type))
get_c_str = types.ExternalFunction("get_c_str", types.voidptr(std_str_type))


@overload_method(StringType, 'c_str')
def str_c_str(str_typ):
    return lambda s: get_c_str(s)


@overload_method(StringType, 'join')
def str_join(str_typ, iterable_typ):
    # TODO: more efficient implementation (e.g. C++ string buffer)
    def str_join_impl(sep_str, str_container):
        res = ""
        counter = 0
Beispiel #29
0
            res *= a
        return res

    return get_tuple_prod_impl


sig = types.void(
            types.voidptr,  # output array
            types.voidptr,  # input array
            types.intp,     # old_len
            types.intp,     # new_len
            types.intp,     # input lower_dim size in bytes
            types.intp,     # output lower_dim size in bytes
            )

oneD_reshape_shuffle = types.ExternalFunction("oneD_reshape_shuffle", sig)

@numba.njit
def dist_oneD_reshape_shuffle(lhs, in_arr, new_0dim_global_len, old_0dim_global_len, dtype_size):  # pragma: no cover
    c_in_arr = np.ascontiguousarray(in_arr)
    in_lower_dims_size = get_tuple_prod(c_in_arr.shape[1:])
    out_lower_dims_size = get_tuple_prod(lhs.shape[1:])
    #print(c_in_arr)
    # print(new_0dim_global_len, old_0dim_global_len, out_lower_dims_size, in_lower_dims_size)
    oneD_reshape_shuffle(lhs.ctypes, c_in_arr.ctypes,
                            new_0dim_global_len, old_0dim_global_len,
                            dtype_size * out_lower_dims_size,
                            dtype_size * in_lower_dims_size)
    #print(in_arr)

permutation_int = types.ExternalFunction("permutation_int",
Beispiel #30
0
    ll.add_symbol('hpat_h5_write', _hdf5.hpat_h5_write)
    ll.add_symbol('hpat_h5_close', _hdf5.hpat_h5_close)
    ll.add_symbol('h5g_get_num_objs', _hdf5.h5g_get_num_objs)
    ll.add_symbol('h5g_get_objname_by_idx', _hdf5.h5g_get_objname_by_idx)
    ll.add_symbol('h5g_close', _hdf5.hpat_h5g_close)

h5file_lir_type = lir.IntType(64)

if hpat.config._has_h5py:
    # hid_t is 32bit in 1.8 but 64bit in 1.10
    if h5py.version.hdf5_version_tuple[1] == 8:
        h5file_lir_type = lir.IntType(32)
    else:
        assert h5py.version.hdf5_version_tuple[1] == 10

h5g_close = types.ExternalFunction("h5g_close", types.none(h5group_type))


@lower_builtin(operator.getitem, h5file_type, string_type)
@lower_builtin(operator.getitem, h5dataset_or_group_type, string_type)
def h5_open_dset_lower(context, builder, sig, args):
    fg_id, dset_name = args
    dset_name = gen_get_unicode_chars(context, builder, dset_name)

    fnty = lir.FunctionType(h5file_lir_type, [h5file_lir_type, lir.IntType(8).as_pointer()])
    fn = builder.module.get_or_insert_function(fnty, name="hpat_h5_open_dset_or_group_obj")
    return builder.call(fn, [fg_id, dset_name])


if hpat.config._has_h5py:
    @lower_builtin(h5py.File, string_type, string_type)