pub mod level0_constant {
pub struct All;
pub struct None;
pub fn coverage() -> &'static str {
"Uniform execution - no divergence needed"
}
}
pub mod level1_lane_id {
#[derive(Copy, Clone)]
pub struct Even;
#[derive(Copy, Clone)]
pub struct Odd;
#[derive(Copy, Clone)]
pub struct LowHalf;
#[derive(Copy, Clone)]
pub struct HighHalf;
#[derive(Copy, Clone)]
pub struct Lane0;
pub fn patterns() -> Vec<&'static str> {
vec![
"lane % 2 == 0 (Even)",
"lane % 2 == 1 (Odd)",
"lane < N/2 (LowHalf)",
"lane >= N/2 (HighHalf)",
"lane == 0 (Lane0)",
"lane < K for const K",
"lane % K == 0 for const K",
]
}
pub fn use_cases() -> Vec<&'static str> {
vec![
"Butterfly reduction (XOR shuffle pattern)",
"Prefix scan (up/down sweep)",
"Leader election (lane 0 broadcasts)",
"Half-warp operations (memory coalescing)",
]
}
pub fn coverage_estimate() -> &'static str {
"~60% of real divergence patterns"
}
}
pub mod level2_data_dependent {
pub fn patterns() -> Vec<&'static str> {
vec![
"data[lane] < threshold",
"data[lane] == target",
"data[lane] != SENTINEL",
"predicate_array[lane]",
]
}
pub fn use_cases() -> Vec<&'static str> {
vec![
"Stream compaction (remove invalid elements)",
"Filtering (select elements matching criteria)",
"Partitioning (split by pivot)",
"Early termination (some lanes find answer)",
]
}
pub fn coverage_estimate() -> &'static str {
"~30% of real divergence patterns"
}
pub fn type_system_approach() -> &'static str {
"Use SomeWarp with runtime complement check, or \
refine to Level 1 when threshold is uniform"
}
}
pub mod level3_cross_lane {
pub fn patterns() -> Vec<&'static str> {
vec![
"lane < popcount(ballot(condition))",
"lane in compacted_indices(condition)",
"role_assignment[lane] == PRODUCER",
]
}
pub fn use_cases() -> Vec<&'static str> {
vec![
"Work redistribution (balance load)",
"Stream compaction destination",
"Dynamic role assignment",
]
}
pub fn coverage_estimate() -> &'static str {
"~8% of real divergence patterns"
}
pub fn type_system_approach() -> &'static str {
"Computed at runtime via ballot/popcount. Type is SomeWarp. \
Can refine to Level 1 if count is power-of-2."
}
}
pub mod level4_arbitrary {
pub fn patterns() -> Vec<&'static str> {
vec![
"complex_condition(lane, data, iteration, ...)",
"external_predicate_from_host",
]
}
pub fn coverage_estimate() -> &'static str {
"~2% of real divergence patterns"
}
pub fn type_system_approach() -> &'static str {
"Full dependent types (like Idris/Agda) or \
escape hatch to SomeWarp with runtime checks"
}
pub fn practical_note() -> &'static str {
"Most Level 4 cases can be restructured to Level 2-3 \
with algorithm changes. The 2% is often avoidable."
}
}
pub mod algorithm_survey {
pub fn reduction() -> (&'static str, &'static str) {
("Level 1", "lane < stride (butterfly pattern)")
}
pub fn prefix_scan() -> (&'static str, &'static str) {
("Level 1", "lane >= offset (up/down sweep)")
}
pub fn stream_compaction() -> (&'static str, &'static str) {
(
"Level 2→1",
"data[lane].valid, then compute destination indices",
)
}
pub fn parallel_sort() -> (&'static str, &'static str) {
("Level 1", "lane XOR distance comparisons")
}
pub fn graph_traversal() -> (&'static str, &'static str) {
("Level 2", "frontier[lane].valid")
}
pub fn sparse_matrix() -> (&'static str, &'static str) {
("Level 2", "row_ptr[lane] < row_ptr[lane+1]")
}
pub fn decision_trees() -> (&'static str, &'static str) {
("Level 2", "feature[lane] < split_value")
}
pub fn ray_tracing() -> (&'static str, &'static str) {
("Level 2", "ray[lane].intersects(node)")
}
pub fn neural_network() -> (&'static str, &'static str) {
("Level 1", "Mostly uniform, occasional activation sparsity")
}
pub fn summary() -> Vec<(&'static str, &'static str, &'static str)> {
vec![
("Reduction", "Level 1", "60%"),
("Prefix Scan", "Level 1", ""),
("Sort", "Level 1", ""),
("Compaction", "Level 2→1", "25%"),
("Graph", "Level 2", ""),
("SpMV", "Level 2", ""),
("Trees", "Level 2", ""),
("Ray Trace", "Level 2", ""),
("Neural Net", "Level 1", "10%"),
("Other", "Level 3-4", "5%"),
]
}
}
pub fn recommendation() -> &'static str {
"Marker types (Level 1) + SomeWarp escape hatch (Level 2-3) \
covers 95%+ of real GPU divergence patterns. \
Full dependent types are theoretically interesting but \
practically unnecessary for most code."
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_level1_patterns() {
let patterns = level1_lane_id::patterns();
assert!(patterns.len() >= 5);
}
#[test]
fn test_level2_patterns() {
let patterns = level2_data_dependent::patterns();
assert!(patterns.len() >= 3);
}
#[test]
fn test_algorithm_coverage() {
let summary = algorithm_survey::summary();
let level1_count = summary
.iter()
.filter(|(_, level, _)| level.starts_with("Level 1"))
.count();
assert!(level1_count >= 3);
}
#[test]
fn test_recommendation_exists() {
let rec = recommendation();
assert!(rec.contains("Marker types"));
assert!(rec.contains("SomeWarp"));
}
}
pub const _ERGONOMICS: () = ();