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
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::Platform;
pub use self::device::{Device, DeviceSpecifier};
pub use self::context::{Context, ContextBuilder};
pub use self::program::{Program, ProgramBuilder, BuildOpt};
pub use self::queue::Queue;
pub use self::kernel::{Kernel, KernelCmd};
pub use self::buffer::{BufferCmdKind, BufferCmdDataShape, BufferCmd, Buffer, QueCtx,
BufferBuilder, BufferReadCmd, BufferWriteCmd, BufferMapCmd,};
pub use self::image::{Image, ImageCmd, ImageCmdKind, 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};
#[cfg(not(feature = "async_block"))]
mod cb {
use libc::c_void;
use num::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;
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);
}
}
}
mod types {
use std::ptr;
use std::cell::Ref;
use standard::{Event, EventList, RawEventArray, Queue};
use core::ffi::cl_event;
use core::{Result as OclResult, Event as EventCore, ClNullEventPtr, ClWaitListPtr};
#[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, ClWaitListPtr>),
BoxTraitObj(Box<ClWaitListPtr>),
}
impl<'a> ClWaitListPtrEnum<'a> {
pub fn into_marker(self, queue: &Queue) -> OclResult<Event> {
queue.enqueue_marker(Some(self))
}
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, ClWaitListPtr>> for ClWaitListPtrEnum<'a> {
fn from(e: Ref<'a, ClWaitListPtr>) -> ClWaitListPtrEnum<'a> {
ClWaitListPtrEnum::RefTraitObj(e)
}
}
impl<'a> From<Box<ClWaitListPtr>> for ClWaitListPtrEnum<'a> {
fn from(e: Box<ClWaitListPtr>) -> ClWaitListPtrEnum<'a> {
ClWaitListPtrEnum::BoxTraitObj(e)
}
}
#[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
}
}
}
mod traits {
use std::fmt::Debug;
use num::{Num, ToPrimitive};
use ::SpatialDims;
use super::spatial_dims::to_usize;
pub trait WorkDims {
fn dim_count(&self) -> u32;
fn to_work_size(&self) -> Option<[usize; 3]>;
fn to_work_offset(&self) -> Option<[usize; 3]>;
}
pub trait MemLen {
fn to_len(&self) -> usize;
fn to_len_padded(&self, incr: usize) -> usize;
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])] }
}
}