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
//! `ocl` standard types.
//!
//! * TODO: This module needs a rename.

mod platform;
mod device;
mod context;
mod program;
mod kernel;
mod queue;
mod buffer;
mod image;
mod sampler;
mod pro_que;
mod event;
mod spatial_dims;

pub use self::platform::{PlatformError, Extensions, Platform};
pub use self::device::{DeviceError, Device, DeviceSpecifier};
pub use self::context::{Context, ContextBuilder};
pub use self::program::{Program, ProgramBuilder, BuildOpt};
pub use self::queue::Queue;
pub use self::kernel::{KernelError, KernelCmd, Kernel, KernelBuilder};
pub use self::buffer::{BufferCmdKind, BufferCmdDataShape, BufferCmd, Buffer, QueCtx,
    BufferBuilder, BufferReadCmd, BufferWriteCmd, BufferMapCmd, BufferCmdError};
pub use self::image::{ImageCmdKind, ImageCmd, Image, ImageBuilder};
pub use self::sampler::Sampler;
pub use self::pro_que::{ProQue, ProQueBuilder};
pub use self::event::{Event, EventArray, EventList, IntoMarker, RawEventArray, IntoRawEventArray};
pub use self::spatial_dims::SpatialDims;
#[cfg(not(feature = "async_block"))]
pub use self::cb::{_unpark_task, box_raw_void};
pub use self::traits::{MemLen, WorkDims};
pub use self::types::{ClNullEventPtrEnum, ClWaitListPtrEnum};


// use core::OclPrm;

#[derive(Debug)]
enum HostSlice<'a, T> where T: 'a {
    None,
    Use(&'a [T]),
    Copy(&'a [T]),
}

impl<'a, T> HostSlice<'a, T> where T: 'a {
    fn is_none(&self) -> bool {
        match *self {
            HostSlice::None => true,
            _ => false,
        }
    }
}

//=============================================================================
//================================ CONSTANTS ==================================
//=============================================================================

// pub const INFO_FORMAT_MULTILINE: bool = false;

//=============================================================================
//================================ FUNCTIONS ==================================
//=============================================================================

#[cfg(not(feature = "async_block"))]
mod cb {
    use core::ffi::c_void;
    use num_traits::FromPrimitive;
    use futures::task::Task;
    use ffi::cl_event;
    use core::{CommandExecutionStatus, Status};

    pub fn box_raw_void<T>(item: T) -> *mut c_void {
        let item_box = Box::new(item);
        Box::into_raw(item_box) as *mut _ as *mut c_void
    }

    pub extern "C" fn _unpark_task(event_ptr: cl_event, event_status: i32, user_data: *mut c_void) {

        let _ = event_ptr;
        // println!("'_unpark_task' has been called.");
        if event_status == CommandExecutionStatus::Complete as i32 && !user_data.is_null() {
            unsafe {
                let task_ptr = user_data as *mut _ as *mut Task;
                let task = Box::from_raw(task_ptr);
                (*task).notify();
            }
        } else {
            let status = if event_status < 0 {
                format!("{:?}", Status::from_i32(event_status))
            } else {
                format!("{:?}", CommandExecutionStatus::from_i32(event_status))
            };

            panic!("ocl::standard::_unpark_task: \n\nWake up user data is null or event is not \
                complete: {{ status: {:?}, user_data: {:?} }}. If you are getting \
                `DEVICE_NOT_AVAILABLE` and you are using Intel drivers, switch to AMD OpenCL \
                drivers instead (will work with Intel CPUs).\n\n", status, user_data);
        }
    }
}




//=============================================================================
//================================== TYPES ====================================
//=============================================================================

mod types {
    use std::ptr;
    use std::cell::Ref;
    use standard::{Event, EventList, RawEventArray, Queue};
    use core::ffi::cl_event;
    use core::{Event as EventCore, ClNullEventPtr, ClWaitListPtr};
    use error::Result as OclResult;

