cuda-rust-wasm 0.1.7

CUDA to Rust transpiler with WebGPU/WASM support
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
//! Cooperative groups for cross-block synchronization
//!
//! Provides a software emulation of CUDA cooperative groups, enabling
//! flexible thread grouping and synchronization patterns beyond the
//! traditional block-level `__syncthreads()`.

use crate::{Result, runtime_error};
use std::sync::{Arc, Barrier, Mutex};

/// Thread group abstraction for cooperative kernel execution
#[derive(Debug, Clone)]
pub struct CooperativeGroup {
    /// Number of threads in this group
    size: u32,
    /// Rank of this thread within the group
    rank: u32,
    /// Synchronization barrier shared among group members
    barrier: Arc<Barrier>,
}

impl CooperativeGroup {
    /// Create a new cooperative group
    pub fn new(size: u32, rank: u32) -> Result<Self> {
        if rank >= size {
            return Err(runtime_error!(
                "Thread rank {} exceeds group size {}",
                rank, size
            ));
        }
        Ok(Self {
            size,
            rank,
            barrier: Arc::new(Barrier::new(size as usize)),
        })
    }

    /// Create a group with a shared barrier
    pub fn with_barrier(size: u32, rank: u32, barrier: Arc<Barrier>) -> Result<Self> {
        if rank >= size {
            return Err(runtime_error!(
                "Thread rank {} exceeds group size {}",
                rank, size
            ));
        }
        Ok(Self { size, rank, barrier })
    }

    /// Get the number of threads in this group
    pub fn size(&self) -> u32 {
        self.size
    }

    /// Get this thread's rank within the group
    pub fn thread_rank(&self) -> u32 {
        self.rank
    }

    /// Synchronize all threads in this group
    pub fn sync(&self) {
        self.barrier.wait();
    }

    /// Check if this thread is the leader (rank 0)
    pub fn is_leader(&self) -> bool {
        self.rank == 0
    }
}

/// Thread block group (all threads in a block)
pub struct ThreadBlockGroup {
    inner: CooperativeGroup,
    block_idx: [u32; 3],
    block_dim: [u32; 3],
}

impl ThreadBlockGroup {
    /// Create a thread block group
    pub fn new(block_dim: [u32; 3], thread_idx: [u32; 3], barrier: Arc<Barrier>) -> Result<Self> {
        let size = block_dim[0] * block_dim[1] * block_dim[2];
        let rank = thread_idx[2] * block_dim[0] * block_dim[1]
            + thread_idx[1] * block_dim[0]
            + thread_idx[0];
        let inner = CooperativeGroup::with_barrier(size, rank, barrier)?;
        Ok(Self {
            inner,
            block_idx: [0, 0, 0],
            block_dim,
        })
    }

    /// Set the block index
    pub fn with_block_idx(mut self, idx: [u32; 3]) -> Self {
        self.block_idx = idx;
        self
    }

    /// Synchronize the thread block
    pub fn sync(&self) {
        self.inner.sync();
    }

    /// Get block dimensions
    pub fn dim_threads(&self) -> [u32; 3] {
        self.block_dim
    }

    /// Get group size (total threads in block)
    pub fn size(&self) -> u32 {
        self.inner.size()
    }

    /// Get thread rank within block
    pub fn thread_rank(&self) -> u32 {
        self.inner.thread_rank()
    }

    /// Get block index
    pub fn block_index(&self) -> [u32; 3] {
        self.block_idx
    }
}

/// Grid group (all threads across all blocks)
pub struct GridGroup {
    /// Total number of threads across all blocks
    total_threads: u32,
    /// Global rank of this thread
    global_rank: u32,
    /// Grid dimensions
    grid_dim: [u32; 3],
    /// Block dimensions
    block_dim: [u32; 3],
    /// Optional grid-level barrier for cooperative launch
    barrier: Option<Arc<Barrier>>,
}

impl GridGroup {
    /// Create a grid group
    pub fn new(
        grid_dim: [u32; 3],
        block_dim: [u32; 3],
        block_idx: [u32; 3],
        thread_idx: [u32; 3],
    ) -> Self {
        let threads_per_block = block_dim[0] * block_dim[1] * block_dim[2];
        let total_blocks = grid_dim[0] * grid_dim[1] * grid_dim[2];
        let total_threads = total_blocks * threads_per_block;

        let block_linear = block_idx[2] * grid_dim[0] * grid_dim[1]
            + block_idx[1] * grid_dim[0]
            + block_idx[0];
        let thread_linear = thread_idx[2] * block_dim[0] * block_dim[1]
            + thread_idx[1] * block_dim[0]
            + thread_idx[0];
        let global_rank = block_linear * threads_per_block + thread_linear;

        Self {
            total_threads,
            global_rank,
            grid_dim,
            block_dim,
            barrier: None,
        }
    }

