def __init__(self, data=None, owner=None, dtype=None, shape=None, order=None): # Checks of parameters if data is None: raise TypeError("To create an empty Array, use the class method" + " Array.empty().") elif isinstance(data, memoryview): data = np.asarray(data) if _check_low_level_type(data): if dtype is None or shape is None or order is None: raise TypeError( "Need to specify dtype, shape and order when" + " creating an Array from {}.".format(type(data))) detailed_construction = True elif dtype is not None and shape is not None and order is not None: detailed_construction = True else: detailed_construction = False ary_interface = False # Base class (Buffer) constructor call size, shape = _get_size_from_shape(shape, dtype) super(CumlArray, self).__init__(data=data, owner=owner, size=size) # Post processing of meta data if detailed_construction: self.shape = shape self.dtype = np.dtype(dtype) self.order = order self.strides = _order_to_strides(order, shape, dtype) elif hasattr(data, "__array_interface__"): ary_interface = data.__array_interface__ elif hasattr(data, "__cuda_array_interface__"): ary_interface = data.__cuda_array_interface__ else: raise TypeError("Unrecognized data type: %s" % str(type(data))) if ary_interface: self.shape = ary_interface['shape'] self.dtype = np.dtype(ary_interface['typestr']) if ary_interface.get('strides', None) is None: self.order = 'C' self.strides = _order_to_strides(self.order, self.shape, self.dtype) else: self.strides = ary_interface['strides'] self.order = _strides_to_order(self.strides, self.dtype)
def full(cls, shape, value, dtype, order='F'): """ Create an Array with an allocated DeviceBuffer initialized to value. Parameters ---------- dtype : data-type, optional Any object that can be interpreted as a numpy or cupy data type. shape : int or tuple of ints, optional Shape of created array. order: string, optional Whether to create a F-major or C-major array. """ size, _ = _get_size_from_shape(shape, dtype) dbuf = DeviceBuffer(size=size) cp.asarray(dbuf).view(dtype=dtype).fill(value) return CumlArray(data=dbuf, shape=shape, dtype=dtype, order=order)
def test_array_init_from_bytes(data_type, dtype, shape, order): dtype = np.dtype(dtype) bts = bytes(_get_size_from_shape(shape, dtype)[0]) if data_type != bytes: bts = data_type(bts) ary = CumlArray(bts, dtype=dtype, shape=shape, order=order) if shape == (10, 5): assert ary.order == order if shape == 10: assert ary.shape == (10, ) else: assert ary.shape == shape assert ary.dtype == dtype cp_ary = cp.zeros(shape, dtype=dtype) assert cp.all(cp.asarray(cp_ary) == cp_ary)