    /// An enum which can represent several different ways of representing a
    /// event wait list.
    ///
    //
    // * TODO:
    //   - Figure out a way to abstract over `&` and `&mut` versions of
    //   things in `From` impls without messing up the `Ref` versions.
    //   - Possibly rename this to something friendlier.
    #[derive(Debug)]
    pub enum ClWaitListPtrEnum<'a> {
        Null,
        RawEventArray(&'a RawEventArray),
        EventCoreOwned(EventCore),
        EventOwned(Event),
        EventCore(&'a EventCore),
        Event(&'a Event),
        EventList(&'a EventList),
        EventSlice(&'a [Event]),
        EventPtrSlice(&'a [cl_event]),
        RefEventList(Ref<'a, EventList>),
        RefTraitObj(Ref<'a, dyn ClWaitListPtr>),
        BoxTraitObj(Box<dyn ClWaitListPtr>),
    }

    impl<'a> ClWaitListPtrEnum<'a> {
        /// Converts this `ClWaitListPtrEnum` into a single marker event.
        pub fn into_marker(self, queue: &Queue) -> OclResult<Event> {
            queue.enqueue_marker(Some(self))
        }

        /// Returns an `EventList` containing owned copies of each element in
        /// this `ClWaitListPtrEnum`.
        pub fn to_list(&self) -> EventList {
            match *self {
                ClWaitListPtrEnum::Null => EventList::with_capacity(0),
                ClWaitListPtrEnum::RawEventArray(ref e) => e.as_slice().into(),
                ClWaitListPtrEnum::EventCoreOwned(ref e) => EventList::from(vec![e.clone().into()]),
                ClWaitListPtrEnum::EventOwned(ref e) => EventList::from(vec![e.clone().into()]),
                ClWaitListPtrEnum::EventCore(e) => EventList::from(vec![e.clone().into()]),
                ClWaitListPtrEnum::Event(e) => EventList::from(vec![e.clone().into()]),
                ClWaitListPtrEnum::EventList(e) => e.clone(),
                ClWaitListPtrEnum::EventSlice(e) => EventList::from(e),
                ClWaitListPtrEnum::EventPtrSlice(e) => EventList::from(e),
                ClWaitListPtrEnum::RefEventList(ref e) => (*e).clone(),
                ClWaitListPtrEnum::RefTraitObj(ref e) => Ref::clone(e).into(),
                ClWaitListPtrEnum::BoxTraitObj(ref e) => e.into(),
            }
        }
    }

    unsafe impl<'a> ClWaitListPtr for ClWaitListPtrEnum<'a> {
        unsafe fn as_ptr_ptr(&self) -> *const cl_event {
            match *self {
                ClWaitListPtrEnum::Null => ptr::null() as *const _ as *const cl_event,
                ClWaitListPtrEnum::RawEventArray(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::EventCoreOwned(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::EventCore(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::EventOwned(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::Event(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::EventList(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::EventSlice(ref e) => e.as_ptr() as *const _ as *const cl_event,
                ClWaitListPtrEnum::EventPtrSlice(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::RefEventList(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::RefTraitObj(ref e) => e.as_ptr_ptr(),
                ClWaitListPtrEnum::BoxTraitObj(ref e) => e.as_ptr_ptr(),

            }
        }

        fn count(&self) -> u32 {
            match *self {
                ClWaitListPtrEnum::Null => 0,
                ClWaitListPtrEnum::RawEventArray(ref e) => e.count(),
                ClWaitListPtrEnum::EventCoreOwned(ref e) => e.count(),
                ClWaitListPtrEnum::EventCore(ref e) => e.count(),
                ClWaitListPtrEnum::EventOwned(ref e) => e.count(),
                ClWaitListPtrEnum::Event(ref e) => e.count(),
                ClWaitListPtrEnum::EventList(ref e) => e.count(),
                ClWaitListPtrEnum::EventSlice(ref e) => e.len() as u32,
                ClWaitListPtrEnum::EventPtrSlice(ref e) => e.count(),
                ClWaitListPtrEnum::RefEventList(ref e) => e.count(),
                ClWaitListPtrEnum::RefTraitObj(ref e) => e.count(),
                ClWaitListPtrEnum::BoxTraitObj(ref e) => e.count(),
            }
        }
    }

    impl<'a> From<&'a RawEventArray> for ClWaitListPtrEnum<'a> {
        fn from(e: &'a RawEventArray) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::RawEventArray(e)
        }
    }

    impl<'a> From<EventCore> for ClWaitListPtrEnum<'a> {
        fn from(e: EventCore) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventCoreOwned(e)
        }
    }

    impl<'a> From<&'a EventCore> for ClWaitListPtrEnum<'a> {
        fn from(e: &'a EventCore) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventCore(e)
        }
    }

    impl<'a> From<&'a mut EventCore> for ClWaitListPtrEnum<'a> {
        fn from(e: &'a mut EventCore) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventCore(e)
        }
    }

    impl<'a> From<Event> for ClWaitListPtrEnum<'a> {
        fn from(e: Event) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventOwned(e)
        }
    }

    impl<'a> From<&'a Event> for ClWaitListPtrEnum<'a> {
        fn from(e: &'a Event) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::Event(e)
        }
    }

    impl<'a> From<&'a mut Event> for ClWaitListPtrEnum<'a> {
        fn from(e: &'a mut Event) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::Event(e)
        }
    }

    impl<'a> From<&'a EventList> for ClWaitListPtrEnum<'a> {
        fn from(el: &'a EventList) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventList(el)
        }
    }

    impl<'a> From<&'a mut EventList> for ClWaitListPtrEnum<'a> {
        fn from(el: &'a mut EventList) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventList(el)
        }
    }

    impl<'a> From<&'a [Event]> for ClWaitListPtrEnum<'a> {
        fn from(es: &'a [Event]) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventSlice(es)
        }
    }

    impl<'a> From<&'a mut [Event]> for ClWaitListPtrEnum<'a> {
        fn from(es: &'a mut [Event]) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventSlice(es)
        }
    }

    impl<'a> From<&'a [cl_event]> for ClWaitListPtrEnum<'a> {
        fn from(el: &'a [cl_event]) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventPtrSlice(el)
        }
    }

    impl<'a> From<&'a mut [cl_event]> for ClWaitListPtrEnum<'a> {
        fn from(el: &'a mut [cl_event]) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::EventPtrSlice(el)
        }
    }

    impl<'a> From<()> for ClWaitListPtrEnum<'a> {
        fn from(_: ()) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::Null
        }
    }

    impl<'a> From<Ref<'a, EventList>> for ClWaitListPtrEnum<'a> {
        fn from(e: Ref<'a, EventList>) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::RefTraitObj(e)
        }
    }

