def bind(self, array, update_device=True): """Create a new OffloadArray that is bound to an existing numpy.ndarray. If update_device is False, then one needs to call update_device() to issue a data transfer to the target device. Otherwise, the OffloadArray will be in unspecified state on the target device. Data transfers to the host overwrite the data in the bound numpy.ndarray. The operation is enqueued into the stream object and completes asynchronously. Parameters ---------- array : numpy.ndarray Array to be bound to a new OffloadArray update_device : bool, optional, default True Control if the device buffer is updated with the array data Returns ------- out : OffloadArray Instance of OffloadArray bound to the input array See Also -------- copy Examples -------- >>> a = numpy.zeros((2,2)) >>> oa = stream.bind(a) >>> b = numpy.ones((1,1)) >>> ob = stream.bind(b, update_device=False) >>> ob.update_device() """ if not isinstance(array, numpy.ndarray): raise ValueError("only numpy.ndarray can be associated " "with OffloadArray") # detect the order of storage for 'array' if array.flags.c_contiguous: order = "C" elif array.flags.f_contiguous: order = "F" else: raise ValueError("could not detect storage order") # construct and return a new OffloadArray bound = pymic.OffloadArray(array.shape, array.dtype, order, False, device=self._device, stream=self) bound.array = array # allocate the buffer on the device (and update data) bound._device_ptr = self.allocate_device_memory(bound._nbytes) if update_device: bound.update_device() return bound
def ones(self, shape, dtype=numpy.float, order='C', update_host=True): """Create a new OffloadArray that is bound to a numpy.ndarray of the given shape and data type, and that is initialized with all elements set to one of the corresponding data type. If update_host is True (the default), then the empty array is automatically transferred to the host numpy.ndarray. If set to False, the data transfer is avoided and the data in the host array is in undefined state. The operation is enqueued into the stream object and completes asynchronously. Parameters ---------- shape : int or tuple of int Shape of the empty array dtype : data-type, optional Desired output data-type of the empty array order : {'C', 'F'}, optional, default 'C' Store multi-dimensional array data in row-major order (C/C++, 'C') or column-major order (Fortran, 'F') update_host : bool, optional, default True Control if the host array is updated with the array data during construction of the OffloadArray Returns ------- out : OffloadArray Instance of OffloadArray with all elements being one See Also -------- empty, empty_like, zeros, zeros_like, ones_like, bcast, bcast_like Examples -------- >>> stream.ones((2,2)) array([[ 1., 1.], [ 1., 1.]]) >>> o = stream.ones((2,2), update_host=False) >>> o array([[ 0.00000000e+000, -3.13334375e-294], [ 2.77063169e-302, 1.16247550e-012]]) # random data >>> o.update_host() array([[ 1., 1.], [ 1., 1.]]) """ array = pymic.OffloadArray(shape, dtype, order, device=self._device, stream=self) array.one() if update_host: array.update_host() return array
def bcast(self, value, shape, dtype=numpy.float, order='C', update_host=True): """Create a new OffloadArray that is bound to a numpy.ndarray of the given shape and data type, and that is initialized with all elements set to a given value of the corresponding data type. If update_host is True (the default), then the empty array is automatically transferred to the host numpy.ndarray. If set to False, the data transfer is avoided and the data in the host array is in undefined state. The operation is enqueued into the stream object and completes asynchronously. Parameters ---------- value : scalar type Value to broadcast to all elements of the created array shape : int or tuple of int Shape of the empty array dtype : data-type, optional Desired output data-type of the empty array order : {'C', 'F'}, optional, default 'C' Store multi-dimensional array data in row-major order (C/C++, 'C') or column-major order (Fortran, 'F') update_host : bool, optional, default True Control if the host array is updated with the array data during construction of the OffloadArray Returns ------- out : OffloadArray Instance of OffloadArray with all elements being set to a given value See Also -------- empty, empty_like, zeros, zeros_like, ones, ones_like, bcast_like Examples -------- >>> stream.bcast(42.0, (2,2)) array([[ 42., 42.], [ 42., 42.]]) >>> o = stream.bcast(42.0, (2,2), update_host=False) >>> o array([[ 0.00000000e+000, 3.24370112e+178], [ 2.53946330e-292, 6.52391278e+091]]) # random data >>> o.update_host() array([[ 42., 42.], [ 42., 42.]]) """ array = pymic.OffloadArray(shape, dtype, order, device=self._device, stream=self) array.fill(value) # TODO: here the stream might become an issue if update_host: array.update_host() return array
def empty(self, shape, dtype=numpy.float, order='C', update_host=True): """Create a new OffloadArray that is bound to an empty numpy.ndarray of the given shape and data type. If update_host is True (the default), then the empty array is automatically transferred to the host numpy.ndarray. If set to False, the data transfer is avoided and the data in the host array is in undefined state. The operation is enqueued into the stream object and completes asynchronously. Parameters ---------- shape : int or tuple of int Shape of the empty array dtype : data-type, optional Desired output data-type of the empty array order : {'C', 'F'}, optional, default 'C' Store multi-dimensional array data in row-major order (C/C++, 'C') or column-major order (Fortran, 'F') update_host : bool, optional, default True Control if the host array is updated with the array data during construction of the OffloadArray Returns ------- out : OffloadArray Instance of OffloadArray bound to the input array See Also -------- empty_like, zeros, zeros_like, ones, ones_like, bcast, bcast_like Examples -------- >>> stream.empty((2,2)) array([[ 4.41694687e-321, 6.92646058e-310], [ 4.71338626e-321, 6.92646058e-310]]) # random data >>> o = stream.empty((2,2), update_host=False) >>> o array([[ 0.00000000e+000, -3.13334375e-294], [ 2.77063169e-302, 1.76125728e-312]]) # random data >>> o.update_host() array([[ 4.26872718e-321, 6.92050581e-310], [ 4.34283703e-321, 6.92050581e-310]]) # random data """ array = pymic.OffloadArray(shape, dtype, order, device=self._device, stream=self) if update_host: array.update_host() return array