Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 6f60e2e

Browse files
committed
[ESIMD] Add test for accessor based gather/scatter_rgba
Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent e1a6e78 commit 6f60e2e

File tree

6 files changed

+213
-23
lines changed

6 files changed

+213
-23
lines changed

SYCL/ESIMD/PrefixSum.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ void cmk_acum_final(unsigned *buf, unsigned h_pos, unsigned int stride_elems,
183183

184184
simd_mask<32> p = elm32 < remaining;
185185

186-
S = gather_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset,
187-
p);
186+
S = gather_rgba<GATHER_SCATTER_MASK>(buf, element_offset, p);
188187

189188
auto cnt_table = S.bit_cast_view<unsigned int, TUPLE_SZ, 32>();
190189
cnt_table.column(0) += prev;
@@ -214,8 +213,7 @@ void cmk_acum_final(unsigned *buf, unsigned h_pos, unsigned int stride_elems,
214213
cnt_table.select<1, 1, 16, 1>(j, 16) +=
215214
cnt_table.replicate_vs_w_hs<1, 0, 16, 0>(j, 15);
216215
}
217-
scatter_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset, S,
218-
p);
216+
scatter_rgba<GATHER_SCATTER_MASK>(buf, element_offset, S, p);
219217
elm32 += 32;
220218
element_offset += stride_elems * TUPLE_SZ * sizeof(unsigned) * 32;
221219
prev = cnt_table.column(31);
@@ -253,7 +251,7 @@ void cmk_prefix_iterative(unsigned *buf, unsigned h_pos,
253251
unsigned n_iter = n_entries / 32;
254252
for (unsigned i = 0; i < n_iter; i++) {
255253

256-
S = gather_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset);
254+
S = gather_rgba<GATHER_SCATTER_MASK>(buf, element_offset);
257255

258256
auto cnt_table = S.bit_cast_view<unsigned int, TUPLE_SZ, 32>();
259257
cnt_table.column(0) += prev;
@@ -289,7 +287,7 @@ void cmk_prefix_iterative(unsigned *buf, unsigned h_pos,
289287
if (i == n_iter - 1)
290288
cnt_table.column(31) -= cnt_table.column(30);
291289

292-
scatter_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset, S);
290+
scatter_rgba<GATHER_SCATTER_MASK>(buf, element_offset, S);
293291

294292
element_offset += stride_elems * TUPLE_SZ * sizeof(unsigned) * 32;
295293
prev = cnt_table.column(31);

SYCL/ESIMD/Prefix_Local_sum2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ void cmk_acum_iterative(unsigned *buf, unsigned h_pos,
7272

7373
simd<unsigned int, 32 * TUPLE_SZ> S, T;
7474

75-
S = gather_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset);
75+
S = gather_rgba<GATHER_SCATTER_MASK>(buf, element_offset);
7676

7777
#pragma unroll
7878
for (int i = 1; i < PREFIX_ENTRIES / 32; i++) {
7979
element_offset += (stride_elems * 32 * TUPLE_SZ) * sizeof(unsigned);
8080
// scattered read, each inst reads 16 entries
81-
T = gather_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset);
81+
T = gather_rgba<GATHER_SCATTER_MASK>(buf, element_offset);
8282
S += T;
8383
}
8484

SYCL/ESIMD/Prefix_Local_sum3.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ void cmk_acum_final(unsigned *buf, unsigned h_pos, unsigned int stride_elems,
196196

197197
simd_mask<32> p = elm32 < remaining;
198198

199-
S = gather_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset,
200-
p);
199+
S = gather_rgba<GATHER_SCATTER_MASK>(buf, element_offset, p);
201200

