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
use std::mem::ManuallyDrop;
use std::time::Duration;

use crate::{
    build_output, ClCommandQueue, ClContext, ClNumber, ClPointer, CommandExecutionStatus,
    Error, EventInfo, Output, ProfilingInfo, Waitlist, ObjectWrapper,
};

use crate::ffi::{
    clGetEventInfo, clGetEventProfilingInfo, cl_command_queue, cl_context, cl_event, cl_event_info,
    cl_profiling_info, cl_ulong,
};

use crate::cl_helpers::cl_get_info5;

// NOTE: Fix cl_profiling_info arg // should be a bitflag or enum.
pub unsafe fn cl_get_event_profiling_info(
    event: cl_event,
    info_flag: cl_profiling_info,
) -> Output<u64> {
    let mut time: cl_ulong = 0;
    let err_code = clGetEventProfilingInfo(
        event,
        info_flag,
        std::mem::size_of::<cl_ulong>() as libc::size_t,
        (&mut time as *mut u64) as *mut libc::c_void,
        std::ptr::null_mut(),
    );
    build_output(time as u64, err_code)
}

pub unsafe fn cl_get_event_info<T: Copy>(
    event: cl_event,
    info_flag: cl_event_info,
) -> Output<ClPointer<T>> {
    cl_get_info5(event, info_flag, clGetEventInfo)
}

pub type ClEvent = ObjectWrapper<cl_event>;

pub unsafe trait EventPtr: Sized {
    unsafe fn event_ptr(&self) -> cl_event;
}

unsafe impl EventPtr for cl_event {
    unsafe fn event_ptr(&self) -> cl_event {
        *self
    }
}

unsafe impl EventPtr for ClEvent {
    unsafe fn event_ptr(&self) -> cl_event {
        self.cl_object()
    }
}

unsafe impl EventPtr for &ClEvent {
    unsafe fn event_ptr(&self) -> cl_event {
        self.cl_object()
    }
}

/// An error related to an Event or WaitList.
#[derive(Debug, Fail, PartialEq, Eq, Clone)]
pub enum EventError {
    #[fail(display = "Encountered a null cl_event.")]
    ClEventCannotBeNull,

    #[fail(display = "Event was already consumed. {:?}", _0)]
    EventAlreadyConsumed(String),
}


impl ClEvent {
    pub fn time(&self, info: ProfilingInfo) -> Output<u64> {
        unsafe { cl_get_event_profiling_info(self.event_ptr(), info.into()) }
    }

    pub fn queue_time(&self) -> Output<u64> {
        self.time(ProfilingInfo::Queued)
    }

    pub fn submit_time(&self) -> Output<u64> {
        self.time(ProfilingInfo::Submit)
    }

    pub fn start_time(&self) -> Output<u64> {
        self.time(ProfilingInfo::Start)
    }

    pub fn end_time(&self) -> Output<u64> {
        self.time(ProfilingInfo::End)
    }

    pub fn profiling(&self) -> Profiling {
        Profiling {
            submit_time: self.submit_time().ok(),
            queue_time: self.queue_time().ok(),
            start_time: self.start_time().ok(),
            end_time: self.end_time().ok(),
        }
    }

    pub unsafe fn info<T: Copy>(&self, flag: EventInfo) -> Output<ClPointer<T>> {
        cl_get_event_info::<T>(self.event_ptr(), flag.into())
    }

    pub fn reference_count(&self) -> Output<u32> {
        unsafe {
            self.info(EventInfo::ReferenceCount)
                .map(|ret| ret.into_one())
        }
    }

    pub unsafe fn cl_command_queue(&self) -> Output<cl_command_queue> {
        self.info(EventInfo::CommandQueue)
            .map(|cl_ptr| cl_ptr.into_one())
    }

    pub unsafe fn command_queue(&self) -> Output<ClCommandQueue> {
        self.cl_command_queue()
            .and_then(|cq| ClCommandQueue::retain_new(cq))
    }

    pub unsafe fn cl_context(&self) -> Output<cl_context> {
        self.info(EventInfo::Context)
            .map(|cl_ptr| cl_ptr.into_one())
    }

