|
| 1 | +// Tests whole graph update of nodes with the work_group_memory extension |
| 2 | + |
| 3 | +#include "../graph_common.hpp" |
| 4 | +#include <sycl/ext/oneapi/experimental/work_group_memory.hpp> |
| 5 | + |
| 6 | +auto add_graph_node( |
| 7 | + exp_ext::command_graph<exp_ext::graph_state::modifiable> &Graph, |
| 8 | + queue &Queue, size_t LocalSize, int *Ptr) { |
| 9 | + return add_node(Graph, Queue, [&](handler &CGH) { |
| 10 | + exp_ext::work_group_memory<int[]> WGMem{LocalSize, CGH}; |
| 11 | + |
| 12 | + CGH.parallel_for(nd_range({Size}, {LocalSize}), [=](nd_item<1> Item) { |
| 13 | + WGMem[Item.get_local_linear_id()] = Item.get_global_linear_id() * 2; |
| 14 | + Ptr[Item.get_global_linear_id()] += |
| 15 | + WGMem[Item.get_local_linear_id()] + Item.get_local_range(0); |
| 16 | + }); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +int main() { |
| 21 | + queue Queue{}; |
| 22 | + |
| 23 | + const size_t LocalSize = 128; |
| 24 | + |
| 25 | + std::vector<int> DataA(Size), DataB(Size); |
| 26 | + |
| 27 | + std::iota(DataA.begin(), DataA.end(), 10); |
| 28 | + std::iota(DataB.begin(), DataB.end(), 10); |
| 29 | + |
| 30 | + exp_ext::command_graph GraphA{Queue.get_context(), Queue.get_device()}; |
| 31 | + |
| 32 | + int *PtrA = malloc_device<int>(Size, Queue); |
| 33 | + int *PtrB = malloc_device<int>(Size, Queue); |
| 34 | + |
| 35 | + Queue.copy(DataA.data(), PtrA, Size); |
| 36 | + Queue.copy(DataB.data(), PtrB, Size); |
| 37 | + Queue.wait_and_throw(); |
| 38 | + |
| 39 | + auto NodeA = add_graph_node(GraphA, Queue, LocalSize / 2, PtrA); |
| 40 | + |
| 41 | + auto GraphExecA = GraphA.finalize(exp_ext::property::graph::updatable{}); |
| 42 | + |
| 43 | + // Create second graph for whole graph update with a different local size |
| 44 | + exp_ext::command_graph GraphB{Queue.get_context(), Queue.get_device()}; |
| 45 | + auto NodeB = add_graph_node(GraphB, Queue, LocalSize, PtrB); |
| 46 | + |
| 47 | + // Execute graphs before updating and check outputs |
| 48 | + for (unsigned n = 0; n < Iterations; n++) { |
| 49 | + Queue.submit([&](handler &CGH) { CGH.ext_oneapi_graph(GraphExecA); }); |
| 50 | + } |
| 51 | + |
| 52 | + Queue.wait_and_throw(); |
| 53 | + |
| 54 | + Queue.copy(PtrA, DataA.data(), Size); |
| 55 | + Queue.copy(PtrB, DataB.data(), Size); |
| 56 | + Queue.wait_and_throw(); |
| 57 | + |
| 58 | + for (size_t i = 0; i < Size; i++) { |
| 59 | + int RefA = 10 + i + Iterations * ((i * 2) + (LocalSize / 2)); |
| 60 | + int RefB = 10 + i; |
| 61 | + assert(check_value(i, RefA, DataA[i], "PtrA")); |
| 62 | + assert(check_value(i, RefB, DataB[i], "PtrB")); |
| 63 | + } |
| 64 | + |
| 65 | + // Update GraphExecA using whole graph update |
| 66 | + |
| 67 | + GraphExecA.update(GraphB); |
| 68 | + |
| 69 | + // Execute graphs again and check outputs |
| 70 | + for (unsigned N = 0; N < Iterations; N++) { |
| 71 | + Queue.submit([&](handler &CGH) { CGH.ext_oneapi_graph(GraphExecA); }); |
| 72 | + } |
| 73 | + |
| 74 | + Queue.wait_and_throw(); |
| 75 | + |
| 76 | + Queue.copy(PtrA, DataA.data(), Size); |
| 77 | + Queue.copy(PtrB, DataB.data(), Size); |
| 78 | + Queue.wait_and_throw(); |
| 79 | + |
| 80 | + for (size_t i = 0; i < Size; i++) { |
| 81 | + int RefA = 10 + i + Iterations * ((i * 2) + (LocalSize / 2)); |
| 82 | + int RefB = 10 + i + Iterations * ((i * 2) + LocalSize); |
| 83 | + assert(check_value(i, RefA, DataA[i], "PtrA")); |
| 84 | + assert(check_value(i, RefB, DataB[i], "PtrB")); |
| 85 | + } |
| 86 | + |
| 87 | + free(PtrA, Queue); |
| 88 | + free(PtrB, Queue); |
| 89 | + return 0; |
| 90 | +} |
0 commit comments