warp-types 0.3.2

Type-safe GPU warp programming via linear typestate: compile-time prevention of shuffle-from-inactive-lane bugs
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Inter-Block Sessions: Scaling Beyond Single Warp
//!
//! **STATUS: Research** — Exploratory prototype, not promoted to main API.
//!
//! This module explores how session types can model communication between
//! blocks in a GPU grid. This is fundamentally different from warp-level
//! sessions because:
//!
//! 1. **No lockstep execution**: Blocks execute independently
//! 2. **Memory-based communication**: No shuffles, use global memory
//! 3. **Explicit synchronization**: Barriers, atomics, cooperative groups
//! 4. **Scale**: Thousands of blocks vs 32 lanes
//!
//! # Key Insight
//!
//! Inter-block is closer to traditional MPST (Multiparty Session Types)
//! than our warp-level SIMT types. The "divergence" model doesn't apply
//! because blocks don't share control flow.
//!
//! # Hierarchy of Parallelism
//!
//! ```text
//! Grid (N blocks)
//!   └── Block (M warps)
//!         └── Warp (32/64 lanes)  <- Current work
//! ```
//!
//! Each level has different communication primitives and type constraints.

use std::marker::PhantomData;

// ============================================================================
// GPU HIERARCHY TYPES
// ============================================================================

/// A lane within a warp (0..31 for NVIDIA, 0..63 for AMD)
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LaneId(pub u32);

/// A warp within a block
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WarpId(pub u32);

/// A block within a grid
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BlockId(pub u32);

/// A thread's full identity in the grid
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ThreadId {
    pub block: BlockId,
    pub warp: WarpId,
    pub lane: LaneId,
}

impl ThreadId {
    pub fn global_id(&self, warps_per_block: u32, lanes_per_warp: u32) -> u32 {
        self.block.0 * warps_per_block * lanes_per_warp + self.warp.0 * lanes_per_warp + self.lane.0
    }
}

// ============================================================================
// COMMUNICATION PRIMITIVES BY LEVEL
// ============================================================================

/// Communication within a warp (our existing work).
/// Warp-level communication: shuffles, votes, ballots.
/// All 32 lanes execute in lockstep. No memory needed.
pub mod intra_warp {

    /// Shuffle: direct register exchange
    pub trait ShuffleOp {
        fn shuffle_xor<T: Copy>(val: T, mask: u32) -> T;
        fn shuffle_down<T: Copy>(val: T, delta: u32) -> T;
        fn shuffle_up<T: Copy>(val: T, delta: u32) -> T;
    }

    /// Vote: warp-wide predicate operations
    pub trait VoteOp {
        fn all(pred: bool) -> bool;
        fn any(pred: bool) -> bool;
        fn ballot(pred: bool) -> u32;
    }
}

/// Communication within a block (between warps).
/// Block-level communication: shared memory, barriers.
/// Warps in same block share SMEM. Block-wide `__syncthreads()`.
pub mod intra_block {

    use super::*;

    /// Shared memory region owned by a block
    pub struct SharedMem<T, const SIZE: usize> {
        _marker: PhantomData<T>,
    }

    impl<T: Copy, const SIZE: usize> SharedMem<T, SIZE> {
        /// All threads in block can read (after barrier)
        pub fn read(&self, _idx: usize) -> T
        where
            T: Default,
        {
            // Placeholder: CPU has no shared memory; return default value
            T::default()
        }

        /// Write requires knowing no conflicts
        pub fn write(&mut self, _idx: usize, _val: T) {
            // Placeholder: CPU has no shared memory; write is a no-op
        }
    }

    /// Block-wide barrier
    pub fn sync_threads() {
        // __syncthreads() - all threads in block must reach
    }

    /// Typed barrier: ensures all threads reach before proceeding
    pub struct BlockBarrier<State>(PhantomData<State>);

    pub struct BeforeSync;
    pub struct AfterSync;

    impl BlockBarrier<BeforeSync> {
        pub fn sync(self) -> BlockBarrier<AfterSync> {
            sync_threads();
            BlockBarrier(PhantomData)
        }
    }
}

/// Communication between blocks (the new frontier).
/// Grid-level communication: global memory, atomics, cooperative groups.
/// Blocks execute independently. No implicit synchronization.
pub mod inter_block {