    pub unsafe fn context(&self) -> Output<ClContext> {
        self.cl_context().and_then(|ctx| ClContext::retain_new(ctx))
    }

    pub fn command_execution_status(&self) -> Output<CommandExecutionStatus> {
        unsafe {
            self.info(EventInfo::CommandExecutionStatus)
                .map(|ret| ret.into_one())
        }
    }
}

pub struct Profiling {
    pub queue_time: Option<u64>,
    pub submit_time: Option<u64>,
    pub start_time: Option<u64>,
    pub end_time: Option<u64>,
}

impl Profiling {
    pub fn total_duration(&self) -> Option<Duration> {
        Some(Duration::from_nanos(self.end_time? - self.queue_time?))
    }

    pub fn duration_waiting_in_queue(&self) -> Option<Duration> {
        Some(Duration::from_nanos(self.submit_time? - self.queue_time?))
    }

    pub fn duration_between_submit_and_start(&self) -> Option<Duration> {
        Some(Duration::from_nanos(self.submit_time? - self.queue_time?))
    }
    pub fn duration_of_execution(&self) -> Option<Duration> {
        Some(Duration::from_nanos(self.submit_time? - self.queue_time?))
    }
}

pub struct BufferReadEvent<T: ClNumber> {
    event: ManuallyDrop<ClEvent>,
    host_buffer: ManuallyDrop<Option<Vec<T>>>,
    is_consumed: bool,
}

impl<T: ClNumber> BufferReadEvent<T> {
    pub fn new(event: ClEvent, host_buffer: Option<Vec<T>>) -> BufferReadEvent<T> {
        BufferReadEvent {
            event: ManuallyDrop::new(event),
            host_buffer: ManuallyDrop::new(host_buffer),
            is_consumed: false,
        }
    }

    pub fn wait(&mut self) -> Output<Option<Vec<T>>> {
        if self.is_consumed {
            return Err(Error::EventError(EventError::EventAlreadyConsumed(
                self.event.address(),
            )));
        }
        unsafe {
            self.event.wait()?;
            match *self.host_buffer {
                Some(_) => {
                    let mut output = Some(vec![]);
                    std::mem::swap(&mut *self.host_buffer, &mut output);
                    self.is_consumed = true;
                    Ok(output)
                }
                None => Ok(None),
            }
        }
    }
}

impl<T: ClNumber> Drop for BufferReadEvent<T> {
    fn drop(&mut self) {
        unsafe {
            self.event.wait().unwrap();
            ManuallyDrop::drop(&mut self.event);
            ManuallyDrop::drop(&mut self.host_buffer);
        }
    }
}

/// A CompleteEvent is the result of making a synchronous ffi call.
///
/// After the `cl_event`'s event is over the event is no longer able
///
/// A CompleteEvent is not for putting into WaitList.
///
/// Don't do it. You'll segfault.
pub struct CompleteEvent {
    event: ClEvent,
    _unconstructable: (),
}

impl CompleteEvent {
    pub fn new(event: ClEvent) -> CompleteEvent {
        CompleteEvent {
            event,
            _unconstructable: (),
        }
    }

    pub unsafe fn inner(&self) -> &ClEvent {
        &self.event
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        BufferCreator, ClCommandQueue, ClContext, ClEvent, ClKernel, ClMem, CommandExecutionStatus,
        Session, SessionBuilder, Work,
    };
    use std::time::Duration;

    const SRC: &'static str = "
    __kernel void add_one(__global uint *data) {
        data[get_global_id(0)] += 1;
    }
    ";

    fn get_event() -> (Session, ClEvent) {
        unsafe {
            let mut session: Session = SessionBuilder::new().with_program_src(SRC).build().unwrap();
            let mut kernel =
                ClKernel::create(session.program(), "add_one").expect("Failed to Kernel::create/2");
            let input_data: Vec<u64> = vec![1, 2, 3];
            let data = &input_data[..];
            let mem_cfg = data.mem_config();
            let mut mem_buffer: ClMem =
                ClMem::create_with_config(session.context(), data, mem_cfg)
                    .unwrap_or_else(|e| panic!("Failed to ClMem::create_with_config() {:?}", e));
            let () = kernel
                .set_arg(0, &mut mem_buffer)
                .expect("Failed to set_arg(0, &mem_buffer)");
            let work = Work::new(input_data.len());
            let event = session
                .enqueue_kernel(0, &mut kernel, &work, None)
                .unwrap_or_else(|e| panic!("Failed to sync_enqueue_kernel {:?}", e));
            (session, event)
        }
    }

