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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use super::*;
use crate::context::RawContext;
use crate::non_null_const;
use blaze_proc::docfg;
use opencl_sys::*;
use std::ptr::addr_of_mut;
use std::{ffi::c_void, mem::MaybeUninit, ptr::NonNull};

#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct RawCommandQueue(NonNull<c_void>);

impl RawCommandQueue {
    #[cfg(not(feature = "cl2"))]
    pub fn new(
        ctx: &RawContext,
        props: CommandQueueProperties,
        device: &RawDevice,
    ) -> Result<Self> {
        let props = props.to_bits();
        let mut err = 0;

        let id = unsafe {
            opencl_sys::clCreateCommandQueue(ctx.id(), device.id(), props, addr_of_mut!(err))
        };

        if err != 0 {
            return Err(Error::from(err));
        }

        Ok(NonNull::new(id).map(Self).unwrap())
    }

    #[cfg(feature = "cl2")]
    pub fn new(
        ctx: &RawContext,
        props: impl Into<QueueProperties>,
        device: &RawDevice,
    ) -> Result<Self> {
        let props: QueueProperties = props.into();
        let mut err = 0;
        let id;

        cfg_if::cfg_if! {
            if #[cfg(feature = "strict")] {
                let props = match props.to_bits() {
                    Left(x) => x.as_ptr(),
                    Right(x) => x.as_ptr()
                };

                id = unsafe { opencl_sys::clCreateCommandQueueWithProperties(ctx.id(), device.id(), props, addr_of_mut!(err)) };
            } else {
                #[allow(deprecated)]
                if ctx.greatest_common_version()? < device::Version::CL2 {
                    id = unsafe { opencl_sys::clCreateCommandQueue(ctx.id(), device.id(), props.props.to_bits(), addr_of_mut!(err)) }
                } else {
                    let props = match props.to_bits() {
                        Left(x) => x.as_ptr(),
                        Right(x) => x.as_ptr()
                    };

                    id = unsafe { opencl_sys::clCreateCommandQueueWithProperties(ctx.id(), device.id(), props, addr_of_mut!(err)) };
                }
            }
        }

        if err != 0 {
            return Err(Error::from(err));
        }

