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