    /// Create with a grid-level barrier for cooperative launch synchronization
    pub fn with_barrier(mut self, barrier: Arc<Barrier>) -> Self {
        self.barrier = Some(barrier);
        self
    }

    /// Get total number of threads in the grid
    pub fn size(&self) -> u32 {
        self.total_threads
    }

    /// Get this thread's global rank
    pub fn thread_rank(&self) -> u32 {
        self.global_rank
    }

    /// Get grid dimensions
    pub fn dim_blocks(&self) -> [u32; 3] {
        self.grid_dim
    }

    /// Get block dimensions
    pub fn dim_threads(&self) -> [u32; 3] {
        self.block_dim
    }

    /// Check if this thread is the leader
    pub fn is_leader(&self) -> bool {
        self.global_rank == 0
    }

    /// Synchronize all threads in the grid (cooperative launch only)
    pub fn sync(&self) -> Result<()> {
        match &self.barrier {
            Some(b) => {
                b.wait();
                Ok(())
            }
            None => Err(runtime_error!(
                "Grid sync requires cooperative launch with a shared barrier"
            )),
        }
    }
}

/// Tiled partition: a subdivision of a thread group
pub struct TiledPartition {
    /// Tile size (must be power of 2, max 32 for warp-level)
    tile_size: u32,
    /// Rank within the tile
    rank: u32,
    /// Barrier for the tile
    barrier: Arc<Barrier>,
    /// Shared data buffer for shuffle operations
    shared_data: Arc<Mutex<Vec<f32>>>,
}

impl TiledPartition {
    /// Create a tiled partition
    pub fn new(tile_size: u32, rank: u32) -> Result<Self> {
        if !tile_size.is_power_of_two() || tile_size > 32 {
            return Err(runtime_error!(
                "Tile size must be a power of 2 and <= 32, got {}",
                tile_size
            ));
        }
        if rank >= tile_size {
            return Err(runtime_error!(
                "Rank {} exceeds tile size {}",
                rank, tile_size
            ));
        }
        Ok(Self {
            tile_size,
            rank,
            barrier: Arc::new(Barrier::new(tile_size as usize)),
            shared_data: Arc::new(Mutex::new(vec![0.0; tile_size as usize])),
        })
    }

    /// Create with shared state
    pub fn with_shared(
        tile_size: u32,
        rank: u32,
        barrier: Arc<Barrier>,
        shared_data: Arc<Mutex<Vec<f32>>>,
    ) -> Result<Self> {
        if rank >= tile_size {
            return Err(runtime_error!(
                "Rank {} exceeds tile size {}",
                rank, tile_size
            ));
        }
        Ok(Self {
            tile_size,
            rank,
            barrier,
            shared_data,
        })
    }

    /// Get tile size
    pub fn size(&self) -> u32 {
        self.tile_size
    }

    /// Get thread rank within tile
    pub fn thread_rank(&self) -> u32 {
        self.rank
    }

    /// Synchronize threads within the tile
    pub fn sync(&self) {
        self.barrier.wait();
    }

    /// Shuffle: get value from thread with given rank
    pub fn shfl(&self, value: f32, src_rank: u32) -> f32 {
        {
            let mut data = self.shared_data.lock().unwrap();
            data[self.rank as usize] = value;
        }
        self.sync();
        let result = {
            let data = self.shared_data.lock().unwrap();
            let idx = (src_rank % self.tile_size) as usize;
            data[idx]
        };
        self.sync();
        result
    }

    /// Shuffle down: get value from thread rank + delta
    pub fn shfl_down(&self, value: f32, delta: u32) -> f32 {
        let src = self.rank + delta;
        if src >= self.tile_size {
            value // Return own value if source is out of range
        } else {
            self.shfl(value, src)
        }
    }

    /// Shuffle up: get value from thread rank - delta
    pub fn shfl_up(&self, value: f32, delta: u32) -> f32 {
        if self.rank < delta {
            value
        } else {
            self.shfl(value, self.rank - delta)
        }
    }

