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

Commit 62f7909

Browse files
authored
[ESIMD] Add a test exercising 2D workitem coordinates. (#73)
Signed-off-by: Konstantin S Bobrovsky <[email protected]>
1 parent ff1fc58 commit 62f7909

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

SYCL/ESIMD/vadd_2d_acc.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//==---------------- vadd_2d_acc.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+
// TODO enable on Windows
9+
// REQUIRES: linux && gpu
10+
// RUN: %clangxx-esimd -fsycl %s -o %t.out
11+
// RUN: %ESIMD_RUN_PLACEHOLDER %t.out
12+
13+
// The test checks that 2D workitem addressing works correctly with SIMD
14+
// kernels.
15+
16+
#include "esimd_test_utils.hpp"
17+
18+
#include <CL/sycl.hpp>
19+
#include <CL/sycl/INTEL/esimd.hpp>
20+
#include <iostream>
21+
22+
using namespace cl::sycl;
23+
24+
int main(void) {
25+
constexpr unsigned Size = 1024 * 128;
26+
constexpr unsigned VL = 16;
27+
28+
float *A = new float[Size];
29+
float *B = new float[Size];
30+
float *C = new float[Size];
31+
32+
for (unsigned i = 0; i < Size; ++i) {
33+
A[i] = B[i] = i + 1;
34+
C[i] = 0.0f;
35+
}
36+
37+
try {
38+
buffer<float, 1> bufa(A, range<1>(Size));
39+
buffer<float, 1> bufb(B, range<1>(Size));
40+
buffer<float, 1> bufc(C, range<1>(Size));
41+
42+
// We need that many workgroups
43+
cl::sycl::range<2> GlobalRange{Size / (16 * VL), 16};
44+
45+
// We need that many threads in each group
46+
cl::sycl::range<2> LocalRange{4, 4};
47+
48+
cl::sycl::nd_range<2> Range(GlobalRange, LocalRange);
49+
50+
queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler());
51+
52+
auto dev = q.get_device();
53+
auto ctxt = q.get_context();
54+
std::cout << "Running on " << dev.get_info<info::device::name>() << "\n";
55+
56+
auto e = q.submit([&](handler &cgh) {
57+
auto PA = bufa.get_access<access::mode::read>(cgh);
58+
auto PB = bufb.get_access<access::mode::read>(cgh);
59+
auto PC = bufc.get_access<access::mode::write>(cgh);
60+
cgh.parallel_for<class Test>(
61+
Range, [=](nd_item<2> ndi) SYCL_ESIMD_KERNEL {
62+
using namespace sycl::INTEL::gpu;
63+
int gid = ndi.get_group_linear_id();
64+
int lid = ndi.get_local_linear_id();
65+
66+
int i = gid * 16 + lid;
67+
unsigned int offset = i * VL * sizeof(float);
68+
simd<float, VL> va = block_load<float, VL>(PA, offset);
69+
simd<float, VL> vb = block_load<float, VL>(PB, offset);
70+
simd<float, VL> vc = va + vb;
71+
block_store(PC, offset, vc);
72+
});
73+
});
74+
e.wait();
75+
} catch (sycl::exception e) {
76+
std::cerr << "SYCL exception caught: " << e.what() << "\n";
77+
return 1;
78+
}
79+
80+
int err_cnt = 0;
81+
82+
for (unsigned i = 0; i < Size; ++i) {
83+
if (A[i] + B[i] != C[i]) {
84+
if (++err_cnt < 10) {
85+
std::cout << "failed at index " << i << ", " << C[i] << " != " << A[i]
86+
<< " + " << B[i] << "\n";
87+
}
88+
}
89+
}
90+
if (err_cnt > 0) {
91+
std::cout << " pass rate: "
92+
<< ((float)(Size - err_cnt) / (float)Size) * 100.0f << "% ("
93+
<< (Size - err_cnt) << "/" << Size << ")\n";
94+
}
95+
96+
delete[] A;
97+
delete[] B;
98+
delete[] C;
99+
100+
std::cout << (err_cnt > 0 ? "FAILED\n" : "Passed\n");
101+
return err_cnt > 0 ? 1 : 0;
102+
}

0 commit comments

Comments
 (0)