oxicuda-blas 0.1.3

OxiCUDA BLAS - GPU-accelerated BLAS operations (cuBLAS equivalent)
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! GEMV -- General matrix-vector multiplication.
//!
//! Computes `y = alpha * op(A) * x + beta * y` where `op(A)` is either `A`,
//! `A^T`, or `A^H` depending on the [`Transpose`] parameter.
//!
//! # GPU Strategy
//!
//! - **Row-major + NoTrans**: each thread computes one element of `y` by
//!   performing a dot product of one row of `A` with `x`.
//! - **Col-major + NoTrans** or **Row-major + Trans**: a warp-collaborative
//!   approach where multiple threads contribute partial sums for each output.
//! - For large inner dimensions the kernel uses shared memory tiling.

use std::sync::Arc;

use oxicuda_driver::Module;
use oxicuda_launch::{Kernel, LaunchParams, grid_size_for};
use oxicuda_memory::DeviceBuffer;
use oxicuda_ptx::prelude::*;

use crate::error::{BlasError, BlasResult};
use crate::handle::BlasHandle;
use crate::types::{GpuFloat, MatrixDesc, Transpose};

/// Default block size for GEMV kernels.
const GEMV_BLOCK_SIZE: u32 = 256;

/// Computes `y = alpha * op(A) * x + beta * y`.
///
/// This is the general matrix-vector multiply operation. The matrix `A` can
/// optionally be transposed via the `trans` parameter.
///
/// # Arguments
///
/// * `handle` -- BLAS handle providing stream and device context.
/// * `trans` -- Whether to transpose `A`.
/// * `m` -- Number of rows of matrix `A`.
/// * `n` -- Number of columns of matrix `A`.
/// * `alpha` -- Scalar multiplier for `op(A) * x`.
/// * `a` -- Matrix descriptor for `A` (device memory).
/// * `x` -- Input vector (device memory).
/// * `incx` -- Stride between consecutive elements of `x`. Must be positive.
/// * `beta` -- Scalar multiplier for the existing `y`.
/// * `y` -- Input/output vector (device memory, modified in-place).
/// * `incy` -- Stride between consecutive elements of `y`. Must be positive.
///
/// # Errors
///
/// Returns [`BlasError::InvalidDimension`] if `m` or `n` is zero.
/// Returns [`BlasError::InvalidArgument`] if `incx` or `incy` is not positive.
/// Returns [`BlasError::BufferTooSmall`] if any buffer is undersized.
/// Returns [`BlasError::PtxGeneration`] if kernel generation fails.
/// Returns [`BlasError::Cuda`] on kernel launch or driver failure.
#[allow(clippy::too_many_arguments)]
pub fn gemv<T: GpuFloat>(
    handle: &BlasHandle,
    trans: Transpose,
    m: u32,
    n: u32,
    alpha: T,
    a: &MatrixDesc<T>,
    x: &DeviceBuffer<T>,
    incx: i32,
    beta: T,
    y: &mut DeviceBuffer<T>,
    incy: i32,
) -> BlasResult<()> {
    // -- Early exit for empty dimensions --
    if m == 0 || n == 0 {
        return Ok(());
    }

    // -- Validate increments --
    if incx <= 0 {
        return Err(BlasError::InvalidArgument(
            "incx must be positive".to_string(),
        ));
    }
    if incy <= 0 {
        return Err(BlasError::InvalidArgument(
            "incy must be positive".to_string(),
        ));
    }

    // -- Validate matrix dimensions --
    validate_gemv_dimensions(trans, m, n, a, x, incx, y, incy)?;

    // -- Determine output vector length --
    let (output_len, inner_len) = match trans {
        Transpose::NoTrans => (m, n),
        Transpose::Trans | Transpose::ConjTrans => (n, m),
    };

    // -- Generate PTX kernel --
    let ptx = generate_gemv_ptx::<T>(handle.sm_version(), trans)?;
    let module = Arc::new(Module::from_ptx(&ptx)?);
    let kernel = Kernel::from_module(module, "gemv")?;

    // -- Configure launch dimensions --
    let block_size = GEMV_BLOCK_SIZE;
    let grid_size = grid_size_for(output_len, block_size);
    let params = LaunchParams::new(grid_size, block_size);

    // -- Launch --
    kernel.launch(
        &params,
        handle.stream(),
        &(
            a.ptr,
            x.as_device_ptr(),
            y.as_device_ptr(),
            alpha.to_bits_u64(),
            beta.to_bits_u64(),
            m,
            n,
            a.ld,
            incx as u32,
            incy as u32,
            output_len,
            inner_len,
        ),
    )?;

    Ok(())
}

