|
| 1 | +use crate::event::{ComptimeEventBus, EventListener, EventListenerExpand}; |
| 2 | +use cubecl::prelude::*; |
| 3 | +use cubecl_core as cubecl; |
| 4 | + |
| 5 | +#[derive(CubeType)] |
| 6 | +pub struct EventUInt { |
| 7 | + #[cube(comptime)] |
| 8 | + pub value: u32, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(CubeType)] |
| 12 | +pub struct EventFloat { |
| 13 | + #[cube(comptime)] |
| 14 | + pub value: f32, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(CubeType, Clone)] |
| 18 | +pub struct EventListenerPosZero { |
| 19 | + items: SliceMut<f32>, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(CubeType, Clone)] |
| 23 | +pub struct EventListenerPosOne { |
| 24 | + items: SliceMut<f32>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(CubeType, Clone)] |
| 28 | +pub struct EventListenerPosTwo { |
| 29 | + items: SliceMut<f32>, |
| 30 | + times: ComptimeCell<Counter>, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(CubeType, Clone)] |
| 34 | +pub struct Counter { |
| 35 | + #[cube(comptime)] |
| 36 | + value: u32, |
| 37 | +} |
| 38 | + |
| 39 | +#[cube] |
| 40 | +impl EventListener for EventListenerPosZero { |
| 41 | + type Event = EventUInt; |
| 42 | + |
| 43 | + fn on_event(&mut self, event: Self::Event, bus: &mut ComptimeEventBus) { |
| 44 | + if comptime!(event.value < 10) { |
| 45 | + comment!("On event pos zero < 10"); |
| 46 | + bus.event::<EventUInt>(EventUInt { |
| 47 | + value: comptime!(15u32 + event.value), |
| 48 | + }); |
| 49 | + } else { |
| 50 | + comment!("On event pos zero >= 10"); |
| 51 | + self.items[0] = f32::cast_from(event.value); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[cube] |
| 57 | +impl EventListener for EventListenerPosOne { |
| 58 | + type Event = EventUInt; |
| 59 | + |
| 60 | + fn on_event(&mut self, event: Self::Event, _bus: &mut ComptimeEventBus) { |
| 61 | + comment!("On event pos one"); |
| 62 | + self.items[1] = (f32::cast_from(event.value) * 2.0) + self.items[1]; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cube] |
| 67 | +impl EventListener for EventListenerPosTwo { |
| 68 | + type Event = EventFloat; |
| 69 | + |
| 70 | + fn on_event(&mut self, event: Self::Event, bus: &mut ComptimeEventBus) { |
| 71 | + comment!("On event pos two"); |
| 72 | + self.items[2] = event.value + self.items[2]; |
| 73 | + |
| 74 | + let times = self.times.read(); |
| 75 | + self.times.store(Counter { |
| 76 | + value: comptime!(times.value + 1), |
| 77 | + }); |
| 78 | + |
| 79 | + if comptime!(times.value < 4) { |
| 80 | + bus.event::<EventFloat>(EventFloat { |
| 81 | + value: comptime!(event.value * 2.0), |
| 82 | + }); |
| 83 | + bus.event::<EventUInt>(EventUInt { |
| 84 | + value: comptime!((event.value * 2.0) as u32), |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cube] |
| 91 | +fn test_1(items: SliceMut<f32>) { |
| 92 | + let mut bus = ComptimeEventBus::new(); |
| 93 | + let listener_zero = EventListenerPosZero { items }; |
| 94 | + let listener_one = EventListenerPosOne { items }; |
| 95 | + |
| 96 | + bus.listener::<EventListenerPosZero>(listener_zero); |
| 97 | + bus.listener::<EventListenerPosOne>(listener_one); |
| 98 | + |
| 99 | + bus.event::<EventUInt>(EventUInt { value: 5u32 }); |
| 100 | +} |
| 101 | + |
| 102 | +#[cube] |
| 103 | +fn test_2(items: SliceMut<f32>) { |
| 104 | + let mut bus = ComptimeEventBus::new(); |
| 105 | + let listener_zero = EventListenerPosZero { items }; |
| 106 | + let listener_one = EventListenerPosOne { items }; |
| 107 | + |
| 108 | + bus.listener::<EventListenerPosZero>(listener_zero); |
| 109 | + bus.listener::<EventListenerPosOne>(listener_one); |
| 110 | + |
| 111 | + bus.event::<EventUInt>(EventUInt { value: 15u32 }); |
| 112 | +} |
| 113 | + |
| 114 | +#[cube] |
| 115 | +fn test_3(items: SliceMut<f32>) { |
| 116 | + let mut bus = ComptimeEventBus::new(); |
| 117 | + let listener_zero = EventListenerPosZero { items }; |
| 118 | + let listener_one = EventListenerPosOne { items }; |
| 119 | + let listener_two = EventListenerPosTwo { |
| 120 | + items, |
| 121 | + times: ComptimeCell::new(Counter { value: 0u32 }), |
| 122 | + }; |
| 123 | + |
| 124 | + bus.listener::<EventListenerPosZero>(listener_zero); |
| 125 | + bus.listener::<EventListenerPosOne>(listener_one); |
| 126 | + bus.listener::<EventListenerPosTwo>(listener_two); |
| 127 | + |
| 128 | + bus.event::<EventFloat>(EventFloat { value: 15.0f32 }); |
| 129 | +} |
| 130 | + |
| 131 | +#[cube(launch_unchecked)] |
| 132 | +fn launch_test_1(output: &mut Array<f32>) { |
| 133 | + output[0] = 0.0; |
| 134 | + output[1] = 0.0; |
| 135 | + test_1(output.to_slice_mut()); |
| 136 | +} |
| 137 | + |
| 138 | +#[cube(launch_unchecked)] |
| 139 | +fn launch_test_2(output: &mut Array<f32>) { |
| 140 | + output[0] = 0.0; |
| 141 | + output[1] = 0.0; |
| 142 | + test_2(output.to_slice_mut()); |
| 143 | +} |
| 144 | + |
| 145 | +#[cube(launch_unchecked)] |
| 146 | +fn launch_test_3(output: &mut Array<f32>) { |
| 147 | + output[0] = 0.0; |
| 148 | + output[1] = 0.0; |
| 149 | + output[2] = 0.0; |
| 150 | + test_3(output.to_slice_mut()); |
| 151 | +} |
| 152 | + |
| 153 | +pub fn event_test_1<R: Runtime>(client: ComputeClient<R::Server>) { |
| 154 | + let output = client.empty(8); |
| 155 | + |
| 156 | + unsafe { |
| 157 | + launch_test_1::launch_unchecked::<R>( |
| 158 | + &client, |
| 159 | + CubeCount::Static(1, 1, 1), |
| 160 | + CubeDim { x: 1, y: 1, z: 1 }, |
| 161 | + ArrayArg::from_raw_parts::<f32>(&output, 2, 1), |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + let bytes = client.read_one(output); |
| 166 | + let actual = f32::from_bytes(&bytes); |
| 167 | + |
| 168 | + assert_eq!(actual, &[20.0, 50.0]); |
| 169 | +} |
| 170 | + |
| 171 | +pub fn event_test_2<R: Runtime>(client: ComputeClient<R::Server>) { |
| 172 | + let output = client.empty(8); |
| 173 | + |
| 174 | + unsafe { |
| 175 | + launch_test_2::launch_unchecked::<R>( |
| 176 | + &client, |
| 177 | + CubeCount::Static(1, 1, 1), |
| 178 | + CubeDim { x: 1, y: 1, z: 1 }, |
| 179 | + ArrayArg::from_raw_parts::<f32>(&output, 2, 1), |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + let bytes = client.read_one(output); |
| 184 | + let actual = f32::from_bytes(&bytes); |
| 185 | + |
| 186 | + assert_eq!(actual, &[15.0, 30.0]); |
| 187 | +} |
| 188 | + |
| 189 | +pub fn event_test_3<R: Runtime>(client: ComputeClient<R::Server>) { |
| 190 | + let output = client.empty(12); |
| 191 | + |
| 192 | + unsafe { |
| 193 | + launch_test_3::launch_unchecked::<R>( |
| 194 | + &client, |
| 195 | + CubeCount::Static(1, 1, 1), |
| 196 | + CubeDim { x: 1, y: 1, z: 1 }, |
| 197 | + ArrayArg::from_raw_parts::<f32>(&output, 3, 1), |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + let bytes = client.read_one(output); |
| 202 | + let actual = f32::from_bytes(&bytes); |
| 203 | + |
| 204 | + assert_eq!(actual, &[30.0, 900.0, 465.0]); |
| 205 | +} |
| 206 | + |
| 207 | +#[macro_export] |
| 208 | +macro_rules! testgen_event { |
| 209 | + () => { |
| 210 | + mod event { |
| 211 | + use super::*; |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn test_1() { |
| 215 | + let client = TestRuntime::client(&Default::default()); |
| 216 | + cubecl_std::tests::event::event_test_1::<TestRuntime>(client); |
| 217 | + } |
| 218 | + |
| 219 | + #[test] |
| 220 | + fn test_2() { |
| 221 | + let client = TestRuntime::client(&Default::default()); |
| 222 | + cubecl_std::tests::event::event_test_2::<TestRuntime>(client); |
| 223 | + } |
| 224 | + |
| 225 | + #[test] |
| 226 | + fn test_3() { |
| 227 | + let client = TestRuntime::client(&Default::default()); |
| 228 | + cubecl_std::tests::event::event_test_3::<TestRuntime>(client); |
| 229 | + } |
| 230 | + } |
| 231 | + }; |
| 232 | +} |
0 commit comments