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

Commit 8d77241

Browse files
[SYCL] Test reduction for complex numbers. (#1609)
This PR adds test case for testing identityless reduction for complex numbers. Corresponding intel/llvm PR: intel/llvm#8425
1 parent 89a42ab commit 8d77241

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
5+
6+
#include <algorithm>
7+
#include <complex>
8+
#include <numeric>
9+
10+
#include <sycl/sycl.hpp>
11+
12+
using namespace sycl;
13+
14+
#define BUFFER_SIZE 255
15+
16+
// Currently, Identityless reduction for complex numbers is
17+
// only valid for plus operator.
18+
// TODO: Extend this test case once we support known_identity for std::complex
19+
// and more operators (apart from plus).
20+
template <typename T>
21+
void test_identityless_reduction_for_complex_nums(queue &q) {
22+
// Allocate and initialize buffer on the host with all 1's.
23+
buffer<std::complex<T>> valuesBuf{BUFFER_SIZE};
24+
{
25+
host_accessor a{valuesBuf};
26+
T n = 0;
27+
std::generate(a.begin(), a.end(), [&n] {
28+
n++;
29+
return std::complex<T>(n, n + 1);
30+
});
31+
}
32+
33+
// Buffer to hold the reduction results.
34+
std::complex<T> sumResult = 0;
35+
buffer<std::complex<T>> sumBuf{&sumResult, 1};
36+
37+
q.submit([&](handler &cgh) {
38+
accessor inputVals{valuesBuf, cgh, sycl::read_only};
39+
auto sumReduction = reduction(sumBuf, cgh, plus<std::complex<T>>());
40+
41+
cgh.parallel_for(nd_range<1>{BUFFER_SIZE, BUFFER_SIZE}, sumReduction,
42+
[=](nd_item<1> idx, auto &sum) {
43+
sum += inputVals[idx.get_global_id(0)];
44+
});
45+
});
46+
47+
assert(sumBuf.get_host_access()[0] == std::complex<T>(32640, 32895));
48+
}
49+
50+
int main() {
51+
queue q;
52+
53+
test_identityless_reduction_for_complex_nums<float>(q);
54+
if (q.get_device().has(aspect::fp64))
55+
test_identityless_reduction_for_complex_nums<double>(q);
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)