|
| 1 | +// RUN: %{build} -fsycl-embed-ir -I . -o %t.out |
| 2 | +// RUN: %{run} %t.out |
| 3 | + |
| 4 | +#include "../../helpers.hpp" |
| 5 | +#include "../helpers.hpp" |
| 6 | +#include "support.h" |
| 7 | +#include <algorithm> |
| 8 | +#include <cassert> |
| 9 | +#include <iostream> |
| 10 | +#include <limits> |
| 11 | +#include <numeric> |
| 12 | +#include <sycl/sycl.hpp> |
| 13 | +#include <vector> |
| 14 | +using namespace sycl; |
| 15 | + |
| 16 | +// COM: Check exclusive_scan works with fusion |
| 17 | + |
| 18 | +template <class SpecializationKernelName, int TestNumber> |
| 19 | +class exclusive_scan_kernel; |
| 20 | + |
| 21 | +template <typename SpecializationKernelName, typename InputContainer, |
| 22 | + typename OutputContainer, class BinaryOperation> |
| 23 | +void test(queue q, InputContainer input, OutputContainer output, |
| 24 | + BinaryOperation binary_op, |
| 25 | + typename OutputContainer::value_type identity) { |
| 26 | + typedef typename InputContainer::value_type InputT; |
| 27 | + typedef typename OutputContainer::value_type OutputT; |
| 28 | + typedef class exclusive_scan_kernel<SpecializationKernelName, 0> kernel_name0; |
| 29 | + typedef class exclusive_scan_kernel<SpecializationKernelName, 1> kernel_name1; |
| 30 | + typedef class exclusive_scan_kernel<SpecializationKernelName, 2> kernel_name2; |
| 31 | + typedef class exclusive_scan_kernel<SpecializationKernelName, 3> kernel_name3; |
| 32 | + OutputT init = 42; |
| 33 | + size_t N = input.size(); |
| 34 | + size_t G = 64; |
| 35 | + std::vector<OutputT> expected(N); |
| 36 | + { |
| 37 | + buffer<InputT> in_buf(input.data(), input.size()); |
| 38 | + buffer<OutputT> out_buf(output.data(), output.size()); |
| 39 | + ext::codeplay::experimental::fusion_wrapper fw{q}; |
| 40 | + fw.start_fusion(); |
| 41 | + |
| 42 | + iota(q, in_buf, 0); |
| 43 | + |
| 44 | + q.submit([&](handler &cgh) { |
| 45 | + accessor in{in_buf, cgh, sycl::read_only}; |
| 46 | + accessor out{out_buf, cgh, sycl::write_only, sycl::no_init}; |
| 47 | + cgh.parallel_for<kernel_name0>(nd_range<1>(G, G), [=](nd_item<1> it) { |
| 48 | + group<1> g = it.get_group(); |
| 49 | + int lid = it.get_local_id(0); |
| 50 | + out[lid] = exclusive_scan_over_group(g, in[lid], binary_op); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + complete_fusion_with_check( |
| 55 | + fw, ext::codeplay::experimental::property::no_barriers{}); |
| 56 | + } |
| 57 | + emu::exclusive_scan(input.begin(), input.begin() + G, expected.begin(), |
| 58 | + identity, binary_op); |
| 59 | + assert(std::equal(output.begin(), output.begin() + G, expected.begin())); |
| 60 | + |
| 61 | + // Fill to test fusion again |
| 62 | + std::fill(input.begin(), input.end(), 0); |
| 63 | + |
| 64 | + { |
| 65 | + buffer<InputT> in_buf(input.data(), input.size()); |
| 66 | + buffer<OutputT> out_buf(output.data(), output.size()); |
| 67 | + |
| 68 | + ext::codeplay::experimental::fusion_wrapper fw{q}; |
| 69 | + fw.start_fusion(); |
| 70 | + |
| 71 | + iota(q, in_buf, 0); |
| 72 | + |
| 73 | + q.submit([&](handler &cgh) { |
| 74 | + accessor in{in_buf, cgh, sycl::read_only}; |
| 75 | + accessor out{out_buf, cgh, sycl::write_only, sycl::no_init}; |
| 76 | + cgh.parallel_for<kernel_name1>(nd_range<1>(G, G), [=](nd_item<1> it) { |
| 77 | + group<1> g = it.get_group(); |
| 78 | + int lid = it.get_local_id(0); |
| 79 | + out[lid] = exclusive_scan_over_group(g, in[lid], init, binary_op); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + complete_fusion_with_check( |
| 84 | + fw, ext::codeplay::experimental::property::no_barriers{}); |
| 85 | + } |
| 86 | + emu::exclusive_scan(input.begin(), input.begin() + G, expected.begin(), init, |
| 87 | + binary_op); |
| 88 | + assert(std::equal(output.begin(), output.begin() + G, expected.begin())); |
| 89 | + |
| 90 | + // Fill to test fusion again |
| 91 | + std::fill(input.begin(), input.end(), 0); |
| 92 | + |
| 93 | + { |
| 94 | + buffer<InputT> in_buf(input.data(), input.size()); |
| 95 | + buffer<OutputT> out_buf(output.data(), output.size()); |
| 96 | + |
| 97 | + ext::codeplay::experimental::fusion_wrapper fw{q}; |
| 98 | + fw.start_fusion(); |
| 99 | + |
| 100 | + iota(q, in_buf, 0); |
| 101 | + |
| 102 | + q.submit([&](handler &cgh) { |
| 103 | + accessor in{in_buf, cgh, sycl::read_only}; |
| 104 | + accessor out{out_buf, cgh, sycl::write_only, sycl::no_init}; |
| 105 | + cgh.parallel_for<kernel_name2>(nd_range<1>(G, G), [=](nd_item<1> it) { |
| 106 | + group<1> g = it.get_group(); |
| 107 | + joint_exclusive_scan( |
| 108 | + g, in.template get_multi_ptr<access::decorated::no>(), |
| 109 | + in.template get_multi_ptr<access::decorated::no>() + N, |
| 110 | + out.template get_multi_ptr<access::decorated::no>(), binary_op); |
| 111 | + }); |
| 112 | + }); |
| 113 | + complete_fusion_with_check( |
| 114 | + fw, ext::codeplay::experimental::property::no_barriers{}); |
| 115 | + } |
| 116 | + emu::exclusive_scan(input.begin(), input.begin() + N, expected.begin(), |
| 117 | + identity, binary_op); |
| 118 | + assert(std::equal(output.begin(), output.begin() + N, expected.begin())); |
| 119 | + |
| 120 | + // Fill to test fusion again |
| 121 | + std::fill(input.begin(), input.end(), 0); |
| 122 | + |
| 123 | + { |
| 124 | + buffer<InputT> in_buf(input.data(), input.size()); |
| 125 | + buffer<OutputT> out_buf(output.data(), output.size()); |
| 126 | + ext::codeplay::experimental::fusion_wrapper fw{q}; |
| 127 | + fw.start_fusion(); |
| 128 | + |
| 129 | + iota(q, in_buf, 0); |
| 130 | + |
| 131 | + q.submit([&](handler &cgh) { |
| 132 | + accessor in{in_buf, cgh, sycl::read_only}; |
| 133 | + accessor out{out_buf, cgh, sycl::write_only, sycl::no_init}; |
| 134 | + cgh.parallel_for<kernel_name3>(nd_range<1>(G, G), [=](nd_item<1> it) { |
| 135 | + group<1> g = it.get_group(); |
| 136 | + joint_exclusive_scan( |
| 137 | + g, in.template get_multi_ptr<access::decorated::no>(), |
| 138 | + in.template get_multi_ptr<access::decorated::no>() + N, |
| 139 | + out.template get_multi_ptr<access::decorated::no>(), init, |
| 140 | + binary_op); |
| 141 | + }); |
| 142 | + }); |
| 143 | + complete_fusion_with_check( |
| 144 | + fw, ext::codeplay::experimental::property::no_barriers{}); |
| 145 | + } |
| 146 | + emu::exclusive_scan(input.begin(), input.begin() + N, expected.begin(), init, |
| 147 | + binary_op); |
| 148 | + assert(std::equal(output.begin(), output.begin() + N, expected.begin())); |
| 149 | +} |
| 150 | + |
| 151 | +int main() { |
| 152 | + queue q{ext::codeplay::experimental::property::queue::enable_fusion{}}; |
| 153 | + if (!isSupportedDevice(q.get_device())) { |
| 154 | + std::cout << "Skipping test\n"; |
| 155 | + return 0; |
| 156 | + } |
| 157 | + |
| 158 | + constexpr int N = 128; |
| 159 | + std::array<int, N> input; |
| 160 | + std::array<int, N> output; |
| 161 | + std::fill(output.begin(), output.end(), 0); |
| 162 | + |
| 163 | + test<class KernelNamePlusV>(q, input, output, sycl::plus<>(), 0); |
| 164 | + test<class KernelNameMinimumV>(q, input, output, sycl::minimum<>(), |
| 165 | + std::numeric_limits<int>::max()); |
| 166 | + test<class KernelNameMaximumV>(q, input, output, sycl::maximum<>(), |
| 167 | + std::numeric_limits<int>::lowest()); |
| 168 | + |
| 169 | + test<class KernelNamePlusI>(q, input, output, sycl::plus<int>(), 0); |
| 170 | + test<class KernelNameMinimumI>(q, input, output, sycl::minimum<int>(), |
| 171 | + std::numeric_limits<int>::max()); |
| 172 | + test<class KernelNameMaximumI>(q, input, output, sycl::maximum<int>(), |
| 173 | + std::numeric_limits<int>::lowest()); |
| 174 | + test<class KernelName_VzAPutpBRRJrQPB>(q, input, output, |
| 175 | + sycl::multiplies<int>(), 1); |
| 176 | + test<class KernelName_UXdGbr>(q, input, output, sycl::bit_or<int>(), 0); |
| 177 | + test<class KernelName_saYaodNyJknrPW>(q, input, output, sycl::bit_xor<int>(), |
| 178 | + 0); |
| 179 | + test<class KernelName_GPcuAlvAOjrDyP>(q, input, output, sycl::bit_and<int>(), |
| 180 | + ~0); |
| 181 | + |
| 182 | + std::cout << "Test passed." << std::endl; |
| 183 | +} |
0 commit comments