    #[test]
    fn event_method_queue_time_works() {
        let (_sess, event) = get_event();
        let output = event
            .queue_time()
            .expect("Failed to call event.queue_time()");
        assert!(output > 0);
    }

    #[test]
    fn event_method_submit_time_works() {
        let (_sess, event) = get_event();
        let output = event
            .submit_time()
            .expect("Failed to call event.submit_time()");
        assert!(output > 0);
    }

    #[test]
    fn event_method_start_time_works() {
        let (_sess, event) = get_event();
        let output = event
            .start_time()
            .expect("Failed to call event.start_time()");
        assert!(output > 0);
    }

    #[test]
    fn event_method_end_time_works() {
        let (_sess, event) = get_event();
        let output = event.end_time().expect("Failed to call event.end_time()");
        assert!(output > 0);
    }

    #[test]
    fn event_method_reference_count_works() {
        let (_sess, event) = get_event();
        let output = event
            .reference_count()
            .expect("Failed to call event.reference_count()");
        assert_eq!(output, 1);
    }

    #[test]
    fn event_method_command_queue_works() {
        let (_sess, event) = get_event();
        let _output: ClCommandQueue =
            unsafe { event.command_queue() }.expect("Failed to call event.command_queue()");
    }

    #[test]
    fn event_method_context_works() {
        let (_sess, event) = get_event();
        let _output: ClContext =
            unsafe { event.context() }.expect("Failed to call event.context()");
    }

    #[test]
    fn event_method_command_execution_status_works() {
        let (_sess, event) = get_event();
        let _output: CommandExecutionStatus = event
            .command_execution_status()
            .expect("Failed to call event.command_exection_status()");
    }

    #[test]
    fn event_profiling_works() {
        let (_sess, event) = get_event();
        let exec_status: CommandExecutionStatus = event
            .command_execution_status()
            .expect("Failed to call event.command_exection_status()");
        assert_eq!(exec_status, CommandExecutionStatus::Complete);
        let prof = event.profiling();
        let submitted_at = prof.submit_time.unwrap();
        let queued_at = prof.queue_time.unwrap();
        let started_at = prof.start_time.unwrap();
        let ended_at = prof.end_time.unwrap();
        assert!(queued_at < submitted_at);
        assert!(queued_at < started_at);
        assert!(started_at < ended_at);

        let total = prof.total_duration().unwrap();
        let max_duration = Duration::from_millis(10);
        assert!(
            total < max_duration,
            "total {:?} was greater than max duration {:?}",
            total,
            max_duration
        );

        let duration_waiting_in_queue = prof.duration_waiting_in_queue().unwrap();
        let max_duration_waiting_in_queue = Duration::from_millis(5);
        assert!(
            duration_waiting_in_queue < max_duration_waiting_in_queue,
            "duration_waiting_in_queue {:?} was greater than max duration {:?}",
            duration_waiting_in_queue,
            max_duration_waiting_in_queue
        );

        let duration_waiting_for_init = prof.duration_between_submit_and_start().unwrap();
        let max_duration_waiting_for_init = Duration::from_millis(5);
        assert!(
            duration_waiting_for_init < max_duration_waiting_for_init,
            "duration_waiting_for_init {:?} was greater than max duration {:?}",
            duration_waiting_for_init,
            max_duration_waiting_in_queue
        );

        let duration_of_execution = prof.duration_of_execution().unwrap();
        let max_duration_of_execution = Duration::from_millis(5);
        assert!(
            duration_of_execution < max_duration_of_execution,
            "time_waiting_for_init {:?} was greater than max duration {:?}",
            duration_of_execution,
            max_duration_of_execution
        );
    }
}