-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathindex.html
351 lines (329 loc) · 12.1 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
<!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>
## Asynchronous Execution
</div>
</section>
<!--Slide 2-->
<section class="hbox" data-markdown>
## Learning Objectives
* Learn about how commands are enqueued asynchronously
* Learn about the different reasons for synchronization
* Learn about the different ways to perform synchronization
</section>
<!--Slide 3-->
<section>
<div class="hbox" data-markdown>
#### Asynchronous execution
</div>
<div class="container" data-markdown>
* All command submitted to a `queue` are done so asynchronously.
* The functions return immediately and the command is run in a background thread.
* This includes individual commands like `memcpy` and collections of commands derived from a command group.
* This means you have to synchronize with those commands.
</div>
</section>
<!--Slide 4-->
<section>
<div class="hbox" data-markdown>
#### Synchronization
</div>
<div class="container" data-markdown>
There are a number of reasons why you need to synchronize with commands
</div>
<div class="container" data-markdown>
* Await completion of a kernel function.
* Await the results of a computation.
* Await error conditions which come from a failure to execute any of the commands.
</div>
</section>
<!--Slide 5-->
<section>
<div class="hbox" data-markdown>
#### Synchronization with kernel functions
</div>
<div class="container" data-markdown>
There are two ways ways to synchronize with kernel functions.
</div>
<div class="container" data-markdown>
* Calling `wait` on an `event` object returned from enqueuing a kernel function command, either via a command group or a shortcut function.
* Calling `wait` or `wait_and_throw` on the `queue` itself.
</div>
</section>
<!--Slide 6-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with kernel functions (buffers/accessors)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
buf = sycl::buffer(data, sycl::range{1024});
gpuQueue.submit([&](sycl::handler &cgh){
auto acc = sycl::accessor{buf, cgh};
cgh.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
acc[idx] = /* some computation */
});
})<mark>.wait();</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* Calling `wait` on an `event` object returned from enqueuing a command group will wait for the commands from that command group to complete.
* This is how we have synchronized in our examples so far.
* This effectively creates a blocking operations that will complete in place by immediately synchronizing.
</div>
</div>
</section>
<!--Slide 7-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with kernel functions (buffers/accessors)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
buf = sycl::buffer(data, sycl::range{1024});
gpuQueue.submit([&](sycl::handler &cgh){
auto acc = sycl::accessor{buf, cgh};
cgh.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
acc[idx] = /* some computation */
});
});
<mark>gpuQueue.wait();</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* Calling `wait` or `wait_and_throw` on a `queue` will wait for all commands enqueued to it to complete.
* Note that command groups do not create commands to copy data back to the host application.
</div>
</div>
</section>
<!--Slide 8-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with kernel functions (USM)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
auto devicePtr = usm_wrapper<int>(
malloc_device<int>(1024, gpuQueue));
gpuQueue.memcpy(devicePtr, data, sizeof(int))<mark>.wait();</mark>
gpuQueue.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
devicePtr[idx] = /* some computation */
})<mark>.wait();</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* Calling `wait` on an `event` object returned from functions such as `memcpy` or the `queue` shortcuts will wait for that specific command to complete.
* Again this is how we have synchronized in our examples so far.
</div>
</div>
</section>
<!--Slide 9-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with kernel functions (USM)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
auto devicePtr = usm_wrapper<int>(
malloc_device<int>(1024, gpuQueue));
gpuQueue.memcpy(devicePtr, data, sizeof(int));
<mark>gpuQueue.wait();</mark>
gpuQueue.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
devicePtr[idx] = /* some computation */
});
<mark>gpuQueue.wait();</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* Again calling `wait` or `wait_and_throw` on a `queue` will wait for all commands enqueued to it to complete.
* Note you generally don't want to call `wait` on the `queue` after every command, instead you want to create dependencies between commands, which we cover in the next lecture.
</div>
</div>
</section>
<!--Slide 10-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with data
</div>
<div class="container" data-markdown>
There are multiple ways ways to synchronize with data, but it differs depending on the data management model you are using.
</div>
<div class="container" data-markdown>
* When using the USM data management model you can synchronize the same way you would for kernel functions, calling `wait` on an `event` or the `queue`.
* When using the buffer/access data management model command groups don't automatically copy data back so there are other ways to synchronize with the data.
* Creating a `host_accessor`.
* Destroying the `buffer`.
</div>
</section>
<!--Slide 11-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with data (USM)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
gpuQueue.memcpy(data, devicePtr, sizeof(int))<mark>.wait();</mark>
</code></pre>
<code class="code-100pc"><pre>
gpuQueue.memcpy(data, devicePtr, sizeof(int));
<mark>gpuQueue.wait();</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* Simply call `wait` on the `event` returned from `memcpy`.
* Alternatively call `wait` on the `queue`.
</div>
</div>
</section>
<!--Slide 12-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with data (buffer/accessor)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
buf = sycl::buffer(data, sycl::range{1024});
gpuQueue.submit([&](sycl::handler &cgh){
auto acc = sycl::accessor{buf, cgh};
cgh.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
acc[idx] = /* some computation */
});
});
{
<mark>auto hostAcc = buf.get_host_access();</mark>
<mark>hostAcc[/* some index */] = /* some computation */</mark>
}
</code></pre>
</div>
<div class="col" data-markdown>
* A `host_accessor` gives immediate access to the data managed by a `buffer` in the host application.
* This will wait for any kernel functions accessing the `buffer` to complete and then copying the data back to the host.
* It will also block any other `accessor` accessing a `buffer` until it is destroyed.
* Note that the data managed by the `buffer` may not be copied back to the original address on the host, in this case `data`.
</div>
</div>
</section>
<!--Slide 13-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with data (buffer/accessor)
</div>
<div class="container">
<div class="col">
<code class="code-100pc"><pre>
<mark>{</mark>
buf = sycl::buffer(data, sycl::range{1024});
gpuQueue.submit([&](sycl::handler &cgh){
auto acc = sycl::accessor{buf, cgh};
cgh.parallel_for<kernel_a>(sycl::range{1024},
[=](sycl::id<1> idx){
acc[idx] = /* some computation */
});
});
<mark>}</mark>
</code></pre>
</div>
<div class="col" data-markdown>
* A `buffer` will also synchronize the data it manages on destruction.
* It will wait for any kernel functions accessing it to complete and copy the data back to the origin address before completing destruction.
</div>
</div>
</section>
<!--Slide 14-->
<section>
<div class="hbox" data-markdown>
#### Synchronizing with errors
</div>
<div class="container" data-markdown>
* Errors are handled by a `queue` and any asynchronous errors can be produced during any of the synchronization methods we've looked at.
* The best way to ensure all errors are caught is to synchronize by calling `wait` or `wait_and_throw` on the the `queue`.
</div>
</section>
<!--Slide 15-->
<section>
<div class="hbox" data-markdown>
## Questions
</div>
</section>
<!--Slide 16-->
<section>
<div class="hbox" data-markdown>
#### Exercise
</div>
<div class="container" data-markdown>
Code_Exercises/Asynchronous_Execution/source
</div>
<div class="container" data-markdown>
Try out the different methods of synchronizing with a kernel function and the resulting data from the computation.
</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>