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
//! Session Type Scalability
//!
//! **STATUS: Research** — Exploratory prototype, not promoted to main API.
//!
//! Research question: "Can session types scale to thousands of participants?"
//!
//! # Background
//!
//! Traditional MPST (Multiparty Session Types) has scaling challenges:
//! - Type size grows with participant count
//! - Projection complexity is O(n²) or worse
//! - Global type verification is expensive
//!
//! GPU kernels can have thousands of blocks. Can session types work?
//!
//! # Key Insight: Hierarchical Sessions
//!
//! GPU parallelism is HIERARCHICAL:
//! - Grid: thousands of blocks (inter-block sessions)
//! - Block: hundreds of threads (intra-block sessions)
//! - Warp: 32 lanes (intra-warp sessions - our focus)
//!
//! Each level has different communication patterns:
//! - Warp: shuffle, ballot (lockstep, no sync needed)
//! - Block: shared memory (explicit sync)
//! - Grid: global memory (atomic ops, no global sync)
//!
//! # Scaling Strategy
//!
//! 1. **Parameterized roles**: Not 1000 distinct types, but `Block<N>`
//! 2. **Symmetric protocols**: All blocks follow same pattern
//! 3. **Local verification**: Check one representative, not all
//! 4. **Hierarchical composition**: Nest session types by level

use std::marker::PhantomData;

// ============================================================================
// PARAMETERIZED ROLES
// ============================================================================

/// A block with a numeric ID (not a distinct type per block)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct BlockId(pub usize);

/// A warp within a block
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct WarpId {
    pub block: BlockId,
    pub warp_in_block: usize,
}

/// A lane within a warp
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LaneId {
    pub warp: WarpId,
    pub lane_in_warp: usize,
}

impl LaneId {
    /// Global lane ID across entire grid
    pub fn global_id(&self, warps_per_block: usize) -> usize {
        let warp_global = self.warp.block.0 * warps_per_block + self.warp.warp_in_block;
        warp_global * 32 + self.lane_in_warp
    }
}

// ============================================================================
// SYMMETRIC PROTOCOLS
// ============================================================================

/// A protocol that all blocks follow identically
///
/// Instead of typing each block separately, we type ONE representative
/// and verify all blocks follow the same pattern.
pub mod symmetric {

    /// Protocol that every block executes
    pub trait BlockProtocol: Copy {
        /// What each block does locally
        fn local_phase();

        /// How blocks communicate (if at all)
        fn communication_pattern() -> CommunicationPattern;
    }

    /// Types of inter-block communication
    #[derive(Copy, Clone, Debug)]
    pub enum CommunicationPattern {
        /// No inter-block communication (embarrassingly parallel)
        None,
        /// All blocks write to disjoint regions (parallel writes)
        DisjointWrites,
        /// Reduction: blocks contribute to shared accumulator
        Reduction,
        /// Neighbor exchange: block N talks to N-1 and N+1
        Stencil,
        /// All-to-all: any block may communicate with any other
        AllToAll,
    }

    /// Complexity of verifying the pattern
    impl CommunicationPattern {
        pub fn verification_complexity(&self, _num_blocks: usize) -> &'static str {
            match self {
                CommunicationPattern::None => "O(1) - verify one block",
                CommunicationPattern::DisjointWrites => "O(1) - verify disjointness property",
                CommunicationPattern::Reduction => "O(log n) - verify reduction tree",
                CommunicationPattern::Stencil => "O(1) - verify neighbor protocol",
                CommunicationPattern::AllToAll => "O(n²) - must verify all pairs",
            }
        }

        pub fn scales_well(&self) -> bool {
            !matches!(self, CommunicationPattern::AllToAll)
        }
    }

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

        #[test]
        fn test_pattern_scaling() {
            assert!(CommunicationPattern::None.scales_well());
            assert!(CommunicationPattern::DisjointWrites.scales_well());
            assert!(CommunicationPattern::Reduction.scales_well());
            assert!(CommunicationPattern::Stencil.scales_well());
            assert!(!CommunicationPattern::AllToAll.scales_well());
        }
    }
}

// ============================================================================
// HIERARCHICAL COMPOSITION
// ============================================================================

/// Nested session types for GPU hierarchy
pub mod hierarchical {
    use super::*;

    /// Grid-level session (inter-block)
    pub struct GridSession<P> {
        num_blocks: usize,
        _protocol: PhantomData<P>,
    }

    impl<P> GridSession<P> {
        pub fn new(num_blocks: usize) -> Self {
            GridSession {
                num_blocks,
                _protocol: PhantomData,
            }
        }