    use super::*;

    /// Global memory region visible to all blocks
    pub struct GlobalMem<T> {
        _marker: PhantomData<T>,
    }

    impl<T: Copy + Default> GlobalMem<T> {
        /// Read from global memory (may be stale without fence)
        pub fn read(&self, _idx: usize) -> T {
            T::default() // CPU placeholder
        }

        /// Write to global memory (may not be visible without fence)
        pub fn write(&mut self, _idx: usize, _val: T) {
            // CPU placeholder — no-op
        }

        /// Atomic add - returns old value
        pub fn atomic_add(&self, _idx: usize, _val: T) -> T
        where
            T: std::ops::Add<Output = T>,
        {
            T::default() // CPU placeholder — returns "old value" of zero
        }
    }

    /// Memory fence - ensures visibility
    pub fn thread_fence_system() {
        // __threadfence_system()
    }

    /// Grid-wide barrier (requires cooperative launch)
    pub fn grid_sync() {
        // cooperative_groups::this_grid().sync()
    }
}

// ============================================================================
// INTER-BLOCK SESSION TYPES
// ============================================================================

/// A role in an inter-block protocol
///
/// Unlike warp lanes (fixed 0..31), block roles are logical.
/// Example: "leader block" vs "worker blocks"
pub trait BlockRole {
    const NAME: &'static str;
}

/// The single leader block (block 0)
pub struct Leader;
impl BlockRole for Leader {
    const NAME: &'static str = "Leader";
}

/// Worker blocks (blocks 1..N-1)
pub struct Worker;
impl BlockRole for Worker {
    const NAME: &'static str = "Worker";
}

/// Protocol states for inter-block sessions
pub trait ProtocolState {}

/// Initial state before any communication
pub struct Initial;
impl ProtocolState for Initial {}

/// After leader has broadcast work
pub struct WorkDistributed;
impl ProtocolState for WorkDistributed {}

/// After workers have signaled completion
pub struct WorkComplete;
impl ProtocolState for WorkComplete {}

/// After results have been collected
pub struct ResultsCollected;
impl ProtocolState for ResultsCollected {}

// ============================================================================
// INTER-BLOCK SESSION TYPES (MPST-like)
// ============================================================================

/// A session between blocks, parameterized by:
/// - Role: which block role we are
/// - State: current protocol state
/// - N: number of blocks (compile-time for typed guarantees)
pub struct BlockSession<Role: BlockRole, State: ProtocolState, const N: usize> {
    block_id: BlockId,
    _role: PhantomData<Role>,
    _state: PhantomData<State>,
}

impl<Role: BlockRole, State: ProtocolState, const N: usize> BlockSession<Role, State, N> {
    /// Create a new session for this block
    pub fn new(block_id: BlockId) -> Self {
        BlockSession {
            block_id,
            _role: PhantomData,
            _state: PhantomData,
        }
    }

    /// Get our block ID
    pub fn block_id(&self) -> BlockId {
        self.block_id
    }
}

// Leader operations
impl<State: ProtocolState, const N: usize> BlockSession<Leader, State, N> {
    /// Broadcast data to all worker blocks
    /// State transition: Initial -> WorkDistributed
    pub fn broadcast<T: Copy>(
        self,
        _data: T,
        _global: &mut inter_block::GlobalMem<T>,
    ) -> BlockSession<Leader, WorkDistributed, N>
    where
        State: Into<Initial>, // Only from Initial state
    {
        // Write to global memory locations for each worker
        // In real code: for i in 1..N { global.write(i, data); }
        BlockSession::new(self.block_id)
    }

    /// Wait for all workers to signal completion
    /// State transition: WorkDistributed -> WorkComplete
    pub fn wait_for_workers(
        self,
        _signal: &inter_block::GlobalMem<u32>,
    ) -> BlockSession<Leader, WorkComplete, N>
    where
        State: Into<WorkDistributed>,
    {
        // Spin on atomic counter until it reaches N-1
        // In real code: while signal.read(0) < N-1 { }
        BlockSession::new(self.block_id)
    }
}

