Example #1
0
def _make_strides(itemsize, shape, order):
    if order == "F":
        return _f_contiguous_strides(itemsize, shape)
    elif order == "C":
        return _c_contiguous_strides(itemsize, shape)
    else:
        raise ValueError("invalid order: %s" % order)
Example #2
0
def _make_strides(itemsize, shape, order):
    if order == "F":
        return _f_contiguous_strides(itemsize, shape)
    elif order == "C":
        return _c_contiguous_strides(itemsize, shape)
    else:
        raise ValueError("invalid order: %s" % order)
Example #3
0
    def __init__(self,
                 cqa,
                 shape,
                 dtype,
                 order="C",
                 allocator=None,
                 data=None,
                 queue=None,
                 strides=None):
        # {{{ backward compatibility

        from warnings import warn
        if queue is not None:
            warn(
                "Passing the queue to the array through anything but the "
                "first argument of the Array constructor is deprecated. "
                "This will be continue to be accepted throughout the 2013.[0-6] "
                "versions of PyOpenCL.", DeprecationWarning, 2)

        if isinstance(cqa, cl.CommandQueue):
            if queue is not None:
                raise TypeError("can't specify queue in 'cqa' and "
                                "'queue' arguments")
            queue = cqa

        elif isinstance(cqa, cl.Context):
            warn(
                "Passing a context for the 'cqa' parameter is deprecated. "
                "This usage will be continue to be accepted throughout the 2013.[0-6] "
                "versions of PyOpenCL.", DeprecationWarning, 2)

            if queue is not None:
                raise TypeError("may not pass a context and a queue "
                                "(just pass the queue)")
            if allocator is not None:
                raise TypeError("may not pass a context and an allocator "
                                "(just pass the queue)")

        else:
            # cqa is assumed to be an allocator
            warn(
                "Passing an allocator for the 'cqa' parameter is deprecated. "
                "This usage will be continue to be accepted throughout the 2013.[0-6] "
                "versions of PyOpenCL.", DeprecationWarning, 2)
            if allocator is not None:
                raise TypeError("can't specify allocator in 'cqa' and "
                                "'allocator' arguments")

            allocator = cqa

        if queue is None:
            warn(
                "Queue-less arrays are deprecated. "
                "They will continue to work throughout the 2013.[0-6] "
                "versions of PyOpenCL.", DeprecationWarning, 2)

        # }}}

        # invariant here: allocator, queue set

        # {{{ determine shape and strides
        dtype = np.dtype(dtype)

        try:
            s = 1
            for dim in shape:
                s *= dim
        except TypeError:
            import sys
            if sys.version_info >= (3, ):
                admissible_types = (int, np.integer)
            else:
                admissible_types = (int, long, np.integer)

            if not isinstance(shape, admissible_types):
                raise TypeError("shape must either be iterable or "
                                "castable to an integer")
            s = shape
            shape = (shape, )

        if isinstance(s, np.integer):
            # bombs if s is a Python integer
            s = np.asscalar(s)

        if strides is None:
            if order == "F":
                strides = _f_contiguous_strides(dtype.itemsize, shape)
            elif order == "C":
                strides = _c_contiguous_strides(dtype.itemsize, shape)
            else:
                raise ValueError("invalid order: %s" % order)

        else:
            # FIXME: We should possibly perform some plausibility
            # checking on 'strides' here.

            strides = tuple(strides)

        # }}}

        self.queue = queue
        self.shape = shape
        self.dtype = dtype
        self.strides = strides

        self.size = s
        alloc_nbytes = self.nbytes = self.dtype.itemsize * self.size

        self.allocator = allocator

        if data is None:
            if not alloc_nbytes:
                # Work around CL not allowing zero-sized buffers.
                alloc_nbytes = 1

            if allocator is None:
                # FIXME remove me when queues become required
                if queue is not None:
                    context = queue.context

                self.data = cl.Buffer(context, cl.mem_flags.READ_WRITE,
                                      alloc_nbytes)
            else:
                self.data = self.allocator(alloc_nbytes)
        else:
            self.data = data
