forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
839 lines (778 loc) · 28.4 KB
/
index.html
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../common-revealjs/css/reveal.css">
<link rel="stylesheet" href="../common-revealjs/css/theme/white.css">
<link rel="stylesheet" href="../common-revealjs/css/custom.css">
<script>
// This is needed when printing the slides to pdf
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<script>
// This is used to display the static images on each slide,
// See global-images in this html file and custom.css
(function() {
if(window.addEventListener) {
window.addEventListener('load', () => {
let slides = document.getElementsByClassName("slide-background");
if (slides.length === 0) {
slides = document.getElementsByClassName("pdf-page")
}
// Insert global images on each slide
for(let i = 0, max = slides.length; i < max; i++) {
let cln = document.getElementById("global-images").cloneNode(true);
cln.removeAttribute("id");
slides[i].appendChild(cln);
}
// Remove top level global images
let elem = document.getElementById("global-images");
elem.parentElement.removeChild(elem);
}, false);
}
})();
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<div id="global-images" class="global-images">
<img src="../common-revealjs/images/sycl_academy.png" />
<img src="../common-revealjs/images/sycl_logo.png" />
<img src="../common-revealjs/images/trademarks.png" />
<img src="../common-revealjs/images/codeplay.png" />
</div>
<!--Slide 1-->
<section class="hbox">
<div class="hbox" data-markdown>
## SYCL Topology Discovery and Queue Creation
</div>
</section>
<!--Slide 2-->
<section class="hbox" data-markdown>
## Learning Objectives
* Learn about the SYCL system topology and how to traverse it
* Learn how to query information about a platform or device
* Learn how to select a device; both manually and using device selectors
* Learn how to configure a queue
* Learn about the SYCL scheduler and how to enqueue work
</section>
<!--Slide 3-->
<section class="hbox" data-markdown>
## SYCL System Topology
</section>
<!--Slide 4-->
<section>
<div class="hbox" data-markdown>
* A SYCL application can execute work across a range of different heterogeneous devices
* The devices that are available in any given system are determined at runtime through topology discovery
</div>
</section>
<!--Slide 5-->
<section>
<div class="container" data-markdown>
* The SYCL runtime will discover a set of platforms that are available in the system
* Each platform represents a backend implementation such as Intel OpenCL and Nvidia OpenCL
* The SYCL runtime will also discover all the devices available for each of those platforms
* CPU, GPU, FPGA, and other kinds of accelerators
</div>
<div class="col" data-markdown>
![SYCL](../common-revealjs/images/devices-1.png "SYCL-Devices")
</div>
</section>
<!--Slide 6-->
<section>
<div class="container" data-markdown>
* In SYCL there is also a host device which executes SYCL kernels as native C++
* The host device emulates the execution and memory model of a SYCL device
* This is very useful for debugging SYCL kernels
* There is only ever one host device and that device is associated with a host platform
* This is generally a CPU implementation
</div>
<div class="container">
<div class="col" data-markdown>
![SYCL](../common-revealjs/images/devices-2.png "SYCL-Devices")
</div>
</div>
</section>
<!--Slide 7-->
<section>
<div class="container" data-markdown>
* Platforms and devices are represented by the **platform** and **device** classes respectively
* A default constructed platform object represents the host platform
* A default constructed device object represents the host device
</div>
<div class="container">
<div class="col" data-markdown>
![SYCL](../common-revealjs/images/devices-3.png "SYCL-Devices")
</div>
</div>
</section>
<!--Slide 8-->
<section class="hbox">
<div class="hbox" data-markdown>
* In SYCL there are two ways to query a system’s topology
* The topology can be manually queried and iterated over via APIs of the platform and device classes
* The topology can be automatically queried and iterated over using a use specified heuristic by a device selector object
</div>
</section>
<!--Slide 9-->
<section class="hbox" data-markdown>
## Querying the topology manually
</section>
<!--Slide 10-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto platforms = platform::get_platforms();
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![SYCL](../common-revealjs/images/devices-5.png "SYCL-Devices")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* The platform class provides the static function **get_platforms**
* It retrieves a vector of all available platforms in the system
* This includes the host platform
</div>
</section>
<!--Slide 11-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto intelDevices = intelPlatform.get_devices();
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![SYCL](../common-revealjs/images/devices-6.png "SYCL-Devices")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* The platform class provides the member function **get_devices** that
* It returns a vector of all devices associated with that platform
* This includes the host device if the platform object represents a host platform
</div>
</section>
<!--Slide 12-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto devices = device::get_devices();
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![SYCL](../common-revealjs/images/devices-7.png "SYCL-Devices")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* The device class also provides the static function **get_devices**
* It retrieves a vector of all available devices in the system
* This includes the host device
</div>
</section>
<!--Slide 13-->
<section class="hbox" data-markdown>
## Querying the topology using a device selector
</section>
<!--Slide 14-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![Device Topology](../common-revealjs/images/topology-1.png "Device-Topology")
</div>
<div class="col-right-1" data-markdown>
* To simplify the process of traversing the system topology SYCL provides device selectors
* A device selector is is a C++ function object, derived from the **device_selector** class, which defines a heuristic for scoring devices
* SYCL provides a number of standard device selectors, e.g. **default_selector**, **gpu_selector**, etc
* Users can also create their own device selectors
</div>
</div>
</section>
<!--Slide 15-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto gpuSelector = gpu_selector{};
auto gpuDevice = gpuSelector.select_device();
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![Device Topology](../common-revealjs/images/topology-1.png "Device-Topology")
</div>
</div>
<div class="container" data-markdown>
* The **device_selector** class provides the member function **select_device**
* Queries all devices and returns the one with the highest "score"
* A device with a negative score will never be chosen
</div>
</section>
<!--Slide 16-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto defSelector = default_selector{};
auto chosenDevice = defSelector.select_device();
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![Device Topology](../common-revealjs/images/topology-1.png "Device-Topology")
</div>
</div>
<div class="container" data-markdown>
* The **default_selector** is a standard device selector type
* Chooses a device based on an implementation defined heuristic
</div>
</section>
<!--Slide 17-->
<section>
<div class="hbox" data-markdown>
## Custom Device Selectors
</div>
</section>
<!--Slide 18-->
<section>
<div class="container">
<code><pre>
#include <CL/sycl.hpp>
using namespace cl::sycl;
struct gpu_selector : <mark>public device_selector</mark> {
<mark>int operator()(const device& dev) const override {</mark>
<mark>}</mark>
};
int main(int argc, char *argv[]) {
}
</code></pre>
</div>
<div class="hbox" data-markdown>
* A device selector must inherit from the **device_selector** class
* A device selector must have a function call operator which takes a reference to a device
</div>
</section>
<!--Slide 19-->
<section>
<div class="container">
<code><pre>
#include <CL/sycl.hpp>
using namespace cl::sycl;
struct gpu_selector : public device_selector {
int operator()(const device& dev) const override {
<mark>if (dev.is_gpu()){</mark>
<mark>return 1;</mark>
<mark>}</mark>
<mark>else {</mark>
<mark>return -1;</mark>
<mark>}</mark>
}
};
int main(int argc, char *argv[]) {
}
</code></pre>
</div>
<div class="hbox" data-markdown>
* The body of the function call operator defines the heuristic for selecting devices
* This is where you write the logic for scoring each device
</div>
</section>
<!--Slide 20-->
<section>
<div class="container">
<code><pre>
#include <CL/sycl.hpp>
using namespace cl::sycl;
struct gpu_selector : public device_selector {
int operator()(const device& dev) const override {
if (dev.is_gpu()){
return 1;
}
else {
return -1;
}
}
};
int main(int argc, char *argv[]) {
<mark>auto gpuQueue = queue{gpu_selector{}};</mark>
}
</code></pre>
</div>
<div class="hbox" data-markdown>
* Now that there is a device selector that chooses a specific device we can use that to construct a queue
</div>
</section>
<!--Slide 21-->
<section class="hbox" data-markdown>
## Selecting A SYCL Device
</section>
<!--Slide 22-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto plt = dev.get_platform();
auto platformName = dev.get_info<info::device::name>();
</code></pre>
</div>
<div class="col-right-2" data-markdown>
![SYCL](../common-revealjs/images/topology-2.png "SYCL-Devices")
</div>
</div>
<div class="container" data-markdown>
* Information about platforms and devices can be queried using the template member function **get_info**
* The info that you are querying is specified by the template parameter
* You can also query a device for its associated platform with the **get_platform** member function
</div>
</section>
<!--Slide 23-->
<section class="hbox" data-markdown>
## What is a SYCL Queue
</section>
<!--Slide 24-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![Queue](../common-revealjs/images/queue-1.png "SYCL-Queue")
</div>
<div class="col-right-1" data-markdown>
* In SYCL the underlying execution and memory resources of a platform and its devices is managed by creating a context
* A context represents one or more devices, but all devices must be associated with the same platform
</div>
</div>
</section>
<!--Slide 25-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![Queue](../common-revealjs/images/queue-2.png "SYCL-Queue")
</div>
<div class="col-right-1" data-markdown>
* In SYCL the object which is used to submit work is the queue
* A queue processes command groups and submits commands to the SYCL scheduler for a particular context and device
</div>
</div>
</section>
<!--Slide 26-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![Queue](../common-revealjs/images/queue-3.png "SYCL-Queue")
</div>
<div class="col-right-1" data-markdown>
* A single SYCL application will often want to target multiple different devices
* This can be useful for task level parallelism and load balancing
</div>
</div>
</section>
<!--Slide 27-->
<section class="hbox" data-markdown>
## Creating a Queue
</section>
<!--Slide 28-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto defaultQueue = queue{};
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![SYCL](../common-revealjs/images/queue-4.png "SYCL-Queue")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* A default constructed queue object will use the **default_selector** to choose a device and create an implicit context
</div>
</section>
<!--Slide 29-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
auto intelGPUSelector = intel_gpu_selector{};
auto intelGPUQeuue = queue{intelGPUSelector};
</code></pre>
</div>
<div class="col-right-1" data-markdown>
![SYCL](../common-revealjs/images/queue-4.png "SYCL-Queue")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* A queue object can be constructed from a device selector which is used to choose a device and create an implicit context
</div>
</section>
<!--Slide 30-->
<section class="hbox" data-markdown>
## Submitting Work to a Queue
</section>
<!--Slide 31-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![SYCL](../common-revealjs/images/queue-6.png "SYCL-Queue")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* In SYCL work is submitted via a queue object
* This is done using the **submit** member function
* This will process the command group and submit commands to the SYCL scheduler for the context and device associated with the queue
</div>
</section>
<!--Slide 32-->
<section>
<div class="container">
<div class="col-left-1" data-markdown>
![SYCL](../common-revealjs/images/queue-7.png "SYCL-Queue")
</div>
</div>
<div class="smaller-size-font" data-markdown>
* The same scheduler is used for all queues in order to share dependency information
</div>
</section>
<!--Slide 33-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
#include <CL/sycl.hpp> using namespace cl::sycl;
int main(int argc, char *argv[]) {
queue gpuQueue(gpu_selector{});
<mark>gpuQueue.submit([&](handler &cgh){</mark>
// Command group
<mark>});</mark>
}
</code></pre>
</div>
<div class="col-right-1" data-markdown>
* The **submit** member function takes a C++ function object, which takes a reference to a **handler** object
* The function object can be a lambda or a class with a function call operator
* The body of the function object represents the command group that is being submitted
* The handler object is created by the SYCL runtime and is used to link commands and requirements declared inside the command group
</div>
</div>
</section>
<!--Slide 34-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
#include <CL/sycl.hpp> using namespace cl::sycl;
int main(int argc, char *argv[]) {
queue gpuQueue(gpu_selector{});
<mark>gpuQueue.submit([&](handler &cgh){</mark>
// Command group
<mark>});</mark>
}
</code></pre>
</div>
<div class="col-right-1" data-markdown>
* The command group is processed exactly once when **submit** is called
* At this point all the commands and requirements declared inside the command group are collected together, processed and passed on to the scheduler
* The work is then enqueued to the device asynchronously by the SYCL scheduler, potentially in another thread
</div>
</div>
</section>
<!--Slide 35-->
<section>
<div class="container">
<div class="col-left-1">
<code><pre>
#include <CL/sycl.hpp> using namespace cl::sycl;
int main(int argc, char *argv[]) {
queue gpuQueue(gpu_selector{});
gpuQueue.submit([&](handler &cgh){
// Command group
});
<mark>gpuQueue.wait();</mark>
}
</code></pre>
</div>
<div class="col-right-1" data-markdown>
* The queue object will not wait for work to complete on destruction
* There are other ways to wait for work to complete if you have data dependencies
* But it's often useful to be able to explicitly wait on a queue to complete any outstanding work
</div>
</div>
</section>
<!--Slide 36-->
<section>
<div class="hbox" data-markdown>
## Handling Errors in SYCL
</div>
</section>
<!--Slide 37-->
<section class="hbox" data-markdown>
* In SYCL errors are handled by throwing exceptions
* It is crucial that these errors are handled otherwise your application may silently fail
* In SYCL there are two kinds of error
* Synchronous errors (thrown in user thread)
* Asynchronous errors (thrown by the SYCL runtime)
</section>
<!--Slide 38-->
<section class="hbox">
<div class="hbox" data-markdown>
![SYCL](../common-revealjs/images/sycl-exceptions.png "SYCL")
</div>
</section>
<!--Slide 39-->
<section>
<div class="hbox" >
<code class="code-60pc"><pre>
#include <CL/sycl.hpp>
using namespace cl::sycl;
class add;
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
queue gpuQueue(gpu_selector{});
buffer<float, 1> bufA(dA.data(), range<1>(dA.size()));
buffer<float, 1> bufB(dB.data(), range<1>(dB.size()));
buffer<float, 1> bufO(dO.data(), range<1>(dO.size()));
gpuQueue.submit([&](handler &cgh){
auto inA = bufA.get_access<access::mode::read>(cgh);
auto inB = bufB.get_access<access::mode::read>(cgh);
auto out = bufO.get_access<access::mode::write>(cgh);
cgh.parallel_for<add>(range<1>(dA.size()), [=](id<1> i){
out[i] = inA[i] + inB[i];
});
});
gpuQueue.wait();
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* If errors are not handled, the application can fail silently
</div>
</section>
<!--Slide 40-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
<mark>try{</mark>
queue gpuQueue(gpu_selector{});
buffer<float, 1> bufA(dA.data(), range<1>(dA.size()));
buffer<float, 1> bufB(dB.data(), range<1>(dB.size()));
buffer<float, 1> bufO(dO.data(), range<1>(dO.size()));
gpuQueue.submit([&](handler &cgh){
auto inA = bufA.get_access<access::mode::read>(cgh);
auto inB = bufB.get_access<access::mode::read>(cgh);
auto out = bufO.get_access<access::mode::write>(cgh);
cgh.parallel_for<add>(range<1>(dA.size()), [=](id<1> i){
out[i] = inA[i] + inB[i];
});
});
gpuQueue.wait();
<mark>} catch (...) { /* handle errors */ }</mark>
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* Synchronous errors are typically thrown by SYCL API functions
* In order to handle all SYCL errors you must wrap everything in a try-catch block
</div>
</section>
<!--Slide 41-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try{
queue gpuQueue(gpu_selector{}, <mark>async_handler{}</mark>);
buffer<float, 1> bufA(dA.data(), range<1>(dA.size()));
buffer<float, 1> bufB(dB.data(), range<1>(dB.size()));
buffer<float, 1> bufO(dO.data(), range<1>(dO.size()));
gpuQueue.submit([&](handler &cgh){
auto inA = bufA.get_access<access::mode::read>(cgh);
auto inB = bufB.get_access<access::mode::read>(cgh);
auto out = bufO.get_access<access::mode::write>(cgh);
cgh.parallel_for<add>(range<1>(dA.size()), [=](id<1> i){
out[i] = inA[i] + inB[i];
});
});
<mark>gpuQueue.wait_and_throw();</mark>
} catch (...) { /* handle errors */
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* Asynchronous errors errors that may have occurred will be thrown after a command group has been submitted to a queue
* To handle these errors you must provide an async handler when constructing the queue object
* Then you must also call the **throw_asynchronous or wait_and_throw** member functions of the queue class
* This will pass the exceptions to the async handler in the user thread so they can be thrown
</div>
</section>
<!--Slide 42-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try{
queue gpuQueue(gpu_selector{}, <mark>[=](sycl::exception_list eL) {</mark>
<mark>for (auto e : eL) { std::rethrow_exception(e); }</mark>
<mark>}</mark>);
buffer<float, 1> bufA(dA.data(), range<1>(dA.size()));
buffer<float, 1> bufB(dB.data(), range<1>(dB.size()));
buffer<float, 1> bufO(dO.data(), range<1>(dO.size()));
gpuQueue.submit([&](handler &cgh){ // Command group submitted to queue
auto inA = bufA.get_access<access::mode::read>(cgh);
auto inB = bufB.get_access<access::mode::read>(cgh);
auto out = bufO.get_access<access::mode::write>(cgh);
cgh.parallel_for<add>(range<1>(dA.size()), [=](id<1> i){
out[i] = inA[i] + inB[i];
});
});
gpuQueue.wait_and_throw(); } catch (...) { /* handle errors */ }
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* The async handler is a C++ lambda or function object that takes as a parameter an **exception_list**
* The exception_list class is a wrapper around a list of **exception_ptrs** which can be iterated over
* The exception_ptrs can be rethrown by passing them to **std::rethrow_exception**
</div>
</section>
<!--Slide 43-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try {
queue gpuQueue(gpu_selector{}, [=](sycl::exception_list eL) {
for (auto e : eL) { std::rethrow_exception(e); }
});
...
gpuQueue.wait_and_throw();
} catch (<mark>std::exception e</mark>) {
<mark>std::cout << “Exception caught: ” << e.what()</mark>
<mark><< std::endl;</mark>
}
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* Once caught, a SYCL exception can provide information about the error
* The **what** member function will return a string with more details
</div>
</section>
<!--Slide 44-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try {
queue gpuQueue(gpu_selector{}, [=](sycl::exception_list eL) {
for (auto e : eL) { std::rethrow_exception(e); }
});
...
gpuQueue.wait_and_throw();
} catch (std::exception e) {
std::cout << “Exception caught: ” << e.what();
<mark>std:: cout << “ With OpenCL error code: ”</mark>
<mark><< e.get_cl_code() << std::endl;</mark>
}
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* If the exception has an OpenCL error code associated with it this can be retrieved by calling the <mark>get_cl_code</mark> member function
* If there is no OpenCL error code this will return <mark>CL_SUCCESS</mark>
</div>
</section>
<!--Slide 45-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try {
queue gpuQueue(gpu_selector{}, [=](sycl::exception_list eL) {
for (auto e : eL) { std::rethrow_exception(e); }
});
...
gpuQueue.wait_and_throw();
} catch (std::exception e) {
<mark>if (e.has_context()) {</mark>
<mark>if (e.get_context() == gpuContext) {</mark>
<mark>/* handle error */</mark>
<mark>}</mark>
<mark>}</mark>
}
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* The **has_context** member function will tell you if there is a SYCL context associated with the error
* If that returns true then the **get_context** member function will return the associated SYCL context object
</div>
</section>
<!--Slide 46-->
<section>
<div class="hbox" data-markdown>
## Debugging SYCL Kernel Functions
</div>
</section>
<!--Slide 47-->
<section>
<div class="hbox" data-markdown>
* Every SYCL implementation is required to provide a host device
* This device executes native C++ code but is guaranteed to emulate the SYCL execution and memory model
* This means you can debug a SYCL kernel function by switching to the host device and using a standard C++ debugger
* For example gdb
</div>
</section>
<!--Slide 48-->
<section>
<div class="hbox">
<code class="code-60pc"><pre>
int main(int argc, char *argv[]) {
std::vector<float> dA{ 7, 5, 16, 8 }, dB{ 8, 16, 5, 7 }, dO{ 0, 0, 0, 0 };
try{
<mark>queue hostQueue(host_selector{}, async_handler{});</mark>
buffer<float, 1> bufA(dA.data(), range<1>(dA.size()));
buffer<float, 1> bufB(dB.data(), range<1>(dB.size()));
buffer<float, 1> bufO(dO.data(), range<1>(dO.size()));
<mark>hostQueue</mark>.submit([&](handler &cgh){
auto inA = bufA.get_access<access::mode::read>(cgh);
auto inB = bufB.get_access<access::mode::read>(cgh);
auto out = bufO.get_access<access::mode::write>(cgh);
cgh.parallel_for<add>(range<1>(dA.size()),
[=](id<1> i){out[i] = inA[i] + inB[i];});
});
gpuQueue.wait_and_throw();
} catch (...) { /* handle errors */ }
}
</code></pre>
</div>
<div class="bottom-bullets" data-markdown>
* Any SYCL application can be debugged on the host device by switching the queue for a host queue
* By replacing the device selector for the host_selector will ensure that the queue submits all work to the host device
</div>
</section>
<!--Slide 49-->
<section data-markdown>
## Questions
</section>
</div>
</div>
<script src="../common-revealjs/js/reveal.js"></script>
<script src="../common-revealjs/plugin/markdown/marked.js"></script>
<script src="../common-revealjs/plugin/markdown/markdown.js"></script>
<script src="../common-revealjs/plugin/notes/notes.js"></script>
<script>
Reveal.initialize({mouseWheel: true, defaultNotes: true});
Reveal.configure({ slideNumber: true });
</script>
</body>
</html>