|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 2 | +// RUN: %HOST_RUN_PLACEHOLDER %t.out |
| 3 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 5 | + |
| 6 | +//==------------------- image.cpp - SYCL image basic test -----------------==// |
| 7 | +// |
| 8 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | +// See https://llvm.org/LICENSE.txt for license information. |
| 10 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include <CL/sycl.hpp> |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "../../helpers.hpp" |
| 20 | + |
| 21 | +using namespace cl; |
| 22 | + |
| 23 | +int main() { |
| 24 | + const sycl::image_channel_order ChanOrder = sycl::image_channel_order::rgba; |
| 25 | + const sycl::image_channel_type ChanType = sycl::image_channel_type::fp32; |
| 26 | + |
| 27 | + constexpr auto SYCLRead = sycl::access::mode::read; |
| 28 | + constexpr auto SYCLWrite = sycl::access::mode::write; |
| 29 | + |
| 30 | + const sycl::range<2> Img1Size(4, 4); |
| 31 | + const sycl::range<2> Img2Size(4, 4); |
| 32 | + |
| 33 | + std::vector<sycl::float4> Img1HostData(Img1Size.size(), {1, 2, 3, 4}); |
| 34 | + std::vector<sycl::float4> Img2HostData(Img2Size.size(), {0, 0, 0, 0}); |
| 35 | + |
| 36 | + { |
| 37 | + sycl::image<2> Img1(Img1HostData.data(), ChanOrder, ChanType, Img1Size); |
| 38 | + sycl::image<2> Img2(Img2HostData.data(), ChanOrder, ChanType, Img2Size); |
| 39 | + TestQueue Q{sycl::default_selector()}; |
| 40 | + Q.submit([&](sycl::handler &CGH) { |
| 41 | + auto Img1Acc = Img1.get_access<sycl::float4, SYCLRead>(CGH); |
| 42 | + auto Img2Acc = Img2.get_access<sycl::float4, SYCLWrite>(CGH); |
| 43 | + |
| 44 | + CGH.parallel_for<class ImgCopy>(Img1Size, [=](sycl::item<2> Item) { |
| 45 | + sycl::float4 Data = Img1Acc.read(sycl::int2{Item[0], Item[1]}); |
| 46 | + Img2Acc.write(sycl::int2{Item[0], Item[1]}, Data); |
| 47 | + }); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + for (int X = 0; X < Img2Size[0]; ++X) |
| 52 | + for (int Y = 0; Y < Img2Size[1]; ++Y) { |
| 53 | + sycl::float4 Vec1 = Img1HostData[X * Img1Size[1] + Y]; |
| 54 | + sycl::float4 Vec2 = Img2HostData[X * Img2Size[1] + Y]; |
| 55 | + |
| 56 | + if (sycl::any(sycl::isnotequal(Vec1, Vec2))) { |
| 57 | + std::cerr << "Failed" << std::endl; |
| 58 | + std::cerr << "Element [ " << X << ", " << Y << " ]" << std::endl; |
| 59 | + std::cerr << "Expected: " << printableVec(Vec1) << std::endl; |
| 60 | + std::cerr << " Got : " << printableVec(Vec2) << std::endl; |
| 61 | + return 1; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + { |
| 66 | + const sycl::range<1> ImgPitch(4 * 4 * 4 * 2); |
| 67 | + sycl::image<2> Img(ChanOrder, ChanType, Img1Size, ImgPitch); |
| 68 | + TestQueue Q{sycl::default_selector()}; |
| 69 | + Q.submit([&](sycl::handler &CGH) { |
| 70 | + auto ImgAcc = Img.get_access<sycl::float4, SYCLRead>(CGH); |
| 71 | + CGH.single_task<class EmptyKernel>([=]() { ImgAcc.get_range(); }); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + const sycl::range<1> ImgPitch(4 * 4 * 4 * 2); |
| 77 | + sycl::image<2> Img(Img1HostData.data(), ChanOrder, ChanType, Img1Size, |
| 78 | + ImgPitch); |
| 79 | + TestQueue Q{sycl::default_selector()}; |
| 80 | + Q.submit([&](sycl::handler &CGH) { |
| 81 | + auto ImgAcc = Img.get_access<sycl::float4, SYCLRead>(CGH); |
| 82 | + CGH.single_task<class ConstTestPitch>([=] { ImgAcc.get_range(); }); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + // image with write accessor to it in kernel |
| 87 | + { |
| 88 | + int NX = 32; |
| 89 | + int NY = 32; |
| 90 | + |
| 91 | + sycl::image<2> Img(sycl::image_channel_order::rgba, |
| 92 | + sycl::image_channel_type::fp32, sycl::range<2>(NX, NY)); |
| 93 | + |
| 94 | + sycl::queue Q; |
| 95 | + Q.submit([&](sycl::handler &CGH) { |
| 96 | + auto ImgAcc = |
| 97 | + Img.get_access<sycl::float4, sycl::access::mode::write>(CGH); |
| 98 | + |
| 99 | + sycl::nd_range<2> Rng(sycl::range<2>(NX, NY), sycl::range<2>(16, 16)); |
| 100 | + |
| 101 | + CGH.parallel_for<class sample>(Rng, [=](sycl::nd_item<2> Item) { |
| 102 | + sycl::id<2> Idx = Item.get_global_id(); |
| 103 | + sycl::float4 C(0.5f, 0.5f, 0.2f, 1.0f); |
| 104 | + ImgAcc.write(sycl::int2(Idx[0], Idx[1]), C); |
| 105 | + }); |
| 106 | + }).wait_and_throw(); |
| 107 | + } |
| 108 | + |
| 109 | + std::cout << "Success" << std::endl; |
| 110 | + return 0; |
| 111 | +} |
0 commit comments