Example #1
0
    def typeof_pyval(cls, val):
        """
        This is called from numba._dispatcher as a fallback if the native code
        cannot decide the type.
        """
        if isinstance(val, numpy.ndarray):
            # TODO complete dtype mapping
            dtype = numpy_support.from_dtype(val.dtype)
            ndim = val.ndim
            if ndim == 0:
                # is array scalar
                return numpy_support.from_dtype(val.dtype)
            layout = numpy_support.map_layout(val)
            aryty = types.Array(dtype, ndim, layout)
            return aryty

        # The following are handled in the C version for exact type match
        # So test these later
        elif isinstance(val, INT_TYPES):
            return types.int64

        elif isinstance(val, float):
            return types.float64

        elif isinstance(val, complex):
            return types.complex128

        elif numpy_support.is_arrayscalar(val):
            # Array scalar
            return numpy_support.from_dtype(numpy.dtype(type(val)))

        # Other object
        else:
            return types.pyobject
Example #2
0
    def __call__(self, *args, **kwargs):
        if (self.neighborhood is not None
                and len(self.neighborhood) != args[0].ndim):
            raise ValueError("{} dimensional neighborhood specified for {} "
                             "dimensional input array".format(
                                 len(self.neighborhood), args[0].ndim))

        if 'out' in kwargs:
            result = kwargs['out']
            rdtype = result.dtype
            rttype = numpy_support.from_dtype(rdtype)
            result_type = types.npytypes.Array(
                rttype, result.ndim, numpy_support.map_layout(result))
            array_types = tuple([typing.typeof.typeof(x) for x in args])
            array_types_full = tuple([typing.typeof.typeof(x)
                                      for x in args] + [result_type])
        else:
            result = None
            array_types = tuple([typing.typeof.typeof(x) for x in args])
            array_types_full = array_types

        if config.DEBUG_ARRAY_OPT == 1:
            print("__call__", array_types, args, kwargs)

        (real_ret, typemap, calltypes) = self.get_return_type(array_types)
        new_func = self._stencil_wrapper(result, None, real_ret, typemap,
                                         calltypes, *array_types_full)

        if result is None:
            return new_func.entry_point(*args)
        else:
            return new_func.entry_point(*(args + (result, )))
Example #3
0
def typeof_pyval(val):
    """
    This is called from numba._dispatcher as a fallback if the native code
    cannot decide the type.
    """
    if isinstance(val, numpy.ndarray):
        # TODO complete dtype mapping
        dtype = numpy_support.from_dtype(val.dtype)
        ndim = val.ndim
        if ndim == 0:
            # is array scalar
            return numpy_support.from_dtype(val.dtype)
        layout = numpy_support.map_layout(val)
        aryty = types.Array(dtype, ndim, layout)
        return aryty

    # The following are handled in the C version for exact type match
    # So test these later
    elif isinstance(val, INT_TYPES):
        return types.int32

    elif isinstance(val, float):
        return types.float64

    elif isinstance(val, complex):
        return types.complex128

    elif numpy_support.is_arrayscalar(val):
        # Array scalar
        return numpy_support.from_dtype(numpy.dtype(type(val)))

    # Other object
    else:
        return types.pyobject
Example #4
0
    def __call__(self, *args, **kwargs):
        if (self.neighborhood is not None and
            len(self.neighborhood) != args[0].ndim):
            raise ValueError("{} dimensional neighborhood specified for {} "
                             "dimensional input array".format(
                                len(self.neighborhood), args[0].ndim))

        if 'out' in kwargs:
            result = kwargs['out']
            rdtype = result.dtype
            rttype = numpy_support.from_dtype(rdtype)
            result_type = types.npytypes.Array(rttype, result.ndim,
                                               numpy_support.map_layout(result))
            array_types = tuple([typing.typeof.typeof(x) for x in args])
            array_types_full = tuple([typing.typeof.typeof(x) for x in args] +
                                     [result_type])
        else:
            result = None
            array_types = tuple([typing.typeof.typeof(x) for x in args])
            array_types_full = array_types

        if config.DEBUG_ARRAY_OPT == 1:
            print("__call__", array_types, args, kwargs)

        (real_ret, typemap, calltypes) = self.get_return_type(array_types)
        new_func = self._stencil_wrapper(result, None, real_ret, typemap,
                                         calltypes, *array_types_full)

        if result is None:
            return new_func.entry_point(*args)
        else:
            return new_func.entry_point(*(args+(result,)))
Example #5
0
def _typeof_ndarray(val, c):
    try:
        dtype = numpy_support.from_dtype(val.dtype)
    except NotImplementedError:
        return
    layout = numpy_support.map_layout(val)
    readonly = not val.flags.writeable
    return types.Array(dtype, val.ndim, layout, readonly=readonly)
Example #6
0
def _typeof_ndarray(val, c):
    try:
        dtype = numpy_support.from_dtype(val.dtype)
    except NotImplementedError:
        raise ValueError("Unsupported array dtype: %s" % (val.dtype,))
    layout = numpy_support.map_layout(val)
    readonly = not val.flags.writeable
    return types.Array(dtype, val.ndim, layout, readonly=readonly)
