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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
use std;
use std::convert::Into;
use std::collections::HashMap;
use core::{self, OclPrm, Kernel as KernelCore, CommandQueue as CommandQueueCore, Mem as MemCore,
KernelArg, KernelInfo, KernelInfoResult, KernelArgInfo, KernelArgInfoResult,
KernelWorkGroupInfo, KernelWorkGroupInfoResult, ClEventPtrNew, ClWaitList};
use core::error::{Result as OclResult, Error as OclError};
use standard::{SpatialDims, Buffer, Image, Program, Queue, WorkDims, Sampler, Device};
const PRINT_DEBUG: bool = false;
pub struct KernelCmd<'k> {
queue: &'k CommandQueueCore,
kernel: &'k KernelCore,
gwo: SpatialDims,
gws: SpatialDims,
lws: SpatialDims,
wait_list: Option<&'k ClWaitList>,
dest_list: Option<&'k mut ClEventPtrNew>,
}
impl<'k> KernelCmd<'k> {
pub fn queue<Q: AsRef<CommandQueueCore>>(mut self, queue: &'k Q) -> KernelCmd<'k> {
self.queue = queue.as_ref();
self
}
pub fn gwo<D: Into<SpatialDims>>(mut self, gwo: D) -> KernelCmd<'k> {
self.gwo = gwo.into();
self
}
pub fn gws<D: Into<SpatialDims>>(mut self, gws: D) -> KernelCmd<'k> {
self.gws = gws.into();
self
}
pub fn lws<D: Into<SpatialDims>>(mut self, lws: D) -> KernelCmd<'k> {
self.lws = lws.into();
self
}
pub fn ewait(mut self, wait_list: &'k ClWaitList) -> KernelCmd<'k> {
self.wait_list = Some(wait_list);
self
}
pub fn ewait_opt(mut self, wait_list: Option<&'k ClWaitList>) -> KernelCmd<'k> {
self.wait_list = wait_list;
self
}
pub fn enew(mut self, new_event_dest: &'k mut ClEventPtrNew) -> KernelCmd<'k> {
self.dest_list = Some(new_event_dest);
self
}
pub fn enew_opt(mut self, new_event_list: Option<&'k mut ClEventPtrNew>) -> KernelCmd<'k> {
self.dest_list = new_event_list;
self
}
pub fn enq(self) -> OclResult<()> {
let dim_count = self.gws.dim_count();
let gws = match self.gws.to_work_size() {
Some(gws) => gws,
None => return OclError::err("ocl::KernelCmd::enqueue: Global Work Size ('gws') \
cannot be left unspecified. Set a default for the kernel or pass a valid parameter."),
};
if PRINT_DEBUG {
println!("Enqueuing kernel: '{}'...",
core::get_kernel_info(self.kernel, KernelInfo::FunctionName));
}
core::enqueue_kernel(self.queue, self.kernel, dim_count, self.gwo.to_work_offset(),
&gws, self.lws.to_work_size(), self.wait_list, self.dest_list)
}
}
#[derive(Debug)]
pub struct Kernel {
obj_core: KernelCore,
named_args: HashMap<&'static str, u32>,
mem_args: Vec<Option<MemCore>>,
arg_count: u32,
queue: Queue,
gwo: SpatialDims,
gws: SpatialDims,
lws: SpatialDims,
}
impl Kernel {
pub fn new<S: Into<String>, >(name: S, program: &Program, queue: &Queue,
) -> OclResult<Kernel>
{
let name = name.into();
let obj_core = try!(core::create_kernel(program, &name));
Ok(Kernel {
obj_core: obj_core,
named_args: HashMap::with_capacity(5),
arg_count: 0,
mem_args: Vec::with_capacity(16),
queue: queue.clone(),
gwo: SpatialDims::Unspecified,
gws: SpatialDims::Unspecified,
lws: SpatialDims::Unspecified,
})
}
pub fn gwo<D: Into<SpatialDims>>(mut self, gwo: D) -> Kernel {
self.gwo = gwo.into();
self
}
pub fn gws<D: Into<SpatialDims>>(mut self, gws: D) -> Kernel {
self.gws = gws.into();
self
}
pub fn lws<D: Into<SpatialDims>>(mut self, lws: D) -> Kernel {
self.lws = lws.into();
self
}
pub fn arg_buf<T: OclPrm>(mut self, buffer: &Buffer<T>) -> Kernel {
self.new_arg_buf(Some(buffer));
self
}
pub fn arg_img<P: OclPrm>(mut self, image: &Image<P>) -> Kernel {
self.new_arg_img(Some(image));
self
}
pub fn arg_smp(mut self, sampler: &Sampler) -> Kernel {
self.new_arg_smp(Some(sampler));
self
}
pub fn arg_scl<T: OclPrm>(mut self, scalar: T) -> Kernel {
self.new_arg_scl(Some(scalar));
self
}
pub fn arg_vec<T: OclPrm>(mut self, vector: T) -> Kernel {
self.new_arg_vec(Some(vector));
self
}
pub fn arg_loc<T: OclPrm>(mut self, length: usize) -> Kernel {
self.new_arg_loc::<T>(length);
self
}
pub fn arg_scl_named<T: OclPrm>(mut self, name: &'static str, scalar_opt: Option<T>) -> Kernel {
let arg_idx = self.new_arg_scl(scalar_opt);
self.named_args.insert(name, arg_idx);
self
}
pub fn arg_vec_named<T: OclPrm>(mut self, name: &'static str, vector_opt: Option<T>) -> Kernel {
let arg_idx = self.new_arg_vec(vector_opt);
self.named_args.insert(name, arg_idx);
self
}
pub fn arg_buf_named<T: OclPrm>(mut self, name: &'static str, buffer_opt: Option<&Buffer<T>>) -> Kernel {
let arg_idx = self.new_arg_buf(buffer_opt);
self.named_args.insert(name, arg_idx);
self
}
pub fn arg_img_named<P: OclPrm>(mut self, name: &'static str, image_opt: Option<&Image<P>>) -> Kernel {
let arg_idx = self.new_arg_img(image_opt);
self.named_args.insert(name, arg_idx);
self
}
pub fn arg_smp_named(mut self, name: &'static str, sampler_opt: Option<&Sampler>) -> Kernel {
let arg_idx = self.new_arg_smp(sampler_opt);
self.named_args.insert(name, arg_idx);
self
}
pub fn set_arg_scl_named<'a, T: OclPrm>(&'a mut self, name: &'static str, scalar: T)
-> OclResult<&'a mut Kernel>
{
let arg_idx = try!(self.resolve_named_arg_idx(name));
self.set_arg::<T>(arg_idx, KernelArg::Scalar(scalar))
.and(Ok(self))
}
pub fn set_arg_vec_named<'a, T: OclPrm>(&'a mut self, name: &'static str, vector: T)
-> OclResult<&'a mut Kernel>
{
let arg_idx = try!(self.resolve_named_arg_idx(name));
self.set_arg::<T>(arg_idx, KernelArg::Vector(vector))
.and(Ok(self))
}
pub fn set_arg_buf_named<'a, T: OclPrm>(&'a mut self, name: &'static str,
buffer_opt: Option<&Buffer<T>>) -> OclResult<&'a mut Kernel>
{
let arg_idx = try!(self.resolve_named_arg_idx(name));
match buffer_opt {
Some(buffer) => {
self.set_arg::<T>(arg_idx, KernelArg::Mem(buffer))
},
None => {
self.set_arg::<T>(arg_idx, KernelArg::MemNull)
},
}.and(Ok(self))
}
pub fn set_arg_img_named<'a, P: OclPrm, T: OclPrm>(&'a mut self, name: &'static str,
image_opt: Option<&Image<P>>) -> OclResult<&'a mut Kernel>
{
let arg_idx = try!(self.resolve_named_arg_idx(name));
match image_opt {
Some(buffer) => {
self.set_arg::<T>(arg_idx, KernelArg::Mem(buffer))
},
None => {
self.set_arg::<T>(arg_idx, KernelArg::MemNull)
},
}.and(Ok(self))
}
#[allow(unused_variables)]
pub fn set_arg_smp_named<'a, T: OclPrm>(&'a mut self, name: &'static str,
sampler_opt: Option<&Sampler>) -> OclResult<&'a mut Kernel>
{
unimplemented!();
}
pub fn cmd(&self) -> KernelCmd {
KernelCmd { queue: &self.queue, kernel: &self.obj_core,
gwo: self.gwo, gws: self.gws, lws: self.lws,
wait_list: None, dest_list: None }
}
pub fn enq(&self) -> OclResult<()> {
self.cmd().enq()
}
pub fn set_default_queue(&mut self, queue: &Queue) -> OclResult<&mut Kernel> {
self.queue = queue.clone();
Ok(self)
}
pub fn default_queue(&self) -> &Queue {
&self.queue
}
pub fn get_gwo(&self) -> SpatialDims {
self.gwo
}
pub fn get_gws(&self) -> SpatialDims {
self.gws
}
pub fn get_lws(&self) -> SpatialDims {
self.lws
}
#[inline]
pub fn arg_count(&self) -> u32 {
self.arg_count
}
pub fn core_as_ref(&self) -> &KernelCore {
&self.obj_core
}
pub fn info(&self, info_kind: KernelInfo) -> KernelInfoResult {
core::get_kernel_info(&self.obj_core, info_kind)
}
pub fn arg_info(&self, arg_index: u32, info_kind: KernelArgInfo) -> KernelArgInfoResult {
core::get_kernel_arg_info(&self.obj_core, arg_index, info_kind,
Some(&[self.queue.device_version()]))
}
pub fn wg_info(&self, device: &Device, info_kind: KernelWorkGroupInfo)
-> KernelWorkGroupInfoResult
{
core::get_kernel_work_group_info(&self.obj_core, device, info_kind)
}
pub fn name(&self) -> String {
core::get_kernel_info(&self.obj_core, KernelInfo::FunctionName).into()
}
fn fmt_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Kernel")
.field("FunctionName", &self.info(KernelInfo::FunctionName))
.field("ReferenceCount", &self.info(KernelInfo::ReferenceCount))
.field("Context", &self.info(KernelInfo::Context))
.field("Program", &self.info(KernelInfo::Program))
.field("Attributes", &self.info(KernelInfo::Attributes))
.finish()
}
fn fmt_wg_info(&self, f: &mut std::fmt::Formatter, device: &Device) -> std::fmt::Result {
f.debug_struct("WorkGroup")
.field("WorkGroupSize", &self.wg_info(device, KernelWorkGroupInfo::WorkGroupSize))
.field("CompileWorkGroupSize", &self.wg_info(device, KernelWorkGroupInfo::CompileWorkGroupSize))
.field("LocalMemSize", &self.wg_info(device, KernelWorkGroupInfo::LocalMemSize))
.field("PreferredWorkGroupSizeMultiple",
&self.wg_info(device, KernelWorkGroupInfo::PreferredWorkGroupSizeMultiple))
.field("PrivateMemSize", &self.wg_info(device, KernelWorkGroupInfo::PrivateMemSize))
.finish()
}
fn resolve_named_arg_idx(&self, name: &'static str) -> OclResult<u32> {
match self.named_args.get(name) {
Some(&ai) => Ok(ai),
None => {
OclError::err(format!("Kernel::set_arg_scl_named(): Invalid argument \
name: '{}'.", name))
},
}
}
fn new_arg_buf<T: OclPrm>(&mut self, buffer_opt: Option<&Buffer<T>>) -> u32 {
match buffer_opt {
Some(buffer) => {
self.new_arg::<T>(KernelArg::Mem(buffer))
},
None => {
self.new_arg::<T>(KernelArg::MemNull)
},
}
}
fn new_arg_img<P: OclPrm>(&mut self, image_opt: Option<&Image<P>>) -> u32 {
match image_opt {
Some(image) => {
self.new_arg::<u8>(KernelArg::Mem(image))
},
None => {
self.new_arg::<u8>(KernelArg::MemNull)
},
}
}
fn new_arg_smp(&mut self, sampler_opt: Option<&Sampler>) -> u32 {
match sampler_opt {
Some(sampler) => {
self.new_arg::<u8>(KernelArg::Sampler(sampler))
},
None => {
self.new_arg::<u8>(KernelArg::SamplerNull)
},
}
}
fn new_arg_scl<T: OclPrm>(&mut self, scalar_opt: Option<T>) -> u32 {
let scalar = match scalar_opt {
Some(s) => s,
None => Default::default(),
};
self.new_arg::<T>(KernelArg::Scalar(scalar))
}
fn new_arg_vec<T: OclPrm>(&mut self, vector_opt: Option<T>) -> u32 {
let vector = match vector_opt {
Some(s) => s,
None => Default::default(),
};
self.new_arg::<T>(KernelArg::Vector(vector))
}
fn new_arg_loc<T: OclPrm>(&mut self, length: usize) -> u32 {
self.new_arg::<T>(KernelArg::Local(&length))
}
fn new_arg<T: OclPrm>(&mut self, arg: KernelArg<T>) -> u32 {
let arg_idx = self.arg_count;
self.mem_args.push(None);
self.set_arg(arg_idx, arg).expect("Kernel::new_arg()");
self.arg_count += 1;
debug_assert!(self.arg_count as usize == self.mem_args.len());
arg_idx
}
fn set_arg<T: OclPrm>(&mut self, arg_idx: u32, arg: KernelArg<T>) -> OclResult<()> {
let arg = match arg {
KernelArg::Mem(mem) => {
self.mem_args[arg_idx as usize] = Some(mem.clone());
let mem_arg_ref = self.mem_args.get(arg_idx as usize).unwrap().as_ref().unwrap();
KernelArg::Mem(mem_arg_ref)
},
arg => {
self.mem_args[arg_idx as usize] = None;
arg
},
};
core::set_kernel_arg::<T>(&self.obj_core, arg_idx, arg)
}
}
impl std::fmt::Display for Kernel {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
try!(self.fmt_info(f));
try!(write!(f, " "));
self.fmt_wg_info(f, self.queue.device())
}
}
use std::ops::{Deref, DerefMut};
impl Deref for Kernel {
type Target = KernelCore;
fn deref(&self) -> &KernelCore {
&self.obj_core
}
}
impl DerefMut for Kernel {
fn deref_mut(&mut self) -> &mut KernelCore {
&mut self.obj_core
}
}