202201
auto cnt_table = S.bit_cast_view<unsigned int, TUPLE_SZ, 32>();
203202
cnt_table.column(0) += prev;
@@ -226,8 +225,7 @@ void cmk_acum_final(unsigned *buf, unsigned h_pos, unsigned int stride_elems,
226225
cnt_table.select<1, 1, 16, 1>(j, 16) +=
227226
cnt_table.replicate_vs_w_hs<1, 0, 16, 0>(j, 15);
228227
}
229-
scatter_rgba<unsigned int, 32, GATHER_SCATTER_MASK>(buf, element_offset, S,
230-
p);
228+
scatter_rgba<GATHER_SCATTER_MASK>(buf, element_offset, S, p);
231229
elm32 += 32;
232230
element_offset += stride_elems * TUPLE_SZ * sizeof(unsigned) * 32;
233231
prev = cnt_table.column(31);
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
//==-------- acc_gather_scatter_rgba.cpp - DPC++ ESIMD on-device test -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// REQUIRES: gpu
9+
// UNSUPPORTED: cuda || hip
10+
// RUN: %clangxx -fsycl %s -o %t.out
11+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
12+
//
13+
// The test checks functionality of the gather_rgba/scatter_rgba accessor-based
14+
// ESIMD intrinsics.
15+
16+
#include "esimd_test_utils.hpp"
17+
18+
#include <CL/sycl.hpp>
19+
#include <iostream>
20+
#include <sycl/ext/intel/esimd.hpp>
21+
22+
using namespace cl::sycl;
23+
24+
template <typename T>
25+
using AccT = accessor<T, 1, access_mode::read_write, access::target::device>;
26+
27+
constexpr int MASKED_LANE_NUM_REV = 1;
28+
constexpr int NUM_RGBA_CHANNELS =
29+
get_num_channels_enabled(sycl::ext::intel::esimd::rgba_channel_mask::ABGR);
30+
31+
template <typename T, unsigned VL, unsigned STRIDE, auto CH_MASK>
32+
struct Kernel {
33+
AccT<T> InAcc;
34+
AccT<T> OutAcc;
35+
Kernel(AccT<T> InAcc, AccT<T> OutAcc) : InAcc(InAcc), OutAcc(OutAcc) {}
36+
37+
void operator()(id<1> i) const SYCL_ESIMD_KERNEL {
38+
using namespace sycl::ext::intel::esimd;
39+
constexpr int numChannels = get_num_channels_enabled(CH_MASK);
40+
41+
// every workitem accesses contiguous block of VL * STRIDE elements,
42+
// where each element consists of RGBA channels.
43+
uint32_t global_offset = i * VL * STRIDE * NUM_RGBA_CHANNELS * sizeof(T);
44+
45+
simd<uint32_t, VL> byteOffsets(0, STRIDE * sizeof(T) * NUM_RGBA_CHANNELS);
46+
simd<T, VL *numChannels> v =
47+
gather_rgba<CH_MASK>(InAcc, byteOffsets, global_offset);
48+
v += (int)i;
49+
50+
simd_mask<VL> pred = 1;
51+
pred[VL - MASKED_LANE_NUM_REV] = 0; // mask out the last lane
52+
scatter_rgba<CH_MASK>(OutAcc, byteOffsets, v, global_offset, pred);
53+
}
54+
};
55+
56+
std::string convertMaskToStr(sycl::ext::intel::esimd::rgba_channel_mask mask) {
57+
using namespace sycl::ext::intel::esimd;
58+
switch (mask) {
59+
case rgba_channel_mask::R:
60+
return "R";
61+
case rgba_channel_mask::GR:
62+
return "GR";
63+
case rgba_channel_mask::ABGR:
64+
return "ABGR";
65+
default:
66+
return "";
67+
}
68+
return "";
69+
}
70+
71+
template <typename T, unsigned VL, unsigned STRIDE, auto CH_MASK>
72+
bool test(queue q) {
73+
size_t numWorkItems = 2;
74+
size_t size = VL * STRIDE * NUM_RGBA_CHANNELS * numWorkItems;
75+
using namespace sycl::ext::intel::esimd;
76+
constexpr int numChannels = get_num_channels_enabled(CH_MASK);
77+
78+
std::cout << "Testing T=" << typeid(T).name() << " VL=" << VL
79+
<< " STRIDE=" << STRIDE << " MASK=" << convertMaskToStr(CH_MASK)
80+
<< "...\t";
81+
82+
T *A = new T[size];
83+
T *B = new T[size];
84+
T *gold = new T[size];
85+
86+
for (int i = 0; i < size; ++i) {
87+
A[i] = (T)i;
88+
B[i] = (T)-i;
89+
gold[i] = (T)-i;
90+
}
91+
92+
// Fill out the array with gold values. The kernel only writes the elements
93+
// that are not masked. For example,
94+
// for STRIDE=1 and MASK=R, we have the following indices written:
95+
// 0, 4, 8, 12 ...
96+
// for STRIDE=2 and MASK=RG, we have the following indices written:
97+
// 0, 1, 8, 9, 16, 17 ...
98+
// All the other elements will be equal to '-A[i]'.
99+
auto blockSize = VL * STRIDE * NUM_RGBA_CHANNELS;
100+
for (unsigned i = 0; i < size; i += NUM_RGBA_CHANNELS * STRIDE)
101+
for (unsigned j = 0; j < numChannels; j++)
102+
gold[i + j] = A[i + j] + (i / (blockSize));
103+
104+
// Account for masked out last lanes (with pred argument to scatter_rgba).
105+
auto maskedElementOffset = (VL - 1) * STRIDE * NUM_RGBA_CHANNELS;
106+
for (unsigned i = maskedElementOffset; i < size; i += blockSize)
107+
for (unsigned j = 0; j < numChannels; j++)
108+
gold[i + j] = -A[i + j];
109+
110+
try {
111+
buffer<T, 1> InBuf(A, range<1>(size));
112+
buffer<T, 1> OutBuf(B, range<1>(size));
113+
range<1> glob_range{numWorkItems};
114+
auto e = q.submit([&](handler &cgh) {
115+
auto InAcc = InBuf.template get_access<access::mode::read_write>(cgh);
116+
auto OutAcc = OutBuf.template get_access<access::mode::read_write>(cgh);
117+
Kernel<T, VL, STRIDE, CH_MASK> kernel(InAcc, OutAcc);
118+
cgh.parallel_for(glob_range, kernel);
119+
});
120+
e.wait();
121+
} catch (sycl::exception const &e) {
122+
std::cerr << "SYCL exception caught: " << e.what() << '\n';
123+
delete[] A;
124+
delete[] B;
125+
delete[] gold;
126+
return false; // not success
127+
}
128+
129+
int err_cnt = 0;
130+
for (unsigned i = 0; i < size; ++i) {
131+
if (B[i] != gold[i]) {
132+
if (++err_cnt < 35) {
133+
std::cout << "\nFAILED at index " << i << ": " << B[i]
134+
<< " != " << gold[i] << " (gold)";
135+
}
136+
}
137+
}
138+
139+
if (err_cnt > 0) {
140+
std::cout << "\n pass rate: "
141+
<< ((float)(size - err_cnt) / (float)size) * 100.0f << "% ("
142+
<< (size - err_cnt) << "/" << size << ")\n";
143+
}
144+
145+
delete[] A;
146+
delete[] B;
147+
delete[] gold;
148+
149+
if (err_cnt == 0)
150+
std::cout << "Passed\n";
151+
return err_cnt == 0;
152+
}
153+
154+
template <typename T, unsigned VL, unsigned STRIDE> bool test(queue q) {
155+
using namespace sycl::ext::intel::esimd;
156+
bool passed = true;
157+
passed &= test<T, VL, STRIDE, rgba_channel_mask::R>(q);
158+
passed &= test<T, VL, STRIDE, rgba_channel_mask::GR>(q);
159+
passed &= test<T, VL, STRIDE, rgba_channel_mask::ABGR>(q);
160+
return passed;
161+
}
162+
163+
int main(void) {
164+
queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler());
165+
166+
auto dev = q.get_device();
167+
std::cout << "Running on " << dev.get_info<info::device::name>() << "\n";
168+
169+
bool passed = true;
170+
passed &= test<int, 16, 1>(q);
171+
passed &= test<int, 16, 2>(q);
172+
passed &= test<int, 16, 4>(q);
173+
passed &= test<int, 32, 1>(q);
174+
passed &= test<int, 32, 3>(q);
175+
passed &= test<int, 32, 8>(q);
176+
passed &= test<float, 16, 1>(q);
177+
passed &= test<float, 16, 2>(q);
178+
passed &= test<float, 16, 4>(q);
179+
passed &= test<float, 32, 1>(q);
180+
passed &= test<float, 32, 3>(q);
181+
passed &= test<float, 32, 8>(q);
182+
183+
passed &= test<int, 8, 1>(q);
184+
passed &= test<int, 8, 3>(q);
185+
passed &= test<int, 8, 8>(q);
186+
passed &= test<float, 8, 1>(q);
187+
passed &= test<float, 8, 2>(q);
188+
passed &= test<float, 8, 4>(q);
189+
190+
std::cout << (passed ? "All tests passed.\n" : "Some tests failed!\n");
191+
return passed ? 0 : 1;
192+
}

