예제 #1
0
 def from_value(cls, val):
     """
     Creates a :py:class:`Type` object corresponding to the given value.
     """
     if isinstance(val, Type):
         # Creating a new object, because ``val`` may be some derivative of Type,
         # used as a syntactic sugar, and we do not want it to confuse us later.
         return cls(val.dtype,
                    shape=val.shape,
                    strides=val.strides,
                    offset=val.offset,
                    nbytes=val.nbytes)
     elif numpy.issctype(val):
         return cls(val)
     elif hasattr(val, 'dtype') and hasattr(val, 'shape'):
         strides = val.strides if hasattr(val, 'strides') else None
         offset = val.offset if hasattr(val, 'offset') else 0
         nbytes = val.nbytes if hasattr(val, 'nbytes') else None
         return cls(val.dtype,
                    shape=val.shape,
                    strides=strides,
                    offset=offset,
                    nbytes=nbytes)
     else:
         return cls(dtypes.detect_type(val))
예제 #2
0
def normalize_value(thr, gpu_array_type, val):
    """
    Transforms a given value (a scalar or an array)
    to a value that can be passed to a kernel.
    """
    if isinstance(val, gpu_array_type):
        return val
    elif isinstance(val, numpy.ndarray):
        return thr.to_device(val)
    else:
        dtype = dtypes.detect_type(val)
        return numpy.cast[dtype](val)
예제 #3
0
def add_const(arr_t, param):
    """
    Returns an addition transformation with a fixed parameter (1 output, 1 input):
    ``output = input + param``.
    """
    param_dtype = dtypes.detect_type(param)
    return Transformation(
        [Parameter('output', Annotation(arr_t, 'o')),
        Parameter('input', Annotation(arr_t, 'i'))],
        "${output.store_same}(${add}(${input.load_same}, ${param}));",
        render_kwds=dict(
            add=functions.add(arr_t.dtype, param_dtype, out_dtype=arr_t.dtype),
            param=dtypes.c_constant(param, dtype=param_dtype)))
예제 #4
0
def div_const(arr_t, param):
    """
    Returns a scaling transformation with a fixed parameter (1 output, 1 input):
    ``output = input / param``.
    """
    param_dtype = dtypes.detect_type(param)
    return Transformation(
        [Parameter('output', Annotation(arr_t, 'o')),
        Parameter('input', Annotation(arr_t, 'i'))],
        "${output.store_same}(${div}(${input.load_same}, ${param}));",
        render_kwds=dict(
            div=functions.div(arr_t.dtype, param_dtype, out_dtype=arr_t.dtype),
            param=dtypes.c_constant(param, dtype=param_dtype)))
예제 #5
0
def normalize_value(thr, gpu_array_type, val):
    """
    Transforms a given value (a scalar or an array)
    to a value that can be passed to a kernel.
    Returns a pair (result, is_array), where ``is_array`` is a boolean
    that helps distinguishing between zero-dimensional arrays and scalars.
    """
    if isinstance(val, gpu_array_type):
        return val, True
    elif isinstance(val, numpy.ndarray):
        return thr.to_device(val), True
    else:
        dtype = dtypes.detect_type(val)
        return numpy.cast[dtype](val), False
예제 #6
0
 def from_value(cls, val):
     """
     Creates a :py:class:`Type` object corresponding to the given value.
     """
     if isinstance(val, Type):
         # Creating a new object, because ``val`` may be some derivative of Type,
         # used as a syntactic sugar, and we do not want it to confuse us later.
         return cls(val.dtype, shape=val.shape, strides=val.strides)
     elif numpy.issctype(val):
         return cls(val)
     elif hasattr(val, 'dtype') and hasattr(val, 'shape'):
         strides = val.strides if hasattr(val, 'strides') else None
         return cls(val.dtype, shape=val.shape, strides=strides)
     else:
         return cls(dtypes.detect_type(val))