Example #7
0
    def resolve_argument_type(self, val):
        """
        Return the numba type of a Python value that is being used
        as a function argument.  Integer types will all be considered
        int64, regardless of size.  Numpy arrays will be accepted in
        "C" or "F" layout.

        Unknown types will be mapped to pyobject.
        """
        if isinstance(val, utils.INT_TYPES):
            # Force all integers to be 64-bit
            return types.int64
        elif numpy_support.is_array(val):
            dtype = numpy_support.from_dtype(val.dtype)
            layout = numpy_support.map_layout(val)
            return types.Array(dtype, val.ndim, layout)

        tp = self.resolve_data_type(val)
        if tp is None:
            tp = getattr(val, "_numba_type_", types.pyobject)
        return tp
Example #8
0
    def resolve_argument_type(self, val):
        """
        Return the numba type of a Python value that is being used
        as a function argument.  Integer types will all be considered
        int64, regardless of size.  Numpy arrays will be accepted in
        "C" or "F" layout.

        Unknown types will be mapped to pyobject.
        """
        if isinstance(val, utils.INT_TYPES):
            # Force all integers to be 64-bit
            return types.int64
        elif numpy_support.is_array(val):
            dtype = numpy_support.from_dtype(val.dtype)
            layout = numpy_support.map_layout(val)
            return types.Array(dtype, val.ndim, layout)

        tp = self.resolve_data_type(val)
        if tp is None:
            tp = getattr(val, "_numba_type_", types.pyobject)
        return tp
Example #9
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and isinstance(val, utils.INT_TYPES + (float,))):
            return self.get_number_type(val)

        elif val is None:
            return types.none

        elif isinstance(val, str):
            return types.string

        elif isinstance(val, complex):
            return types.complex128

        elif isinstance(val, tuple):
            tys = [self.resolve_value_type(v) for v in val]
            distinct_types = set(tys)
            if len(distinct_types) == 1:
                return types.UniTuple(tys[0], len(tys))
            else:
                return types.Tuple(tys)

        elif ctypes_utils.is_ctypes_funcptr(val):
            return ctypes_utils.make_function_type(val)

        elif cffi_utils.SUPPORTED and cffi_utils.is_cffi_func(val):
            return cffi_utils.make_function_type(val)

        elif numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return
            layout = numpy_support.map_layout(ary)
            readonly = not ary.flags.writeable
            return types.Array(dtype, ary.ndim, layout, readonly=readonly)

        elif sys.version_info >= (2, 7) and not isinstance(val, numpy.generic):
            try:
                m = memoryview(val)
            except TypeError:
                pass
            else:
                # Object has the buffer protocol
                try:
                    dtype = bufproto.decode_pep3118_format(m.format, m.itemsize)
                except ValueError:
                    pass
                else:
                    type_class = bufproto.get_type_class(type(val))
                    layout = bufproto.infer_layout(m)
                    return type_class(dtype, m.ndim, layout=layout,
                                      readonly=m.readonly)

        if isinstance(val, numpy.dtype):
            tp = numpy_support.from_dtype(val)
            return types.DType(tp)

        else:
            # Matching here is quite broad, so we have to do it after
            # the more specific matches above.
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        return
Example #10
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and isinstance(val, utils.INT_TYPES + (float, ))):
            return self.get_number_type(val)

        elif val is None:
            return types.none

        elif isinstance(val, str):
            return types.string

        elif isinstance(val, complex):
            return types.complex128

        elif isinstance(val, tuple):
            tys = [self.resolve_value_type(v) for v in val]
            distinct_types = set(tys)
            if len(distinct_types) == 1:
                return types.UniTuple(tys[0], len(tys))
            else:
                return types.Tuple(tys)

        elif ctypes_utils.is_ctypes_funcptr(val):
            return ctypes_utils.make_function_type(val)

        elif cffi_utils.SUPPORTED and cffi_utils.is_cffi_func(val):
            return cffi_utils.make_function_type(val)

        elif numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return
            layout = numpy_support.map_layout(ary)
            readonly = not ary.flags.writeable
            return types.Array(dtype, ary.ndim, layout, readonly=readonly)

        elif sys.version_info >= (2, 7) and not isinstance(val, numpy.generic):
            try:
                m = memoryview(val)
            except TypeError:
                pass
            else:
                # Object has the buffer protocol
                try:
                    dtype = bufproto.decode_pep3118_format(
                        m.format, m.itemsize)
                except ValueError:
                    pass
                else:
                    type_class = bufproto.get_type_class(type(val))
                    layout = bufproto.infer_layout(m)
                    return type_class(dtype,
                                      m.ndim,
                                      layout=layout,
                                      readonly=m.readonly)

        if isinstance(val, numpy.dtype):
            tp = numpy_support.from_dtype(val)
            return types.DType(tp)

        else:
            # Matching here is quite broad, so we have to do it after
            # the more specific matches above.
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        return