1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
use super::sys;
use core::ffi::{c_int, c_longlong};
use core::mem::MaybeUninit;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CublasError(pub sys::cublasStatus_t);
impl sys::cublasStatus_t {
pub fn result(self) -> Result<(), CublasError> {
match self {
sys::cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
_ => Err(CublasError(self)),
}
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for CublasError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
#[cfg(feature = "std")]
impl std::error::Error for CublasError {}
/// Creates a handle to the cuBLAS library. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublascreate)
pub fn create_handle() -> Result<sys::cublasHandle_t, CublasError> {
let mut handle = MaybeUninit::uninit();
unsafe {
sys::cublasCreate_v2(handle.as_mut_ptr()).result()?;
Ok(handle.assume_init())
}
}
/// Destroys a handle previously created with [create_handle()]. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublasdestroy)
///
/// # Safety
///
/// `handle` must not have been freed already.
pub unsafe fn destroy_handle(handle: sys::cublasHandle_t) -> Result<(), CublasError> {
sys::cublasDestroy_v2(handle).result()
}
/// Sets the stream cuBLAS will use. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublassetstream)
///
/// # Safety
///
/// `handle` and `stream` must be valid.
pub unsafe fn set_stream(
handle: sys::cublasHandle_t,
stream: sys::cudaStream_t,
) -> Result<(), CublasError> {
sys::cublasSetStream_v2(handle, stream).result()
}
/// Single precision matrix vector multiplication. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemv)
///
/// # Safety
///
/// - `a`, `x`, and `y` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn sgemv(
handle: sys::cublasHandle_t,
trans: sys::cublasOperation_t,
m: c_int,
n: c_int,
alpha: *const f32,
a: *const f32,
lda: c_int,
x: *const f32,
incx: c_int,
beta: *const f32,
y: *mut f32,
incy: c_int,
) -> Result<(), CublasError> {
sys::cublasSgemv_v2(handle, trans, m, n, alpha, a, lda, x, incx, beta, y, incy).result()
}
/// Double precision matrix vector multiplication. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemv)
///
/// # Safety
///
/// - `a`, `x`, and `y` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn dgemv(
handle: sys::cublasHandle_t,
trans: sys::cublasOperation_t,
m: c_int,
n: c_int,
alpha: *const f64,
a: *const f64,
lda: c_int,
x: *const f64,
incx: c_int,
beta: *const f64,
y: *mut f64,
incy: c_int,
) -> Result<(), CublasError> {
sys::cublasDgemv_v2(handle, trans, m, n, alpha, a, lda, x, incx, beta, y, incy).result()
}
#[cfg(feature = "f16")]
/// Half precision matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemm)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn hgemm(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const half::f16,
a: *const half::f16,
lda: c_int,
b: *const half::f16,
ldb: c_int,
beta: *const half::f16,
c: *mut half::f16,
ldc: c_int,
) -> Result<(), CublasError> {
super::half::cublasHgemm(
handle, transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
)
.result()
}
/// Single precision matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemm)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn sgemm(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const f32,
a: *const f32,
lda: c_int,
b: *const f32,
ldb: c_int,
beta: *const f32,
c: *mut f32,
ldc: c_int,
) -> Result<(), CublasError> {
sys::cublasSgemm_v2(
handle, transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
)
.result()
}
/// Double precision matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemm)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn dgemm(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const f64,
a: *const f64,
lda: c_int,
b: *const f64,
ldb: c_int,
beta: *const f64,
c: *mut f64,
ldc: c_int,
) -> Result<(), CublasError> {
sys::cublasDgemm_v2(
handle, transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
)
.result()
}
#[cfg(feature = "f16")]
/// Half precision batched matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemmstridedbatched)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn hgemm_strided_batched(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const half::f16,
a: *const half::f16,
lda: c_int,
stride_a: c_longlong,
b: *const half::f16,
ldb: c_int,
stride_b: c_longlong,
beta: *const half::f16,
c: *mut half::f16,
ldc: c_int,
stride_c: c_longlong,
batch_size: c_int,
) -> Result<(), CublasError> {
super::half::cublasHgemmStridedBatched(
handle, transa, transb, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b, beta, c, ldc,
stride_c, batch_size,
)
.result()
}
/// Single precision batched matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemmstridedbatched)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn sgemm_strided_batched(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const f32,
a: *const f32,
lda: c_int,
stride_a: c_longlong,
b: *const f32,
ldb: c_int,
stride_b: c_longlong,
beta: *const f32,
c: *mut f32,
ldc: c_int,
stride_c: c_longlong,
batch_size: c_int,
) -> Result<(), CublasError> {
sys::cublasSgemmStridedBatched(
handle, transa, transb, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b, beta, c, ldc,
stride_c, batch_size,
)
.result()
}
/// Double precision batched matmul. See
/// [nvidia docs](https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-gemmstridedbatched)
///
/// # Safety
///
/// - `a`, `b`, and `c` must be valid device pointers that have not been freed.
/// - `alpha` and `beta` can be pointers to host memory, but must be not null
/// - the strides and sizes must be sized correctly
#[allow(clippy::too_many_arguments)]
pub unsafe fn dgemm_strided_batched(
handle: sys::cublasHandle_t,
transa: sys::cublasOperation_t,
transb: sys::cublasOperation_t,
m: c_int,
n: c_int,
k: c_int,
alpha: *const f64,
a: *const f64,
lda: c_int,
stride_a: c_longlong,
b: *const f64,
ldb: c_int,
stride_b: c_longlong,
beta: *const f64,
c: *mut f64,
ldc: c_int,
stride_c: c_longlong,
batch_size: c_int,
) -> Result<(), CublasError> {
sys::cublasDgemmStridedBatched(
handle, transa, transb, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b, beta, c, ldc,
stride_c, batch_size,
)
.result()
}