/// Validates dimensions and buffer sizes for GEMV.
#[allow(clippy::too_many_arguments)]
fn validate_gemv_dimensions<T: GpuFloat>(
    trans: Transpose,
    m: u32,
    n: u32,
    a: &MatrixDesc<T>,
    x: &DeviceBuffer<T>,
    incx: i32,
    y: &DeviceBuffer<T>,
    incy: i32,
) -> BlasResult<()> {
    // Matrix must have at least m rows and n cols
    if a.rows < m {
        return Err(BlasError::InvalidDimension(format!(
            "A.rows ({}) < m ({})",
            a.rows, m
        )));
    }
    if a.cols < n {
        return Err(BlasError::InvalidDimension(format!(
            "A.cols ({}) < n ({})",
            a.cols, n
        )));
    }

    // Determine x and y lengths based on transpose
    let (x_len, y_len) = match trans {
        Transpose::NoTrans => (n, m),
        Transpose::Trans | Transpose::ConjTrans => (m, n),
    };

    // Validate x buffer size
    let x_required = required_elements(x_len, incx);
    if x.len() < x_required {
        return Err(BlasError::BufferTooSmall {
            expected: x_required,
            actual: x.len(),
        });
    }

    // Validate y buffer size
    let y_required = required_elements(y_len, incy);
    if y.len() < y_required {
        return Err(BlasError::BufferTooSmall {
            expected: y_required,
            actual: y.len(),
        });
    }

    Ok(())
}

