|
| 1 | +//==----- simd_view_copy_move_assign.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 | + |
| 14 | +// This test checks the behavior of simd_view constructors |
| 15 | +// and assignment operators. |
| 16 | + |
| 17 | +#include "../esimd_test_utils.hpp" |
| 18 | + |
| 19 | +#include <CL/sycl.hpp> |
| 20 | +#include <sycl/ext/intel/experimental/esimd.hpp> |
| 21 | + |
| 22 | +#include <iostream> |
| 23 | + |
| 24 | +using namespace cl::sycl; |
| 25 | +using namespace sycl::ext::intel::experimental::esimd; |
| 26 | + |
| 27 | +template <class T> bool test(queue q, std::string str, T funcUnderTest) { |
| 28 | + std::cout << "Testing " << str << " ...\n"; |
| 29 | + constexpr unsigned VL = 8; |
| 30 | + |
| 31 | + int A[VL]; |
| 32 | + int B[VL]; |
| 33 | + // As a result, A should have first 4 values from B, next 4 values from A. |
| 34 | + int gold[VL] = {0, 1, 2, 3, -4, -5, -6, -7}; |
| 35 | + |
| 36 | + for (unsigned i = 0; i < VL; ++i) { |
| 37 | + A[i] = -i; |
| 38 | + B[i] = i; |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + buffer<int, 1> bufA(A, range<1>(VL)); |
| 43 | + buffer<int, 1> bufB(B, range<1>(VL)); |
| 44 | + range<1> glob_range{1}; |
| 45 | + |
| 46 | + auto e = q.submit([&](handler &cgh) { |
| 47 | + auto PA = bufA.template get_access<access::mode::read_write>(cgh); |
| 48 | + auto PB = bufB.template get_access<access::mode::read_write>(cgh); |
| 49 | + cgh.parallel_for(glob_range, [=](id<1> i) SYCL_ESIMD_KERNEL { |
| 50 | + using namespace sycl::ext::intel::experimental::esimd; |
| 51 | + unsigned int offset = i * VL * sizeof(int); |
| 52 | + simd<int, VL> va; |
| 53 | + va.copy_from(PA, offset); |
| 54 | + simd<int, VL> vb; |
| 55 | + vb.copy_from(PB, offset); |
| 56 | + auto va_view = va.select<4, 1>(0); |
| 57 | + auto vb_view = vb.select<4, 1>(0); |
| 58 | + |
| 59 | + funcUnderTest(va_view, vb_view); |
| 60 | + |
| 61 | + va.copy_to(PA, offset); |
| 62 | + vb.copy_to(PB, offset); |
| 63 | + }); |
| 64 | + }); |
| 65 | + q.wait_and_throw(); |
| 66 | + } catch (cl::sycl::exception const &e) { |
| 67 | + std::cout << "SYCL exception caught: " << e.what() << '\n'; |
| 68 | + return e.get_cl_code(); |
| 69 | + } |
| 70 | + |
| 71 | + int err_cnt = 0; |
| 72 | + for (unsigned i = 0; i < VL; ++i) { |
| 73 | + if (A[i] != gold[i]) { |
| 74 | + err_cnt++; |
| 75 | + std::cout << "failed at index " << i << ": " << A[i] << " != " << gold[i] |
| 76 | + << " (gold)\n"; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (err_cnt > 0) { |
| 81 | + std::cout << " pass rate: " << ((float)(VL - err_cnt) / (float)VL) * 100.0f |
| 82 | + << "% (" << (VL - err_cnt) << "/" << VL << ")\n"; |
| 83 | + } |
| 84 | + |
| 85 | + std::cout << (err_cnt > 0 ? " FAILED\n" : " Passed\n"); |
| 86 | + return err_cnt > 0 ? false : true; |
| 87 | +} |
| 88 | + |
| 89 | +int main(void) { |
| 90 | + queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler()); |
| 91 | + auto dev = q.get_device(); |
| 92 | + std::cout << "Running on " << dev.get_info<info::device::name>() << "\n"; |
| 93 | + |
| 94 | + bool passed = true; |
| 95 | + // copy constructor creates the same view of the underlying data. |
| 96 | + passed &= test(q, "copy constructor", |
| 97 | + [](auto &va_view, auto &vb_view) SYCL_ESIMD_FUNCTION { |
| 98 | + auto va_view_copy(va_view); |
| 99 | + auto vb_view_copy(vb_view); |
| 100 | + va_view_copy = vb_view_copy; |
| 101 | + }); |
| 102 | + // move constructor transfers the same view of the underlying data. |
| 103 | + passed &= test(q, "move constructor", |
| 104 | + [](auto &va_view, auto &vb_view) SYCL_ESIMD_FUNCTION { |
| 105 | + auto va_view_move(std::move(va_view)); |
| 106 | + auto vb_view_move(std::move(vb_view)); |
| 107 | + va_view_move = vb_view_move; |
| 108 | + }); |
| 109 | + // assignment operator copies the underlying data. |
| 110 | + passed &= test(q, "assignment operator", |
| 111 | + [](auto &va_view, auto &vb_view) |
| 112 | + SYCL_ESIMD_FUNCTION { va_view = vb_view; }); |
| 113 | + // move assignment operator copies the underlying data. |
| 114 | + passed &= test(q, "move assignment operator", |
| 115 | + [](auto &va_view, auto &vb_view) |
| 116 | + SYCL_ESIMD_FUNCTION { va_view = std::move(vb_view); }); |
| 117 | + |
| 118 | + return passed ? 0 : 1; |
| 119 | +} |
0 commit comments