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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Multi-stream batched GEMM.
//!
//! Distributes batched GEMM work across multiple CUDA streams for improved
//! throughput. Each stream processes a subset of the total batch, enabling
//! concurrent kernel execution on GPUs with sufficient resources.
//!
//! # Distribution strategies
//!
//! * [`StreamDistribution::RoundRobin`] — assigns batches to streams in
//!   round-robin order, producing balanced but non-contiguous assignments.
//! * [`StreamDistribution::EqualSplit`] — assigns contiguous, roughly equal
//!   chunks of batches to each stream, which is better for memory locality.

use oxicuda_driver::Stream;
use oxicuda_driver::ffi::CUdeviceptr;
use oxicuda_launch::{Dim3, Kernel, LaunchParams};
use oxicuda_memory::DeviceBuffer;
use oxicuda_ptx::arch::SmVersion;
use std::sync::Arc;

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

use super::batched_gemm::{TILE_M, TILE_N, generate_batched_gemm_ptx, validate_batched_args};

// ---------------------------------------------------------------------------
// Configuration types
// ---------------------------------------------------------------------------

/// Strategy for distributing batches across streams.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StreamDistribution {
    /// Assigns batches to streams in round-robin order (batch 0 -> stream 0,
    /// batch 1 -> stream 1, ..., batch N -> stream N % num_streams).
    /// This produces maximally balanced assignments but non-contiguous memory
    /// access patterns per stream.
    RoundRobin,
    /// Assigns contiguous, roughly equal chunks of batches to each stream.
    /// Streams earlier in the list may receive one extra batch if the total
    /// count is not evenly divisible.
    EqualSplit,
}

/// Configuration for multi-stream batched GEMM execution.
#[derive(Debug, Clone)]
pub struct MultiStreamBatchedConfig {
    /// Number of streams to distribute work across.
    pub num_streams: u32,
    /// Strategy for assigning batches to streams.
    pub distribution: StreamDistribution,
}

impl MultiStreamBatchedConfig {
    /// Creates a new configuration.
    ///
    /// # Arguments
    ///
    /// * `num_streams` — number of streams to use (must be >= 1).
    /// * `distribution` — how to distribute batches across streams.
    #[must_use]
    pub fn new(num_streams: u32, distribution: StreamDistribution) -> Self {
        Self {
            num_streams,
            distribution,
        }
    }

