abtem.core.backend#
Module for handling the array backend (NumPy, CuPy, Dask, etc.) of the library.
Module Contents#
Functions#
Convert an array to NumPy. |
|
Check if CuPy is installed, raise an error if not. |
|
Copy an array to a different device (CPU or GPU) using CuPy. |
|
Get the device string from the array module. The array module must be either NumPy or CuPy. |
|
Start a dask-cuda cluster spanning all visible GPUs and return its client. |
|
Get the array module (NumPy or CuPy) for a given array or string. |
|
Return the dask-cuda cluster client started by |
|
Get the ndimage module for a given array or device string. |
|
Get the SciPy module for a given array or device string. |
|
Check whether a distributed client can safely execute CuPy computations. |
|
Mirror this process’s abTEM configuration onto the client’s workers. |
|
Validate the device string. |
Data#
API#
- abtem.core.backend.ArrayModule#
None
- abtem.core.backend.check_cupy_is_installed()[source]#
Check if CuPy is installed, raise an error if not.
- abtem.core.backend.copy_to_device(...)[source]#
Copy an array to a different device (CPU or GPU) using CuPy.
- abtem.core.backend.device_name_from_array_module(...) str[source]#
Get the device string from the array module. The array module must be either NumPy or CuPy.
- Parameters:
xp (numpy or cupy) – The array module.
- Returns:
The device string.
- Return type:
- abtem.core.backend.ensure_cuda_cluster()[source]#
Start a dask-cuda cluster spanning all visible GPUs and return its client.
The cluster assigns one worker process to each GPU, allowing dask to distribute computations across all of them. It is created once per process and reused on subsequent calls. Requires the optional dask-cuda package.
- Returns:
The client connected to the dask-cuda cluster.
- Return type:
distributed.Client
- abtem.core.backend.get_array_module(...) ModuleType[source]#
Get the array module (NumPy or CuPy) for a given array or string.
- abtem.core.backend.get_cuda_cluster_client()[source]#
Return the dask-cuda cluster client started by
ensure_cuda_cluster.Returns the running client, or None when no cluster has been started or the previous one was shut down. Unlike
ensure_cuda_clusterthis never starts a cluster, which makes it suitable for inspecting whether multi-GPU execution is active (e.g. from benchmark or verification scripts).- Returns:
The client connected to the running dask-cuda cluster, if any.
- Return type:
distributed.Client or None
- abtem.core.backend.get_ndimage_module(...) ModuleType[source]#
Get the ndimage module for a given array or device string.
- abtem.core.backend.get_scipy_module(...)[source]#
Get the SciPy module for a given array or device string.
- abtem.core.backend.is_gpu_dask_client(...) bool[source]#
Check whether a distributed client can safely execute CuPy computations.
Only a client whose workers are each single-threaded — as produced by
dask_cuda.LocalCUDACluster, which additionally pins one GPU per worker — is considered suitable. The threaded scheduler and multi-threaded workers share a single CUDA context per process, which cannot be used with CuPy.- Parameters:
client (distributed.Client or None) – The client to check.
- Returns:
True if the client is running and all of its workers are single-threaded.
- Return type:
- abtem.core.backend.logger#
‘getLogger(…)’
- abtem.core.backend.push_config_to_workers(...)[source]#
Mirror this process’s abTEM configuration onto the client’s workers.
abTEM resolves configuration inside tasks, in the worker process –
get_dtypereadsprecisionat call time, for example – but worker processes only ever see the defaults:abtem.config.setin the client does not reach them, silently changing results (a float64 computation dispatched to default-configured workers runs in float32).The snapshot is carried by a named worker plugin, so workers that join or restart later also receive it; when the configuration changes, re-registering under the same name replaces the plugin and re-runs its setup on all workers. Repeated pushes of an unchanged configuration to the same client (keyed on
client.id) are skipped.