Backends API

Backend Interface

Backend abstraction layer for array operations.

This module provides a unified interface for array operations that can be executed on different backends (CPU with NumPy, GPU with CuPy, etc.).

class prismo.backends.Backend[source]

Bases: ABC

Abstract base class for computational backends.

All backends must implement this interface to provide array operations for electromagnetic field computations.

__repr__()[source]

String representation.

Return type:

str

abstractmethod abs(array)[source]

Element-wise absolute value.

Return type:

Any

abstractmethod array(data, dtype=None)[source]

Convert data to backend array.

Return type:

Any

abstractmethod asarray(data, dtype=None)[source]

Convert data to backend array (no-copy if possible).

Return type:

Any

abstract property complex128: Any

128-bit complex dtype.

abstract property complex64: Any

64-bit complex dtype.

abstractmethod copy(array)[source]

Create a copy of an array.

Return type:

Any

abstractmethod cos(array)[source]

Element-wise cosine.

Return type:

Any

abstractmethod dot(a, b)[source]

Dot product of two arrays.

Return type:

Any

abstractmethod empty(shape, dtype=None)[source]

Create an uninitialized array.

Return type:

Any

abstractmethod exp(array)[source]

Element-wise exponential.

Return type:

Any

abstractmethod fft(array, axis=-1)[source]

1D Fast Fourier Transform.

Return type:

Any

abstractmethod fft2(array)[source]

2D Fast Fourier Transform.

Return type:

Any

abstract property float32: Any

32-bit float dtype.

abstract property float64: Any

64-bit float dtype.

abstractmethod get_memory_info()[source]

Get memory usage information.

Return type:

dict

abstractmethod ifft(array, axis=-1)[source]

1D Inverse Fast Fourier Transform.

Return type:

Any

abstractmethod ifft2(array)[source]

2D Inverse Fast Fourier Transform.

Return type:

Any

abstract property int32: Any

32-bit integer dtype.

abstract property int64: Any

64-bit integer dtype.

abstract property is_gpu: bool

Whether this backend uses GPU acceleration.

abstractmethod matmul(a, b)[source]

Matrix multiplication.

Return type:

Any

abstractmethod max(array, axis=None)[source]

Maximum of array elements.

Return type:

Any

abstractmethod mean(array, axis=None)[source]

Mean of array elements.

Return type:

Any

abstractmethod min(array, axis=None)[source]

Minimum of array elements.

Return type:

Any

abstract property name: str

Name of the backend (e.g., ‘numpy’, ‘cupy’).

abstractmethod ones(shape, dtype=None)[source]

Create an array filled with ones.

Return type:

Any

abstract property pi: float

Value of pi.

abstractmethod sin(array)[source]

Element-wise sine.

Return type:

Any

abstractmethod sqrt(array)[source]

Element-wise square root.

Return type:

Any

abstractmethod sum(array, axis=None)[source]

Sum array elements.

Return type:

Any

abstractmethod synchronize()[source]

Synchronize device (for GPU backends).

Return type:

None

abstractmethod to_numpy(array)[source]

Convert backend array to NumPy array (for CPU).

Return type:

ndarray

abstractmethod where(condition, x, y)[source]

Return elements chosen from x or y depending on condition.

Return type:

Any

abstractmethod zeros(shape, dtype=None)[source]

Create an array filled with zeros.

Return type:

Any

prismo.backends.get_backend(backend=None, device_id=0)[source]

Get a backend instance.

If no backend is specified, returns the current global backend or automatically selects the best available backend (GPU preferred).

Parameters:
  • backend (str, optional) – Backend name (‘numpy’, ‘cupy’, or ‘metal’). If None, uses current or auto-detects.

  • device_id (int, optional) – Device ID for GPU backends. Default is 0.

Returns:

The backend instance.

Return type:

Backend

prismo.backends.set_backend(backend, device_id=0)[source]

Set the global backend for computations.

Parameters:
  • backend (str) – Backend name (‘numpy’, ‘cupy’, or ‘metal’).

  • device_id (int, optional) – Device ID for GPU backends. Default is 0.

Returns:

The initialized backend instance.

Return type:

Backend

Raises:

ValueError – If the requested backend is not available.

prismo.backends.list_available_backends()[source]

List all available backends on this system.

Returns:

List of backend names (e.g., [‘numpy’, ‘cupy’, ‘metal’]).

