CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
General utility functions for array inspection, memory management, and CuPy-specific operations. These functions provide essential tools for working with GPU arrays and managing CuPy's runtime environment.
Functions to inspect array properties and get information about CuPy arrays.
def get_array_module(*args):
"""
Returns the array module for arguments.
Parameters:
- args: sequence of arrays or array-like objects
Returns:
module: cupy if any argument is cupy.ndarray, otherwise numpy
"""
def asnumpy(a, stream=None, blocking=True):
"""
Returns a copy of the array on host memory.
Parameters:
- a: cupy.ndarray, input GPU array
- stream: cupy.cuda.Stream, CUDA stream for the copy operation
- blocking: bool, if True, synchronize before returning
Returns:
numpy.ndarray: copy of array on host memory
"""
def size(a, axis=None):
"""
Return the number of elements along a given axis.
Parameters:
- a: cupy.ndarray, input array
- axis: int or None, axis along which to count elements
Returns:
int: number of elements
"""
def shape(a):
"""
Return the shape of an array.
Parameters:
- a: cupy.ndarray, input array
Returns:
tuple: shape tuple
"""
def ndim(a):
"""
Return the number of dimensions of an array.
Parameters:
- a: cupy.ndarray, input array
Returns:
int: number of dimensions
"""Functions for memory management and performance monitoring specific to GPU operations.
def get_default_memory_pool():
"""
Gets the default memory pool for GPU memory allocation.
Returns:
cupy.cuda.MemoryPool: default memory pool instance
"""
def get_default_pinned_memory_pool():
"""
Gets the default pinned memory pool for host memory allocation.
Returns:
cupy.cuda.PinnedMemoryPool: default pinned memory pool instance
"""
def may_share_memory(a, b, max_work=None):
"""
Determine if two arrays might share memory.
Parameters:
- a: cupy.ndarray, first input array
- b: cupy.ndarray, second input array
- max_work: int, maximum work to do in checking
Returns:
bool: True if arrays might share memory
"""
def shares_memory(a, b, max_work=None):
"""
Determine if two arrays share memory.
Parameters:
- a: cupy.ndarray, first input array
- b: cupy.ndarray, second input array
- max_work: int, maximum work to do in checking
Returns:
bool: True if arrays definitely share memory
"""
def byte_bounds(a):
"""
Returns pointers to the end-points of an array.
Parameters:
- a: cupy.ndarray, input array
Returns:
tuple: (low, high) memory addresses
"""Functions for debugging and testing array operations.
def who(vardict=None):
"""
Print the CuPy arrays in the given dictionary.
Parameters:
- vardict: dict, dictionary to inspect (default: globals())
Returns:
None: prints array information to stdout
"""
def show_config():
"""
Show CuPy build and runtime configuration information.
Returns:
None: prints configuration information to stdout
"""
def is_available():
"""
Returns True if CuPy is available for use.
Returns:
bool: True if CUDA/ROCm is available and CuPy can be used
"""Functions for functional programming patterns and advanced array operations.
def vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False,
signature=None):
"""
Returns an object that acts like pyfunc, but takes arrays as input.
Parameters:
- pyfunc: function, Python function to vectorize
- otypes: str or list, output data type(s)
- doc: str, docstring for vectorized function
- excluded: set, set of strings or integers representing excluded arguments
- cache: bool, whether to cache compiled kernels
- signature: str, signature for universal function
Returns:
vectorized function object
"""
def piecewise(x, condlist, funclist, *args, **kw):
"""
Evaluate a piecewise-defined function.
Parameters:
- x: cupy.ndarray, input domain
- condlist: list of bool arrays or callables, conditions for each piece
- funclist: list of functions or scalars, functions/values for each piece
- args: additional arguments for functions in funclist
- kw: additional keyword arguments for functions in funclist
Returns:
cupy.ndarray: evaluation of piecewise function
"""
def iterable(y):
"""
Check whether or not an object can be iterated over.
Parameters:
- y: object, input object to check
Returns:
bool: True if object is iterable
"""Functions for padding arrays with various padding modes.
def pad(array, pad_width, mode='constant', **kwargs):
"""
Pad an array according to the given parameters.
Parameters:
- array: cupy.ndarray, array to pad
- pad_width: sequence or int, padding width for each dimension
- mode: str or function, padding mode
- kwargs: additional arguments for padding mode
Returns:
cupy.ndarray: padded array
"""Additional utilities for working with data types and type conversion.
def binary_repr(num, width=None):
"""
Return the binary representation of the input number as a string.
Parameters:
- num: int, input number
- width: int, minimum width of output string
Returns:
str: binary representation
"""import cupy as cp
import numpy as np
# Array module detection
x_cpu = np.array([1, 2, 3])
x_gpu = cp.array([1, 2, 3])
module = cp.get_array_module(x_gpu) # Returns cupy module
module_mixed = cp.get_array_module(x_cpu, x_gpu) # Returns cupy module
# Memory transfer
gpu_array = cp.array([[1, 2, 3], [4, 5, 6]])
cpu_copy = cp.asnumpy(gpu_array) # Transfer to CPU
print(type(cpu_copy)) # <class 'numpy.ndarray'>
# Array inspection
print(f"Size: {cp.size(gpu_array)}") # Total elements
print(f"Shape: {cp.shape(gpu_array)}") # Shape tuple
print(f"Dimensions: {cp.ndim(gpu_array)}") # Number of dimensions
# Memory management
mempool = cp.get_default_memory_pool()
print(f"Memory used: {mempool.used_bytes()} bytes")
print(f"Memory free: {mempool.free_bytes()} bytes")
pinned_pool = cp.get_default_pinned_memory_pool()
print(f"Pinned memory used: {pinned_pool.used_bytes()} bytes")
# Memory sharing checks
a = cp.array([1, 2, 3, 4, 5])
b = a[1:4] # View of a
print(cp.may_share_memory(a, b)) # True - b is a view of a
print(cp.shares_memory(a, b)) # True - definite sharing
c = cp.array([1, 2, 3])
print(cp.may_share_memory(a, c)) # False - different arrays
# System information
cp.show_config() # Prints CuPy configuration
print(f"CuPy available: {cp.is_available()}")
# Debug information about arrays in scope
x = cp.array([1, 2, 3])
y = cp.random.rand(10, 10)
cp.who() # Prints information about x and y
# Vectorize a Python function
def python_func(a, b):
return a**2 + b**2
vectorized_func = cp.vectorize(python_func)
result = vectorized_func(cp.array([1, 2, 3]), cp.array([4, 5, 6]))
# Piecewise function
x = cp.linspace(-2, 2, 100)
conditions = [x < 0, x >= 0]
functions = [lambda x: x**2, lambda x: x**3]
y = cp.piecewise(x, conditions, functions)
# Array padding
arr = cp.array([[1, 2], [3, 4]])
padded = cp.pad(arr, pad_width=1, mode='constant', constant_values=0)
# Result: [[0, 0, 0, 0],
# [0, 1, 2, 0],
# [0, 3, 4, 0],
# [0, 0, 0, 0]]
# Binary representation
binary_str = cp.binary_repr(42, width=8) # '00101010'
# Check if objects are iterable
print(cp.iterable([1, 2, 3])) # True
print(cp.iterable(5)) # FalseInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3