|
| 1 | +// This test is intended to check basic operations with SYCL 2020 specialization |
| 2 | +// constants using Graph and sycl::handler and sycl::kernel_handler APIs |
| 3 | +// This test was taken from `SpecConstants/2020/handler-api.cpp`. |
| 4 | +// Variable names have been changed to meet PascalCase naming convention |
| 5 | +// requirements. |
| 6 | + |
| 7 | +#include "../graph_common.hpp" |
| 8 | + |
| 9 | +constexpr sycl::specialization_id<int> IntId; |
| 10 | +constexpr sycl::specialization_id<int> IntId2(2); |
| 11 | +constexpr sycl::specialization_id<float> FloatId(3.14); |
| 12 | + |
| 13 | +class TestDefaultValuesKernel; |
| 14 | +class EmptyKernel; |
| 15 | +class TestSetAndGetOnDevice; |
| 16 | + |
| 17 | +bool test_default_values(sycl::queue Queue); |
| 18 | +bool test_set_and_get_on_host(sycl::queue Queue); |
| 19 | +bool test_set_and_get_on_device(sycl::queue Queue); |
| 20 | + |
| 21 | +bool test_set_and_get_on_device(sycl::queue Queue); |
| 22 | + |
| 23 | +int main() { |
| 24 | + auto ExceptionHandler = [&](sycl::exception_list Exceptions) { |
| 25 | + for (std::exception_ptr const &E : Exceptions) { |
| 26 | + try { |
| 27 | + std::rethrow_exception(E); |
| 28 | + } catch (sycl::exception const &E) { |
| 29 | + std::cout << "An async SYCL exception was caught: " << E.what() |
| 30 | + << std::endl; |
| 31 | + std::exit(1); |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + queue Queue{ExceptionHandler, |
| 37 | + {sycl::ext::intel::property::queue::no_immediate_command_list{}}}; |
| 38 | + |
| 39 | + unsigned Errors = 0; |
| 40 | + if (!test_default_values(Queue)) { |
| 41 | + std::cout << "Test for default values of specialization constants failed!" |
| 42 | + << std::endl; |
| 43 | + Errors++; |
| 44 | + } |
| 45 | + |
| 46 | + if (!test_set_and_get_on_host(Queue)) { |
| 47 | + std::cout << "Test for set and get API on host failed!" << std::endl; |
| 48 | + Errors++; |
| 49 | + } |
| 50 | + |
| 51 | + if (!test_set_and_get_on_device(Queue)) { |
| 52 | + std::cout << "Test for set and get API on device failed!" << std::endl; |
| 53 | + Errors++; |
| 54 | + } |
| 55 | + |
| 56 | + return (Errors == 0) ? 0 : 1; |
| 57 | +}; |
| 58 | + |
| 59 | +bool test_default_values(sycl::queue Queue) { |
| 60 | + sycl::buffer<int> IntBuffer(1); |
| 61 | + IntBuffer.set_write_back(false); |
| 62 | + sycl::buffer<int> IntBuffer2(1); |
| 63 | + IntBuffer2.set_write_back(false); |
| 64 | + sycl::buffer<float> FloatBuffer(1); |
| 65 | + FloatBuffer.set_write_back(false); |
| 66 | + |
| 67 | + { |
| 68 | + exp_ext::command_graph Graph{ |
| 69 | + Queue.get_context(), |
| 70 | + Queue.get_device(), |
| 71 | + {exp_ext::property::graph::assume_buffer_outlives_graph{}}}; |
| 72 | + |
| 73 | + add_node(Graph, Queue, ([&](sycl::handler &CGH) { |
| 74 | + auto IntAcc = |
| 75 | + IntBuffer.get_access<sycl::access::mode::write>(CGH); |
| 76 | + auto IntAcc2 = |
| 77 | + IntBuffer2.get_access<sycl::access::mode::write>(CGH); |
| 78 | + auto FloatAcc = |
| 79 | + FloatBuffer.get_access<sycl::access::mode::write>(CGH); |
| 80 | + |
| 81 | + CGH.single_task<TestDefaultValuesKernel>( |
| 82 | + [=](sycl::kernel_handler KH) { |
| 83 | + IntAcc[0] = KH.get_specialization_constant<IntId>(); |
| 84 | + IntAcc2[0] = KH.get_specialization_constant<IntId2>(); |
| 85 | + FloatAcc[0] = KH.get_specialization_constant<FloatId>(); |
| 86 | + }); |
| 87 | + })); |
| 88 | + |
| 89 | + auto GraphExec = Graph.finalize(); |
| 90 | + |
| 91 | + Queue.submit([&](handler &CGH) { CGH.ext_oneapi_graph(GraphExec); }); |
| 92 | + Queue.wait_and_throw(); |
| 93 | + } |
| 94 | + |
| 95 | + unsigned Errors = 0; |
| 96 | + sycl::host_accessor IntAcc(IntBuffer, sycl::read_only); |
| 97 | + if (!check_value( |
| 98 | + 0, IntAcc[0], |
| 99 | + "integer specialization constant (defined without default value)")) |
| 100 | + Errors++; |
| 101 | + |
| 102 | + sycl::host_accessor IntAcc2(IntBuffer2, sycl::read_only); |
| 103 | + if (!check_value(2, IntAcc2[0], "integer specialization constant")) |
| 104 | + Errors++; |
| 105 | + |
| 106 | + sycl::host_accessor FloatAcc(FloatBuffer, sycl::read_only); |
| 107 | + if (!check_value(3.14f, FloatAcc[0], "float specialization constant")) |
| 108 | + Errors++; |
| 109 | + |
| 110 | + return Errors == 0; |
| 111 | +} |
| 112 | + |
| 113 | +bool test_set_and_get_on_host(sycl::queue Queue) { |
| 114 | + unsigned Errors = 0; |
| 115 | + |
| 116 | + exp_ext::command_graph Graph{ |
| 117 | + Queue.get_context(), |
| 118 | + Queue.get_device(), |
| 119 | + {exp_ext::property::graph::assume_buffer_outlives_graph{}}}; |
| 120 | + |
| 121 | + add_node( |
| 122 | + Graph, Queue, ([&](sycl::handler &CGH) { |
| 123 | + if (!check_value( |
| 124 | + 0, CGH.get_specialization_constant<IntId>(), |
| 125 | + "integer specializaiton constant before setting any value")) |
| 126 | + ++Errors; |
| 127 | + |
| 128 | + if (!check_value( |
| 129 | + 3.14f, CGH.get_specialization_constant<FloatId>(), |
| 130 | + "float specializaiton constant before setting any value")) |
| 131 | + ++Errors; |
| 132 | + |
| 133 | + int NewIntValue = 8; |
| 134 | + float NewFloatValue = 3.0f; |
| 135 | + CGH.set_specialization_constant<IntId>(NewIntValue); |
| 136 | + CGH.set_specialization_constant<FloatId>(NewFloatValue); |
| 137 | + |
| 138 | + if (!check_value( |
| 139 | + NewIntValue, CGH.get_specialization_constant<IntId>(), |
| 140 | + "integer specializaiton constant after setting a new value")) |
| 141 | + ++Errors; |
| 142 | + |
| 143 | + if (!check_value( |
| 144 | + NewFloatValue, CGH.get_specialization_constant<FloatId>(), |
| 145 | + "float specializaiton constant after setting a new value")) |
| 146 | + ++Errors; |
| 147 | + |
| 148 | + CGH.single_task<EmptyKernel>([=]() {}); |
| 149 | + })); |
| 150 | + |
| 151 | + return Errors == 0; |
| 152 | +} |
| 153 | + |
| 154 | +bool test_set_and_get_on_device(sycl::queue Queue) { |
| 155 | + sycl::buffer<int> IntBuffer(1); |
| 156 | + IntBuffer.set_write_back(false); |
| 157 | + sycl::buffer<int> IntBuffer2(1); |
| 158 | + IntBuffer2.set_write_back(false); |
| 159 | + sycl::buffer<float> FloatBuffer(1); |
| 160 | + FloatBuffer.set_write_back(false); |
| 161 | + |
| 162 | + int NewIntValue = 8; |
| 163 | + int NewIntValue2 = 0; |
| 164 | + float NewFloatValue = 3.0f; |
| 165 | + |
| 166 | + { |
| 167 | + exp_ext::command_graph Graph{ |
| 168 | + Queue.get_context(), |
| 169 | + Queue.get_device(), |
| 170 | + {exp_ext::property::graph::assume_buffer_outlives_graph{}}}; |
| 171 | + |
| 172 | + add_node( |
| 173 | + Graph, Queue, ([&](sycl::handler &CGH) { |
| 174 | + auto IntAcc = IntBuffer.get_access<sycl::access::mode::write>(CGH); |
| 175 | + auto IntAcc2 = IntBuffer2.get_access<sycl::access::mode::write>(CGH); |
| 176 | + auto FloatAcc = |
| 177 | + FloatBuffer.get_access<sycl::access::mode::write>(CGH); |
| 178 | + |
| 179 | + CGH.set_specialization_constant<IntId>(NewIntValue); |
| 180 | + CGH.set_specialization_constant<IntId2>(NewIntValue2); |
| 181 | + CGH.set_specialization_constant<FloatId>(NewFloatValue); |
| 182 | + |
| 183 | + CGH.single_task<TestSetAndGetOnDevice>([=](sycl::kernel_handler KH) { |
| 184 | + IntAcc[0] = KH.get_specialization_constant<IntId>(); |
| 185 | + IntAcc2[0] = KH.get_specialization_constant<IntId2>(); |
| 186 | + FloatAcc[0] = KH.get_specialization_constant<FloatId>(); |
| 187 | + }); |
| 188 | + })); |
| 189 | + |
| 190 | + auto GraphExec = Graph.finalize(); |
| 191 | + |
| 192 | + Queue.submit([&](handler &CGH) { CGH.ext_oneapi_graph(GraphExec); }); |
| 193 | + Queue.wait_and_throw(); |
| 194 | + } |
| 195 | + |
| 196 | + unsigned Errors = 0; |
| 197 | + sycl::host_accessor IntAcc(IntBuffer, sycl::read_only); |
| 198 | + if (!check_value(NewIntValue, IntAcc[0], "integer specialization constant")) |
| 199 | + Errors++; |
| 200 | + |
| 201 | + sycl::host_accessor IntAcc2(IntBuffer2, sycl::read_only); |
| 202 | + if (!check_value(NewIntValue2, IntAcc2[0], "integer specialization constant")) |
| 203 | + Errors++; |
| 204 | + |
| 205 | + sycl::host_accessor FloatAcc(FloatBuffer, sycl::read_only); |
| 206 | + if (!check_value(NewFloatValue, FloatAcc[0], "float specialization constant")) |
| 207 | + Errors++; |
| 208 | + |
| 209 | + return Errors == 0; |
| 210 | +} |
0 commit comments