|
| 1 | +// ====------ onedpl_test_group_load.cpp------------ *- C++ -* ----===// |
| 2 | + |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +// |
| 9 | +// ===----------------------------------------------------------------------===// |
| 10 | +#include <dpct/dpct.hpp> |
| 11 | +#include <dpct/dpl_utils.hpp> |
| 12 | +#include <iostream> |
| 13 | +#include <oneapi/dpl/iterator> |
| 14 | +#include <sycl/sycl.hpp> |
| 15 | + |
| 16 | +template <dpct::group::load_algorithm T> |
| 17 | +bool helper_validation_function(const int *ptr, const char *func_name) { |
| 18 | + if constexpr (T == dpct::group::load_algorithm::BLOCK_LOAD_DIRECT) { |
| 19 | + for (int i = 0; i < 512; ++i) { |
| 20 | + if (ptr[i] != i) { |
| 21 | + std::cout << func_name << "_blocked" |
| 22 | + << " failed\n"; |
| 23 | + std::ostream_iterator<int> Iter(std::cout, ", "); |
| 24 | + std::copy(ptr, ptr + 512, Iter); |
| 25 | + std::cout << std::endl; |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + std::cout << func_name << "_blocked" |
| 30 | + << " pass\n"; |
| 31 | + } else { |
| 32 | + int expected[512]; |
| 33 | + int num_threads = 128; |
| 34 | + int items_per_thread = 4; |
| 35 | + for (int i = 0; i < num_threads; ++i) { |
| 36 | + for (int j = 0; j < items_per_thread; ++j) { |
| 37 | + expected[i * items_per_thread + j] = j * num_threads + i; |
| 38 | + } |
| 39 | + } |
| 40 | + for (int i = 0; i < 512; ++i) { |
| 41 | + if (ptr[i] != expected[i]) { |
| 42 | + std::cout << func_name << "_striped" |
| 43 | + << " failed\n"; |
| 44 | + std::ostream_iterator<int> Iter(std::cout, ", "); |
| 45 | + std::copy(ptr, ptr + 512, Iter); |
| 46 | + std::cout << std::endl; |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + std::cout << func_name << "_striped" |
| 51 | + << " pass\n"; |
| 52 | + } |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +bool subgroup_helper_validation_function(const int *ptr, const uint32_t *sg_sz, |
| 57 | + const char *func_name) { |
| 58 | + int expected[512]; |
| 59 | + int num_threads = 128; |
| 60 | + int items_per_thread = 4; |
| 61 | + uint32_t sg_sz_val = *sg_sz; |
| 62 | + for (int i = 0; i < num_threads; ++i) { |
| 63 | + for (int j = 0; j < items_per_thread; ++j) { |
| 64 | + expected[items_per_thread * i + j] = |
| 65 | + (i / sg_sz_val) * sg_sz_val * items_per_thread + sg_sz_val * j + |
| 66 | + i % sg_sz_val; |
| 67 | + } |
| 68 | + } |
| 69 | + for (int i = 0; i < 512; ++i) { |
| 70 | + if (ptr[i] != expected[i]) { |
| 71 | + std::cout << " failed\n"; |
| 72 | + std::ostream_iterator<int> Iter(std::cout, ", "); |
| 73 | + std::copy(ptr, ptr + 512, Iter); |
| 74 | + std::cout << std::endl; |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + std::cout << func_name << " pass\n"; |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +template <dpct::group::load_algorithm T> bool test_group_load() { |
| 84 | + // Tests dpct::group::load_algorithm::BLOCK_LOAD_DIRECT & |
| 85 | + // dpct::group::load_algorithm::BLOCK_LOAD_STRIPED in its entirety as API |
| 86 | + // functions |
| 87 | + sycl::queue q(dpct::get_default_queue()); |
| 88 | + oneapi::dpl::counting_iterator<int> count_it(0); |
| 89 | + sycl::buffer<int, 1> buffer(count_it, count_it + 512); |
| 90 | + int data_out[512]; |
| 91 | + for (int i = 0; i < 512; i++) |
| 92 | + data_out[i] = 0; |
| 93 | + sycl::buffer<int, 1> buffer_out(data_out, 512); |
| 94 | + |
| 95 | + q.submit([&](sycl::handler &h) { |
| 96 | + using group_load = |
| 97 | + dpct::group::workgroup_load<4, T, int, const int *, sycl::nd_item<3>>; |
| 98 | + size_t temp_storage_size = group_load::get_local_memory_size(128); |
| 99 | + sycl::local_accessor<uint8_t, 1> tacc(sycl::range<1>(temp_storage_size), h); |
| 100 | + sycl::accessor data_accessor_read(buffer, h, sycl::read_only); |
| 101 | + sycl::accessor data_accessor_write(buffer_out, h, sycl::write_only); |
| 102 | + h.parallel_for( |
| 103 | + sycl::nd_range<3>(sycl::range<3>(1, 1, 128), sycl::range<3>(1, 1, 128)), |
| 104 | + [=](sycl::nd_item<3> item) { |
| 105 | + int thread_data[4]; |
| 106 | + auto *d_r = |
| 107 | + data_accessor_read.get_multi_ptr<sycl::access::decorated::yes>() |
| 108 | + .get(); |
| 109 | + auto *tmp = tacc.get_multi_ptr<sycl::access::decorated::yes>().get(); |
| 110 | + group_load(tmp).load(item, d_r, thread_data); |
| 111 | + // Write thread_data of each work item at index to the global buffer |
| 112 | + int global_index = |
| 113 | + item.get_group(2) * item.get_local_range().get(2) + |
| 114 | + item.get_local_id(2); // Each thread_data has 4 elements |
| 115 | +#pragma unroll |
| 116 | + for (int i = 0; i < 4; ++i) { |
| 117 | + data_accessor_write[global_index * 4 + i] = thread_data[i]; |
| 118 | + } |
| 119 | + }); |
| 120 | + }); |
| 121 | + q.wait_and_throw(); |
| 122 | + |
| 123 | + sycl::host_accessor data_accessor(buffer_out, sycl::read_only); |
| 124 | + const int *ptr = data_accessor.get_multi_ptr<sycl::access::decorated::yes>(); |
| 125 | + return helper_validation_function<T>(ptr, "test_group_load"); |
| 126 | +} |
| 127 | + |
| 128 | +bool test_load_subgroup_striped_standalone() { |
| 129 | + // Tests dpct::group::load_subgroup_striped as standalone method |
| 130 | + sycl::queue q(dpct::get_default_queue()); |
| 131 | + int data[512]; |
| 132 | + for (int i = 0; i < 512; i++) |
| 133 | + data[i] = i; |
| 134 | + sycl::buffer<int, 1> buffer(data, 512); |
| 135 | + sycl::buffer<uint32_t, 1> sg_sz_buf{sycl::range<1>(1)}; |
| 136 | + int data_out[512]; |
| 137 | + for (int i = 0; i < 512; i++) |
| 138 | + data_out[i] = 0; |
| 139 | + sycl::buffer<int, 1> buffer_out(data_out, 512); |
| 140 | + |
| 141 | + q.submit([&](sycl::handler &h) { |
| 142 | + sycl::accessor dacc_read(buffer, h, sycl::read_only); |
| 143 | + sycl::accessor dacc_write(buffer_out, h, sycl::write_only); |
| 144 | + sycl::accessor sg_sz_dacc(sg_sz_buf, h, sycl::read_write); |
| 145 | + h.parallel_for( |
| 146 | + sycl::nd_range<3>(sycl::range<3>(1, 1, 128), sycl::range<3>(1, 1, 128)), |
| 147 | + [=](sycl::nd_item<3> item) { |
| 148 | + int thread_data[4]; |
| 149 | + auto *d_r = |
| 150 | + dacc_read.get_multi_ptr<sycl::access::decorated::yes>().get(); |
| 151 | + auto *sg_sz_acc = |
| 152 | + sg_sz_dacc.get_multi_ptr<sycl::access::decorated::yes>().get(); |
| 153 | + size_t gid = item.get_global_linear_id(); |
| 154 | + if (gid == 0) { |
| 155 | + sg_sz_acc[0] = item.get_sub_group().get_local_linear_range(); |
| 156 | + } |
| 157 | + dpct::group::uninitialized_load_subgroup_striped<4, int>(item, d_r, |
| 158 | + thread_data); |
| 159 | + // Write thread_data of each work item at index to the global buffer |
| 160 | + int global_index = |
| 161 | + (item.get_group(2) * item.get_local_range().get(2)) + |
| 162 | + item.get_local_id(2); // Each thread_data has 4 elements |
| 163 | +#pragma unroll |
| 164 | + for (int i = 0; i < 4; ++i) { |
| 165 | + dacc_write[global_index * 4 + i] = thread_data[i]; |
| 166 | + } |
| 167 | + }); |
| 168 | + }); |
| 169 | + q.wait_and_throw(); |
| 170 | + |
| 171 | + sycl::host_accessor data_accessor(buffer_out, sycl::read_only); |
| 172 | + const int *ptr = data_accessor.get_multi_ptr<sycl::access::decorated::yes>(); |
| 173 | + sycl::host_accessor data_accessor_sg(sg_sz_buf, sycl::read_only); |
| 174 | + const uint32_t *ptr_sg = |
| 175 | + data_accessor_sg.get_multi_ptr<sycl::access::decorated::yes>(); |
| 176 | + return subgroup_helper_validation_function( |
| 177 | + ptr, ptr_sg, "test_subgroup_striped_standalone"); |
| 178 | +} |
| 179 | + |
| 180 | +template <dpct::group::load_algorithm T> bool test_group_load_standalone() { |
| 181 | + // Tests dpct::group::load_algorithm::BLOCK_LOAD_DIRECT & |
| 182 | + // dpct::group::load_algorithm::BLOCK_LOAD_STRIPED as standalone methods |
| 183 | + sycl::queue q(dpct::get_default_queue()); |
| 184 | + int data[512]; |
| 185 | + for (int i = 0; i < 512; i++) |
| 186 | + data[i] = i; |
| 187 | + sycl::buffer<int, 1> buffer(data, 512); |
| 188 | + int data_out[512]; |
| 189 | + for (int i = 0; i < 512; i++) |
| 190 | + data_out[i] = 0; |
| 191 | + sycl::buffer<int, 1> buffer_out(data_out, 512); |
| 192 | + |
| 193 | + q.submit([&](sycl::handler &h) { |
| 194 | + sycl::accessor dacc_read(buffer, h, sycl::read_only); |
| 195 | + sycl::accessor dacc_write(buffer_out, h, sycl::write_only); |
| 196 | + h.parallel_for( |
| 197 | + sycl::nd_range<3>(sycl::range<3>(1, 1, 128), sycl::range<3>(1, 1, 128)), |
| 198 | + [=](sycl::nd_item<3> item) { |
| 199 | + int thread_data[4]; |
| 200 | + auto *d_r = |
| 201 | + dacc_read.get_multi_ptr<sycl::access::decorated::yes>().get(); |
| 202 | + if (T == dpct::group::load_algorithm::BLOCK_LOAD_DIRECT) { |
| 203 | + dpct::group::load_blocked<4, int>(item, d_r, thread_data); |
| 204 | + } else { |
| 205 | + dpct::group::load_striped<4, int>(item, d_r, thread_data); |
| 206 | + } |
| 207 | + // Write thread_data of each work item at index to the global buffer |
| 208 | + int global_index = |
| 209 | + item.get_group(2) * item.get_local_range().get(2) + |
| 210 | + item.get_local_id(2); // Each thread_data has 4 elements |
| 211 | +#pragma unroll |
| 212 | + for (int i = 0; i < 4; ++i) { |
| 213 | + dacc_write[global_index * 4 + i] = thread_data[i]; |
| 214 | + } |
| 215 | + }); |
| 216 | + }); |
| 217 | + q.wait_and_throw(); |
| 218 | + |
| 219 | + sycl::host_accessor data_accessor(buffer_out, sycl::read_only); |
| 220 | + const int *ptr = data_accessor.get_multi_ptr<sycl::access::decorated::yes>(); |
| 221 | + return helper_validation_function<T>(ptr, "test_group_load"); |
| 222 | +} |
| 223 | + |
| 224 | +int main() { |
| 225 | + |
| 226 | + return !( |
| 227 | + // Calls test_group_load with blocked and striped strategies , should pass |
| 228 | + // both results. |
| 229 | + test_group_load<dpct::group::load_algorithm::BLOCK_LOAD_DIRECT>() && |
| 230 | + test_group_load<dpct::group::load_algorithm::BLOCK_LOAD_STRIPED>() && |
| 231 | + // Calls test_load_subgroup_striped_standalone and should pass |
| 232 | + test_load_subgroup_striped_standalone() && |
| 233 | + // Calls test_group_load_standalone with blocked and striped strategies as |
| 234 | + // free functions, should pass both results. |
| 235 | + test_group_load_standalone< |
| 236 | + dpct::group::load_algorithm::BLOCK_LOAD_STRIPED>() && |
| 237 | + test_group_load_standalone< |
| 238 | + dpct::group::load_algorithm::BLOCK_LOAD_DIRECT>()); |
| 239 | +} |
0 commit comments