        Ok(NonNull::new(id).map(Self).unwrap())
    }

    #[inline(always)]
    pub const unsafe fn from_id(id: cl_command_queue) -> Option<Self> {
        match non_null_const(id) {
            Some(x) => Some(Self(x)),
            None => None,
        }
    }

    #[inline(always)]
    pub const unsafe fn from_id_unchecked(id: cl_command_queue) -> Self {
        Self(NonNull::new_unchecked(id))
    }

    #[inline(always)]
    pub const fn id(&self) -> cl_command_queue {
        self.0.as_ptr()
    }

    #[inline(always)]
    pub unsafe fn retain(&self) -> Result<()> {
        tri!(clRetainCommandQueue(self.id()));
        Ok(())
    }

    /// Return the context specified when the command-queue is created.
    #[inline(always)]
    pub fn context(&self) -> Result<RawContext> {
        let ctx = self.get_info::<cl_context>(CL_QUEUE_CONTEXT)?;
        unsafe {
            tri!(clRetainContext(ctx));
            // SAFETY: Context checked to be valid by `clRetainContext`.
            Ok(RawContext::from_id_unchecked(ctx))
        }
    }

    /// Return the device specified when the command-queue is created.
    #[inline(always)]
    pub fn device(&self) -> Result<RawDevice> {
        let dev = self.get_info::<cl_device_id>(CL_QUEUE_DEVICE)?;
        unsafe {
            cfg_if::cfg_if! {
                if #[cfg(feature = "cl1_2")] {
                    tri!(clRetainDevice(dev));
                    // SAFETY: Context checked to be valid by `clRetainContext`.
                    Ok(RawDevice::from_id_unchecked(dev))
                } else {
                    if let Some(dev) = RawDevice::from_id(dev) {
                        return Ok(dev);
                    }

                    Err(ErrorKind::InvalidDevice.into())
                }
            }
        }
    }

    /// Return the command-queue reference count.
    #[inline(always)]
    pub fn reference_count(&self) -> Result<u32> {
        self.get_info(CL_QUEUE_REFERENCE_COUNT)
    }

    /// Return the currently specified properties for the command-queue.
    #[inline(always)]
    pub fn properties(&self) -> Result<CommandQueueProperties> {
        let props = self.get_info(CL_QUEUE_PROPERTIES)?;
        Ok(CommandQueueProperties::from_bits(props))
    }

    /// Return the properties argument specified in creation.
    #[docfg(feature = "cl3")]
    #[inline(always)]
    pub fn queue_properties(&self) -> Result<QueueProperties> {
        let v =
            self.get_info_array::<cl_queue_properties>(opencl_sys::CL_QUEUE_PROPERTIES_ARRAY)?;
        Ok(QueueProperties::from_bits(&v))
    }

    /// Return the size of the device command-queue. To be considered valid for this query, command_queue must be a device command-queue.
    #[docfg(feature = "cl2")]
    #[inline(always)]
    pub fn size(&self) -> Result<u32> {
        self.get_info(opencl_sys::CL_QUEUE_SIZE)
    }

    /// Return the current default command queue for the underlying device.
    #[docfg(feature = "cl2_1")]
    #[inline(always)]
    pub fn device_default(&self) -> Result<RawCommandQueue> {
        // TODO FIX
        let queue = self.get_info::<cl_command_queue>(opencl_sys::CL_QUEUE_DEVICE_DEFAULT)?;

        unsafe {
            tri!(clRetainCommandQueue(queue));
            // SAFETY: Queue checked to be valid by `clRetainCommandQueue`.
            Ok(RawCommandQueue::from_id_unchecked(queue))
        }
    }

    /// Issues all previously queued OpenCL commands in a command-queue to the device associated with the command-queue.
    #[inline(always)]
    pub fn flush(&self) -> Result<()> {
        unsafe {
            tri!(clFlush(self.id()));
        }

        Ok(())
    }

    /// Blocks the current thread until all previously queued OpenCL commands in a command-queue are issued to the associated device and have completed.
    #[inline(always)]
    pub fn finish(&self) -> Result<()> {
        unsafe {
            tri!(clFinish(self.id()));
        }

        Ok(())
    }

    /// A synchronization point that enqueues a barrier operation.\
    /// If the wait list is empty, then this particular command waits until all previous enqueued commands to command_queue have completed.
    /// The barrier command either waits for a list of events to complete, or if the list is empty it waits for all commands previously enqueued in the queue to complete before it completes.
    /// This command blocks command execution, that is, any following commands enqueued after it do not execute until it completes.
    /// This command returns an event which can be waited on, i.e. this event can be waited on to insure that all events either in the wait list or all previously enqueued commands,
    /// queued before this command to the command queue, have completed.
    #[docfg(feature = "cl1_2")]
    #[inline(always)]
    pub fn barrier(&self, wait: crate::WaitList) -> Result<crate::prelude::RawEvent> {
        let (num_events_in_wait_list, event_wait_list) = crate::wait_list(wait)?;

        let mut evt = core::ptr::null_mut();
        unsafe {
            tri!(clEnqueueBarrierWithWaitList(
                self.id(),
                num_events_in_wait_list,
                event_wait_list,
                addr_of_mut!(evt)
            ));
            Ok(crate::prelude::RawEvent::from_id(evt).unwrap())
        }
    }

    /// Enqueues a marker command which waits for either a list of events to complete, or all previously enqueued commands to complete.
    #[docfg(feature = "cl1_2")]
    #[inline(always)]
    pub fn marker(&self, wait: crate::WaitList) -> Result<crate::prelude::RawEvent> {
        let (num_events_in_wait_list, event_wait_list) = crate::wait_list(wait)?;

        let mut evt = core::ptr::null_mut();
        unsafe {
            tri!(clEnqueueMarkerWithWaitList(
                self.id(),
                num_events_in_wait_list,
                event_wait_list,
                addr_of_mut!(evt)
            ));
            Ok(crate::prelude::RawEvent::from_id(evt).unwrap())
        }
    }

    #[inline]
    fn get_info<T: Copy>(&self, ty: cl_command_queue_info) -> Result<T> {
        let mut result = MaybeUninit::<T>::uninit();
        unsafe {
            tri!(clGetCommandQueueInfo(
                self.id(),
                ty,
                core::mem::size_of::<T>(),
                result.as_mut_ptr().cast(),
                core::ptr::null_mut()
            ));
            Ok(result.assume_init())
        }
    }

    #[allow(unused)]
    #[inline]
    fn get_info_array<T: Copy>(&self, ty: cl_command_queue_info) -> Result<Box<[T]>> {
        let mut size = 0;
        unsafe {
            tri!(clGetCommandQueueInfo(
                self.id(),
                ty,
                0,
                core::ptr::null_mut(),
                addr_of_mut!(size)
            ));
        }

        let mut result;
        cfg_if::cfg_if! {
            if #[cfg(feature = "nightly")] {
                result = Box::<[T]>::new_uninit_slice(size / core::mem::size_of::<T>());
            } else {
                let mut vec = Vec::<MaybeUninit<T>>::with_capacity(size / core::mem::size_of::<T>());
                unsafe { vec.set_len(vec.capacity()) };
                result = vec.into_boxed_slice();
            }
        }

        unsafe {
            tri!(clGetCommandQueueInfo(
                self.id(),
                ty,
                size,
                result.as_mut_ptr().cast(),
                core::ptr::null_mut()
            ));

            cfg_if::cfg_if! {
                if #[cfg(feature = "nightly")] {
                    Ok(result.assume_init())
                } else {
                    Ok(Box::from_raw(Box::into_raw(result) as *mut [T]))
                }
            }
        }
    }
}

