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

Commit f91e684

Browse files
[ESIMD] Added a test on simd subscript operator (#323)
1 parent ab21899 commit f91e684

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//==----- simd_subscript_operator.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
10+
// RUN: %clangxx -fsycl %s -o %t.out
11+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
12+
//
13+
// The test checks that it's possible to write through the simd subscript
14+
// operator. E.g.:
15+
// simd<int, 4> v = 1;
16+
// v[1] = 0; // v[1] returns writable simd_view
17+
18+
#include "../esimd_test_utils.hpp"
19+
20+
#include <CL/sycl.hpp>
21+
#include <sycl/ext/intel/experimental/esimd.hpp>
22+
23+
#include <iostream>
24+
25+
using namespace cl::sycl;
26+
using namespace sycl::ext::intel::experimental::esimd;
27+
28+
int main(int argc, char **argv) {
29+
queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler());
30+
31+
auto dev = q.get_device();
32+
std::cout << "Running on " << dev.get_info<info::device::name>() << "\n";
33+
34+
constexpr unsigned VL = 16;
35+
36+
int A[VL];
37+
int B[VL];
38+
int gold[VL];
39+
40+
for (unsigned i = 0; i < VL; ++i) {
41+
A[i] = -i;
42+
B[i] = i;
43+
gold[i] = B[i];
44+
}
45+
46+
// some random indices to overwrite elements in B with elements from A.
47+
std::array<int, 5> indicesToCopy = {2, 5, 9, 10, 13};
48+
49+
try {
50+
buffer<int, 1> bufA(A, range<1>(VL));
51+
buffer<int, 1> bufB(B, range<1>(VL));
52+
range<1> glob_range{1};
53+
54+
auto e = q.submit([&](handler &cgh) {
55+
auto PA = bufA.get_access<access::mode::read>(cgh);
56+
auto PB = bufB.template get_access<access::mode::read_write>(cgh);
57+
cgh.parallel_for<class Test>(glob_range, [=](id<1> i) SYCL_ESIMD_KERNEL {
58+
using namespace sycl::ext::intel::experimental::esimd;
59+
unsigned int offset = i * VL * sizeof(int);
60+
simd<int, VL> va;
61+
va.copy_from(PA, offset);
62+
simd<int, VL> vb;
63+
vb.copy_from(PB, offset);
64+
for (auto idx : indicesToCopy)
65+
vb[idx] = va[idx];
66+
vb.copy_to(PB, offset);
67+
});
68+
});
69+
q.wait_and_throw();
70+
} catch (cl::sycl::exception const &e) {
71+
std::cout << "SYCL exception caught: " << e.what() << '\n';
72+
return e.get_cl_code();
73+
}
74+
75+
int err_cnt = 0;
76+
77+
for (auto i : indicesToCopy)
78+
gold[i] = A[i];
79+
80+
for (unsigned i = 0; i < VL; ++i) {
81+
int val = B[i];
82+
83+
if (val != gold[i]) {
84+
if (++err_cnt < 10) {
85+
std::cout << "failed at index " << i << ": " << val << " != " << gold[i]
86+
<< " (gold)\n";
87+
}
88+
}
89+
}
90+
if (err_cnt > 0) {
91+
std::cout << " pass rate: " << ((float)(VL - err_cnt) / (float)VL) * 100.0f
92+
<< "% (" << (VL - err_cnt) << "/" << VL << ")\n";
93+
}
94+
95+
std::cout << (err_cnt > 0 ? " FAILED\n" : " Passed\n");
96+
97+
return err_cnt > 0 ? 1 : 0;
98+
}

0 commit comments

Comments
 (0)