    impl<'a> From<Ref<'a, dyn ClWaitListPtr>> for ClWaitListPtrEnum<'a> {
        fn from(e: Ref<'a, dyn ClWaitListPtr>) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::RefTraitObj(e)
        }
    }

    impl<'a> From<Box<dyn ClWaitListPtr>> for ClWaitListPtrEnum<'a> {
        fn from(e: Box<dyn ClWaitListPtr>) -> ClWaitListPtrEnum<'a> {
            ClWaitListPtrEnum::BoxTraitObj(e)
        }
    }

    impl<'a, Ewl> From<Option<Ewl>> for ClWaitListPtrEnum<'a>
            where Ewl: Into<ClWaitListPtrEnum<'a>> {
        fn from(e: Option<Ewl>) -> ClWaitListPtrEnum<'a> {
            match e {
                Some(e) => e.into(),
                None => ClWaitListPtrEnum::Null,
            }
        }
    }


    #[derive(Debug)]
    pub enum ClNullEventPtrEnum<'a> {
        Null,
        Event(&'a mut Event),
        EventList(&'a mut EventList),
    }

    unsafe impl<'a> ClNullEventPtr for ClNullEventPtrEnum<'a> {
        fn alloc_new(&mut self) -> *mut cl_event {
            match *self {
                ClNullEventPtrEnum::Null => panic!("Void events cannot be used."),
                ClNullEventPtrEnum::Event(ref mut e) => e.alloc_new(),
                ClNullEventPtrEnum::EventList(ref mut e) => e.alloc_new(),
            }
        }

        #[inline] unsafe fn clone_from<E: AsRef<EventCore>>(&mut self, ev: E) {
            match *self {
                ClNullEventPtrEnum::Null => panic!("Void events cannot be used."),
                ClNullEventPtrEnum::Event(ref mut e) => e.clone_from(ev),
                ClNullEventPtrEnum::EventList(ref mut e) => e.clone_from(ev),
            }
        }
    }

    impl<'a> From<&'a mut Event> for ClNullEventPtrEnum<'a> {
        fn from(e: &'a mut Event) -> ClNullEventPtrEnum<'a> {
            ClNullEventPtrEnum::Event(e)
        }
    }

    impl<'a> From<&'a mut EventList> for ClNullEventPtrEnum<'a> {
        fn from(el: &'a mut EventList) -> ClNullEventPtrEnum<'a> {
            ClNullEventPtrEnum::EventList(el)
        }
    }

    impl<'a> From<()> for ClNullEventPtrEnum<'a> {
        fn from(_: ()) -> ClNullEventPtrEnum<'a> {
            ClNullEventPtrEnum::Null
        }
    }

    impl<'a, E> From<Option<E>> for ClNullEventPtrEnum<'a>
            where E: Into<ClNullEventPtrEnum<'a>> {
        fn from(e: Option<E>) -> ClNullEventPtrEnum<'a> {
            match e {
                Some(e) => e.into(),
                None => ClNullEventPtrEnum::Null,
            }
        }
    }
}

