forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
458 lines (410 loc) · 14.3 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
<!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 ) ? '../common-revealjs/css/print/pdf.css' : '../common-revealjs/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" />
</div>
<!--Slide 1-->
<section class="hbox">
<div class="hbox" data-markdown>
## Using USM
</div>
</section>
<!--Slide 2-->
<section class="hbox" data-markdown>
## Learning Objectives
* Learn how to allocate memory using USM
* Learn how to copy data to and from USM allocated memory
* Learn how to access data from USM allocated memory in a kernel function
* Learn how to free USM memory allocations
</section>
<!--Slide 3-->
<section>
<div class="hbox" data-markdown>
#### Focus on Explicit USM
</div>
<div class="container" data-markdown>
* Remember that there are different variants of USM; explicit, restricted, concurrent and system.
* Remember also that there are different ways USM memory can be allocated; host, device and shared.
* We're going to focus explicit USM and device allocations - this is the minimum required variant.
</div>
</section>
<!--Slide 3A-->
<section>
<div class="hbox" data-markdown>
#### USM Allocation Types
</div>
<div class="container"data-markdown>
![SYCL](./Figure6-1bookUSMtypes.png "SYCL")
(from book)
</div>
</section>
<!--Slide 4-->
<section>
<div class="hbox" data-markdown>
#### Malloc_device
</div>
<div class="container">
<code class="code-100pc"><pre>
void* malloc_device(size_t numBytes, const queue& syclQueue, const property_list &propList = {});
template <typename T>
T* malloc_device(size_t count, const queue& syclQueue, const property_list &propList = {});
</code></pre>
</div>
<div class="container" data-markdown>
* A USM device allocation is performed by calling one of the `malloc_device` functions.
* Both of these functions allocate the specified region of memory on the `device` associated with the specified `queue`.
* The pointer returned is only accessible in a kernel function running on that `device`.
* Synchronous exception if the device does not have aspect::usm_device_allocations
* This is a blocking operation.
</div>
</section>
<!--Slide 5-->
<section>
<div class="hbox" data-markdown>
#### Free
</div>
<div class="container">
<code class="code-100pc"><pre>
void free(void* ptr, queue& syclQueue);
</code></pre>
</div>
<div class="container" data-markdown>
* In order to prevent memory leaks USM device allocations must be free by calling the `free` function.
* The `queue` must be the same as was used to allocate the memory.
* This is a blocking operation.
</div>
</section>
<!--Slide 6-->
<section>
<div class="hbox" data-markdown>
#### Memcpy
</div>
<div class="container">
<code class="code-100pc"><pre>
event queue::memcpy(void* dest, const void* src, size_t numBytes, const std::vector<event> &depEvents);
</code></pre>
</div>
<div class="container" data-markdown>
* Data can be copied to and from a USM device allocation by calling the `queue`'s `memcpy` member function.
* The source and destination can be either a host application pointer or a USM device allocation.
* This is an asynchronous operation enqueued to the `queue`.
* An `event` is returned which can be used to synchronize with the completion of copy operation.
* May depend on other events via `depEvents`
</div>
</section>
<!--Slide 7-->
<section>
<div class="hbox" data-markdown>
#### Memset & fill
</div>
<div class="container">
<code class="code-100pc"><pre>
event queue::memset(void* ptr, int value, size_t numBytes, const std::vector<event> &depEvents);
event queue::fill(void* ptr, const T& pattern, size_t count, const std::vector<event> &depEvents);
</code></pre>
</div>
<div class="container" data-markdown>
* The additional `queue` member functions `memset` and `fill` provide operations for initializing the data of a USM device allocation.
* The member function `memset` initializes each byte of the data with the value interpreted as an unsigned char.
* The member function `fill` initializes the data with a recurring pattern.
* These are also asynchronous operations.
</div>
</section>
<!--Slide 8-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{};
myQueue.submit([&](handler &cgh){
cgh.single_task<square_number>([=](){
/* square some number */
});
}).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
We start with a basic SYCL application which invokes a kernel function with `single_task`.
</div>
</section>
<!--Slide 9-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{<mark>usm_selector{}</mark>};
myQueue.submit([&](handler &cgh){
cgh.single_task<square_number>([=](){
/* square some number */
});
}).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
We initialize the `queue` with the `usm_selector` we wrote in the last exercise, which will choose a device which supports USM device allocations.
</div>
</section>
<!--Slide 10-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
<mark>auto devicePtr = malloc_device<int>(1, myQueue);</mark>
myQueue.submit([&](handler &cgh){
cgh.single_task<square_number>([=](){
/* square some number */
});
}).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
We allocate USM device memory by calling `malloc_device`.
Here we use the template variant and specify type `int`.
</div>
</section>
<!--Slide 11-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
auto devicePtr = malloc_device<int>(1, myQueue);
<mark>myQueue.memcpy(devicePtr, &x, sizeof(int)).wait();</mark>
myQueue.submit([&](handler &cgh){
cgh.single_task<square>([=](){
/* square some number */
});
}).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
We copy the value of `x` in the host application to the USM device memory by calling `memcpy` on `myQueue`.
We immediately call `wait` on the returned `event` to synchronize with the completion of the copy operation.
</div>
</section>
<!--Slide 12-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
auto devicePtr = malloc_device<int>(1, myQueue);
myQueue.memcpy(devicePtr, &x, sizeof(int)).wait();
myQueue.submit([&](handler &cgh){
cgh.single_task<square>([=](){
<mark>*devicePtr = (*devicePtr) * (*devicePtr);</mark>
});
}).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
We then pass the `devicePtr` directly to the kernel function and access it can then be deferenced and the data written to.
</div>
</section>
<!--Slide 13-->
<section>
<div class="hbox" data-markdown>
#### Putting it all together
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
auto devicePtr = malloc_device<int>(1, myQueue);
myQueue.memcpy(devicePtr, &x, sizeof(int)).wait();
myQueue.submit([&](handler &cgh){
cgh.single_task<square>([=](){
*devicePtr = (*devicePtr) * (*devicePtr);
});
}).wait();
<mark>myQueue.memcpy(&x, devicePtr, sizeof(int)).wait();</mark>
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
Finally we copy the result from USM device memory back to the variable x in the host application by calling `memcpy` on `myQueue`.
</div>
</section>
<!--Slide 14-->
<section>
<div class="hbox" data-markdown>
#### Queue shortcuts
</div>
<div class="container">
<code class="code-100pc"><pre>
template <typename KernelName, typename KernelType>
event queue::single_task(const KernelType &KernelFunc);
template <typename KernelName, typename KernelType, int Dims>
event queue::parallel_for(range<Dims> GlobalRange, const KernelType &KernelFunc);
</code></pre>
</div>
<div class="container" data-markdown>
* The `queue` provides shortcut member functions which allow you to invoke a `single_task` or a `parallel_for` without defining a command group.
* These can only be used when using the USM data management model.
</div>
</section>
<!--Slide 15-->
<section>
<div class="hbox" data-markdown>
#### With the queue shortcut
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
auto devicePtr = malloc_device<int>(1, myQueue);
myQueue.memcpy(devicePtr, &x, sizeof(int)).wait();
<mark>myQueue.single_task<square>([=](){</mark>
<mark>*devicePtr = (*devicePtr) * (*devicePtr);</mark>
<mark>}).wait();</mark>
myQueue.memcpy(&x, devicePtr, sizeof(int)).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
If we use the queue shortcut here it reduces the complexity of the code.
</div>
</section>
<!--Slide 16-->
<section>
<div class="hbox" data-markdown>
#### Usm_wrapper **ComputeCpp only**
</div>
<div class="container">
<code class="code-100pc"><pre>
using namespace experimental {
template <typename T>
class usm_wrapper;
}
</code></pre>
</div>
<div class="container" data-markdown>
* USM support in ComputeCpp is still experimental.
* You currently need to wrap USM pointers in a `usm_wrapper` to pass them to a kernel function.
* The `usm_wrapper` will behave like and convert to the raw pointer type.
* This will be removed when ComputeCpp fully supports SYCL 2020.
</div>
</section>
<!--Slide 17-->
<section>
<div class="hbox" data-markdown>
#### With the usm_wrapper **ComputeCpp only**
</div>
<div class="container">
<code class="code-100pc"><pre>
int square_number(int x){
auto myQueue = sycl::queue{usm_selector{}};
auto devicePtr = <mark>experimental::usm_wrapper<int>(</mark>malloc_device<int>(1, myQueue)<mark>)</mark>;
myQueue.memcpy(devicePtr, &x, sizeof(int)).wait();
myQueue.single_task<square>([=](){
*devicePtr = (*devicePtr) * (*devicePtr);
}).wait();
myQueue.memcpy(&x, devicePtr, sizeof(int)).wait();
return x;
}
</code></pre>
</div>
<div class="container" data-markdown>
In ComputeCpp we wrap the result of `malloc_device` with a `usm_wrapper<int>` so it can be passed to the kernel function.
</div>
</section>
<!--Slide 18-->
<section>
<div class="hbox" data-markdown>
## Questions
</div>
</section>
<!--Slide 19-->
<section>
<div class="hbox" data-markdown>
#### Exercise
</div>
<div class="container" data-markdown>
Code_Exercises/Exercise_8_USM_Vector_Add/source
</div>
<div class="container" data-markdown>
Implement the vector add from lesson 3 using the USM data management model.
</div>
</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>