Dolphin darray
- class dolphin.darray(shape: Optional[Tuple[int, ...]] = None, dtype: dtype = dtype.float32, stream: Optional[pycuda.driver.Stream] = None, array: Optional[ndarray] = None, strides: Optional[Tuple[int, ...]] = None, allocation: Optional[pycuda.driver.DeviceAllocation] = None, allocation_size: Optional[pycuda.driver.DeviceAllocation] = None)[source]
Bases:
object
This class implements a generic numpy style array that can be used with the dolphin library. It implements common features available with numpy arrays such as astype, transpose, copy…
darray is made with the same philosophy as numpy.ndarray. The usability is really close to numpy arrays. However, darray is meant to be much more performant than numpy.ndarray since it is GPU accelerated.
- Parameters:
shape (Tuple[int, ...], optional) – Shape of the darray, defaults to None
dtype (dolphin.dtype, optional) – dtype of the darray, defaults to None
stream (cuda.Stream, optional) – CUDA stream to use, defaults to None
array (numpy.ndarray, optional) – numpy array to copy, defaults to None
strides (Tuple[int, ...], optional) – strides of the darray, defaults to None
allocation (cuda.DeviceAllocation, optional) – CUDA allocation to use, defaults to None
allocation_size (int, optional) – Size of the allocation, defaults to None
- __init__(shape: Optional[Tuple[int, ...]] = None, dtype: dtype = dtype.float32, stream: Optional[pycuda.driver.Stream] = None, array: Optional[ndarray] = None, strides: Optional[Tuple[int, ...]] = None, allocation: Optional[pycuda.driver.DeviceAllocation] = None, allocation_size: Optional[pycuda.driver.DeviceAllocation] = None) None [source]
- static broadcastable(shape_1: Tuple[int, ...], shape_2: Tuple[int, ...]) bool [source]
Checks if two shapes are broadcastable.
- Parameters:
shape_1 (Tuple[int, ...]) – First shape
shape_2 (Tuple[int, ...]) – Second shape
- Returns:
True if the shapes are broadcastable, False otherwise
- Return type:
bool
- static compute_strides(shape: Tuple[int, ...]) Tuple[int, ...] [source]
Computes the strides of an array from the shape. The strides are the number of elements to skip to get to the next element. Also, the strides are in elements, not bytes.
- Parameters:
shape (Tuple[int, ...]) – shape of the ndarray
- Returns:
Strides
- Return type:
Tuple[int, …]
- property shape_allocation: pycuda.driver.DeviceAllocation
Property to access the cuda allocation of the shape.
- Returns:
The cuda allocation of the shape
- Return type:
cuda.DeviceAllocation
- property strides_allocation: pycuda.driver.DeviceAllocation
Property to access the cuda allocation of the strides.
- Returns:
The cuda allocation of the strides
- Return type:
cuda.DeviceAllocation
- property ndim: uint32
Computes the number of dimensions of the array.
- Returns:
Number of dimensions of the array
- Return type:
numpy.uint32
- property strides: Tuple[int, ...]
Property to access the strides of the array.
- Returns:
Strides of the array
- Return type:
Tuple[int, …]
- property size: uint32
Property to access the size of the array. Size is defined as the number of elements in the array.
- Returns:
The size of the array, in terms of number of elements
- Return type:
numpy.uint32
- property dtype: dtype
Property to access the dolphin.dtype of the array
- Returns:
dolphin.dtype of the array
- Return type:
- property shape: Tuple[int, ...]
Property to access the shape of the array
- Returns:
Shape of the array
- Return type:
Tuple[int, …]
- property nbytes: int
Property to access the number of bytes of the array
- Returns:
Number of bytes of the array
- Return type:
int
- property T: darray
Performs a transpose operation on the darray. This transpose reverse the order of the axes:
a = darray(shape=(2, 3, 4)) a.T.shape >>> (4, 3, 2)
Also, please note that this function is not efficient as it performs a copy of the darray.
- Returns:
Transposed darray
- Return type:
- property stream: pycuda.driver.Stream
Property to access (Read/Write) the cuda stream of the array
- Returns:
Stream used by the darray
- Return type:
cuda.Stream
- property allocation: pycuda.driver.DeviceAllocation
Property to access (Read/Write) the cuda allocation of the array
- Returns:
The cuda allocation of the array
- Return type:
cuda.DeviceAllocation
- property np: ndarray
Alias for to_numpy()
- Returns:
numpy.ndarray of the darray
- Return type:
numpy.ndarray
- to_numpy() ndarray [source]
Converts the darray to a numpy.ndarray. Note that a copy from the device to the host is performed.
- Returns:
numpy.ndarray of the darray
- Return type:
numpy.ndarray
- from_numpy(array: ndarray) None [source]
Writes allocation from a numpy array. If the array is not the same shape or dtype as the darray, an error is raised.
- Parameters:
array (numpy.ndarray) – Numpy array create the darray from
- astype(dtype: dtype, dst: Optional[darray] = None) darray [source]
Converts the darray to a different dtype. Note that a copy from device to device is performed.
- Parameters:
dtype (dolphin.dtype) – dtype to convert to
dst (darray, optional) – darray to write the result of the operation, defaults to None
- Raises:
ValueError – In case the dst shape or dtype doesn’t match
- Returns:
darray with the new dtype
- Return type:
- transpose(*axes: int) darray [source]
Transposes the darray according to the axes.
- Parameters:
axes (Tuple[int]) – Axes to permute
- Returns:
Transposed darray
- Return type:
- copy() darray [source]
Returns a copy of the current darray. Note that a copy from device to device is performed.
- Returns:
Copy of the array with another cuda allocation
- Return type:
- __getitem__(index: Union[int, slice, tuple]) darray [source]
Returns a view of the darray with the given index.
- Parameters:
index (Union[int, slice, tuple]) – Index to use
- Raises:
IndexError – Too many indexes. The number of indexes must be less than the number of dimensions of the array.
IndexError – Axes must be in the range of the number of dimensions of the array.
- Returns:
View of the darray
- Return type:
- __setitem__(index: Union[int, slice, tuple], other: Union[int, float, number, darray]) None [source]
Sets the value of the darray with the given index.
- __str__() str [source]
Returns the string representation of the numpy array. Note that a copy from the device to the host is performed.
- Returns:
String representation of the numpy array
- Return type:
str
- __repr__() str [source]
Returns the representation of the numpy array. Note that a copy from the device to the host is performed.
- Returns:
Representation of the numpy array
- Return type:
str
- flatten(dst: Optional[darray] = None) darray [source]
Returns a flattened view of the darray. Order = C.
- fill(value: Union[int, float, number]) darray [source]
Fills the darray with the value of value.
- Parameters:
value (Union[int, float, numpy.number]) – Value to fill the array with
- Returns:
Filled darray
- Return type:
- add(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient addition of a darray with another object. Can be a darray or a scalar. If dst is None, normal __add__ is called. This method is much more efficient than the __add__ method because __add__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- __add__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient addition of a darray with another object. It is not efficient because it implies a copy of the array where the result is written. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to add
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
A copy of the darray where the result is written
- Return type:
- __radd__(other: Union[int, float, number, darray]) darray
Non-Efficient addition of a darray with another object. It is not efficient because it implies a copy of the array where the result is written. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to add
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
A copy of the darray where the result is written
- Return type:
- __iadd__(other: Union[int, float, number, darray]) darray [source]
Implements += operator. As __add__, this method is not efficient because it implies a copy of the array where the usage of cuda.memalloc which is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to add
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- substract(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient substraction of a darray with another object.
- sub(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray
Efficient substraction of a darray with another object.
- __sub__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient substraction of a darray with another object. It is not efficient because it implies a copy of the array where the result is written. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to substract
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
A copy of the darray where the result is written
- Return type:
- reversed_substract(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient reverse substraction of an object with darray. It is efficient if dst is provided because it does not invoke cuda.memalloc. If dst is not provided, normal __rsub__ is called.
- __rsub__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient substraction of another object with darray. It is not efficient because it implies a copy of the array where the result is written. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to substract
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
A copy of the darray where the result is written
- Return type:
- __isub__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient -= operation. It is not efficient because it implies a copy of the array where the result is written. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to substract
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
A copy of the darray where the result is written
- Return type:
- multiply(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient multiplication of a darray with another object. Can be a darray or a scalar. If dst is None, normal __add__ is called. This method is much more efficient than the __mul__ method because __mul__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- Parameters:
dst (darray) – darray where to write the result
other (Union[int, float, numpy.number, 'darray']) – numpy.ndarray or scalar to multiply
- Raises:
ValueError – If the size, dtype or shape of dst is not matching
- Returns:
darray where the result is written. Can be dst or self
- Return type:
- mul(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray
Efficient multiplication of a darray with another object. Can be a darray or a scalar. If dst is None, normal __add__ is called. This method is much more efficient than the __mul__ method because __mul__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- Parameters:
dst (darray) – darray where to write the result
other (Union[int, float, numpy.number, 'darray']) – numpy.ndarray or scalar to multiply
- Raises:
ValueError – If the size, dtype or shape of dst is not matching
- Returns:
darray where the result is written. Can be dst or self
- Return type:
- __mul__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient multiplication of a darray with another object. This multiplication is element-wise multiplication, not matrix multiplication. For matrix multiplication please refer to matmul. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to multiply
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __rmul__(other: Union[int, float, number, darray]) darray
Non-Efficient multiplication of a darray with another object. This multiplication is element-wise multiplication, not matrix multiplication. For matrix multiplication please refer to matmul. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to multiply
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __imul__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient multiplication of a darray with another object. This multiplication is element-wise multiplication, not matrix multiplication. For matrix multiplication please refer to matmul. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to multiply
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- divide(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient division of a darray with another object. Can be a darray or a scalar. If dst is None, normal __div__ is called. This method is much more efficient than the __div__ method because __div__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- div(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray
Efficient division of a darray with another object. Can be a darray or a scalar. If dst is None, normal __div__ is called. This method is much more efficient than the __div__ method because __div__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- __div__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient division of a darray with another object. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- reversed_divide(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Efficient division of a darray with another object.
Can be a darray or a scalar. If dst is None, normal __rdiv__ is called. This method is much more efficient than the __rdiv__ method because __rdiv__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- rdiv(other: Union[int, float, number, darray], dst: Optional[darray] = None) darray
Efficient division of a darray with another object.
Can be a darray or a scalar. If dst is None, normal __rdiv__ is called. This method is much more efficient than the __rdiv__ method because __rdiv__ implies a copy of the array. cuda.memalloc is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only).
- __rdiv__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient reverse division of an object by darray. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __idiv__(other: Union[int, float, number, darray]) darray [source]
Non-Efficient division of a darray with another object. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __truediv__(other: Union[int, float, number, darray]) darray
Non-Efficient division of a darray with another object. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __itruediv__(other: Union[int, float, number, darray]) darray
Non-Efficient division of a darray with another object. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __rtruediv__(other: Union[int, float, number, darray]) darray
Non-Efficient reverse division of an object by darray. This division is element-wise. This operation is not efficient because it implies a copy of the array using cuda.memalloc. This is really time consuming (up to 95% of the total latency is spent in cuda.memalloc only)
- Parameters:
other (Union[int, float, numpy.number, 'darray']) – scalar or darray to divide by
- Raises:
ValueError – If other is not a scalar or a darray
- Returns:
The darray where the result is written
- Return type:
- __len__() int [source]
Returns the length of the first dimension of the array.
- Returns:
Size of the first dimension of the array
- Return type:
int
- __abs__() darray [source]
Returns the absolute value of the array.
- Returns:
Absolute value of the array
- Return type:
- absolute(dst: Optional[darray] = None) darray [source]
Returns the absolute value of the array.
- Returns:
Absolute value of the array
- Return type:
- __module__ = 'dolphin.core.darray'
Built-in functions
- dolphin.zeros(shape: Tuple[int, ...], dtype: dtype = dtype.float32) darray [source]
Returns a darray for a given shape and dtype filled with zeros.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- Parameters:
shape (Tuple[int, ...]) – Shape of the array
dtype (dolphin.dtype) – Type of the array
- Returns:
darray filled with zeros
- Return type:
- dolphin.zeros_like(other: Union[darray, array]) darray [source]
Returns a darray filled with zeros with the same shape and dtype as another darray.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- dolphin.empty(shape: Tuple[int, ...], dtype: dtype = dtype.float32) darray [source]
Returns a darray of a given shape and dtype without initializing entries.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- Parameters:
shape (Tuple[int, ...]) – Shape of the array
dtype (dolphin.dtype) – Type of the array
- Returns:
darray filled with random values
- Return type:
- dolphin.empty_like(other: Union[darray, array]) darray [source]
Returns a darray without initializing entries with the same shape and dtype as another darray.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- dolphin.ones(shape: Tuple[int, ...], dtype: dtype = dtype.float32) darray [source]
Returns a darray for a given shape and dtype filled with ones.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- Parameters:
shape (Tuple[int, ...]) – Shape of the array
dtype (dolphin.dtype) – Type of the array
- Returns:
darray filled with ones
- Return type:
- dolphin.ones_like(other: Union[darray, array]) darray [source]
Returns a darray filled with ones with the same shape and dtype as another darray.
This function is a creation function, thus, it does not take an optional destination darray as argument.
- dolphin.transpose(src: darray, axes: Tuple[int, ...]) darray [source]
Returns a darray with the axes transposed.
- dolphin.add(src: darray, other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Returns the addition of two darrays. It works that way:
result = src + other
- dolphin.multiply(src: darray, other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Returns the multiplication of two darrays. It works that way:
result = src * other
- dolphin.divide(src: darray, other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Returns the division of a darray by an object. It works that way:
result = src / other
- dolphin.reversed_divide(src: darray, other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Returns the division of a darray and an object. It works that way:
result = other / src
- dolphin.substract(src: darray, other: Union[int, float, number, darray], dst: Optional[darray] = None) darray [source]
Returns the substraction of two darrays. It works that way:
result = src - other