//=============================================================================
//================================== TRAITS ===================================
//=============================================================================

mod traits {
    use std::fmt::Debug;
    use num_traits::{Num, ToPrimitive};
    use ::SpatialDims;
    use super::spatial_dims::to_usize;

    /// Types which have properties describing the amount of work to be done
    /// in multiple dimensions.
    ///
    pub trait WorkDims {
        /// Returns the number of dimensions defined.
        fn dim_count(&self) -> u32;

        /// Returns an array representing the amount of work to be done by a kernel.
        ///
        /// Unspecified dimensions (for example, the 3rd dimension in a
        /// 1-dimensional work size) are set equal to `1`.
        fn to_work_size(&self) -> Option<[usize; 3]>;

        /// Returns an array representing the offset of a work item or memory
        /// location.
        ///
        /// Unspecified dimensions (for example, the 3rd dimension in a
        /// 1-dimensional work size) are set equal to `0`.
        fn to_work_offset(&self) -> Option<[usize; 3]>;
    }


    /// Types which have properties allowing them to be used to define the size
    /// of a volume of memory.
    ///
    /// Units are expressed in `bytes / size_of(T)` just like `Vec::len()`.
    ///
    pub trait MemLen {
        /// Returns the exact number of elements of a volume of memory
        /// (equivalent to `Vec::len()`).
        fn to_len(&self) -> usize;

        /// Returns the length of a volume of memory padded to the next
        /// multiple of `incr`.
        fn to_len_padded(&self, incr: usize) -> usize;

        /// Returns the exact lengths of each dimension of a volume of memory.
        fn to_lens(&self) -> [usize; 3];
    }

    impl<'a, D> MemLen for &'a D where D: MemLen {
        fn to_len(&self) -> usize { (*self).to_len() }
        fn to_len_padded(&self, incr: usize) -> usize { (*self).to_len_padded(incr) }
        fn to_lens(&self) -> [usize; 3] { (*self).to_lens() }
    }

    impl<D> MemLen for (D, ) where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::One(to_usize(self.0)).to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::One(to_usize(self.0)).to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self.0), 1, 1] }
    }

    impl<D> MemLen for [D; 1] where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::One(to_usize(self[0])).to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::One(to_usize(self[0])).to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self[0]), 1, 1] }
    }

    impl<D> MemLen for (D, D) where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::Two(to_usize(self.0), to_usize(self.1)).to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::Two(to_usize(self.0), to_usize(self.1)).to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self.0), to_usize(self.1), 1] }
    }

    impl<D> MemLen for [D; 2] where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::Two(to_usize(self[0]), to_usize(self[1])).to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::Two(to_usize(self[0]), to_usize(self[1])).to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self[0]), to_usize(self[1]), 1] }
    }

    impl<'a, D> MemLen for (D, D, D) where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::Three(to_usize(self.0), to_usize(self.1), to_usize(self.2))
                .to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::Three(to_usize(self.0), to_usize(self.1), to_usize(self.2))
                .to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self.0), to_usize(self.1), to_usize(self.2)] }
    }

    impl<'a, D> MemLen for [D; 3] where D: Num + ToPrimitive + Debug + Copy {
        fn to_len(&self) -> usize {
            SpatialDims::Three(to_usize(self[0]), to_usize(self[1]), to_usize(self[2]))
                .to_len()
        }
        fn to_len_padded(&self, incr: usize) -> usize {
            SpatialDims::Three(to_usize(self[0]), to_usize(self[1]), to_usize(self[2]))
                .to_len_padded(incr)
        }
        fn to_lens(&self) -> [usize; 3] { [to_usize(self[0]), to_usize(self[1]), to_usize(self[2])] }
    }
}