|
| 1 | +// ============================================================== |
| 2 | +// Copyright © 2025 Codeplay Software |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// ============================================================= |
| 6 | + |
| 7 | +#include "common/aspect_queries.hpp" |
| 8 | + |
| 9 | +#include <sycl/sycl.hpp> |
| 10 | + |
| 11 | +namespace sycl_ext = sycl::ext::oneapi::experimental; |
| 12 | +using namespace sycl; |
| 13 | + |
| 14 | +int main() { |
| 15 | + constexpr size_t Size = 1024; |
| 16 | + |
| 17 | + queue Queue{}; |
| 18 | + |
| 19 | + ensure_graph_support(Queue.get_device()); |
| 20 | + |
| 21 | + std::vector<int> DataA(Size), DataB(Size), DataC(Size); |
| 22 | + |
| 23 | + // Lifetime of buffers must exceed the lifetime of graphs they are used in. |
| 24 | + buffer<int> BufferA{DataA.data(), range<1>{Size}}; |
| 25 | + BufferA.set_write_back(false); |
| 26 | + buffer<int> BufferB{DataB.data(), range<1>{Size}}; |
| 27 | + BufferB.set_write_back(false); |
| 28 | + buffer<int> BufferC{DataC.data(), range<1>{Size}}; |
| 29 | + BufferC.set_write_back(false); |
| 30 | + |
| 31 | + { |
| 32 | + // New object representing graph of command-groups |
| 33 | + sycl_ext::command_graph Graph( |
| 34 | + Queue.get_context(), Queue.get_device(), |
| 35 | + {sycl_ext::property::graph::assume_buffer_outlives_graph{}}); |
| 36 | + |
| 37 | + // `Queue` will be put in the recording state where commands are recorded to |
| 38 | + // `Graph` rather than submitted for execution immediately. |
| 39 | + Graph.begin_recording(Queue); |
| 40 | + |
| 41 | + // Record commands to `Graph` with the following topology. |
| 42 | + // |
| 43 | + // increment_kernel |
| 44 | + // / \ |
| 45 | + // A->/ A->\ |
| 46 | + // / \ |
| 47 | + // add_kernel subtract_kernel |
| 48 | + // \ / |
| 49 | + // B->\ C->/ |
| 50 | + // \ / |
| 51 | + // decrement_kernel |
| 52 | + |
| 53 | + Queue.submit([&](handler &CGH) { |
| 54 | + auto Pdata = BufferA.get_access<access::mode::read_write>(CGH); |
| 55 | + CGH.parallel_for<class Increment_kernel>( |
| 56 | + range<1>(Size), [=](item<1> Id) { Pdata[Id]++; }); |
| 57 | + }); |
| 58 | + |
| 59 | + Queue.submit([&](handler &CGH) { |
| 60 | + auto Pdata1 = BufferA.get_access<access::mode::read>(CGH); |
| 61 | + auto Pdata2 = BufferB.get_access<access::mode::read_write>(CGH); |
| 62 | + CGH.parallel_for<class Add_kernel>( |
| 63 | + range<1>(Size), [=](item<1> Id) { Pdata2[Id] += Pdata1[Id]; }); |
| 64 | + }); |
| 65 | + |
| 66 | + Queue.submit([&](handler &CGH) { |
| 67 | + auto Pdata1 = BufferA.get_access<access::mode::read>(CGH); |
| 68 | + auto Pdata2 = BufferC.get_access<access::mode::read_write>(CGH); |
| 69 | + CGH.parallel_for<class Subtract_kernel>( |
| 70 | + range<1>(Size), [=](item<1> Id) { Pdata2[Id] -= Pdata1[Id]; }); |
| 71 | + }); |
| 72 | + |
| 73 | + Queue.submit([&](handler &CGH) { |
| 74 | + auto Pdata1 = BufferB.get_access<access::mode::read_write>(CGH); |
| 75 | + auto Pdata2 = BufferC.get_access<access::mode::read_write>(CGH); |
| 76 | + CGH.parallel_for<class Decrement_kernel>(range<1>(Size), [=](item<1> Id) { |
| 77 | + Pdata1[Id]--; |
| 78 | + Pdata2[Id]--; |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // `Queue` will be returned to the executing state where commands are |
| 83 | + // submitted immediately for extension. |
| 84 | + Graph.end_recording(); |
| 85 | + |
| 86 | + // Finalize the modifiable graph to create an executable graph that can be |
| 87 | + // submitted for execution. |
| 88 | + auto Exec_graph = Graph.finalize(); |
| 89 | + |
| 90 | + // Execute graph |
| 91 | + Queue.submit([&](handler &CGH) { CGH.ext_oneapi_graph(Exec_graph); }) |
| 92 | + .wait(); |
| 93 | + } |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments