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
#[cfg(all(feature = "device", not(doc)))]
use core::arch::nvptx;

pub struct Thread {
    _private: (),
}

#[expect(clippy::module_name_repetitions)]
pub struct ThreadBlock {
    _private: (),
}

#[expect(clippy::module_name_repetitions)]
pub struct ThreadBlockGrid {
    _private: (),
}

impl Thread {
    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub const fn this() -> Self {
        Self { _private: () }
    }

    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn index(&self) -> usize {
        let block = self.block();
        let grid = block.grid();

        let block_id = block.idx().as_id(&grid.dim());
        let thread_id = self.idx().as_id(&block.dim());

        block_id * block.dim().size() + thread_id
    }

    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn idx(&self) -> Idx3 {
        #[expect(clippy::cast_sign_loss)]
        Idx3 {
            x: unsafe { nvptx::_thread_idx_x() } as u32,
            y: unsafe { nvptx::_thread_idx_y() } as u32,
            z: unsafe { nvptx::_thread_idx_z() } as u32,
        }
    }

    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub const fn block(&self) -> ThreadBlock {
        ThreadBlock { _private: () }
    }
}

impl ThreadBlock {
    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn dim(&self) -> Dim3 {
        #[expect(clippy::cast_sign_loss)]
        Dim3 {
            x: unsafe { nvptx::_block_dim_x() } as u32,
            y: unsafe { nvptx::_block_dim_y() } as u32,
            z: unsafe { nvptx::_block_dim_z() } as u32,
        }
    }

    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn idx(&self) -> Idx3 {
        #[expect(clippy::cast_sign_loss)]
        Idx3 {
            x: unsafe { nvptx::_block_idx_x() } as u32,
            y: unsafe { nvptx::_block_idx_y() } as u32,
            z: unsafe { nvptx::_block_idx_z() } as u32,
        }
    }

    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub const fn grid(&self) -> ThreadBlockGrid {
        ThreadBlockGrid { _private: () }
    }

    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn synchronize(&self) {
        unsafe { nvptx::_syncthreads() }
    }
}

impl ThreadBlockGrid {
    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub fn dim(&self) -> Dim3 {
        #[expect(clippy::cast_sign_loss)]
        Dim3 {
            x: unsafe { nvptx::_grid_dim_x() } as u32,
            y: unsafe { nvptx::_grid_dim_y() } as u32,
            z: unsafe { nvptx::_grid_dim_z() } as u32,
        }
    }
}

/// Dimension specified in kernel launching
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Dim3 {
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

/// Indices that the kernel code is running on
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Idx3 {
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

impl Dim3 {
    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub const fn size(&self) -> usize {
        (self.x as usize) * (self.y as usize) * (self.z as usize)
    }
}

impl Idx3 {
    #[must_use]
    #[expect(clippy::inline_always)]
    #[inline(always)]
    pub const fn as_id(&self, dim: &Dim3) -> usize {
        (self.x as usize)
            + (self.y as usize) * (dim.x as usize)
            + (self.z as usize) * (dim.x as usize) * (dim.y as usize)
    }
}