impl Clone for RawCommandQueue {
    #[inline(always)]
    fn clone(&self) -> Self {
        unsafe { tri_panic!(clRetainCommandQueue(self.id())) }

        Self(self.0)
    }
}

impl Drop for RawCommandQueue {
    #[inline(always)]
    fn drop(&mut self) {
        unsafe { tri_panic!(clReleaseCommandQueue(self.id())) }
    }
}

unsafe impl Send for RawCommandQueue {}
unsafe impl Sync for RawCommandQueue {}

cfg_if::cfg_if! {
    if #[cfg(feature = "cl2")] {
        use core::num::NonZeroU32;
        use opencl_sys::{cl_queue_properties, CL_QUEUE_SIZE};
        use elor::prelude::*;

        #[cfg_attr(docsrs, doc(cfg(feature = "cl2")))]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
        #[non_exhaustive]
        pub struct QueueProperties {
            pub props: CommandQueueProperties,
            /// Specifies the size of the device queue in bytes.\
            /// This can only be specified if [on device](OutOfOrderExec::OnDevice) is set in `props`. This must be a value less or equal to the [max size](crate::prelude::RawDevice::queue_on_device_max_size).\
            /// For best performance, this should be less or equal to the [preferred size](crate::prelude::RawDevice::queue_on_device_preferred_size).\
            /// If `size` is not specified, the device queue is created with the [preferred size](crate::prelude::RawDevice::queue_on_device_preferred_size) as the size of the queue.
            pub size: Option<NonZeroU32>
        }

        impl QueueProperties {
            const PROPERTIES : cl_queue_properties = CL_QUEUE_PROPERTIES as cl_queue_properties;
            const SIZE : cl_queue_properties = CL_QUEUE_SIZE as cl_queue_properties;

            #[inline(always)]
            pub fn new (props: CommandQueueProperties, size: impl Into<Option<NonZeroU32>>) -> Self {
                Self {
                    props,
                    size: size.into()
                }
            }

            #[inline(always)]
            pub const fn const_new (props: CommandQueueProperties, size: Option<NonZeroU32>) -> Self {
                Self { props, size }
            }

            #[inline(always)]
            pub fn to_bits (self) -> Either<[cl_queue_properties; 5], [cl_queue_properties; 3]> {
                let props = self.props.to_bits();

                if let Some(size) = self.size {
                    return Left (
                        [
                            Self::PROPERTIES, props,
                            Self::SIZE, size.get() as cl_queue_properties,
                            0
                        ]
                    )
                }

                Right([CL_QUEUE_PROPERTIES as cl_queue_properties, props, 0])
            }

            #[inline]
            pub fn from_bits (bits: &[cl_queue_properties]) -> Self {
                if bits.len() == 0 {
                    return Self::default()
                }

                let mut props = CommandQueueProperties::default();
                let mut size = None;

                match bits[0] {
                    Self::PROPERTIES => props = CommandQueueProperties::from_bits(bits[1]),
                    Self::SIZE => size = NonZeroU32::new(u32::try_from(bits[1]).unwrap()),
                    0 => return Self::new(props, size),
                    _ => panic!()
                }

                match bits[2] {
                    Self::PROPERTIES => props = CommandQueueProperties::from_bits(bits[3]),
                    Self::SIZE => size = NonZeroU32::new(u32::try_from(bits[3]).unwrap()),
                    0 => return Self::new(props, size),
                    _ => panic!()
                }

                Self::new(props, size)
            }
        }

        impl From<CommandQueueProperties> for QueueProperties {
            #[inline(always)]
            fn from (props: CommandQueueProperties) -> Self {
                Self::new(props, None)
            }
        }
    } else {
        pub type QueueProperties = CommandQueueProperties;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CommandQueueProperties {
    /// Determines whether the commands queued in the command-queue are executed in-order or out-of-order.
    pub out_of_order_exec: OutOfOrderExec,
    /// Enable or disable profiling of commands in the command-queue. If set, the profiling of commands is enabled. Otherwise profiling of commands is disabled.
    pub profiling: bool,
}

impl CommandQueueProperties {
    #[inline(always)]
    pub fn new(out_of_order_exec: impl Into<OutOfOrderExec>, profiling: bool) -> Self {
        Self {
            out_of_order_exec: out_of_order_exec.into(),
            profiling,
        }
    }

    #[inline(always)]
    pub const fn const_new(out_of_order_exec: OutOfOrderExec, profiling: bool) -> Self {
        Self {
            out_of_order_exec,
            profiling,
        }
    }

    #[inline(always)]
    pub const fn from_bits(v: cl_command_queue_properties) -> Self {
        let out_of_order_exec = OutOfOrderExec::from_bits(v);
        let profiling = v & CL_QUEUE_PROFILING_ENABLE != 0;
        Self {
            out_of_order_exec,
            profiling,
        }
    }

    #[inline(always)]
    pub const fn to_bits(self) -> cl_command_queue_properties {
        let mut bits = self.out_of_order_exec.to_bits();
        if self.profiling {
            bits |= CL_QUEUE_PROFILING_ENABLE
        }

        bits
    }
}

impl Default for CommandQueueProperties {
    fn default() -> Self {
        Self {
            out_of_order_exec: Default::default(),
            #[cfg(any(test, debug_assertions))]
            profiling: true,
            #[cfg(not(any(test, debug_assertions)))]
            profiling: false,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OutOfOrderExec {
    /// The commands in the command-queue are executed out-of-order
    Enabled,
    /// Commands are executed in-order
    Disabled,
    /// Indicates that this is a device queue, and the commands in the queue are executed out-of-order.\
    /// The boolean value indicates if this is the default device queue or not.
    #[cfg_attr(docsrs, doc(cfg(feature = "cl2")))]
    #[cfg(feature = "cl2")]
    OnDevice(bool),
}

impl OutOfOrderExec {
    #[inline]
    pub const fn from_bits(v: cl_command_queue_properties) -> Self {
        if v & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE == 0 {
            return Self::Disabled;
        }

        #[cfg(feature = "cl2")]
        if v & opencl_sys::CL_QUEUE_ON_DEVICE == 0 {
            return Self::Enabled;
        }

        #[cfg(feature = "cl2")]
        if v & opencl_sys::CL_QUEUE_ON_DEVICE_DEFAULT == 0 {
            return Self::OnDevice(false);
        }

        #[cfg(feature = "cl2")]
        return Self::OnDevice(true);
        #[cfg(not(feature = "cl2"))]
        return Self::Enabled;
    }

    #[inline]
    pub const fn to_bits(self) -> cl_command_queue_properties {
        #[cfg(feature = "cl2")]
        const ON_DEVICE: cl_command_queue_properties =
            opencl_sys::CL_QUEUE_ON_DEVICE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
        #[cfg(feature = "cl2")]
        const ON_DEVICE_DEFAULT: cl_command_queue_properties =
            opencl_sys::CL_QUEUE_ON_DEVICE_DEFAULT | ON_DEVICE;

        match self {
            Self::Enabled => CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,
            #[cfg(feature = "cl2")]
            Self::OnDevice(false) => ON_DEVICE,
            #[cfg(feature = "cl2")]
            Self::OnDevice(true) => ON_DEVICE_DEFAULT,
            Self::Disabled => 0,
        }
    }
}

impl From<bool> for OutOfOrderExec {
    #[inline(always)]
    fn from(v: bool) -> Self {
        if v {
            return Self::Enabled;
        }

        Self::Disabled
    }
}

impl Default for OutOfOrderExec {
    #[inline(always)]
    fn default() -> Self {
        Self::Disabled
    }
}