// Worker operations
impl<State: ProtocolState, const N: usize> BlockSession<Worker, State, N> {
    /// Receive broadcast data from leader
    /// State transition: Initial -> WorkDistributed
    pub fn receive<T: Copy + Default>(
        self,
        _global: &inter_block::GlobalMem<T>,
    ) -> (T, BlockSession<Worker, WorkDistributed, N>)
    where
        State: Into<Initial>,
    {
        // Read from our global memory slot
        // In real code: let data = global.read(self.block_id.0);
        let data: T = T::default(); // Placeholder
        (data, BlockSession::new(self.block_id))
    }

    /// Signal completion to leader
    /// State transition: WorkDistributed -> WorkComplete
    pub fn signal_done(
        self,
        _signal: &inter_block::GlobalMem<u32>,
    ) -> BlockSession<Worker, WorkComplete, N>
    where
        State: Into<WorkDistributed>,
    {
        // Atomically increment completion counter
        // In real code: signal.atomic_add(0, 1);
        BlockSession::new(self.block_id)
    }
}

// ============================================================================
// COOPERATIVE GROUPS: BRIDGING THE GAP
// ============================================================================

/// Cooperative groups blur the boundary between warp, block, and grid.
/// They provide a uniform interface for different granularities.
pub mod cooperative {
    use super::*;

    /// A cooperative group - a subset of threads that can synchronize
    pub trait CooperativeGroup {
        /// Number of threads in this group
        fn size(&self) -> u32;

        /// This thread's rank within the group (0..size-1)
        fn thread_rank(&self) -> u32;

        /// Synchronize all threads in the group
        fn sync(&self);
    }

    /// Thread block group (all threads in a block)
    pub struct ThreadBlockGroup {
        block_id: BlockId,
        num_threads: u32,
    }

    impl CooperativeGroup for ThreadBlockGroup {
        fn size(&self) -> u32 {
            self.num_threads
        }
        fn thread_rank(&self) -> u32 {
            0
        } // CPU single-thread placeholder
        fn sync(&self) {
            intra_block::sync_threads();
        }
    }

    /// Grid group (all threads in the grid) - requires cooperative launch
    pub struct GridGroup {
        num_blocks: u32,
        threads_per_block: u32,
    }

    impl CooperativeGroup for GridGroup {
        fn size(&self) -> u32 {
            self.num_blocks * self.threads_per_block
        }
        fn thread_rank(&self) -> u32 {
            0
        } // CPU single-thread placeholder
        fn sync(&self) {
            inter_block::grid_sync();
        }
    }

    /// Coalesced group (subset of a warp with active lanes)
    /// This connects to our warp divergence work!
    pub struct CoalescedGroup {
        mask: u32, // Active lane mask
    }

    impl CooperativeGroup for CoalescedGroup {
        fn size(&self) -> u32 {
            self.mask.count_ones()
        }
        fn thread_rank(&self) -> u32 {
            0
        } // CPU single-thread placeholder
        fn sync(&self) { /* Implicit in warp execution */
        }
    }

    /// Tiled partition (fixed-size subset of a warp)
    pub struct TiledPartition<const SIZE: u32> {
        _marker: PhantomData<()>,
    }

    impl<const SIZE: u32> CooperativeGroup for TiledPartition<SIZE> {
        fn size(&self) -> u32 {
            SIZE
        }
        fn thread_rank(&self) -> u32 {
            0
        } // CPU single-thread placeholder
        fn sync(&self) { /* Implicit in warp execution for SIZE <= 32 */
        }
    }
}

// ============================================================================
// UNIFIED SESSION HIERARCHY
// ============================================================================

/// A unified view of sessions at any level of the GPU hierarchy.
///
/// The key insight: each level has similar operations but different
/// implementations and constraints.
pub trait SessionLevel {
    /// The identity type at this level
    type Id: Copy + Eq;

    /// The communication primitive
    type CommPrimitive;

    /// The synchronization primitive
    type SyncPrimitive;

    /// Maximum participants at this level
    const MAX_PARTICIPANTS: u32;
}

/// Warp level: 32 lanes, shuffle, implicit sync
pub struct WarpLevel;
impl SessionLevel for WarpLevel {
    type Id = LaneId;
    type CommPrimitive = (); // Shuffles
    type SyncPrimitive = (); // Implicit (SIMT)
    const MAX_PARTICIPANTS: u32 = 32;
}

