Python wrapper for OpenCL enabling GPU and parallel computing with comprehensive array operations and mathematical functions
86
High-level NumPy-like GPU array interface providing familiar array operations, mathematical functions, and data manipulation. Enables seamless transition from CPU to GPU computing with automatic memory management and comprehensive array operations.
Create and manage GPU arrays with NumPy-compatible interface and automatic memory management.
class Array:
"""
GPU array with NumPy-like interface for device computation.
Attributes:
- context (Context): OpenCL context
- queue (CommandQueue): Command queue
- allocator: Memory allocator
- shape (tuple[int, ...]): Array dimensions
- dtype (numpy.dtype): Data type
- size (int): Total number of elements
- nbytes (int): Total bytes
- ndim (int): Number of dimensions
- strides (tuple[int, ...]): Memory strides
"""
def get(self, queue=None, ary=None, async_=False):
"""
Transfer array data to host.
Parameters:
- queue (CommandQueue, optional): Command queue
- ary (numpy.ndarray, optional): Destination array
- async_ (bool): Asynchronous transfer
Returns:
numpy.ndarray: Host array with data
"""
def set(self, ary, queue=None, async_=False):
"""
Transfer data from host to array.
Parameters:
- ary (numpy.ndarray): Source host array
- queue (CommandQueue, optional): Command queue
- async_ (bool): Asynchronous transfer
"""
def copy(self, queue=None):
"""Create copy of array."""
def __getitem__(self, subscript):
"""Array indexing and slicing."""
def __setitem__(self, subscript, value):
"""Array assignment with indexing."""
# Element-wise operations
def __add__(self, other):
"""Element-wise addition."""
def __sub__(self, other):
"""Element-wise subtraction."""
def __mul__(self, other):
"""Element-wise multiplication."""
def __truediv__(self, other):
"""Element-wise division."""
def __pow__(self, other):
"""Element-wise power."""
def to_device(queue, ary, allocator=None, async_=False):
"""
Transfer numpy array to device.
Parameters:
- queue (CommandQueue): Command queue
- ary (numpy.ndarray): Source array
- allocator (Allocator, optional): Memory allocator
- async_ (bool): Asynchronous transfer
Returns:
Array: GPU array with copied data
"""
def zeros(queue, shape, dtype=float, order="C", allocator=None):
"""
Create zero-filled array on device.
Parameters:
- queue (CommandQueue): Command queue
- shape (int | tuple[int, ...]): Array shape
- dtype: Data type
- order (str): Memory layout ("C" or "F")
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Zero-filled GPU array
"""
def zeros_like(ary):
"""
Create zero array with same shape and type as existing array.
Parameters:
- ary (Array): Template array
Returns:
Array: Zero-filled array
"""
def empty_like(ary):
"""
Create uninitialized array with same shape and type.
Parameters:
- ary (Array): Template array
Returns:
Array: Uninitialized array
"""
def arange(queue, *args, **kwargs):
"""
Create array with evenly spaced values.
Parameters:
- queue (CommandQueue): Command queue
- start (number): Start value
- stop (number): Stop value (exclusive)
- step (number): Step size
- dtype: Data type
Returns:
Array: Array with evenly spaced values
"""Perform mathematical operations and reductions on GPU arrays.
def sum(a, dtype=None, queue=None, slice=None):
"""
Sum array elements.
Parameters:
- a (Array): Input array
- dtype: Result data type
- queue (CommandQueue, optional): Command queue
- slice: Array slice to sum
Returns:
scalar | Array: Sum result
"""
def dot(a_gpu, b_gpu, dtype=None, queue=None):
"""
Dot product of two arrays.
Parameters:
- a_gpu (Array): First array
- b_gpu (Array): Second array
- dtype: Result data type
- queue (CommandQueue, optional): Command queue
Returns:
Array: Dot product result
"""
def vdot(a_gpu, b_gpu, dtype=None, queue=None):
"""
Vector dot product with complex conjugation.
Parameters:
- a_gpu (Array): First array (conjugated)
- b_gpu (Array): Second array
- dtype: Result data type
- queue (CommandQueue, optional): Command queue
Returns:
scalar: Dot product result
"""
def subset_dot(subset, a, b, dtype_out=None, queue=None):
"""
Dot product on array subset.
Parameters:
- subset (Array): Index subset
- a (Array): First array
- b (Array): Second array
- dtype_out: Output data type
- queue (CommandQueue, optional): Command queue
Returns:
Array: Subset dot product
"""Reshape, transpose, and manipulate array structure.
def transpose(a_gpu, axes=None):
"""
Transpose array dimensions.
Parameters:
- a_gpu (Array): Input array
- axes (tuple[int, ...], optional): Axis permutation
Returns:
Array: Transposed array
"""
def reshape(a_gpu, shape):
"""
Reshape array to new dimensions.
Parameters:
- a_gpu (Array): Input array
- shape (tuple[int, ...]): New shape
Returns:
Array: Reshaped array
"""
def as_strided(a_gpu, shape=None, strides=None):
"""
Create strided view of array.
Parameters:
- a_gpu (Array): Input array
- shape (tuple[int, ...], optional): New shape
- strides (tuple[int, ...], optional): New strides
Returns:
Array: Strided array view
"""
def concatenate(arrays, axis=0, queue=None, allocator=None):
"""
Concatenate arrays along axis.
Parameters:
- arrays (list[Array]): Arrays to concatenate
- axis (int): Concatenation axis
- queue (CommandQueue, optional): Command queue
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Concatenated array
"""
def stack(arrays, axis=0, queue=None, allocator=None):
"""
Stack arrays along new axis.
Parameters:
- arrays (list[Array]): Arrays to stack
- axis (int): New axis position
- queue (CommandQueue, optional): Command queue
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Stacked array
"""
def hstack(arrays, queue=None, allocator=None):
"""
Stack arrays horizontally (column-wise).
Parameters:
- arrays (list[Array]): Arrays to stack
- queue (CommandQueue, optional): Command queue
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Horizontally stacked array
"""Advanced indexing, element selection, and data rearrangement.
def take(a_gpu, indices, queue=None, out=None):
"""
Take elements by indices.
Parameters:
- a_gpu (Array): Source array
- indices (Array): Index array
- queue (CommandQueue, optional): Command queue
- out (Array, optional): Output array
Returns:
Array: Selected elements
"""
def multi_take(arrays, indices, queue=None, out=None):
"""
Take elements from multiple arrays using same indices.
Parameters:
- arrays (list[Array]): Source arrays
- indices (Array): Index array
- queue (CommandQueue, optional): Command queue
- out (list[Array], optional): Output arrays
Returns:
list[Array]: Selected elements from each array
"""
def multi_put(arrays, dest_indices, dest_shape, dest_dtype, queue=None, out=None):
"""
Put elements into multiple destination arrays.
Parameters:
- arrays (list[Array]): Source arrays
- dest_indices (Array): Destination indices
- dest_shape (tuple): Destination shape
- dest_dtype: Destination data type
- queue (CommandQueue, optional): Command queue
- out (list[Array], optional): Output arrays
Returns:
list[Array]: Destination arrays with put elements
"""
def multi_take_put(arrays, dest_indices, src_indices, dest_shape,
src_offsets, dest_dtype, queue=None, out=None):
"""
Combined take and put operation.
Parameters:
- arrays (list[Array]): Source arrays
- dest_indices (Array): Destination indices
- src_indices (Array): Source indices
- dest_shape (tuple): Destination shape
- src_offsets (Array): Source offsets
- dest_dtype: Destination data type
- queue (CommandQueue, optional): Command queue
- out (list[Array], optional): Output arrays
Returns:
list[Array]: Result arrays
"""Compute differences, cumulative operations, and array statistics.
def diff(a_gpu, queue=None, allocator=None):
"""
Calculate differences between consecutive elements.
Parameters:
- a_gpu (Array): Input array
- queue (CommandQueue, optional): Command queue
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Array of differences
"""
def cumsum(a_gpu, queue=None, allocator=None):
"""
Cumulative sum along array.
Parameters:
- a_gpu (Array): Input array
- queue (CommandQueue, optional): Command queue
- allocator (Allocator, optional): Memory allocator
Returns:
Array: Cumulative sum array
"""Element-wise logical operations and boolean array manipulation.
def all(a_gpu, queue=None):
"""
Test if all elements are true.
Parameters:
- a_gpu (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
bool: True if all elements are true
"""
def any(a_gpu, queue=None):
"""
Test if any elements are true.
Parameters:
- a_gpu (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
bool: True if any elements are true
"""
def logical_and(a_gpu, b_gpu, queue=None):
"""
Element-wise logical AND.
Parameters:
- a_gpu (Array): First array
- b_gpu (Array): Second array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Logical AND result
"""
def logical_or(a_gpu, b_gpu, queue=None):
"""
Element-wise logical OR.
Parameters:
- a_gpu (Array): First array
- b_gpu (Array): Second array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Logical OR result
"""
def logical_not(a_gpu, queue=None):
"""
Element-wise logical NOT.
Parameters:
- a_gpu (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Logical NOT result
"""Element-wise comparisons and conditional selection.
def maximum(a_gpu, b_gpu, queue=None):
"""
Element-wise maximum of arrays.
Parameters:
- a_gpu (Array): First array
- b_gpu (Array): Second array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Element-wise maximum
"""
def minimum(a_gpu, b_gpu, queue=None):
"""
Element-wise minimum of arrays.
Parameters:
- a_gpu (Array): First array
- b_gpu (Array): Second array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Element-wise minimum
"""
def if_positive(criterion, then_, else_, queue=None, out=None):
"""
Conditional selection based on sign.
Parameters:
- criterion (Array): Condition array
- then_ (Array): Values for positive elements
- else_ (Array): Values for non-positive elements
- queue (CommandQueue, optional): Command queue
- out (Array, optional): Output array
Returns:
Array: Conditionally selected values
"""import pyopencl as cl
import pyopencl.array as cl_array
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create arrays
a = cl_array.to_device(queue, np.random.randn(1000).astype(np.float32))
b = cl_array.to_device(queue, np.random.randn(1000).astype(np.float32))
# Element-wise operations
c = a + b
d = a * 2.0
e = cl_array.sum(a)
print(f"Sum: {e}")
print(f"First 5 elements of c: {c.get()[:5]}")import pyopencl as cl
import pyopencl.array as cl_array
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create 2D array
data = np.random.randn(100, 50).astype(np.float32)
gpu_array = cl_array.to_device(queue, data)
# Transpose
transposed = cl_array.transpose(gpu_array)
print(f"Original shape: {gpu_array.shape}")
print(f"Transposed shape: {transposed.shape}")
# Indexing and slicing
subset = gpu_array[10:20, :]
print(f"Subset shape: {subset.shape}")
# Take elements by indices
indices = cl_array.arange(queue, 0, 100, 2, dtype=np.int32) # Even indices
selected = cl_array.take(gpu_array.reshape(-1), indices)
print(f"Selected shape: {selected.shape}")import pyopencl as cl
import pyopencl.array as cl_array
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Matrix multiplication using dot product
A = cl_array.to_device(queue, np.random.randn(100, 50).astype(np.float32))
B = cl_array.to_device(queue, np.random.randn(50, 80).astype(np.float32))
# Dot product (matrix multiplication)
C = cl_array.dot(A, B)
print(f"Matrix multiplication result shape: {C.shape}")
# Vector operations
x = cl_array.to_device(queue, np.random.randn(1000).astype(np.float32))
y = cl_array.to_device(queue, np.random.randn(1000).astype(np.float32))
# Dot product
dot_result = cl_array.vdot(x, y)
print(f"Vector dot product: {dot_result}")import pyopencl as cl
import pyopencl.array as cl_array
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create array
data = cl_array.to_device(queue, np.random.randn(10000).astype(np.float32))
# Reductions
total_sum = cl_array.sum(data)
all_positive = cl_array.all(data > 0)
any_negative = cl_array.any(data < 0)
print(f"Sum: {total_sum}")
print(f"All positive: {all_positive}")
print(f"Any negative: {any_negative}")
# Cumulative operations
cumulative = cl_array.cumsum(data)
print(f"Cumulative sum shape: {cumulative.shape}")Install with Tessl CLI
npx tessl i tessl/pypi-pyopencldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10