pub use cl3::event::*;
use super::Result;
use cl3::types::{cl_command_queue, cl_context, cl_event, cl_uint, cl_ulong};
#[derive(Debug)]
pub struct Event {
event: cl_event,
}
impl Drop for Event {
fn drop(&mut self) {
release_event(self.event).expect("Error: clReleaseEvent");
}
}
unsafe impl Send for Event {}
impl Event {
pub fn new(event: cl_event) -> Self {
Self { event }
}
pub fn get(&self) -> cl_event {
self.event
}
pub fn wait(&self) -> Result<()> {
let events = [self.get()];
Ok(wait_for_events(&events)?)
}
pub fn command_execution_status(&self) -> Result<CommandExecutionStatus> {
Ok(CommandExecutionStatus(
get_event_info(self.event, EventInfo::CL_EVENT_COMMAND_EXECUTION_STATUS)?.to_int(),
))
}
pub fn command_type(&self) -> Result<EventCommandType> {
Ok(EventCommandType(
get_event_info(self.event, EventInfo::CL_EVENT_COMMAND_TYPE)?.to_uint(),
))
}
pub fn reference_count(&self) -> Result<cl_uint> {
Ok(get_event_info(self.event, EventInfo::CL_EVENT_REFERENCE_COUNT)?.to_uint())
}
pub fn command_queue(&self) -> Result<cl_command_queue> {
Ok(
get_event_info(self.event, EventInfo::CL_EVENT_COMMAND_QUEUE)?.to_ptr()
as cl_command_queue,
)
}
pub fn context(&self) -> Result<cl_context> {
Ok(get_event_info(self.event, EventInfo::CL_EVENT_CONTEXT)?.to_ptr() as cl_context)
}
pub fn profiling_command_queued(&self) -> Result<cl_ulong> {
Ok(
get_event_profiling_info(self.event, ProfilingInfo::CL_PROFILING_COMMAND_QUEUED)?
.to_ulong(),
)
}
pub fn profiling_command_submit(&self) -> Result<cl_ulong> {
Ok(
get_event_profiling_info(self.event, ProfilingInfo::CL_PROFILING_COMMAND_SUBMIT)?
.to_ulong(),
)
}
pub fn profiling_command_start(&self) -> Result<cl_ulong> {
Ok(
get_event_profiling_info(self.event, ProfilingInfo::CL_PROFILING_COMMAND_START)?
.to_ulong(),
)
}
pub fn profiling_command_end(&self) -> Result<cl_ulong> {
Ok(
get_event_profiling_info(self.event, ProfilingInfo::CL_PROFILING_COMMAND_END)?
.to_ulong(),
)
}
#[cfg(feature = "CL_VERSION_2_0")]
pub fn profiling_command_complete(&self) -> Result<cl_ulong> {
Ok(
get_event_profiling_info(self.event, ProfilingInfo::CL_PROFILING_COMMAND_COMPLETE)?
.to_ulong(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command_queue::{CommandQueue, CL_QUEUE_PROFILING_ENABLE};
use crate::context::Context;
use crate::device::{Device, CL_DEVICE_TYPE_GPU};
use crate::memory::{Buffer, CL_MEM_WRITE_ONLY};
use crate::platform::get_platforms;
use crate::types::{cl_float, CL_FALSE};
use std::ptr;
#[test]
fn test_event() {
let platforms = get_platforms().unwrap();
assert!(0 < platforms.len());
let platform = &platforms[0];
let devices = platform.get_devices(CL_DEVICE_TYPE_GPU).unwrap();
assert!(0 < devices.len());
let device = Device::new(devices[0]);
let context = Context::from_device(&device).unwrap();
let queue = CommandQueue::create(
&context,
context.default_device(),
CL_QUEUE_PROFILING_ENABLE,
)
.expect("CommandQueue::create failed");
const ARRAY_SIZE: usize = 1024;
let ones: [cl_float; ARRAY_SIZE] = [1.0; ARRAY_SIZE];
let buffer =
Buffer::<cl_float>::create(&context, CL_MEM_WRITE_ONLY, ARRAY_SIZE, ptr::null_mut())
.unwrap();
let events: Vec<cl_event> = Vec::default();
let event = queue
.enqueue_write_buffer(&buffer, CL_FALSE, 0, &ones, &events)
.unwrap();
let value = event.command_execution_status().unwrap();
println!("event.command_execution_status(): {}", value);
let value = event.command_type().unwrap();
println!("event.command_type(): {}", value);
assert_eq!(CL_COMMAND_WRITE_BUFFER, value.0);
let value = event.reference_count().unwrap();
println!("event.reference_count(): {}", value);
let value = event.command_queue().unwrap();
assert!(queue.get() == value);
let value = event.context().unwrap();
assert!(context.get() == value);
event.wait().unwrap();
let value = event.command_execution_status().unwrap();
println!("event.command_execution_status(): {}", value);
assert_eq!(CL_COMPLETE, value.0);
let value = event.profiling_command_queued().unwrap();
println!("event.profiling_command_queued(): {}", value);
assert!(0 < value);
let value = event.profiling_command_submit().unwrap();
println!("event.profiling_command_submit(): {}", value);
assert!(0 < value);
let value = event.profiling_command_start().unwrap();
println!("event.profiling_command_start(): {}", value);
assert!(0 < value);
let value = event.profiling_command_end().unwrap();
println!("event.profiling_command_end(): {}", value);
assert!(0 < value);
match event.profiling_command_complete() {
Ok(value) => println!("event.profiling_command_complete(): {}", value),
Err(e) => println!("OpenCL error, event.profiling_command_complete(): {}", e),
}
}
}