|
| 1 | +// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS -fsycl-targets=%sycl_triple %s -o %t.out |
| 2 | +// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out |
| 3 | +// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUN: env SYCL_DEVICE_FILTER=opencl %ACC_RUN_PLACEHOLDER %t.out |
| 5 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 6 | +// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out |
| 7 | +// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out |
| 8 | +// RUN: env SYCL_DEVICE_FILTER=opencl %ACC_RUN_PLACEHOLDER %t.out |
| 9 | + |
| 10 | +//==---------- buffer_allocator.cpp - SYCL buffer allocator tests ----------==// |
| 11 | +// |
| 12 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 13 | +// See https://llvm.org/LICENSE.txt for license information. |
| 14 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +// Tests that the default (and explicit with SYCL 2020) buffer_allocator used by |
| 19 | +// buffers are as defined by the spec and will allocate the right amount of |
| 20 | +// memory on the device. |
| 21 | + |
| 22 | +#include <CL/sycl.hpp> |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +constexpr size_t NumElems = 67; |
| 26 | + |
| 27 | +template <typename T> class TestKernel; |
| 28 | + |
| 29 | +template <typename T, typename AllocT> |
| 30 | +bool checkResult(sycl::buffer<T, 1, AllocT> &Buf, size_t N, std::string &TName, |
| 31 | + std::string AllocTName) { |
| 32 | + auto HostAcc = Buf.get_host_access(); |
| 33 | + |
| 34 | + bool Success = true; |
| 35 | + for (size_t I = 0; I < NumElems; ++I) { |
| 36 | + if (HostAcc[I] != static_cast<T>(I)) { |
| 37 | + std::cout << "Wrong value was written at index " << I << " for buffer<" |
| 38 | + << TName << ", 1, " << AllocTName << ">" << std::endl; |
| 39 | + Success = false; |
| 40 | + } |
| 41 | + } |
| 42 | + return Success; |
| 43 | +} |
| 44 | + |
| 45 | +template <typename T> bool runTest(sycl::queue &Q, std::string TName) { |
| 46 | + sycl::buffer<T, 1> DefaultBuf{NumElems}; |
| 47 | + |
| 48 | +#ifdef SYCL2020_CONFORMANT_APIS |
| 49 | + static_assert(std::is_same_v<decltype(DefaultBuf), |
| 50 | + sycl::buffer<T, 1, sycl::buffer_allocator<T>>>); |
| 51 | + |
| 52 | + sycl::buffer<T, 1, sycl::buffer_allocator<char>> CharAllocBuf{NumElems}; |
| 53 | + sycl::buffer<T, 1, sycl::buffer_allocator<long>> LongAllocBuf{NumElems}; |
| 54 | +#else |
| 55 | + static_assert(std::is_same_v<decltype(DefaultBuf), |
| 56 | + sycl::buffer<T, 1, sycl::buffer_allocator>>); |
| 57 | +#endif |
| 58 | + |
| 59 | + Q.submit([&](sycl::handler &CGH) { |
| 60 | + auto DefaultAcc = DefaultBuf.get_access(CGH); |
| 61 | +#ifdef SYCL2020_CONFORMANT_APIS |
| 62 | + auto CharAllocAcc = CharAllocBuf.get_access(CGH); |
| 63 | + auto LongAllocAcc = LongAllocBuf.get_access(CGH); |
| 64 | +#endif |
| 65 | + CGH.parallel_for<TestKernel<T>>(NumElems, [=](sycl::item<1> It) { |
| 66 | + DefaultAcc[It] = static_cast<T>(It[0]); |
| 67 | +#ifdef SYCL2020_CONFORMANT_APIS |
| 68 | + CharAllocAcc[It] = static_cast<T>(It[0]); |
| 69 | + LongAllocAcc[It] = static_cast<T>(It[0]); |
| 70 | +#endif |
| 71 | + }); |
| 72 | + }).wait(); |
| 73 | + |
| 74 | +#ifdef SYCL2020_CONFORMANT_APIS |
| 75 | + return checkResult(DefaultBuf, NumElems, TName, |
| 76 | + "buffer_allocator<" + TName + ">") && |
| 77 | + checkResult(CharAllocBuf, NumElems, TName, "buffer_allocator<char>") && |
| 78 | + checkResult(LongAllocBuf, NumElems, TName, "buffer_allocator<long>"); |
| 79 | +#else |
| 80 | + return checkResult(DefaultBuf, NumElems, TName, "buffer_allocator"); |
| 81 | +#endif |
| 82 | +} |
| 83 | + |
| 84 | +#define RUN_TEST(TYPE, Q) runTest<TYPE>(Q, #TYPE) |
| 85 | + |
| 86 | +int main() { |
| 87 | + sycl::queue Queue; |
| 88 | + return !(RUN_TEST(char, Queue) && RUN_TEST(unsigned short, Queue) && |
| 89 | + RUN_TEST(float, Queue) && RUN_TEST(long, Queue)); |
| 90 | +} |
| 91 | + |
| 92 | +#undef RUN_TEST |
0 commit comments