sporco.cupy package

This subpackage provides GPU acceleration for selected SPORCO modules via copies of these modules that are patched to replace NumPy arrays and operations with the equivalent ones provided by CuPy. The boolean value of attribute sporco.cupy.have_cupy indicates whether CuPy is installed and a GPU device is available. The modules within the sporco.cupy subpackage can still be used when sporco.cupy.have_cupy is False, but they will not be GPU accelerated.

Note that the sporco.cupy subpackage is not supported under versions of Python, such as Python 2.7.x, that do not have the importlib.util module.

See also

NumPy/CuPy compatibility
Table of NumPy functions that are implemented in CuPy

Installation and Configuration

To use CuPy, first install CUDA and then install CuPy. Note that it may be necessary to set the environment variables as in (substitute the appropriate path to the CUDA installation)

export CUDAHOME=/usr/local/cuda-9.2
export PATH=${CUDAHOME}/bin:${PATH}

to avoid a cupy.cuda.compiler.CompileException when using CuPy. If this does not rectify the problem, the following may also be necessary:


Utility Functions

Since it is necessary to explicitly convert between NumPy arrays and CuPy arrays, a number of utility functions in sporco.cupy support this conversion in a way that behaves correctly independent of the value of sporco.cupy.have_cupy, in that conversion is performed when the value is True, and no conversion is perfomed when it is False.


Get the array module (numpy or cupy) of the array argument. This function is an alias for cupy.get_array_module.


Convert a numpy ndarray to a cupy array. This function is an alias for cupy.asarray


Convert a cupy array to a numpy ndarray. This function is an alias for cupy.asnumpy


A wrapper function that converts numpy ndarray arguments to cupy arrays, and convert any cupy arrays returned by the wrapped function into numpy ndarrays.

Some additional utility functions provide useful functionality when package GPUtil is installed, and return fixed default return values when it is not installed:


Return a list of namedtuples representing attributes of each GPU device. Returns an empty list if GPUtil is not installed.

sporco.cupy.gpu_load(wproc=0.5, wmem=0.5)

Return a list of namedtuples representing the current load for each GPU device. The processor and memory loads are fractions between 0 and 1. The weighted load represents a weighted average of processor and memory loads using the parameters wproc and wmem respectively. Returns an empty list if GPUtil is not installed.

sporco.cupy.device_by_load(wproc=0.5, wmem=0.5)

Get a list of GPU device ids ordered by increasing weighted average of processor and memory load. Returns an empty list if GPUtil is not installed.

sporco.cupy.select_device_by_load(wproc=0.5, wmem=0.5)

Set the current device for cupy as the device with the lowest weighted average of processor and memory load. Returns 0 if GPUtil is not installed.

sporco.cupy.available_gpu(*args, **kwargs)

Get the device id for an available GPU when multiple GPUs are installed. This function is an alias for GPUtil.getAvailable. Returns 0 if GPUtil is not installed.

The current GPU device can be also selected using cupy.cuda.Device.use, e.g. to select device id 1


CuPy also provides a context manager for GPU device selection.

Supported Modules

The sporco.cupy subpackage currently provides CuPy acceleration of the following standard sporco modules:

sporco.cupy module sporco module
sporco.cupy.cnvrep sporco.cnvrep
sporco.cupy.common sporco.common
sporco.cupy.linalg sporco.linalg
sporco.cupy.metric sporco.metric
sporco.cupy.prox sporco.prox
sporco.cupy.util sporco.util
sporco.cupy.admm.admm sporco.admm.admm
sporco.cupy.admm.cbpdn sporco.admm.cbpdn
sporco.cupy.admm.cbpdntv sporco.admm.cbpdntv
sporco.cupy.admm.tvl1 sporco.admm.tvl1
sporco.cupy.admm.tvl2 sporco.admm.tvl2
sporco.cupy.fista.cbpdn sporco.fista.cbpdn
sporco.cupy.dictlrn.onlinecdl sporco.dictlrn.onlinecdl

Usage Examples

Usage examples are available for sporco.cupy.admm.tvl1 and sporco.cupy.dictlrn.onlinecdl.