Skip to content

qttools.utils.gpu_utils#

source module qttools.utils.gpu_utils

Functions

  • get_array_module_name Given an array, returns the array's module name.

  • get_host Returns the host array of the given array.

  • get_device Returns the device array of the given array.

  • get_any_location Returns the array in the desired location.

  • empty_pinned Returns a new, uninitialized NumPy array with the given shape and dtype. The array is allocated in pinned memory if using cupy.

  • zeros_pinned Returns a new array of given shape and type, filled with zeros. The array is allocated in pinned memory if using cupy.

  • empty_like_pinned Returns a new array with the same shape and type as a given array. The array is allocated in pinned memory if using cupy.

  • zeros_like_pinned Returns an array of zeros with the same shape and type as a given array. The array is allocated in pinned memory if using cupy.

  • synchronize_current_stream Synchronizes the current stream if using cupy.

  • synchronize_device Synchronizes the device if using cupy.

  • free_mempool Frees the memory pool if using cupy.

source get_array_module_name(arr: NDArray)str

Given an array, returns the array's module name.

This works for numpy even when cupy is not available.

Parameters

  • arr : NDArray The array to check.

Returns

  • str The array module name used by the array.

source get_host(arr: NDArray, out: None | NDArray = None)NDArray

Returns the host array of the given array.

Note: special behaviour if numpy is used: If out is not set, then the returned array is the same as the input and the pointers alias.

Parameters

  • arr : NDArray The array to convert.

  • out : NDArray, optional The output array.

Returns

  • np.ndarray The equivalent numpy array.

source get_device(arr: NDArray, out: None | NDArray = None)NDArray

Returns the device array of the given array.

Note: special behaviour if cupy is used: If out is not set, then the returned array is the same as the input and the pointers alias.

Parameters

  • arr : NDArray The array to convert.

  • out : NDArray, optional The output array.

Returns

  • NDArray The equivalent cupy array.

source get_any_location(arr: NDArray, output_module: str, use_pinned_memory: bool = False)

Returns the array in the desired location.

Parameters

  • arr : NDArray The array to convert.

  • output_module : str The desired location. The location can be either "numpy" or "cupy".

  • use_pinned_memory : bool, optional Whether to use pinnend memory if cupy is used. Default is True.

Returns

  • NDArray The equivalent array in the desired location

Raises

  • ValueError

source empty_pinned(shape: int | tuple[int, ...], dtype: xp.dtype = float, order: str = 'C')

Returns a new, uninitialized NumPy array with the given shape and dtype. The array is allocated in pinned memory if using cupy.

Parameters

  • shape : int or tuple of ints Shape of the empty array.

  • dtype : data-type, optional Desired data-type for the array. Default is float.

  • order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Default is 'C'.

Returns

  • NDArray The empty array.

  • .. seealso : : :func:numpy.<span class="mkapi-tooltip" title="numpy._core.empty">empty</span> :func:cupy.empty :func:cupyx.empty\_pinned

source zeros_pinned(shape: int | tuple[int, ...], dtype: xp.dtype = float, order: str = 'C')

Returns a new array of given shape and type, filled with zeros. The array is allocated in pinned memory if using cupy.

Parameters

  • shape : int or tuple of ints Shape of the new array.

  • dtype : data-type, optional The desired data-type for the array. Default is float.

  • order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Default is 'C'.

Returns

  • NDArray The array of zeros.

  • .. seealso : : :func:numpy.<span class="mkapi-tooltip" title="numpy._core.zeros">zeros</span> :func:cupy.zeros :func:cupyx.zeros\_pinned

source empty_like_pinned(a: NDArray, dtype: xp.dtype = None, order: str = 'K', shape: int | tuple[int, ...] = None)

Returns a new array with the same shape and type as a given array. The array is allocated in pinned memory if using cupy.

Parameters

  • a : NDArray The shape and data-type of a define these same attributes of the returned array.

  • dtype : data-type, optional Overrides the data type of the result.

  • order : {'C', 'F', 'A', 'K'}, optional Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if a is Fortran contiguous, 'C' otherwise. 'K' means match the layout of a as closely as possible.

  • shape : int or tuple of ints, optional Overrides the shape of the result.

Returns

  • NDArray The empty array.

  • .. seealso : : :func:numpy.<span class="mkapi-tooltip" title="numpy._core.multiarray.empty_like">empty\_like</span> :func:cupy.empty\_like :func:cupyx.empty\_like\_pinned

source zeros_like_pinned(a: NDArray, dtype: xp.dtype = None, order: str = 'K', shape: int | tuple[int, ...] = None)

Returns an array of zeros with the same shape and type as a given array. The array is allocated in pinned memory if using cupy.

Parameters

  • a : NDArray The shape and data-type of a define these same attributes of the returned array.

  • dtype : data-type, optional Overrides the data type of the result.

  • order : {'C', 'F', 'A', 'K'}, optional Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if a is Fortran contiguous, 'C' otherwise. 'K' means match the layout of a as closely as possible.

  • shape : int or tuple of ints, optional Overrides the shape of the result.

Returns

  • NDArray The array of zeros.

  • .. seealso : : :func:numpy.<span class="mkapi-tooltip" title="numpy._core.numeric.zeros_like">zeros\_like</span> :func:cupy.zeros\_like :func:cupyx.zeros\_like\_pinned

source synchronize_current_stream()

Synchronizes the current stream if using cupy.

Does nothing if using numpy.

source synchronize_device()

Synchronizes the device if using cupy.

Does nothing if using numpy.

source free_mempool()

Frees the memory pool if using cupy.

Does nothing if using numpy.