SYCL/ESIMD/api/esimd_rgba_smoke.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ bool test_impl(queue q) {
9292
cgh.single_task<TestID<T, NPixels, static_cast<int>(ChMask)>>(
9393
[=]() SYCL_ESIMD_KERNEL {
9494
constexpr unsigned NElems = NPixels * NOnChs;
95-
simd<T, NPixels> offsets(0, sizeof(T) * NAllChs);
96-
simd<T, NElems> p = gather_rgba<T, NPixels, ChMask>(A, offsets);
95+
simd<unsigned int, NPixels> offsets(0, sizeof(T) * NAllChs);
96+
simd<T, NElems> p = gather_rgba<ChMask>(A, offsets);
9797
// simply scatter back to B - should give same results as A in
9898
// enabled channels, the rest should remain zero:
99-
scatter_rgba<T, NPixels, ChMask>(B, offsets, p);
99+
scatter_rgba<ChMask>(B, offsets, p);
100100
// copy instead of scattering to C - thus getting AOS to SOA layout
101101
// layout conversion:
102102
// R0 R1 ... G0 G1 ... B0 B1 ... A0 A1 ...

SYCL/ESIMD/usm_gather_scatter_rgba.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ struct Kernel {
4141

4242
simd<uint32_t, VL> byteOffsets(0, STRIDE * sizeof(T) * NUM_RGBA_CHANNELS);
4343
simd<T, VL *numChannels> v =
44-
gather_rgba<T, VL, CH_MASK>(bufIn + global_offset, byteOffsets);
44+
gather_rgba<CH_MASK>(bufIn + global_offset, byteOffsets);
4545
v += (int)i;
4646

4747
simd_mask<VL> pred = 1;
4848
pred[VL - MASKED_LANE_NUM_REV] = 0; // mask out the last lane
49-
scatter_rgba<T, VL, CH_MASK>(bufOut + global_offset, byteOffsets, v, pred);
49+
scatter_rgba<CH_MASK>(bufOut + global_offset, byteOffsets, v, pred);
5050
}
5151
};
5252

@@ -74,7 +74,7 @@ bool test(queue q) {
7474

7575
std::cout << "Testing T=" << typeid(T).name() << " VL=" << VL
7676
<< " STRIDE=" << STRIDE << " MASK=" << convertMaskToStr(CH_MASK)
77-
<< "...\n";
77+
<< "...\t";
7878

7979
T *A = malloc_shared<T>(size, q);
8080
T *B = malloc_shared<T>(size, q);
@@ -123,14 +123,14 @@ bool test(queue q) {
123123
for (unsigned i = 0; i < size; ++i) {
124124
if (B[i] != gold[i]) {
125125
if (++err_cnt < 35) {
126-
std::cout << "failed at index " << i << ": " << B[i]
127-
<< " != " << gold[i] << " (gold)\n";
126+
std::cout << "\nFAILED at index " << i << ": " << B[i]
127+
<< " != " << gold[i] << " (gold)";
128128
}
129129
}
130130
}
131131

132132
if (err_cnt > 0) {
133-
std::cout << " pass rate: "
133+
std::cout << "\n pass rate: "
134134
<< ((float)(size - err_cnt) / (float)size) * 100.0f << "% ("
135135
<< (size - err_cnt) << "/" << size << ")\n";
136136
}
@@ -139,8 +139,9 @@ bool test(queue q) {
139139
free(B, q);
140140
delete[] gold;
141141

142-
std::cout << (err_cnt > 0 ? " FAILED\n" : " Passed\n");
143-
return err_cnt > 0 ? false : true;
142+
if (err_cnt == 0)
143+
std::cout << "Passed\n";
144+
return err_cnt == 0;
144145
}
145146

146147
template <typename T, unsigned VL, unsigned STRIDE> bool test(queue q) {
@@ -179,5 +180,6 @@ int main(void) {
179180
passed &= test<float, 8, 2>(q);
180181
passed &= test<float, 8, 4>(q);
181182

183+
std::cout << (passed ? "All tests passed.\n" : "Some tests failed!\n");
182184
return passed ? 0 : 1;
183185
}

0 commit comments

Comments
 (0)