forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpdate.cpp
438 lines (354 loc) · 15.8 KB
/
Update.cpp
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
//==----------------------------- Update.cpp -------------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Common.hpp"
using namespace sycl;
using namespace sycl::ext::oneapi;
TEST_F(CommandGraphTest, UpdatableException) {
auto Node = Graph.add(
[&](sycl::handler &cgh) { cgh.single_task<TestKernel<>>([]() {}); });
auto ExecGraphUpdatable =
Graph.finalize(experimental::property::graph::updatable{});
EXPECT_NO_THROW(ExecGraphUpdatable.update(Node));
auto ExecGraphNoUpdatable = Graph.finalize();
// Graph without the property should throw
EXPECT_ANY_THROW(ExecGraphNoUpdatable.update(Node));
}
TEST_F(CommandGraphTest, DynamicParamRegister) {
// Check that registering a dynamic param with a node from a graph that was
// not passed to its constructor throws.
experimental::dynamic_parameter DynamicParam(Graph, int{});
auto OtherGraph =
experimental::command_graph(Queue.get_context(), Queue.get_device());
auto Node = OtherGraph.add([&](sycl::handler &cgh) {
// This should throw since OtherGraph is not associated with DynamicParam
EXPECT_ANY_THROW(cgh.set_arg(0, DynamicParam));
cgh.single_task<TestKernel<>>([]() {});
});
}
TEST_F(CommandGraphTest, UpdateNodeNotInGraph) {
// Check that updating a graph with a node which is not part of that graph is
// an error.
auto OtherGraph =
experimental::command_graph(Queue.get_context(), Queue.get_device());
auto OtherNode = OtherGraph.add(
[&](sycl::handler &cgh) { cgh.single_task<TestKernel<>>([]() {}); });
auto ExecGraph = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_ANY_THROW(ExecGraph.update(OtherNode));
}
TEST_F(CommandGraphTest, UpdateWithUnchangedNode) {
// Tests that updating a graph with a node with unchanged
// parameters is not an error
auto Node = Graph.add(
[&](sycl::handler &cgh) { cgh.single_task<TestKernel<>>([]() {}); });
auto ExecGraph = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_NO_THROW(ExecGraph.update(Node));
}
TEST_F(CommandGraphTest, UpdateNodeTypeExceptions) {
// Check that registering a dynamic parameter with various node types either
// throws or does not throw as appropriate
// Allocate some pointers for memory nodes
int *PtrA = malloc_device<int>(16, Queue);
int *PtrB = malloc_device<int>(16, Queue);
experimental::dynamic_parameter DynamicParam{Graph, int{}};
ASSERT_NO_THROW(auto NodeKernel = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.single_task<TestKernel<>>([]() {});
}));
ASSERT_ANY_THROW(auto NodeMemcpy = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.memcpy(PtrA, PtrB, 16 * sizeof(int));
}));
ASSERT_ANY_THROW(auto NodeMemset = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.memset(PtrB, 7, 16 * sizeof(int));
}));
ASSERT_ANY_THROW(auto NodeMemfill = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.fill(PtrB, 7, 16);
}));
ASSERT_ANY_THROW(auto NodePrefetch = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.prefetch(PtrA, 16 * sizeof(int));
}));
ASSERT_ANY_THROW(auto NodeMemadvise = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.mem_advise(PtrA, 16 * sizeof(int), 1);
}));
ASSERT_ANY_THROW(auto NodeHostTask = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.host_task([]() {});
}));
ASSERT_ANY_THROW(auto NodeBarreriTask = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.ext_oneapi_barrier();
}));
Graph.begin_recording(Queue);
ASSERT_ANY_THROW(auto NodeBarrierTask = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.ext_oneapi_barrier();
}));
Graph.end_recording(Queue);
auto NodeEmpty = Graph.add();
experimental::command_graph Subgraph(Queue.get_context(), Dev);
// Add an empty node to the subgraph
Subgraph.add();
auto SubgraphExec = Subgraph.finalize();
ASSERT_ANY_THROW(auto NodeSubgraph = Graph.add([&](sycl::handler &cgh) {
cgh.set_arg(0, DynamicParam);
cgh.ext_oneapi_graph(SubgraphExec);
}));
}
TEST_F(CommandGraphTest, UpdateRangeErrors) {
// Test that the correct errors are throw when trying to update node ranges
nd_range<1> NDRange{range{128}, range{32}};
range<1> Range{128};
auto NodeNDRange = Graph.add([&](sycl::handler &cgh) {
cgh.parallel_for<TestKernel<>>(NDRange, [](nd_item<1>) {});
});
// OK
EXPECT_NO_THROW(NodeNDRange.update_nd_range(NDRange));
// OK to update an nd_range node with a range of the same dimension
EXPECT_NO_THROW(NodeNDRange.update_range(Range));
// Can't update with a different number of dimensions
EXPECT_ANY_THROW(NodeNDRange.update_nd_range(
nd_range<2>{range<2>{128, 128}, range<2>{32, 32}}));
EXPECT_ANY_THROW(NodeNDRange.update_range(range<3>{32, 32, 1}));
auto NodeRange = Graph.add([&](sycl::handler &cgh) {
cgh.parallel_for<TestKernel<>>(range<1>{128}, [](item<1>) {});
});
// OK
EXPECT_NO_THROW(NodeRange.update_range(Range));
// OK to update a range node with an nd_range of the same dimension
EXPECT_NO_THROW(NodeRange.update_nd_range(NDRange));
// Can't update with a different number of dimensions
EXPECT_ANY_THROW(NodeRange.update_range(range<2>{128, 128}));
EXPECT_ANY_THROW(NodeRange.update_nd_range(
nd_range<3>{range<3>{8, 8, 8}, range<3>{8, 8, 8}}));
}
class WholeGraphUpdateTest : public CommandGraphTest {
protected:
static constexpr size_t Size = 1024;
WholeGraphUpdateTest()
: UpdateGraph{
Queue.get_context(),
Dev,
{experimental::property::graph::assume_buffer_outlives_graph{}}} {}
experimental::command_graph<experimental::graph_state::modifiable>
UpdateGraph;
std::function<void(::sycl::_V1::handler &)> EmptyKernel = [&](handler &CGH) {
CGH.parallel_for<TestKernel<>>(range<1>(Size), [=](item<1> Id) {});
};
};
TEST_F(WholeGraphUpdateTest, NoUpdates) {
// Test that using an update graph that has no updates is fine.
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
EmptyKernel, experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeD = UpdateGraph.add(
EmptyKernel,
experimental::property::node::depends_on(UpdateNodeB, UpdateNodeC));
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_NO_THROW(GraphExec.update(UpdateGraph));
}
TEST_F(WholeGraphUpdateTest, MoreNodes) {
// Test that using an update graph that has extra nodes results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
EmptyKernel, experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeD = UpdateGraph.add(
EmptyKernel,
experimental::property::node::depends_on(UpdateNodeB, UpdateNodeC));
// NodeE is the extra node
auto UpdateNodeE = UpdateGraph.add(EmptyKernel);
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, LessNodes) {
// Test that using an update graph that has less nodes results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
EmptyKernel, experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
// NodeD is missing in the update
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, ExtraEdges) {
// Test that using an update graph with extra nodes results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
EmptyKernel, experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeD = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(
UpdateNodeA, UpdateNodeB, UpdateNodeC /* Extra Edge */));
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, MissingEdges) {
// Test that using an update graph with missing edges results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
EmptyKernel, experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeD = UpdateGraph.add(
EmptyKernel,
experimental::property::node::depends_on(/* Missing Edge */ UpdateNodeB));
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, UnsupportedNodeType) {
// Test that using an update graph that contains unsupported node types
// results in an error.
buffer<int> Buffer{range<1>{1}};
auto NodeA = Graph.add(EmptyKernel);
auto NodeB =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeC =
Graph.add(EmptyKernel, experimental::property::node::depends_on(NodeA));
auto NodeD = Graph.add(
[&](handler &CGH) {
auto Acc = Buffer.get_access(CGH);
CGH.fill(Acc, 1);
},
experimental::property::node::depends_on(NodeB, NodeC));
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto UpdateNodeB = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeC = UpdateGraph.add(
EmptyKernel, experimental::property::node::depends_on(UpdateNodeA));
auto UpdateNodeD = UpdateGraph.add(
[&](handler &CGH) {
auto Acc = Buffer.get_access(CGH);
CGH.fill(Acc, 1);
},
experimental::property::node::depends_on(UpdateNodeB, UpdateNodeC));
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, WrongContext) {
// Test that using an update graph that was created with a different context
// (when compared to the original graph) results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
context OtherContext(Dev);
experimental::command_graph<experimental::graph_state::modifiable>
WrongContextGraph{
OtherContext,
Dev,
{experimental::property::graph::assume_buffer_outlives_graph{}}};
auto UpdateNodeA = WrongContextGraph.add(EmptyKernel);
EXPECT_THROW(GraphExec.update(WrongContextGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, WrongDevice) {
// Test that using an update graph that was created with a different device
// (when compared to the original graph) results in an error.
auto devices = device::get_devices();
if (devices.size() > 1) {
device &OtherDevice = (devices[0] == Dev ? devices[1] : devices[0]);
auto NodeA = Graph.add(EmptyKernel);
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
experimental::command_graph<experimental::graph_state::modifiable>
WrongDeviceGraph{
Queue.get_context(),
OtherDevice,
{experimental::property::graph::assume_buffer_outlives_graph{}}};
auto UpdateNodeA = WrongDeviceGraph.add(EmptyKernel);
EXPECT_THROW(GraphExec.update(WrongDeviceGraph), sycl::exception);
}
}
TEST_F(WholeGraphUpdateTest, MissingUpdatableProperty) {
// Test that updating a graph that was not created with the updatable property
// results in an error.
auto NodeA = Graph.add(EmptyKernel);
auto UpdateNodeA = UpdateGraph.add(EmptyKernel);
auto GraphExec = Graph.finalize();
EXPECT_THROW(GraphExec.update(UpdateGraph), sycl::exception);
}
TEST_F(WholeGraphUpdateTest, EmptyNode) {
// Test that updating a graph that has an empty node is not an error
auto NodeEmpty = Graph.add();
auto UpdateNodeEmpty = UpdateGraph.add();
auto NodeKernel = Graph.add(EmptyKernel);
auto UpdateNodeKernel = UpdateGraph.add(EmptyKernel);
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
GraphExec.update(UpdateGraph);
}
// Vars and callbacks for tracking how many times mocked functions are called
static int GetInfoCount = 0;
static int AppendKernelLaunchCount = 0;
static ur_result_t redefinedCommandBufferGetInfoExpAfter(void *pParams) {
GetInfoCount++;
return UR_RESULT_SUCCESS;
}
static ur_result_t
redefinedCommandBufferAppendKernelLaunchExpAfter(void *pParams) {
AppendKernelLaunchCount++;
return UR_RESULT_SUCCESS;
}
TEST_F(CommandGraphTest, CheckFinalizeBehavior) {
// Check that both finalize with and without updatable property work as
// expected
auto Node = Graph.add(
[&](sycl::handler &cgh) { cgh.single_task<TestKernel<>>([]() {}); });
mock::getCallbacks().set_after_callback(
"urCommandBufferGetInfoExp", &redefinedCommandBufferGetInfoExpAfter);
mock::getCallbacks().set_after_callback(
"urCommandBufferAppendKernelLaunchExp",
&redefinedCommandBufferAppendKernelLaunchExpAfter);
ASSERT_NO_THROW(Graph.finalize(experimental::property::graph::updatable{}));
// GetInfo and AppendKernelLaunch should be called once each time a node is
// added to a command buffer during finalization
ASSERT_EQ(GetInfoCount, 1);
ASSERT_EQ(AppendKernelLaunchCount, 1);
ASSERT_NO_THROW(Graph.finalize());
ASSERT_EQ(GetInfoCount, 2);
ASSERT_EQ(AppendKernelLaunchCount, 2);
}