Skip to main content

tenferro_gpu/
lib.rs

1//! GPU backend implementations for tenferro tensors.
2//!
3//! # Examples
4//!
5//! ```rust
6//! #[cfg(feature = "cuda")]
7//! use tenferro_gpu::{cuda::cuda_devices, cuda::CudaEngine};
8//!
9//! #[cfg(feature = "cuda")]
10//! fn first_cuda_backend() -> Result<Option<CudaEngine>, Box<dyn std::error::Error>> {
11//!     let devices = cuda_devices()?;
12//!     let Some(device) = devices.first() else {
13//!         return Ok(None);
14//!     };
15//!     Ok(Some(CudaEngine::open(device.id())?))
16//! }
17//!
18//! // This ordinary doctest checks the discovery-based selection API without
19//! // requiring CUDA hardware at test time.
20//! #[cfg(feature = "cuda")]
21//! let _example: fn() -> Result<Option<CudaEngine>, Box<dyn std::error::Error>> = first_cuda_backend;
22//! ```
23// A misaligned pointer handed to a CUDA library is undefined behaviour and
24// fails only on some library versions: `cuDoubleComplex` is `double2`, which
25// CUDA declares `__align__(16)`, while `num_complex::Complex64` is 8-aligned,
26// so casting `&Complex64` to `*const cuDoubleComplex` produced a pointer
27// cuBLAS >= 12.9 faults on (issue #1870). Deny the whole cast class at this
28// FFI boundary rather than re-auditing it by hand.
29#![deny(clippy::cast_ptr_alignment)]
30
31#[cfg(feature = "cuda")]
32mod event_domain_admission;
33#[cfg(feature = "cuda")]
34mod event_retirement;
35#[cfg(any(feature = "cuda", feature = "metal"))]
36pub mod gpu;
37
38// The capability tables are plain `const` data; they stay compiled without their feature so
39// the guide table can be checked in a default-features build. Only the feature-gated
40// readers name the CUDA rows otherwise, which is why the module allows dead code there.
41#[cfg_attr(not(feature = "cuda"), allow(dead_code))]
42pub(crate) mod capability;
43
44#[cfg(feature = "cuda")]
45pub mod cuda;
46
47#[cfg(feature = "metal")]
48pub mod metal;
49
50#[cfg(any(feature = "cuda", feature = "metal"))]
51#[allow(unused_imports)]
52use tenferro_tensor::*;