diff --git a/SYCL/Reduction/reduction_complex_nums.cpp b/SYCL/Reduction/reduction_complex_nums.cpp new file mode 100644 index 0000000000..ec9432d19d --- /dev/null +++ b/SYCL/Reduction/reduction_complex_nums.cpp @@ -0,0 +1,58 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out +// RUN: %ACC_RUN_PLACEHOLDER %t.out + +#include +#include +#include + +#include + +using namespace sycl; + +#define BUFFER_SIZE 255 + +// Currently, Identityless reduction for complex numbers is +// only valid for plus operator. +// TODO: Extend this test case once we support known_identity for std::complex +// and more operators (apart from plus). +template +void test_identityless_reduction_for_complex_nums(queue &q) { + // Allocate and initialize buffer on the host with all 1's. + buffer> valuesBuf{BUFFER_SIZE}; + { + host_accessor a{valuesBuf}; + T n = 0; + std::generate(a.begin(), a.end(), [&n] { + n++; + return std::complex(n, n + 1); + }); + } + + // Buffer to hold the reduction results. + std::complex sumResult = 0; + buffer> sumBuf{&sumResult, 1}; + + q.submit([&](handler &cgh) { + accessor inputVals{valuesBuf, cgh, sycl::read_only}; + auto sumReduction = reduction(sumBuf, cgh, plus>()); + + cgh.parallel_for(nd_range<1>{BUFFER_SIZE, BUFFER_SIZE}, sumReduction, + [=](nd_item<1> idx, auto &sum) { + sum += inputVals[idx.get_global_id(0)]; + }); + }); + + assert(sumBuf.get_host_access()[0] == std::complex(32640, 32895)); +} + +int main() { + queue q; + + test_identityless_reduction_for_complex_nums(q); + if (q.get_device().has(aspect::fp64)) + test_identityless_reduction_for_complex_nums(q); + + return 0; +}