Exemplo n.º 1
0
    def alloc(self, shape=None, cl_dtype=None, order="C", 
              like=None, flags=mem_flags.READ_WRITE):
        """Allocates an uninitialized :class:`Buffer` with provided metadata.

        ``shape``, ``cl_dtype`` and ``order``
            See :class:`Buffer` for descriptions of these. 
            
        ``like``
            If ``like`` is specified, any of the parameters ``shape``, 
            ``cl_dtype`` or ``order`` that are not explicitly specified are 
            taken from the referenced array or Buffer.
            
        ``flags``
            One or more :class:`mem_flags`. Defaults to 
            ``mem_flags.READ_WRITE``.            
        """
        if cypy.is_int_like(shape):
            shape = (shape,)
        elif shape is None:
            shape = Buffer.infer_shape(like)
            
        if cl_dtype is None:
            if like is None:
                cl_dtype = cl_float
            else:
                cl_dtype = Buffer.infer_cl_dtype(like)
                
        if order is None:
            if like is None:
                order = "C"
            else:
                order = Buffer.infer_order(like)

        return Buffer.shaped(self, shape, cl_dtype, order, flags)
Exemplo n.º 2
0
 def convert_arg(cls, arg):
     if cypy.is_int_like(arg):
         if cl_int.min <= arg <= cl_int.max:
             yield _numpy.int32(arg)
         elif cl_long.min <= arg <= cl_long.max:
             yield _numpy.int64(arg)
         else:
             raise Error("Integer-like number is out of range of long: %s" %
                         str(arg))
     elif cypy.is_float_like(arg):
         if cl_float.min <= arg <= cl_float.max:
             yield _numpy.float32(arg)
         elif cl_float.min <= arg <= cl_float.max:
             yield _numpy.float64(arg)
         else:
             raise Error("Float-like number is out of range of double: %s" %
                         str(arg))
     else:
         raise Error("Invalid argument: %s" % str(arg))
Exemplo n.º 3
0
 def shaped(cls, ctx, shape=None, cl_dtype=None, order=None,
            flags=mem_flags.READ_WRITE, hostbuf=None, constant=False):
     """Creates a Buffer with shape, element type and order metadata.        
     
     - If ``hostbuf`` is provided, it will be synchronously copied to the 
       device.
     - If mem_flags.USE_HOST_PTR is not included and a ``hostbuf`` is 
       specified, it will be added automatically.
     - Metadata will be inferred from ``hostbuf`` if not explicitly provided.
 
     ``shape``
         If an int is provided, converted to a one-dimensional tuple. 
         Dimensions be positive integers (arr). 
 
     It is highly recommended that you use the methods available in
     :class:`Context` to create buffers rather than doing so explicitly.
     """
     if cypy.is_int_like(shape):
         shape = (shape,)
     if hostbuf is not None:
         if shape is None:
             shape = cls.infer_shape(hostbuf)
         if cl_dtype is None:
             cl_dtype = cls.infer_cl_dtype(hostbuf)
         if order is None:
             try:
                 order = cls.infer_order(hostbuf)
             except Error:
                 order = "C"
         flags |= mem_flags.USE_HOST_PTR
     size = cl_dtype.sizeof_for(ctx.device)
     size *= cypy.prod(shape)
     if size <= 0:
         raise Error("Invalid buffer size %s." % str(size))
     assert size > 0
     buffer = cls(ctx, flags, size, hostbuf)
     buffer.shape = shape
     buffer.cl_dtype = cl_dtype
     buffer.order = order
     buffer.constant = constant
     return buffer
Exemplo n.º 4
0
    def from_device(self, src, shape=None, dtype=None, order=None, like=None,
                    wait_for=None, queue=None):
        """Copies the ``src`` buffer to a new array synchronously.

        If not explicitly specified, the ``shape``, ``dtype`` and ``order`` of
        the resulting array are inferred from the ``src`` buffer if possible, 
        or ``like`` if not.
        
        ``wait_for`` can be specified as a list of :class:`Event` instances to
        block on before copying.
        
        If ``queue`` is not specified, uses the default queue.
        """
        if cypy.is_int_like(shape):
            shape = (shape,)
        elif shape is None:
            try:
                shape = Buffer.infer_shape(src)
            except Error:
                shape = Buffer.infer_shape(like)

        if dtype is None:
            try:
                dtype = Buffer.infer_dtype(src)
            except Error:
                dtype = Buffer.infer_dtype(like)

        if order is None:
            try:
                order = Buffer.infer_order(src)
            except Error:
                try:
                    order = Buffer.infer_order(like)
                except:
                    order = "C"

        dest = _numpy.empty(shape, dtype, order=order)
        self.copy(src, dest, block=True, wait_for=wait_for, queue=queue).wait()
        return dest
Exemplo n.º 5
0
 def resolve_Num(self, context, node):
     n = node.n
     if cypy.is_int_like(n):
         return self.int_t
     else:
         return self.float_t