/// Generates PTX source for the GEMV kernel.
///
/// Each thread computes one element of the output vector by performing
/// a dot product of the corresponding row (or column, for transposed case)
/// of A with x, scaled by alpha, plus beta * y.
fn generate_gemv_ptx<T: GpuFloat>(sm: SmVersion, trans: Transpose) -> BlasResult<String> {
    let suffix = T::NAME;
    let ptx_ty = T::PTX_TYPE;
    let elem_bytes = T::size_u32();
    let is_f64 = elem_bytes == 8;

    // Choose the load/store helpers based on precision
    let _kernel_name = format!("gemv_{suffix}_{}", trans_label(trans));

    KernelBuilder::new("gemv")
        .target(sm)
        .param("a_ptr", PtxType::U64)
        .param("x_ptr", PtxType::U64)
        .param("y_ptr", PtxType::U64)
        .param("alpha_bits", PtxType::U64)
        .param("beta_bits", PtxType::U64)
        .param("m", PtxType::U32)
        .param("n", PtxType::U32)
        .param("lda", PtxType::U32)
        .param("incx", PtxType::U32)
        .param("incy", PtxType::U32)
        .param("output_len", PtxType::U32)
        .param("inner_len", PtxType::U32)
        .body(move |b| {
            // Compute global thread ID -- each thread handles one output element
            let gid = b.global_thread_id_x();
            let output_len = b.load_param_u32("output_len");

            // Bounds check: skip threads beyond output vector
            let gid_inner = gid.clone();
            b.if_lt_u32(gid, output_len, move |b| {
                let gid = gid_inner;
                let a_ptr = b.load_param_u64("a_ptr");
                let x_ptr = b.load_param_u64("x_ptr");
                let y_ptr = b.load_param_u64("y_ptr");
                let inner_len = b.load_param_u32("inner_len");
                let lda = b.load_param_u32("lda");
                let incx = b.load_param_u32("incx");
                let incy = b.load_param_u32("incy");

                // Load alpha and beta from their bit representations
                let alpha_bits = b.load_param_u64("alpha_bits");
                let beta_bits = b.load_param_u64("beta_bits");

                // Reinterpret bits as float
                let alpha = if is_f64 {
                    let r = b.alloc_reg(PtxType::F64);
                    b.raw_ptx(&format!("mov.b64 {r}, {alpha_bits};"));
                    r
                } else {
                    let lo32 = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("cvt.u32.u64 {lo32}, {alpha_bits};"));
                    let r = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.b32 {r}, {lo32};"));
                    r
                };

                let beta = if is_f64 {
                    let r = b.alloc_reg(PtxType::F64);
                    b.raw_ptx(&format!("mov.b64 {r}, {beta_bits};"));
                    r
                } else {
                    let lo32 = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("cvt.u32.u64 {lo32}, {beta_bits};"));
                    let r = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.b32 {r}, {lo32};"));
                    r
                };

                // Initialize accumulator to zero
                let acc = b.alloc_reg(ptx_ty);
                if is_f64 {
                    b.raw_ptx(&format!("mov.b64 {acc}, 0d0000000000000000;"));
                } else {
                    b.raw_ptx(&format!("mov.b32 {acc}, 0f00000000;"));
                }

                // Compute base address for this row/col in A
                // For NoTrans (row-major): row gid, iterate over columns
                // For Trans (row-major): col gid, iterate over rows
                // Address of A[gid, k] = a_ptr + (gid * lda + k) * elem_bytes
                // (row-major, NoTrans)
                // Address of A[k, gid] = a_ptr + (k * lda + gid) * elem_bytes
                // (row-major, Trans)

                let use_trans = matches!(trans, Transpose::Trans | Transpose::ConjTrans);

                // Row base offset: gid * lda * elem_bytes (NoTrans) or
                //                  gid * elem_bytes (Trans)
                let row_base = if !use_trans {
                    // stride = lda * elem_bytes (runtime), so use raw PTX
                    let stride = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {stride}, {lda}, {};", elem_bytes));
                    let row_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {row_idx}, {gid}, {stride};"));
                    let row_off64 = b.cvt_u32_to_u64(row_idx);
                    b.add_u64(a_ptr, row_off64)
                } else {
                    // a_ptr + gid * elem_bytes
                    b.byte_offset_addr(a_ptr, gid.clone(), elem_bytes)
                };

                // Loop over inner dimension
                let loop_label = b.fresh_label("gemv_loop");
                let done_label = b.fresh_label("gemv_done");
                let k = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("mov.u32 {k}, 0;"));

                b.label(&loop_label);

                // Check k < inner_len
                let pred_loop = b.alloc_reg(PtxType::Pred);
                b.raw_ptx(&format!("setp.lo.u32 {pred_loop}, {k}, {inner_len};"));
                b.raw_ptx(&format!("@!{pred_loop} bra {done_label};"));

                // Compute address of A element:
                // NoTrans: A[gid][k] => row_base + k * elem_bytes
                // Trans:   A[k][gid] => row_base + k * lda * elem_bytes
                let a_addr = if !use_trans {
                    b.byte_offset_addr(row_base.clone(), k.clone(), elem_bytes)
                } else {
                    // For trans, stride between elements is lda * elem_bytes
                    // but we cannot use lda directly as a const -- emit as raw
                    let stride_reg = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {stride_reg}, {lda}, {};", elem_bytes));
                    let k64 = b.cvt_u32_to_u64(k.clone());
                    let stride64 = b.cvt_u32_to_u64(stride_reg);
                    let off = b.alloc_reg(PtxType::U64);
                    b.raw_ptx(&format!("mul.lo.u64 {off}, {k64}, {stride64};"));
                    b.add_u64(row_base.clone(), off)
                };

                // Compute address of x[k * incx]
                let x_elem_idx = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("mul.lo.u32 {x_elem_idx}, {k}, {incx};"));
                let x_addr = b.byte_offset_addr(x_ptr.clone(), x_elem_idx, elem_bytes);

                // Load A[..][..] and x[k*incx]
                let a_val = if is_f64 {
                    b.load_global_f64(a_addr)
                } else {
                    b.load_global_f32(a_addr)
                };
                let x_val = if is_f64 {
                    b.load_global_f64(x_addr)
                } else {
                    b.load_global_f32(x_addr)
                };

                // acc += a_val * x_val  (FMA: acc = a_val * x_val + acc)
                let new_acc = if is_f64 {
                    b.fma_f64(a_val, x_val, acc.clone())
                } else {
                    b.fma_f32(a_val, x_val, acc.clone())
                };
                b.raw_ptx(&format!(
                    "mov.{} {acc}, {new_acc};",
                    if is_f64 { "f64" } else { "f32" }
                ));

                // k++
                b.raw_ptx(&format!("add.u32 {k}, {k}, 1;"));
                b.branch(&loop_label);

                b.label(&done_label);

                // Compute y address: y_ptr + gid * incy * elem_bytes
                let y_elem_idx = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("mul.lo.u32 {y_elem_idx}, {gid}, {incy};"));
                let y_addr = b.byte_offset_addr(y_ptr, y_elem_idx, elem_bytes);

                // Load current y value
                let y_cur = if is_f64 {
                    b.load_global_f64(y_addr.clone())
                } else {
                    b.load_global_f32(y_addr.clone())
                };

                // result = alpha * acc + beta * y_cur
                let alpha_acc = if is_f64 {
                    let tmp = b.alloc_reg(PtxType::F64);
                    b.raw_ptx(&format!("mul.rn.f64 {tmp}, {alpha}, {acc};"));
                    tmp
                } else {
                    // alpha * acc
                    let tmp = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.rn.f32 {tmp}, {alpha}, {acc};"));
                    tmp
                };
                let beta_y = if is_f64 {
                    let tmp = b.alloc_reg(PtxType::F64);
                    b.raw_ptx(&format!("mul.rn.f64 {tmp}, {beta}, {y_cur};"));
                    tmp
                } else {
                    let tmp = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.rn.f32 {tmp}, {beta}, {y_cur};"));
                    tmp
                };
                let result = if is_f64 {
                    b.add_f64(alpha_acc, beta_y)
                } else {
                    b.add_f32(alpha_acc, beta_y)
                };

                // Store result to y
                if is_f64 {
                    b.store_global_f64(y_addr, result);
                } else {
                    b.store_global_f32(y_addr, result);
                }
            });

            b.ret();
        })
        .build()
        .map_err(|e| BlasError::PtxGeneration(e.to_string()))
}