/// Block level: variable warps, shared memory, __syncthreads
pub struct BlockLevel;
impl SessionLevel for BlockLevel {
    type Id = WarpId;
    type CommPrimitive = (); // Shared memory
    type SyncPrimitive = (); // __syncthreads
    const MAX_PARTICIPANTS: u32 = 32; // Typical max warps per block
}

/// Grid level: variable blocks, global memory, cooperative groups
pub struct GridLevel;
impl SessionLevel for GridLevel {
    type Id = BlockId;
    type CommPrimitive = (); // Global memory
    type SyncPrimitive = (); // grid.sync()
    const MAX_PARTICIPANTS: u32 = 65535; // Max blocks
}

// ============================================================================
// COMPARISON: WARP vs INTER-BLOCK
// ============================================================================

// Key differences between warp-level and inter-block sessions:
//
// | Aspect           | Warp-Level              | Inter-Block            |
// |------------------|-------------------------|------------------------|
// | Execution model  | SIMT (lockstep)         | Independent            |
// | Communication    | Shuffles (registers)    | Memory (global)        |
// | Synchronization  | Implicit                | Explicit barriers      |
// | Divergence       | Yes (some lanes idle)   | N/A (blocks run fully) |
// | Session model    | Quiescent participants  | Traditional MPST       |
// | Participants     | Fixed (32/64)           | Variable (1..65535)    |
// | Failure modes    | Deadlock-free (SIMT)    | Deadlock possible      |
//
// The warp-level "session-typed divergence" model is novel because:
// 1. Divergence = some participants go quiescent (not in traditional MPST)
// 2. Reconvergence = quiescent participants rejoin
// 3. Implicit sync means no deadlock (if divergence matches)
//
// Inter-block is closer to traditional MPST:
// 1. All blocks always "active" (no quiescence)
// 2. Explicit message passing via memory
// 3. Deadlock possible if barriers misaligned

// ============================================================================
// CROSS-LEVEL PROTOCOLS
// ============================================================================

/// A protocol that spans multiple levels of the hierarchy.
///
/// Example: Hierarchical reduction
///
/// 1. Each warp reduces its 32 values (warp-level)
/// 2. Warp 0 collects results from all warps (block-level)
/// 3. Block 0 collects results from all blocks (grid-level)
pub trait HierarchicalProtocol {
    /// Phase 1: Intra-warp (32 values -> 1)
    fn warp_reduce(&self) -> u32;

    /// Phase 2: Intra-block (N warps -> 1)
    fn block_reduce(&self) -> u32;

    /// Phase 3: Inter-block (M blocks -> 1)
    fn grid_reduce(&self) -> u32;
}

/// Type-safe hierarchical reduction
///
/// Each phase transitions the protocol state, preventing out-of-order execution.
pub mod hierarchical_reduce {
    use super::*;

    /// Phase states
    pub struct WarpPhase;
    pub struct BlockPhase;
    pub struct GridPhase;
    pub struct Complete;

    /// Reduction session
    pub struct ReductionSession<Phase> {
        value: u32,
        _phase: PhantomData<Phase>,
    }

    impl ReductionSession<WarpPhase> {
        pub fn new(value: u32) -> Self {
            ReductionSession {
                value,
                _phase: PhantomData,
            }
        }

        /// Warp reduction using shuffles
        /// Returns the result (in lane 0) and advances to block phase
        pub fn warp_reduce(self) -> (u32, ReductionSession<BlockPhase>) {
            // In real code: use shuffle_down reduction
            let result = self.value; // Placeholder
            (
                result,
                ReductionSession {
                    value: result,
                    _phase: PhantomData,
                },
            )
        }
    }

    impl ReductionSession<BlockPhase> {
        /// Block reduction using shared memory
        /// Only warp 0 continues to grid phase
        pub fn block_reduce(self) -> (u32, ReductionSession<GridPhase>) {
            // In real code: write to shared mem, sync, reduce
            let result = self.value;
            (
                result,
                ReductionSession {
                    value: result,
                    _phase: PhantomData,
                },
            )
        }
    }