    /// Validates the configuration.
    fn validate(&self) -> BlasResult<()> {
        if self.num_streams == 0 {
            return Err(BlasError::InvalidArgument(
                "num_streams must be at least 1".into(),
            ));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Distribution logic
// ---------------------------------------------------------------------------

/// Computes `(start_index, count)` pairs for distributing `batch_count`
/// batches across `num_streams` streams using the given strategy.
///
/// For [`StreamDistribution::EqualSplit`], the first `batch_count % num_streams`
/// streams receive `batch_count / num_streams + 1` batches, and the remaining
/// streams receive `batch_count / num_streams`.
///
/// For [`StreamDistribution::RoundRobin`], the returned ranges are a
/// simplification: we still produce contiguous ranges, but balance them the
/// same way as `EqualSplit`. The actual round-robin assignment is handled at
/// kernel launch time by selecting the correct pointer offsets.
///
/// Returns an empty vector if `batch_count` is zero.
pub fn distribute_batches(
    batch_count: u32,
    num_streams: u32,
    distribution: &StreamDistribution,
) -> Vec<(u32, u32)> {
    if batch_count == 0 || num_streams == 0 {
        return Vec::new();
    }

    let effective_streams = num_streams.min(batch_count);

    match distribution {
        StreamDistribution::EqualSplit => {
            let base = batch_count / effective_streams;
            let remainder = batch_count % effective_streams;
            let mut result = Vec::with_capacity(effective_streams as usize);
            let mut offset = 0u32;

            for i in 0..effective_streams {
                let count = if i < remainder { base + 1 } else { base };
                result.push((offset, count));
                offset = offset.saturating_add(count);
            }
            result
        }
        StreamDistribution::RoundRobin => {
            // Round-robin: each stream gets every N-th batch.
            // We report (logical_start, count) where logical_start is the
            // first batch index assigned to this stream, and count is how
            // many batches it handles.
            let base = batch_count / effective_streams;
            let remainder = batch_count % effective_streams;
            let mut result = Vec::with_capacity(effective_streams as usize);

            for i in 0..effective_streams {
                let count = if i < remainder { base + 1 } else { base };
                // For round-robin, the "start" is the stream index itself.
                // Batches assigned: i, i + N, i + 2N, ...
                result.push((i, count));
            }
            result
        }
    }
}

// ---------------------------------------------------------------------------
// Multi-stream batched GEMM
// ---------------------------------------------------------------------------

/// Executes a batched GEMM across multiple CUDA streams.
///
/// ```text
/// D[i] = alpha * op(A[i]) * op(B[i]) + beta * C[i]   for i in 0..batch_count
/// ```
///
/// The batch is partitioned according to `config.distribution` and each
/// partition is launched on a separate stream from `streams`.
///
/// # Arguments
///
/// * `handle` — BLAS handle (used for SM version and validation).
/// * `config` — multi-stream configuration (number of streams, distribution).
/// * `trans_a`, `trans_b` — transposition modes for A and B.
/// * `m`, `n`, `k` — matrix dimensions.
/// * `alpha`, `beta` — scalar coefficients.
/// * `a_ptrs`, `b_ptrs`, `c_ptrs` — device-pointer arrays (one per batch).
/// * `lda`, `ldb`, `ldc`, `ldd` — leading dimensions.
/// * `d_ptrs` — output device-pointer array (one per batch).
/// * `batch_count` — total number of batches.
/// * `streams` — slice of streams to distribute work across.
///
/// # Errors
///
/// * [`BlasError::InvalidArgument`] if `config.num_streams` is zero or
///   `streams.len()` is less than `config.num_streams`.
/// * All errors from [`gemm_batched`](super::gemm_batched) (dimension,
///   buffer, PTX, launch errors).
#[allow(clippy::too_many_arguments)]
pub fn gemm_batched_multi_stream<T: GpuFloat>(
    handle: &BlasHandle,
    config: &MultiStreamBatchedConfig,
    trans_a: Transpose,
    trans_b: Transpose,
    m: u32,
    n: u32,
    k: u32,
    alpha: T,
    a_ptrs: &DeviceBuffer<CUdeviceptr>,
    lda: u32,
    b_ptrs: &DeviceBuffer<CUdeviceptr>,
    ldb: u32,
    beta: T,
    c_ptrs: &DeviceBuffer<CUdeviceptr>,
    ldc: u32,
    d_ptrs: &mut DeviceBuffer<CUdeviceptr>,
    ldd: u32,
    batch_count: u32,
    streams: &[&Stream],
) -> BlasResult<()> {
    // Early-out for zero batches.
    if batch_count == 0 {
        return Ok(());
    }

    // Validate configuration.
    config.validate()?;

    if (streams.len() as u32) < config.num_streams {
        return Err(BlasError::InvalidArgument(format!(
            "streams slice has {} entries but config requires {}",
            streams.len(),
            config.num_streams
        )));
    }

    // Validate matrix arguments using shared helper.
    validate_batched_args::<T>(
        m,
        n,
        k,
        a_ptrs,
        lda,
        b_ptrs,
        ldb,
        c_ptrs,
        ldc,
        d_ptrs,
        ldd,
        batch_count,
        trans_a,
        trans_b,
    )?;

    let sm = handle.sm_version();

    // Single-stream fast path: delegate to the standard batched dispatch.
    if config.num_streams == 1 {
        return launch_batch_on_stream::<T>(
            sm,
            streams[0],
            trans_a,
            trans_b,
            m,
            n,
            k,
            alpha,
            a_ptrs,
            lda,
            b_ptrs,
            ldb,
            beta,
            c_ptrs,
            ldc,
            d_ptrs,
            ldd,
            0,
            batch_count,
        );
    }

    // Partition batches across streams.
    let partitions = distribute_batches(batch_count, config.num_streams, &config.distribution);

    // Generate PTX once (shared across all streams).
    let (ptx_source, kernel_name) = generate_batched_gemm_ptx::<T>(sm, m, n, k, trans_a, trans_b)?;
    let module = oxicuda_driver::Module::from_ptx(&ptx_source).map_err(BlasError::Cuda)?;
    let module = Arc::new(module);

    let alpha_bits = alpha.to_bits_u64();
    let beta_bits = beta.to_bits_u64();

    for (stream_idx, &(start, count)) in partitions.iter().enumerate() {
        if count == 0 {
            continue;
        }

        let stream = streams[stream_idx];
        let kernel =
            Kernel::from_module(Arc::clone(&module), &kernel_name).map_err(BlasError::Cuda)?;

        let grid = Dim3::new(m.div_ceil(TILE_M), n.div_ceil(TILE_N), count);
        let block = Dim3::new(TILE_M, TILE_N, 1);
        let params = LaunchParams::new(grid, block);

        // Offset into the pointer arrays for this partition.
        // For EqualSplit, start is the contiguous offset.
        // For RoundRobin, we still pass contiguous sub-ranges; the caller
        // is responsible for arranging pointer arrays accordingly, or we
        // launch with the batch offset as a kernel parameter.
        let a_ptr_offset = a_ptrs
            .as_device_ptr()
            .wrapping_add(start as u64 * std::mem::size_of::<CUdeviceptr>() as u64);
        let b_ptr_offset = b_ptrs
            .as_device_ptr()
            .wrapping_add(start as u64 * std::mem::size_of::<CUdeviceptr>() as u64);
        let c_ptr_offset = c_ptrs
            .as_device_ptr()
            .wrapping_add(start as u64 * std::mem::size_of::<CUdeviceptr>() as u64);
        let d_ptr_offset = d_ptrs
            .as_device_ptr()
            .wrapping_add(start as u64 * std::mem::size_of::<CUdeviceptr>() as u64);

        let args = (
            m,
            n,
            k,
            alpha_bits,
            a_ptr_offset,
            lda,
            b_ptr_offset,
            ldb,
            beta_bits,
            c_ptr_offset,
            ldc,
            d_ptr_offset,
            ldd,
        );

        kernel
            .launch(&params, stream, &args)
            .map_err(|e| BlasError::LaunchFailed(format!("stream {stream_idx}: {e}")))?;
    }

    Ok(())
}

/// Launches a batched GEMM kernel on a single stream for a contiguous
/// sub-range of the batch.
#[allow(clippy::too_many_arguments)]
fn launch_batch_on_stream<T: GpuFloat>(
    sm: SmVersion,
    stream: &Stream,
    trans_a: Transpose,
    trans_b: Transpose,
    m: u32,
    n: u32,
    k: u32,
    alpha: T,
    a_ptrs: &DeviceBuffer<CUdeviceptr>,
    lda: u32,
    b_ptrs: &DeviceBuffer<CUdeviceptr>,
    ldb: u32,
    beta: T,
    c_ptrs: &DeviceBuffer<CUdeviceptr>,
    ldc: u32,
    d_ptrs: &mut DeviceBuffer<CUdeviceptr>,
    ldd: u32,
    start: u32,
    count: u32,
) -> BlasResult<()> {
    if count == 0 {
        return Ok(());
    }

    let (ptx_source, kernel_name) = generate_batched_gemm_ptx::<T>(sm, m, n, k, trans_a, trans_b)?;
    let module = oxicuda_driver::Module::from_ptx(&ptx_source).map_err(BlasError::Cuda)?;
    let module = Arc::new(module);
    let kernel = Kernel::from_module(module, &kernel_name).map_err(BlasError::Cuda)?;

    let grid = Dim3::new(m.div_ceil(TILE_M), n.div_ceil(TILE_N), count);
    let block = Dim3::new(TILE_M, TILE_N, 1);
    let params = LaunchParams::new(grid, block);

    let alpha_bits = alpha.to_bits_u64();
    let beta_bits = beta.to_bits_u64();

    let ptr_size = std::mem::size_of::<CUdeviceptr>() as u64;
    let offset = start as u64 * ptr_size;

    let args = (
        m,
        n,
        k,
        alpha_bits,
        a_ptrs.as_device_ptr().wrapping_add(offset),
        lda,
        b_ptrs.as_device_ptr().wrapping_add(offset),
        ldb,
        beta_bits,
        c_ptrs.as_device_ptr().wrapping_add(offset),
        ldc,
        d_ptrs.as_device_ptr().wrapping_add(offset),
        ldd,
    );

    kernel
        .launch(&params, stream, &args)
        .map_err(|e| BlasError::LaunchFailed(e.to_string()))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- distribute_batches tests -------------------------------------------

    #[test]
    fn equal_split_even_division() {
        let result = distribute_batches(12, 3, &StreamDistribution::EqualSplit);
        assert_eq!(result, vec![(0, 4), (4, 4), (8, 4)]);
    }

    #[test]
    fn equal_split_uneven_division() {
        let result = distribute_batches(10, 3, &StreamDistribution::EqualSplit);
        // 10 / 3 = 3 remainder 1, so first stream gets 4, rest get 3.
        assert_eq!(result, vec![(0, 4), (4, 3), (7, 3)]);
    }

    #[test]
    fn equal_split_more_streams_than_batches() {
        let result = distribute_batches(2, 5, &StreamDistribution::EqualSplit);
        // Only 2 effective streams, each gets 1 batch.
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], (0, 1));
        assert_eq!(result[1], (1, 1));
    }

    #[test]
    fn equal_split_single_batch() {
        let result = distribute_batches(1, 4, &StreamDistribution::EqualSplit);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], (0, 1));
    }

    #[test]
    fn equal_split_single_stream() {
        let result = distribute_batches(100, 1, &StreamDistribution::EqualSplit);
        assert_eq!(result, vec![(0, 100)]);
    }

    #[test]
    fn round_robin_even_division() {
        let result = distribute_batches(12, 3, &StreamDistribution::RoundRobin);
        assert_eq!(result.len(), 3);
        // Each stream gets 4 batches.
        assert_eq!(result[0], (0, 4));
        assert_eq!(result[1], (1, 4));
        assert_eq!(result[2], (2, 4));
    }

    #[test]
    fn round_robin_uneven_division() {
        let result = distribute_batches(10, 3, &StreamDistribution::RoundRobin);
        // 10 / 3 = 3 remainder 1, first stream gets 4.
        assert_eq!(result[0], (0, 4));
        assert_eq!(result[1], (1, 3));
        assert_eq!(result[2], (2, 3));
    }

    #[test]
    fn zero_batches_returns_empty() {
        let result = distribute_batches(0, 4, &StreamDistribution::EqualSplit);
        assert!(result.is_empty());
    }

    #[test]
    fn zero_streams_returns_empty() {
        let result = distribute_batches(10, 0, &StreamDistribution::EqualSplit);
        assert!(result.is_empty());
    }

    #[test]
    fn config_validation_rejects_zero_streams() {
        let config = MultiStreamBatchedConfig::new(0, StreamDistribution::EqualSplit);
        let res = config.validate();
        assert!(res.is_err());
    }

    #[test]
    fn config_validation_accepts_positive_streams() {
        let config = MultiStreamBatchedConfig::new(4, StreamDistribution::RoundRobin);
        let res = config.validate();
        assert!(res.is_ok());
    }

    #[test]
    fn total_batches_preserved_equal_split() {
        for batch_count in 1u32..=50 {
            for num_streams in 1u32..=10 {
                let partitions =
                    distribute_batches(batch_count, num_streams, &StreamDistribution::EqualSplit);
                let total: u32 = partitions.iter().map(|&(_, c)| c).sum();
                assert_eq!(
                    total, batch_count,
                    "batch_count={batch_count}, num_streams={num_streams}"
                );
            }
        }
    }

    #[test]
    fn total_batches_preserved_round_robin() {
        for batch_count in 1u32..=50 {
            for num_streams in 1u32..=10 {
                let partitions =
                    distribute_batches(batch_count, num_streams, &StreamDistribution::RoundRobin);
                let total: u32 = partitions.iter().map(|&(_, c)| c).sum();
                assert_eq!(
                    total, batch_count,
                    "batch_count={batch_count}, num_streams={num_streams}"
                );
            }
        }
    }

    #[test]
    fn equal_split_contiguous_coverage() {
        // Verify that EqualSplit partitions cover [0, batch_count) contiguously.
        let partitions = distribute_batches(17, 4, &StreamDistribution::EqualSplit);
        let mut expected_start = 0u32;
        for &(start, count) in &partitions {
            assert_eq!(start, expected_start);
            expected_start += count;
        }
        assert_eq!(expected_start, 17);
    }

    #[test]
    fn distribution_balance_property() {
        // No stream should have more than ceil(batch_count / num_streams) batches.
        let batch_count = 23u32;
        let num_streams = 5u32;
        let max_per_stream = batch_count.div_ceil(num_streams);

        for dist in &[
            StreamDistribution::EqualSplit,
            StreamDistribution::RoundRobin,
        ] {
            let partitions = distribute_batches(batch_count, num_streams, dist);
            for &(_, count) in &partitions {
                assert!(
                    count <= max_per_stream,
                    "stream got {count} batches, max allowed {max_per_stream} ({dist:?})"
                );
            }
        }
    }
}