|
| 1 | +//==----------- sycl_esimd_mix.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 | +// This is basic test for mixing SYCL and ESIMD kernels in the same source and |
| 9 | +// in the same program . |
| 10 | + |
| 11 | +// REQUIRES: gpu |
| 12 | +// UNSUPPORTED: cuda |
| 13 | +// RUN: %clangxx-esimd -fsycl %s -o %t.out |
| 14 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 15 | + |
| 16 | +#include "esimd_test_utils.hpp" |
| 17 | + |
| 18 | +#include <CL/sycl.hpp> |
| 19 | +#include <CL/sycl/INTEL/esimd.hpp> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +using namespace cl::sycl; |
| 23 | + |
| 24 | +bool checkResult(const std::vector<float> &A, int Inc) { |
| 25 | + int err_cnt = 0; |
| 26 | + unsigned Size = A.size(); |
| 27 | + |
| 28 | + for (unsigned i = 0; i < Size; ++i) { |
| 29 | + if (A[i] != i + Inc) |
| 30 | + if (++err_cnt < 10) |
| 31 | + std::cerr << "failed at A[" << i << "]: " << A[i] << " != " << i + Inc |
| 32 | + << "\n"; |
| 33 | + } |
| 34 | + |
| 35 | + if (err_cnt > 0) { |
| 36 | + std::cout << " pass rate: " |
| 37 | + << ((float)(Size - err_cnt) / (float)Size) * 100.0f << "% (" |
| 38 | + << (Size - err_cnt) << "/" << Size << ")\n"; |
| 39 | + return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | +} |
| 43 | + |
| 44 | +int main(void) { |
| 45 | + constexpr unsigned Size = 32; |
| 46 | + constexpr unsigned VL = 16; |
| 47 | + |
| 48 | + std::vector<float> A(Size); |
| 49 | + |
| 50 | + for (unsigned i = 0; i < Size; ++i) { |
| 51 | + A[i] = i; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + buffer<float, 1> bufa(A.data(), range<1>(Size)); |
| 56 | + |
| 57 | + // We need that many workgroups |
| 58 | + cl::sycl::range<1> GlobalRange{Size}; |
| 59 | + // We need that many threads in each group |
| 60 | + cl::sycl::range<1> LocalRange{1}; |
| 61 | + |
| 62 | + queue q(gpu_selector{}, esimd_test::createExceptionHandler()); |
| 63 | + |
| 64 | + auto dev = q.get_device(); |
| 65 | + std::cout << "Running on " << dev.get_info<info::device::name>() << "\n"; |
| 66 | + |
| 67 | + auto e = q.submit([&](handler &cgh) { |
| 68 | + auto PA = bufa.get_access<access::mode::read_write>(cgh); |
| 69 | + cgh.parallel_for<class SyclKernel>(GlobalRange * LocalRange, |
| 70 | + [=](id<1> i) { PA[i] = PA[i] + 1; }); |
| 71 | + }); |
| 72 | + e.wait(); |
| 73 | + } catch (cl::sycl::exception const &e) { |
| 74 | + std::cout << "SYCL exception caught: " << e.what() << '\n'; |
| 75 | + return 2; |
| 76 | + } |
| 77 | + |
| 78 | + if (checkResult(A, 1)) { |
| 79 | + std::cout << "SYCL kernel passed\n"; |
| 80 | + } else { |
| 81 | + std::cout << "SYCL kernel failed\n"; |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + buffer<float, 1> bufa(A.data(), range<1>(Size)); |
| 87 | + |
| 88 | + // We need that many workgroups |
| 89 | + cl::sycl::range<1> GlobalRange{Size / VL}; |
| 90 | + // We need that many threads in each group |
| 91 | + cl::sycl::range<1> LocalRange{1}; |
| 92 | + |
| 93 | + queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler()); |
| 94 | + |
| 95 | + auto dev = q.get_device(); |
| 96 | + std::cout << "Running on " << dev.get_info<info::device::name>() << "\n"; |
| 97 | + |
| 98 | + auto e = q.submit([&](handler &cgh) { |
| 99 | + auto PA = bufa.get_access<access::mode::read_write>(cgh); |
| 100 | + cgh.parallel_for<class EsimdKernel>( |
| 101 | + GlobalRange * LocalRange, [=](id<1> i) SYCL_ESIMD_KERNEL { |
| 102 | + using namespace sycl::INTEL::gpu; |
| 103 | + unsigned int offset = i * VL * sizeof(float); |
| 104 | + simd<float, VL> va = block_load<float, VL>(PA, offset); |
| 105 | + simd<float, VL> vc = va + 1; |
| 106 | + block_store(PA, offset, vc); |
| 107 | + }); |
| 108 | + }); |
| 109 | + e.wait(); |
| 110 | + } catch (cl::sycl::exception const &e) { |
| 111 | + std::cout << "SYCL exception caught: " << e.what() << '\n'; |
| 112 | + return 2; |
| 113 | + } |
| 114 | + |
| 115 | + if (checkResult(A, 2)) { |
| 116 | + std::cout << "ESIMD kernel passed\n"; |
| 117 | + } else { |
| 118 | + std::cout << "ESIMD kernel failed\n"; |
| 119 | + return 1; |
| 120 | + } |
| 121 | + return 0; |
| 122 | +} |
0 commit comments