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

Commit ebc478f

Browse files
[ESIMD] E2E test for writeable simd_view subscript operator (#415)
1 parent 0f5fb49 commit ebc478f

File tree

1 file changed

+100
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)