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

Commit dc9caa8

Browse files
committed
[ESIMD] Add test for esimd::free
Signed-off-by: Konstantin S Bobrovsky <[email protected]>
1 parent 3ee23b6 commit dc9caa8

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

SYCL/ESIMD/api/esimd_merge.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//==---------------- esimd_merge.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: %HOST_RUN_PLACEHOLDER %t.out
12+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
13+
14+
#include "../esimd_test_utils.hpp"
15+
16+
#include <CL/sycl.hpp>
17+
#include <iostream>
18+
#include <sycl/ext/intel/experimental/esimd.hpp>
19+
20+
using namespace sycl::ext::intel::experimental::esimd;
21+
using namespace sycl::ext::intel::experimental;
22+
using namespace cl::sycl;
23+
24+
int main(void) {
25+
constexpr unsigned VL = 16;
26+
constexpr unsigned FACTOR = 2;
27+
constexpr unsigned SUB_VL = VL / FACTOR / FACTOR;
28+
constexpr unsigned Size = VL * 2;
29+
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+
int *A = malloc_shared<int>(Size, q);
36+
int *B = malloc_shared<int>(Size, q);
37+
using MaskElT = typename simd_mask<1>::element_type;
38+
MaskElT *M = malloc_shared<MaskElT>(Size, q);
39+
int *C = malloc_shared<int>(Size, q);
40+
constexpr int VAL0 = 0;
41+
constexpr int VAL1 = 1;
42+
constexpr int VAL2 = 3;
43+
44+
for (int i = 0; i < Size; ++i) {
45+
A[i] = i % 2 + VAL1; // 1 2 1 2 ...
46+
B[i] = i % 2 + VAL2; // 3 4 3 4 ...
47+
// mask out first half of sub-vector:
48+
M[i] = (i % SUB_VL) >= (SUB_VL / 2) ? 1 : 0; // 0 0 1 1 0 0 1 1 ...
49+
C[i] = VAL0;
50+
}
51+
52+
try {
53+
auto e = q.submit([&](handler &cgh) {
54+
cgh.parallel_for<class Test>(Size, [=](id<1> i) SYCL_ESIMD_KERNEL {
55+
simd<int, VL> va(A + i * VL);
56+
simd<int, VL> vb(B + i * VL);
57+
simd_mask<SUB_VL> m(M + i * VL);
58+
// va: 1212121212121212
59+
// vb: 3434343434343434
60+
// m: 0011001100110011
61+
// va.sel.sel: 1111
62+
// vb.sel.sel: 4444
63+
// vc: 1144
64+
simd<int, SUB_VL> vc = esimd::merge(
65+
va.template select<SUB_VL * 2, 2>(0).template select<SUB_VL, 1>(1),
66+
vb.template select<SUB_VL * 1, 2>(1).template select<SUB_VL, 1>(0),
67+
m);
68+
vc.copy_to(C + i * VL);
69+
});
70+
});
71+
e.wait();
72+
} catch (sycl::exception const &e) {
73+
std::cout << "SYCL exception caught: " << e.what() << '\n';
74+
sycl::free(A, q);
75+
sycl::free(B, q);
76+
sycl::free(C, q);
77+
return 1;
78+
}
79+
80+
int err_cnt = 0;
81+
82+
for (int i = 0; i < Size; ++i) {
83+
std::cout << " " << C[i];
84+
}
85+
std::cout << "\n";
86+
87+
for (int i = 0; i < Size; ++i) {
88+
int j = i % VL;
89+
int gold =
90+
j >= SUB_VL ? VAL0 : ((j % SUB_VL) >= (SUB_VL / 2) ? VAL2 + 1 : VAL1);
91+
92+
if (C[i] != gold) {
93+
if (++err_cnt < 10) {
94+
std::cout << "failed at index " << i << ", " << C[i] << " != " << gold
95+
<< " (gold)\n";
96+
}
97+
}
98+
}
99+
if (err_cnt > 0) {
100+
std::cout << " pass rate: "
101+
<< ((float)(Size - err_cnt) / (float)Size) * 100.0f << "% ("
102+
<< (Size - err_cnt) << "/" << Size << ")\n";
103+
}
104+
105+
sycl::free(A, q);
106+
sycl::free(B, q);
107+
sycl::free(C, q);
108+
std::cout << (err_cnt > 0 ? "FAILED\n" : "Passed\n");
109+
return err_cnt > 0 ? 1 : 0;
110+
}

0 commit comments

Comments
 (0)