Return type:

List[str]

class prismo.backends.MetalBackend(device_id=0, use_unified_memory=True)[source]

Bases: Backend

Metal-based backend for GPU computations on macOS.

This backend uses Metal for GPU-accelerated array operations. Requires macOS and Metal framework to be available.

Parameters:
  • device_id (int, optional) – Metal device ID to use. Default is 0.

  • use_unified_memory (bool, optional) – Whether to use unified memory (shared storage mode). Default is True.

__init__(device_id=0, use_unified_memory=True)[source]
__repr__()[source]

String representation.

Return type:

str

abs(array)[source]

Element-wise absolute value.

Return type:

Any

array(data, dtype=None)[source]

Convert data to backend array.

Return type:

Any

asarray(data, dtype=None)[source]

Convert data to backend array (no-copy if possible).

Return type:

Any

property complex128: Any

128-bit complex dtype.

property complex64: Any

64-bit complex dtype.

copy(array)[source]

Create a copy of an array.

Return type:

Any

cos(array)[source]

Element-wise cosine.

Return type:

Any

dot(a, b)[source]

Dot product of two arrays.

Return type:

Any

empty(shape, dtype=None)[source]

Create an uninitialized array.

Return type:

Any

exp(array)[source]

Element-wise exponential.

Return type:

Any

fft(array, axis=-1)[source]

1D Fast Fourier Transform.

Return type:

Any

fft2(array)[source]

2D Fast Fourier Transform.

Return type:

Any

property float32: Any

32-bit float dtype.

property float64: Any

64-bit float dtype.

get_memory_info()[source]

Get Metal device memory usage information.

Return type:

dict

ifft(array, axis=-1)[source]

1D Inverse Fast Fourier Transform.

Return type:

Any

ifft2(array)[source]

2D Inverse Fast Fourier Transform.

Return type:

Any

property int32: Any

32-bit integer dtype.

property int64: Any

64-bit integer dtype.

property is_gpu: bool

Whether this backend uses GPU acceleration.

matmul(a, b)[source]

Matrix multiplication.

Return type:

Any

max(array, axis=None)[source]

Maximum of array elements.

Return type:

Any

mean(array, axis=None)[source]

Mean of array elements.

Return type:

Any

min(array, axis=None)[source]

Minimum of array elements.

Return type:

Any

property name: str

Name of the backend (e.g., ‘numpy’, ‘cupy’).

ones(shape, dtype=None)[source]

Create an array filled with ones.

Return type:

Any

property pi: float

Value of pi.

sin(array)[source]

Element-wise sine.

Return type:

Any

sqrt(array)[source]

Element-wise square root.

Return type:

Any

sum(array, axis=None)[source]

Sum array elements.

Return type:

Any

synchronize()[source]

Synchronize Metal device.

Return type:

None

to_numpy(array)[source]

Convert Metal buffer to NumPy array.

Return type:

ndarray

where(condition, x, y)[source]

Return elements chosen from x or y depending on condition.

Return type:

Any

zeros(shape, dtype=None)[source]

Create an array filled with zeros.

Return type:

Any

Backend Functions

get_backend

prismo.backends.get_backend(backend=None, device_id=0)[source]

Get a backend instance.

If no backend is specified, returns the current global backend or automatically selects the best available backend (GPU preferred).

Parameters:
  • backend (str, optional) – Backend name (‘numpy’, ‘cupy’, or ‘metal’). If None, uses current or auto-detects.

  • device_id (int, optional) – Device ID for GPU backends. Default is 0.

Returns:

The backend instance.

Return type:

Backend

set_backend

prismo.backends.set_backend(backend, device_id=0)[source]

Set the global backend for computations.

Parameters:
  • backend (str) – Backend name (‘numpy’, ‘cupy’, or ‘metal’).

  • device_id (int, optional) – Device ID for GPU backends. Default is 0.

Returns:

The initialized backend instance.

Return type:

Backend

Raises:

ValueError – If the requested backend is not available.

list_available_backends

prismo.backends.list_available_backends()[source]

List all available backends on this system.

Returns:

List of backend names (e.g., [‘numpy’, ‘cupy’, ‘metal’]).

Return type:

List[str]

Backend Classes

Backend (Abstract)

class prismo.backends.base.Backend[source]

Abstract base class for computational backends.

All backends must implement this interface to provide array operations for electromagnetic field computations.