    /// Shuffle XOR: get value from thread rank ^ mask
    pub fn shfl_xor(&self, value: f32, mask: u32) -> f32 {
        self.shfl(value, self.rank ^ mask)
    }
}

/// Create a cooperative group for the current thread block
pub fn this_thread_block(
    block_dim: [u32; 3],
    thread_idx: [u32; 3],
    barrier: Arc<Barrier>,
) -> Result<ThreadBlockGroup> {
    ThreadBlockGroup::new(block_dim, thread_idx, barrier)
}

/// Create a grid group for cooperative kernel launch
pub fn this_grid(
    grid_dim: [u32; 3],
    block_dim: [u32; 3],
    block_idx: [u32; 3],
    thread_idx: [u32; 3],
) -> GridGroup {
    GridGroup::new(grid_dim, block_dim, block_idx, thread_idx)
}

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

    #[test]
    fn test_cooperative_group_creation() {
        let group = CooperativeGroup::new(32, 0).unwrap();
        assert_eq!(group.size(), 32);
        assert_eq!(group.thread_rank(), 0);
        assert!(group.is_leader());
    }

    #[test]
    fn test_cooperative_group_invalid_rank() {
        let result = CooperativeGroup::new(32, 32);
        assert!(result.is_err());
    }

    #[test]
    fn test_thread_block_group() {
        let barrier = Arc::new(Barrier::new(1));
        let group = ThreadBlockGroup::new([4, 4, 1], [2, 1, 0], barrier).unwrap();
        assert_eq!(group.size(), 16);
        assert_eq!(group.thread_rank(), 1 * 4 + 2); // y * dim_x + x = 6
        assert_eq!(group.dim_threads(), [4, 4, 1]);
    }

    #[test]
    fn test_grid_group() {
        let gg = GridGroup::new([2, 2, 1], [4, 4, 1], [1, 0, 0], [2, 1, 0]);
        assert_eq!(gg.size(), 4 * 16); // 4 blocks * 16 threads
        assert_eq!(gg.dim_blocks(), [2, 2, 1]);
        assert_eq!(gg.dim_threads(), [4, 4, 1]);
        // Block 1 (linear), thread 6 (linear) = 1*16 + 6 = 22
        assert_eq!(gg.thread_rank(), 22);
    }

    #[test]
    fn test_grid_group_leader() {
        let gg = GridGroup::new([1, 1, 1], [1, 1, 1], [0, 0, 0], [0, 0, 0]);
        assert!(gg.is_leader());

        let gg2 = GridGroup::new([2, 1, 1], [4, 1, 1], [1, 0, 0], [2, 0, 0]);
        assert!(!gg2.is_leader());
    }

    #[test]
    fn test_grid_sync_without_barrier() {
        let gg = GridGroup::new([1, 1, 1], [1, 1, 1], [0, 0, 0], [0, 0, 0]);
        assert!(gg.sync().is_err());
    }

    #[test]
    fn test_grid_sync_with_barrier() {
        let barrier = Arc::new(Barrier::new(1));
        let gg = GridGroup::new([1, 1, 1], [1, 1, 1], [0, 0, 0], [0, 0, 0])
            .with_barrier(barrier);
        assert!(gg.sync().is_ok());
    }

    #[test]
    fn test_tiled_partition_creation() {
        let tile = TiledPartition::new(4, 0).unwrap();
        assert_eq!(tile.size(), 4);
        assert_eq!(tile.thread_rank(), 0);
    }

    #[test]
    fn test_tiled_partition_invalid_size() {
        // Not a power of two
        assert!(TiledPartition::new(3, 0).is_err());
        // Too large
        assert!(TiledPartition::new(64, 0).is_err());
    }

    #[test]
    fn test_cooperative_group_sync() {
        // Single-thread sync should not deadlock
        let group = CooperativeGroup::new(1, 0).unwrap();
        group.sync();
    }

    #[test]
    fn test_multi_thread_cooperative_sync() {
        let barrier = Arc::new(Barrier::new(4));
        let handles: Vec<_> = (0..4)
            .map(|rank| {
                let b = barrier.clone();
                std::thread::spawn(move || {
                    let group = CooperativeGroup::with_barrier(4, rank, b).unwrap();
                    group.sync();
                    group.thread_rank()
                })
            })
            .collect();

        let mut ranks: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
        ranks.sort();
        assert_eq!(ranks, vec![0, 1, 2, 3]);
    }
}