Example #4
0
    def __init__(self, cqa, shape, dtype, order="C", allocator=None,
            base=None, data=None, queue=None, strides=None):
        # {{{ backward compatibility for pre-cqa days

        if isinstance(cqa, cl.CommandQueue):
            if queue is not None:
                raise TypeError("can't specify queue in 'cqa' and "
                        "'queue' arguments")
            queue = cqa

            if allocator is None: 
                context = queue.context
                allocator = DefaultAllocator(context)

        elif isinstance(cqa, cl.Context):
            if queue is not None: 
                _should_be_cqa("queue")

            if allocator is not None: 
                _should_be_cqa("allocator")
            else:
                allocator = DefaultAllocator(cqa)

        else:
            # cqa is assumed to be an allocator
            if allocator is not None:
                raise TypeError("can't specify allocator in 'cqa' and "
                        "'allocator' arguments")

            allocator = cqa

        # }}}

        # invariant here: allocator, queue set

        # {{{ determine shape and strides
        dtype = np.dtype(dtype)

        try:
            s = 1
            for dim in shape:
                s *= dim
        except TypeError:
            if not isinstance(shape, int):
                raise TypeError("shape must either be iterable or "
                        "castable to an integer")
            s = shape
            shape = (shape,)

        if strides is None:
            if order == "F":
                strides = _f_contiguous_strides(
                        dtype.itemsize, shape)
            elif order == "C":
                strides = _c_contiguous_strides(
                        dtype.itemsize, shape)
            else:
                raise ValueError("invalid order: %s" % order)
        else:
            # FIXME: We should possibly perform some plausibility
            # checking on 'strides' here.

            strides = tuple(strides)

        # }}}

        self.queue = queue
        self.shape = shape
        self.dtype = dtype
        self.strides = strides

        self.mem_size = self.size = s
        self.nbytes = self.dtype.itemsize * self.size

        self.allocator = allocator
        if data is None:
            if self.size:
                self.data = self.allocator(self.size * self.dtype.itemsize)
            else:
                self.data = None

            if base is not None:
                raise ValueError("If data is specified, base must be None.")
        else:
            self.data = data

        self.base = base

        self.context = self.data.context
Example #5
0
    def __init__(self, cqa, shape, dtype, order="C", allocator=None,
            data=None, queue=None, strides=None):
        # {{{ backward compatibility

        from warnings import warn
        if queue is not None:
            warn("Passing the queue to the array through anything but the "
                    "first argument of the Array constructor is deprecated. "
                    "This will be continue to be accepted throughout the 2013.[0-6] "
                    "versions of PyOpenCL.",
                    DeprecationWarning, 2)

        if isinstance(cqa, cl.CommandQueue):
            if queue is not None:
                raise TypeError("can't specify queue in 'cqa' and "
                        "'queue' arguments")
            queue = cqa

        elif isinstance(cqa, cl.Context):
            warn("Passing a context for the 'cqa' parameter is deprecated. "
                    "This usage will be continue to be accepted throughout the 2013.[0-6] "
                    "versions of PyOpenCL.",
                    DeprecationWarning, 2)

            if queue is not None:
                raise TypeError("may not pass a context and a queue "
                        "(just pass the queue)")
            if allocator is not None:
                raise TypeError("may not pass a context and an allocator "
                        "(just pass the queue)")

        else:
            # cqa is assumed to be an allocator
            warn("Passing an allocator for the 'cqa' parameter is deprecated. "
                    "This usage will be continue to be accepted throughout the 2013.[0-6] "
                    "versions of PyOpenCL.",
                    DeprecationWarning, 2)
            if allocator is not None:
                raise TypeError("can't specify allocator in 'cqa' and "
                        "'allocator' arguments")

            allocator = cqa

        if queue is None:
            warn("Queue-less arrays are deprecated. "
                    "They will continue to work throughout the 2013.[0-6] "
                    "versions of PyOpenCL.",
                    DeprecationWarning, 2)

        # }}}

        # invariant here: allocator, queue set

        # {{{ determine shape and strides
        dtype = np.dtype(dtype)

        try:
            s = 1
            for dim in shape:
                s *= dim
        except TypeError:
            import sys
            if sys.version_info >= (3,):
                admissible_types = (int, np.integer)
            else:
                admissible_types = (int, long, np.integer)

            if not isinstance(shape, admissible_types):
                raise TypeError("shape must either be iterable or "
                        "castable to an integer")
            s = shape
            shape = (shape,)

        if isinstance(s, np.integer):
            # bombs if s is a Python integer
            s = np.asscalar(s)

        if strides is None:
            if order == "F":
                strides = _f_contiguous_strides(
                        dtype.itemsize, shape)
            elif order == "C":
                strides = _c_contiguous_strides(
                        dtype.itemsize, shape)
            else:
                raise ValueError("invalid order: %s" % order)

        else:
            # FIXME: We should possibly perform some plausibility
            # checking on 'strides' here.

            strides = tuple(strides)

        # }}}

        self.queue = queue
        self.shape = shape
        self.dtype = dtype
        self.strides = strides

        self.size = s
        alloc_nbytes = self.nbytes = self.dtype.itemsize * self.size

        self.allocator = allocator

        if data is None:
            if not alloc_nbytes:
                # Work around CL not allowing zero-sized buffers.
                alloc_nbytes = 1

            if allocator is None:
                # FIXME remove me when queues become required
                if queue is not None:
                    context = queue.context

                self.data = cl.Buffer(context, cl.mem_flags.READ_WRITE, alloc_nbytes)
            else:
                self.data = self.allocator(alloc_nbytes)
        else:
            self.data = data