/// Returns a short label for the transpose mode.
fn trans_label(t: Transpose) -> &'static str {
    match t {
        Transpose::NoTrans => "n",
        Transpose::Trans => "t",
        Transpose::ConjTrans => "c",
    }
}

/// Computes the minimum buffer elements required for a strided vector.
fn required_elements(n: u32, inc: i32) -> usize {
    if n == 0 {
        return 0;
    }
    1 + (n as usize - 1) * inc.unsigned_abs() as usize
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trans_label_values() {
        assert_eq!(trans_label(Transpose::NoTrans), "n");
        assert_eq!(trans_label(Transpose::Trans), "t");
        assert_eq!(trans_label(Transpose::ConjTrans), "c");
    }

    #[test]
    fn required_elements_basic() {
        assert_eq!(required_elements(0, 1), 0);
        assert_eq!(required_elements(1, 1), 1);
        assert_eq!(required_elements(5, 1), 5);
        assert_eq!(required_elements(5, 2), 9);
    }

    #[test]
    fn gemv_ptx_generation_f32() {
        let ptx = generate_gemv_ptx::<f32>(SmVersion::Sm80, Transpose::NoTrans);
        assert!(ptx.is_ok());
        let ptx = ptx.expect("test: PTX generation should succeed");
        assert!(ptx.contains(".entry gemv"));
        assert!(ptx.contains(".target sm_80"));
    }

    #[test]
    fn gemv_ptx_generation_f64() {
        let ptx = generate_gemv_ptx::<f64>(SmVersion::Sm80, Transpose::Trans);
        assert!(ptx.is_ok());
        let ptx = ptx.expect("test: PTX generation should succeed");
        assert!(ptx.contains(".entry gemv"));
    }
}