abstract property name: str

Name of the backend (e.g., ‘numpy’, ‘cupy’).

abstract property is_gpu: bool

Whether this backend uses GPU acceleration.

abstractmethod zeros(shape, dtype=None)[source]

Create an array filled with zeros.

Return type:

Any

abstractmethod ones(shape, dtype=None)[source]

Create an array filled with ones.

Return type:

Any

abstractmethod empty(shape, dtype=None)[source]

Create an uninitialized array.

Return type:

Any

abstractmethod array(data, dtype=None)[source]

Convert data to backend array.

Return type:

Any

abstractmethod asarray(data, dtype=None)[source]

Convert data to backend array (no-copy if possible).

Return type:

Any

abstractmethod to_numpy(array)[source]

Convert backend array to NumPy array (for CPU).

Return type:

ndarray

abstractmethod copy(array)[source]

Create a copy of an array.

Return type:

Any

abstractmethod sqrt(array)[source]

Element-wise square root.

Return type:

Any

abstractmethod exp(array)[source]

Element-wise exponential.

Return type:

Any

abstractmethod sin(array)[source]

Element-wise sine.

Return type:

Any

abstractmethod cos(array)[source]

Element-wise cosine.

Return type:

Any

abstractmethod abs(array)[source]

Element-wise absolute value.

Return type:

Any

abstractmethod sum(array, axis=None)[source]

Sum array elements.

Return type:

Any

abstractmethod max(array, axis=None)[source]

Maximum of array elements.

Return type:

Any

abstractmethod min(array, axis=None)[source]

Minimum of array elements.

Return type:

Any

abstractmethod mean(array, axis=None)[source]

Mean of array elements.

Return type:

Any

abstractmethod fft(array, axis=-1)[source]

1D Fast Fourier Transform.

Return type:

Any

abstractmethod ifft(array, axis=-1)[source]

1D Inverse Fast Fourier Transform.

Return type:

Any

abstractmethod fft2(array)[source]

2D Fast Fourier Transform.

Return type:

Any

abstractmethod ifft2(array)[source]

2D Inverse Fast Fourier Transform.

Return type:

Any

abstractmethod dot(a, b)[source]

Dot product of two arrays.

Return type:

Any

abstractmethod matmul(a, b)[source]

Matrix multiplication.

Return type:

Any

abstractmethod where(condition, x, y)[source]

Return elements chosen from x or y depending on condition.

Return type:

Any

abstractmethod synchronize()[source]

Synchronize device (for GPU backends).

Return type:

None

abstractmethod get_memory_info()[source]

Get memory usage information.

Return type:

dict

abstract property float32: Any

32-bit float dtype.

abstract property float64: Any

64-bit float dtype.

abstract property complex64: Any

64-bit complex dtype.

abstract property complex128: Any

128-bit complex dtype.

abstract property int32: Any

32-bit integer dtype.

abstract property int64: Any

64-bit integer dtype.

abstract property pi: float

Value of pi.

__repr__()[source]

String representation.

Return type:

str

NumPyBackend

class prismo.backends.numpy_backend.NumPyBackend[source]

NumPy-based backend for CPU computations.

This backend uses NumPy for all array operations and is always available.

property name: str

Name of the backend (e.g., ‘numpy’, ‘cupy’).

property is_gpu: bool

Whether this backend uses GPU acceleration.

zeros(shape, dtype=None)[source]

Create an array filled with zeros.

Return type:

ndarray

ones(shape, dtype=None)[source]

Create an array filled with ones.

Return type:

ndarray

empty(shape, dtype=None)[source]

Create an uninitialized array.

Return type:

ndarray

array(data, dtype=None)[source]

Convert data to backend array.

Return type:

ndarray

asarray(data, dtype=None)[source]

Convert data to backend array (no-copy if possible).

Return type:

ndarray

to_numpy(array)[source]

Convert to NumPy array (no-op for NumPy backend).

Return type:

ndarray

copy(array)[source]

Create a copy of an array.

Return type:

ndarray

sqrt(array)[source]

Element-wise square root.

Return type:

ndarray

exp(array)[source]

Element-wise exponential.

Return type:

ndarray

sin(array)[source]

Element-wise sine.

Return type:

ndarray

cos(array)[source]

Element-wise cosine.

Return type:

ndarray

abs(array)[source]

Element-wise absolute value.

Return type:

ndarray

