cuda_oxide/
kernel_params.rs

1use crate::{DeviceBox, DevicePtr};
2
3/// Some data able to represent one or more kernel parameters
4pub trait KernelParameters {
5    fn params(&self, out: &mut Vec<Vec<u8>>);
6}
7
8impl KernelParameters for u8 {
9    fn params(&self, out: &mut Vec<Vec<u8>>) {
10        out.push(vec![*self]);
11    }
12}
13
14impl KernelParameters for u16 {
15    fn params(&self, out: &mut Vec<Vec<u8>>) {
16        out.push(self.to_le_bytes().to_vec());
17    }
18}
19
20impl KernelParameters for u32 {
21    fn params(&self, out: &mut Vec<Vec<u8>>) {
22        out.push(self.to_le_bytes().to_vec());
23    }
24}
25
26impl KernelParameters for u64 {
27    fn params(&self, out: &mut Vec<Vec<u8>>) {
28        out.push(self.to_le_bytes().to_vec());
29    }
30}
31
32impl KernelParameters for usize {
33    fn params(&self, out: &mut Vec<Vec<u8>>) {
34        out.push(self.to_le_bytes().to_vec());
35    }
36}
37
38impl KernelParameters for i8 {
39    fn params(&self, out: &mut Vec<Vec<u8>>) {
40        out.push(self.to_le_bytes().to_vec());
41    }
42}
43
44impl KernelParameters for i16 {
45    fn params(&self, out: &mut Vec<Vec<u8>>) {
46        out.push(self.to_le_bytes().to_vec());
47    }
48}
49
50impl KernelParameters for i32 {
51    fn params(&self, out: &mut Vec<Vec<u8>>) {
52        out.push(self.to_le_bytes().to_vec());
53    }
54}
55
56impl KernelParameters for i64 {
57    fn params(&self, out: &mut Vec<Vec<u8>>) {
58        out.push(self.to_le_bytes().to_vec());
59    }
60}
61
62impl KernelParameters for f32 {
63    fn params(&self, out: &mut Vec<Vec<u8>>) {
64        out.push(self.to_le_bytes().to_vec());
65    }
66}
67
68impl KernelParameters for f64 {
69    fn params(&self, out: &mut Vec<Vec<u8>>) {
70        out.push(self.to_le_bytes().to_vec());
71    }
72}
73
74/// WARNING: this is unsafe!
75impl<'a> KernelParameters for DevicePtr<'a> {
76    fn params(&self, out: &mut Vec<Vec<u8>>) {
77        out.push(self.inner.to_le_bytes().to_vec());
78    }
79}
80
81/// WARNING: this is unsafe!
82impl<'a, 'b> KernelParameters for &'b DeviceBox<'a> {
83    fn params(&self, out: &mut Vec<Vec<u8>>) {
84        out.push(self.inner.inner.to_le_bytes().to_vec());
85    }
86}
87
88impl KernelParameters for &[u8] {
89    fn params(&self, out: &mut Vec<Vec<u8>>) {
90        out.push(self.to_vec());
91    }
92}
93
94impl KernelParameters for Vec<u8> {
95    fn params(&self, out: &mut Vec<Vec<u8>>) {
96        out.push(self.clone());
97    }
98}
99
100impl<T: KernelParameters + Default + Copy, const N: usize> KernelParameters for [T; N] {
101    fn params(&self, out: &mut Vec<Vec<u8>>) {
102        for x in self {
103            x.params(out);
104        }
105    }
106}
107
108impl<T: KernelParameters> KernelParameters for Box<T> {
109    fn params(&self, out: &mut Vec<Vec<u8>>) {
110        (&**self).params(out);
111    }
112}
113
114impl KernelParameters for () {
115    fn params(&self, _out: &mut Vec<Vec<u8>>) {}
116}
117
118macro_rules! tuple_impls {
119    ($($len:expr => ($($n:tt $name:ident)+))+) => {
120        $(
121            impl<$($name: KernelParameters),+> KernelParameters for ($($name,)+) {
122                fn params(&self, out: &mut Vec<Vec<u8>>) {
123                    $(
124                        $name::params(&self.$n, out);
125                    )+
126                }
127            }
128        )+
129    }
130}
131
132tuple_impls! {
133    1 => (0 T0)
134    2 => (0 T0 1 T1)
135    3 => (0 T0 1 T1 2 T2)
136    4 => (0 T0 1 T1 2 T2 3 T3)
137    5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
138    6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
139    7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
140    8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
141    9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
142    10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
143    11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
144    12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
145    13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
146    14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
147    15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
148    16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
149}