    impl ReductionSession<GridPhase> {
        /// Grid reduction using global memory
        /// Only block 0 gets final result
        pub fn grid_reduce(self) -> (u32, ReductionSession<Complete>) {
            // In real code: write to global mem, grid sync, reduce
            let result = self.value;
            (
                result,
                ReductionSession {
                    value: result,
                    _phase: PhantomData,
                },
            )
        }
    }

    impl ReductionSession<Complete> {
        /// Get the final result (only meaningful in block 0, warp 0, lane 0)
        pub fn result(self) -> u32 {
            self.value
        }
    }
}

// ============================================================================
// RESEARCH QUESTIONS
// ============================================================================

/// Open questions for inter-block session types:
///
/// 1. **Deadlock freedom**: How to statically verify no deadlock?
///    - Warp-level: trivial (SIMT guarantees convergence)
///    - Inter-block: need progress analysis (all blocks reach barriers)
///
/// 2. **Partial participation**: What if some blocks don't participate?
///    - Unlike warp divergence, blocks can't "go quiescent"
///    - Must exit entirely or participate fully
///
/// 3. **Dynamic block count**: Grid size often runtime-determined
///    - Type-level N requires const generics
///    - Runtime N needs existential types or runtime checks
///
/// 4. **Failure modes**: What if a block crashes?
///    - Warp: other lanes continue (masked out)
///    - Block: undefined behavior, likely hang
///
/// 5. **Composability**: How to compose warp/block/grid sessions?
///    - Hierarchical protocols (reduce) are straightforward
///    - General composition needs more thought
///
/// 6. **Performance**: Do types add overhead?
///    - State machine approach should compile away
///    - But global memory communication has inherent cost

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_thread_id() {
        let tid = ThreadId {
            block: BlockId(2),
            warp: WarpId(3),
            lane: LaneId(7),
        };

        // 2 blocks * 4 warps * 32 lanes + 3 * 32 + 7
        let global = tid.global_id(4, 32);
        assert_eq!(global, 2 * 4 * 32 + 3 * 32 + 7);
    }

    #[test]
    fn test_hierarchical_reduction_types() {
        // This test verifies the type-state transitions compile correctly
        let session = ReductionSession::<WarpPhase>::new(42);

        // Phase 1: Warp reduce
        let (warp_result, session) = session.warp_reduce();
        assert_eq!(warp_result, 42);

        // Phase 2: Block reduce
        let (block_result, session) = session.block_reduce();
        assert_eq!(block_result, 42);

        // Phase 3: Grid reduce
        let (grid_result, session) = session.grid_reduce();
        assert_eq!(grid_result, 42);

        // Complete
        let final_result = session.result();
        assert_eq!(final_result, 42);
    }

    #[test]
    fn test_block_session_state_transitions() {
        // Leader session
        let leader: BlockSession<Leader, Initial, 4> = BlockSession::new(BlockId(0));
        assert_eq!(leader.block_id().0, 0);

        // Worker session
        let worker: BlockSession<Worker, Initial, 4> = BlockSession::new(BlockId(1));
        assert_eq!(worker.block_id().0, 1);
    }

    // Compile-time test: this should NOT compile if uncommented
    // #[test]
    // fn test_wrong_state_transition() {
    //     let session = ReductionSession::<WarpPhase>::new(42);
    //     // Try to skip to grid phase - should fail!
    //     let (_, session) = session.grid_reduce();  // ERROR: no method grid_reduce on WarpPhase
    // }
}

// ============================================================================
// SUMMARY: KEY INSIGHTS
// ============================================================================

// 1. **Inter-block ≠ warp divergence**: Blocks don't go quiescent, they
//    either run or don't exist. The "session-typed divergence" model
//    is specific to SIMT warp execution.
//
// 2. **Cooperative groups bridge levels**: They provide a uniform API
//    across warp/block/grid, but types still differ per level.
//
// 3. **Traditional MPST applies**: Inter-block is closer to distributed
//    systems. Existing MPST research (Honda/Yoshida/Carbone) applies.
//
// 4. **Hierarchical protocols are natural**: Warp → Block → Grid
//    reduction is a clean type-state machine.
//
// 5. **Deadlock is the main concern**: Unlike warp-level (where SIMT
//    guarantees convergence), inter-block needs explicit deadlock analysis.
//
// 6. **Research opportunity**: Unify warp-level divergence typing with
//    inter-block MPST. Cooperative groups might be the bridge.