sum(array, axis=None)[source]

Sum array elements.

Return type:

Any

max(array, axis=None)[source]

Maximum of array elements.

Return type:

Any

min(array, axis=None)[source]

Minimum of array elements.

Return type:

Any

mean(array, axis=None)[source]

Mean of array elements.

Return type:

Any

fft(array, axis=-1)[source]

1D Fast Fourier Transform.

Return type:

ndarray

ifft(array, axis=-1)[source]

1D Inverse Fast Fourier Transform.

Return type:

ndarray

fft2(array)[source]

2D Fast Fourier Transform.

Return type:

ndarray

ifft2(array)[source]

2D Inverse Fast Fourier Transform.

Return type:

ndarray

dot(a, b)[source]

Dot product of two arrays.

Return type:

ndarray

matmul(a, b)[source]

Matrix multiplication.

Return type:

ndarray

where(condition, x, y)[source]

Return elements chosen from x or y depending on condition.

Return type:

ndarray

synchronize()[source]

No-op for CPU backend.

Return type:

None

get_memory_info()[source]

Get memory usage information (limited for CPU).

Return type:

dict

property float32: Any

32-bit float dtype.

property float64: Any

64-bit float dtype.

property complex64: Any

64-bit complex dtype.

property complex128: Any

128-bit complex dtype.

property int32: Any

32-bit integer dtype.

property int64: Any

64-bit integer dtype.

property pi: float

Value of pi.

CuPyBackend

class prismo.backends.cupy_backend.CuPyBackend(device_id=0)[source]

CuPy-based backend for GPU computations.

This backend uses CuPy for GPU-accelerated array operations. Requires CUDA and CuPy to be installed.

Parameters:

device_id (int, optional) – CUDA device ID to use. Default is 0.

__init__(device_id=0)[source]
property name: str

Name of the backend (e.g., ‘numpy’, ‘cupy’).

property is_gpu: bool

Whether this backend uses GPU acceleration.

zeros(shape, dtype=None)[source]

Create an array filled with zeros.

Return type:

Any

ones(shape, dtype=None)[source]

Create an array filled with ones.

Return type:

Any

empty(shape, dtype=None)[source]

Create an uninitialized array.

Return type:

Any

array(data, dtype=None)[source]

Convert data to backend array.

Return type:

Any

asarray(data, dtype=None)[source]

Convert data to backend array (no-copy if possible).

Return type:

Any

to_numpy(array)[source]

Convert CuPy array to NumPy array (transfers from GPU to CPU).

Return type:

ndarray

copy(array)[source]

Create a copy of an array.

Return type:

Any

sqrt(array)[source]

Element-wise square root.

Return type:

Any

exp(array)[source]

Element-wise exponential.

Return type:

Any

sin(array)[source]

Element-wise sine.

Return type:

Any

cos(array)[source]

Element-wise cosine.

Return type:

Any

abs(array)[source]

Element-wise absolute value.

Return type:

Any

sum(array, axis=None)[source]

Sum array elements.

Return type:

Any

max(array, axis=None)[source]

Maximum of array elements.

Return type:

Any

min(array, axis=None)[source]

Minimum of array elements.

Return type:

Any

mean(array, axis=None)[source]

Mean of array elements.

Return type:

Any

fft(array, axis=-1)[source]

1D Fast Fourier Transform.

Return type:

Any

ifft(array, axis=-1)[source]

1D Inverse Fast Fourier Transform.

Return type:

Any

fft2(array)[source]

2D Fast Fourier Transform.

Return type:

Any

ifft2(array)[source]

2D Inverse Fast Fourier Transform.

Return type:

Any

dot(a, b)[source]

Dot product of two arrays.

Return type:

Any

matmul(a, b)[source]

Matrix multiplication.

Return type:

Any

where(condition, x, y)[source]

Return elements chosen from x or y depending on condition.

Return type:

Any

synchronize()[source]

Synchronize CUDA device.

Return type:

None

get_memory_info()[source]

Get GPU memory usage information.

Return type:

dict

property float32: Any

32-bit float dtype.

property float64: Any

64-bit float dtype.

property complex64: Any

64-bit complex dtype.

property complex128: Any

128-bit complex dtype.

property int32: Any

32-bit integer dtype.

property int64: Any

64-bit integer dtype.

property pi: float

Value of pi.

__repr__()[source]

String representation.

Return type:

str