|
| 1 | +//==---------------- template.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: %GPU_RUN_PLACEHOLDER %t.out |
| 12 | + |
| 13 | +#include "esimd_test_utils.hpp" |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | +#include <sycl/ext/intel/esimd.hpp> |
| 17 | +#include <sycl/sycl.hpp> |
| 18 | + |
| 19 | +using namespace sycl; |
| 20 | + |
| 21 | +constexpr unsigned Size = 1024 * 128; |
| 22 | +constexpr unsigned VL = 16; |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +sycl::event createKernel(sycl::queue &q, buffer<T, 1> &bufa, buffer<T, 1> &bufb, |
| 26 | + buffer<T, 1> &bufc) { |
| 27 | + // We need that many workgroups |
| 28 | + range<1> GlobalRange{Size / VL}; |
| 29 | + |
| 30 | + // We need that many threads in each group |
| 31 | + range<1> LocalRange{1}; |
| 32 | + return q.submit([&](handler &cgh) { |
| 33 | + auto PA = bufa.template get_access<access::mode::read>(cgh); |
| 34 | + auto PB = bufb.template get_access<access::mode::read>(cgh); |
| 35 | + auto PC = bufc.template get_access<access::mode::write>(cgh); |
| 36 | + cgh.parallel_for<class Test>(GlobalRange * LocalRange, |
| 37 | + [=](id<1> i) SYCL_ESIMD_KERNEL { |
| 38 | + using namespace sycl::ext::intel::esimd; |
| 39 | + unsigned int offset = i * VL * sizeof(T); |
| 40 | + simd<T, VL> va; |
| 41 | + va.copy_from(PA, offset); |
| 42 | + simd<T, VL> vb; |
| 43 | + vb.copy_from(PB, offset); |
| 44 | + simd<T, VL> vc = va + vb; |
| 45 | + vc.copy_to(PC, offset); |
| 46 | + }); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +int main(void) { |
| 51 | + |
| 52 | + float *A = new float[Size]; |
| 53 | + float *B = new float[Size]; |
| 54 | + float *C = new float[Size]; |
| 55 | + |
| 56 | + for (unsigned i = 0; i < Size; ++i) { |
| 57 | + A[i] = B[i] = i; |
| 58 | + C[i] = 0.0f; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + |
| 63 | + buffer<float, 1> bufa(A, range<1>(Size)); |
| 64 | + buffer<float, 1> bufb(B, range<1>(Size)); |
| 65 | + buffer<float, 1> bufc(C, range<1>(Size)); |
| 66 | + |
| 67 | + queue q(esimd_test::ESIMDSelector, esimd_test::createExceptionHandler()); |
| 68 | + |
| 69 | + auto dev = q.get_device(); |
| 70 | + std::cout << "Running on " << dev.get_info<info::device::name>() << "\n"; |
| 71 | + auto e = createKernel(q, bufa, bufb, bufc); |
| 72 | + e.wait(); |
| 73 | + } catch (sycl::exception const &e) { |
| 74 | + std::cout << "SYCL exception caught: " << e.what() << '\n'; |
| 75 | + |
| 76 | + delete[] A; |
| 77 | + delete[] B; |
| 78 | + delete[] C; |
| 79 | + return 1; |
| 80 | + } |
| 81 | + |
| 82 | + int err_cnt = 0; |
| 83 | + |
| 84 | + for (unsigned i = 0; i < Size; ++i) { |
| 85 | + if (A[i] + B[i] != C[i]) { |
| 86 | + if (++err_cnt < 10) { |
| 87 | + std::cout << "failed at index " << i << ", " << C[i] << " != " << A[i] |
| 88 | + << " + " << B[i] << "\n"; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if (err_cnt > 0) { |
| 93 | + std::cout << " pass rate: " |
| 94 | + << ((float)(Size - err_cnt) / (float)Size) * 100.0f << "% (" |
| 95 | + << (Size - err_cnt) << "/" << Size << ")\n"; |
| 96 | + } |
| 97 | + |
| 98 | + delete[] A; |
| 99 | + delete[] B; |
| 100 | + delete[] C; |
| 101 | + |
| 102 | + std::cout << (err_cnt > 0 ? "FAILED\n" : "Passed\n"); |
| 103 | + return err_cnt > 0 ? 1 : 0; |
| 104 | +} |
0 commit comments