        pub fn num_blocks(&self) -> usize {
            self.num_blocks
        }
    }

    /// Block-level session (intra-block, inter-warp)
    pub struct BlockSession<P> {
        block_id: BlockId,
        num_warps: usize,
        _protocol: PhantomData<P>,
    }

    impl<P> BlockSession<P> {
        pub fn new(block_id: BlockId, num_warps: usize) -> Self {
            BlockSession {
                block_id,
                num_warps,
                _protocol: PhantomData,
            }
        }

        pub fn block_id(&self) -> BlockId {
            self.block_id
        }

        pub fn num_warps(&self) -> usize {
            self.num_warps
        }
    }

    /// Warp-level session (intra-warp, inter-lane)
    /// This is where our session-typed divergence lives!
    pub struct WarpSession<S> {
        warp_id: WarpId,
        _active_set: PhantomData<S>,
    }

    impl<S> WarpSession<S> {
        pub fn new(warp_id: WarpId) -> Self {
            WarpSession {
                warp_id,
                _active_set: PhantomData,
            }
        }

        pub fn warp_id(&self) -> WarpId {
            self.warp_id
        }
    }

    /// Compose sessions hierarchically
    pub fn decompose_grid<GP, BP>(grid: GridSession<GP>) -> Vec<BlockSession<BP>> {
        (0..grid.num_blocks())
            .map(|i| BlockSession::new(BlockId(i), 32)) // Assume 32 warps/block
            .collect()
    }

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

        #[test]
        fn test_hierarchical_decomposition() {
            struct GridProtocol;
            struct BlockProtocol;

            let grid: GridSession<GridProtocol> = GridSession::new(1000);
            assert_eq!(grid.num_blocks(), 1000);

            let blocks: Vec<BlockSession<BlockProtocol>> = decompose_grid(grid);
            assert_eq!(blocks.len(), 1000);
            assert_eq!(blocks[0].block_id(), BlockId(0));
            assert_eq!(blocks[999].block_id(), BlockId(999));
        }
    }
}

// ============================================================================
// INDEXED SESSION TYPES
// ============================================================================

/// Instead of distinct types per participant, use indexed types
pub mod indexed {
    use super::*;

    /// A session indexed by block ID
    ///
    /// `Session<N>` represents "the session from block N's perspective"
    /// All blocks have the SAME type structure, just different index.
    pub struct IndexedSession<const N: usize> {
        _marker: PhantomData<()>,
    }

    impl<const N: usize> IndexedSession<N> {
        pub fn new() -> Self {
            IndexedSession {
                _marker: PhantomData,
            }
        }

        pub fn block_id() -> usize {
            N
        }
    }

    /// Reduction: each block contributes to partial sum
    ///
    /// Type: IndexedSession<N> -[contribute]-> IndexedSession<N>
    ///
    /// All blocks have same type! Verification is O(1).
    pub fn contribute_to_reduction<const N: usize>(
        _session: IndexedSession<N>,
        value: i32,
    ) -> (IndexedSession<N>, i32) {
        // In real GPU: atomicAdd to global accumulator
        (IndexedSession::new(), value)
    }

    /// Stencil: exchange with neighbors
    ///
    /// Block N reads from N-1 and N+1 (with wrapping).
    /// All blocks have same pattern!
    pub fn stencil_exchange<const N: usize, const NUM_BLOCKS: usize>(
        _session: IndexedSession<N>,
        my_value: i32,
    ) -> (IndexedSession<N>, i32, i32) {
        // Neighbors (with wrapping)
        let _left = (N + NUM_BLOCKS - 1) % NUM_BLOCKS;
        let _right = (N + 1) % NUM_BLOCKS;

        // In real GPU: read from global memory at neighbor positions
        (IndexedSession::new(), my_value, my_value) // Placeholder
    }

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

        #[test]
        fn test_indexed_sessions() {
            // All blocks have the same type structure
            let s0: IndexedSession<0> = IndexedSession::new();
            let s1: IndexedSession<1> = IndexedSession::new();
            let s999: IndexedSession<999> = IndexedSession::new();

            assert_eq!(IndexedSession::<0>::block_id(), 0);
            assert_eq!(IndexedSession::<1>::block_id(), 1);
            assert_eq!(IndexedSession::<999>::block_id(), 999);

            // Same operations available on all
            let (_, _) = contribute_to_reduction(s0, 10);
            let (_, _) = contribute_to_reduction(s1, 20);
            let (_, _) = contribute_to_reduction(s999, 30);
        }
    }
}

// ============================================================================
// COMPLEXITY ANALYSIS
// ============================================================================