Example #6
0
    def __init__(self,
                 cqa,
                 shape,
                 dtype,
                 order="C",
                 allocator=None,
                 base=None,
                 data=None,
                 queue=None,
                 strides=None):
        # {{{ backward compatibility for pre-cqa days

        if isinstance(cqa, cl.CommandQueue):
            if queue is not None:
                raise TypeError("can't specify queue in 'cqa' and "
                                "'queue' arguments")
            queue = cqa

            if allocator is None:
                context = queue.context
                allocator = DefaultAllocator(context)

        elif isinstance(cqa, cl.Context):
            if queue is not None:
                _should_be_cqa("queue")

            if allocator is not None:
                _should_be_cqa("allocator")
            else:
                allocator = DefaultAllocator(cqa)

        else:
            # cqa is assumed to be an allocator
            if allocator is not None:
                raise TypeError("can't specify allocator in 'cqa' and "
                                "'allocator' arguments")

            allocator = cqa

        # }}}

        # invariant here: allocator, queue set

        # {{{ determine shape and strides
        dtype = np.dtype(dtype)

        try:
            s = 1
            for dim in shape:
                s *= dim
        except TypeError:
            import sys
            if sys.version_info >= (3, ):
                admissible_types = (int, np.integer)
            else:
                admissible_types = (int, long, np.integer)

            if not isinstance(shape, admissible_types):
                raise TypeError("shape must either be iterable or "
                                "castable to an integer")
            s = shape
            shape = (shape, )

        if strides is None:
            if order == "F":
                strides = _f_contiguous_strides(dtype.itemsize, shape)
            elif order == "C":
                strides = _c_contiguous_strides(dtype.itemsize, shape)
            else:
                raise ValueError("invalid order: %s" % order)
        else:
            # FIXME: We should possibly perform some plausibility
            # checking on 'strides' here.

            strides = tuple(strides)

        # }}}

        self.queue = queue
        self.shape = shape
        self.dtype = dtype
        self.strides = strides

        self.mem_size = self.size = s
        self.nbytes = self.dtype.itemsize * self.size

        self.allocator = allocator
        if data is None:
            if self.size:
                self.data = self.allocator(self.size * self.dtype.itemsize)
            else:
                self.data = None
        else:
            self.data = data

        self.base = base