/// Analysis of session type complexity at scale
pub mod complexity {
    /// Traditional MPST complexity
    pub fn traditional_mpst(participants: usize) -> String {
        format!(
            "Traditional MPST with {} participants:\n\
             - Global type size: O(n²) - all pairwise interactions\n\
             - Projection: O(n) per participant\n\
             - Total verification: O(n³)\n\
             - NOT practical for n > 100",
            participants
        )
    }

    /// Our hierarchical approach
    pub fn hierarchical_approach(blocks: usize, warps_per_block: usize) -> String {
        format!(
            "Hierarchical sessions with {} blocks × {} warps:\n\
             - Grid level: O(1) if symmetric, O(n) for reduction\n\
             - Block level: O(1) - same for all blocks\n\
             - Warp level: O(1) - 32 lanes, fixed\n\
             - Total: O(1) to O(n) depending on pattern\n\
             - PRACTICAL for n > 10000",
            blocks, warps_per_block
        )
    }

    /// When does it scale?
    pub fn scaling_analysis() -> &'static str {
        "Session types scale when:\n\
         1. Protocols are SYMMETRIC (all participants same role)\n\
         2. Communication is STRUCTURED (reduction, stencil, not all-to-all)\n\
         3. Verification is LOCAL (check one, prove all)\n\
         4. Hierarchy is EXPLOITED (don't flatten to single level)\n\
         \n\
         GPU kernels naturally have these properties!"
    }

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

        #[test]
        fn test_complexity_comparison() {
            let trad = traditional_mpst(1000);
            assert!(trad.contains("O(n³)"));

            let hier = hierarchical_approach(1000, 32);
            assert!(hier.contains("O(1)"));
        }
    }
}

// ============================================================================
// SUMMARY
// ============================================================================

/// Summary: Can session types scale to thousands of participants?
///
/// ## Answer: YES, with the right approach
///
/// ### Why Traditional MPST Doesn't Scale
///
/// - Global type encodes ALL pairwise interactions: O(n²) size
/// - Projection must consider all other participants: O(n) each
/// - Verification is O(n³) - impractical for n > 100
///
/// ### Why GPU Sessions DO Scale
///
/// 1. **Symmetric protocols**: All blocks/warps follow same pattern
///    - Verify ONE representative, prove ALL correct
///    - O(1) instead of O(n)
///
/// 2. **Structured communication**: Not arbitrary all-to-all
///    - Reduction: O(log n) tree structure
///    - Stencil: O(1) neighbor pattern
///    - Disjoint: O(1) no communication
///
/// 3. **Hierarchical decomposition**: Don't flatten
///    - Grid → Block → Warp → Lane
///    - Each level has bounded participants (32 lanes, ~32 warps)
///    - Compose levels, don't multiply
///
/// 4. **Indexed types**: `Block<N>` not distinct types per block
///    - Same type structure, parameterized by index
///    - Compiler sees ONE type, not 1000
///
/// ### Practical Numbers
///
/// | Approach | 32 participants | 1000 participants | 100000 participants |
/// |----------|-----------------|-------------------|---------------------|
/// | Traditional MPST | O(32³) ≈ 32K | O(10⁹) ✗ | O(10¹⁵) ✗ |
/// | Symmetric | O(1) ✓ | O(1) ✓ | O(1) ✓ |
/// | Reduction | O(5) ✓ | O(10) ✓ | O(17) ✓ |
/// | Hierarchical | O(32) ✓ | O(32) ✓ | O(32) ✓ |
///
/// ### Conclusion
///
/// Session types SCALE to thousands of GPU participants because:
/// 1. GPU parallelism is naturally symmetric
/// 2. Communication patterns are structured
/// 3. Hierarchy bounds verification at each level
///
/// The "thousands of participants" problem is a non-issue for GPUs.
pub const _SUMMARY: () = ();

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

    #[test]
    fn test_full_hierarchy() {
        use hierarchical::*;

        struct GridProto;
        struct BlockProto;
        struct All;

        // Create a grid with 1000 blocks
        let grid: GridSession<GridProto> = GridSession::new(1000);

        // Decompose to blocks
        let blocks: Vec<BlockSession<BlockProto>> = decompose_grid(grid);
        assert_eq!(blocks.len(), 1000);

        // Each block has warps
        for block in &blocks {
            assert_eq!(block.num_warps(), 32);
        }

        // Each warp has lanes (our session-typed divergence)
        let warp: WarpSession<All> = WarpSession::new(WarpId {
            block: BlockId(0),
            warp_in_block: 0,
        });

        assert_eq!(warp.warp_id().block, BlockId(0));
    }
}