|
| 1 | +// REQUIRES: cuda |
| 2 | + |
| 3 | +// RUN: %{build} -o %t.out |
| 4 | +// RUN: %t.out |
| 5 | + |
| 6 | +#include <iostream> |
| 7 | +#include <sycl/detail/core.hpp> |
| 8 | + |
| 9 | +#include <sycl/ext/oneapi/bindless_images.hpp> |
| 10 | + |
| 11 | +// Uncomment to print additional test information |
| 12 | +// #define VERBOSE_PRINT |
| 13 | + |
| 14 | +class image_kernel; |
| 15 | + |
| 16 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 17 | + |
| 18 | +int main() { |
| 19 | + sycl::device dev; |
| 20 | + sycl::queue q(dev); |
| 21 | + auto ctxt = q.get_context(); |
| 22 | + |
| 23 | + constexpr size_t width = 512; |
| 24 | + std::vector<float> out(width); |
| 25 | + std::vector<float> expected(width); |
| 26 | + std::vector<sycl::float3> dataIn(width); |
| 27 | + float exp = 512; |
| 28 | + for (int i = 0; i < width; i++) { |
| 29 | + expected[i] = exp; |
| 30 | + dataIn[i] = sycl::float3(exp, width, i); |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + // Main point of this test is to check creating an image |
| 35 | + // with a 3-channel format |
| 36 | + syclexp::image_descriptor desc({width}, 3, sycl::image_channel_type::fp32); |
| 37 | + |
| 38 | + syclexp::image_mem imgMem(desc, dev, ctxt); |
| 39 | + |
| 40 | + q.ext_oneapi_copy(dataIn.data(), imgMem.get_handle(), desc); |
| 41 | + q.wait_and_throw(); |
| 42 | + |
| 43 | + // Some backends don't support 3-channel formats |
| 44 | + // We still try to create the image, |
| 45 | + // but we expect it to fail with UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT |
| 46 | + syclexp::unsampled_image_handle imgHandle = |
| 47 | + sycl::ext::oneapi::experimental::create_image(imgMem, desc, dev, ctxt); |
| 48 | + |
| 49 | + sycl::buffer<float> buf(out.data(), width); |
| 50 | + |
| 51 | + q.submit([&](sycl::handler &cgh) { |
| 52 | + sycl::accessor outAcc{buf}; |
| 53 | + |
| 54 | + cgh.parallel_for<image_kernel>(width, [=](sycl::id<1> id) { |
| 55 | +#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) |
| 56 | + // This shouldn't be hit anyway since CUDA doesn't support |
| 57 | + // 3-channel formats, but we need to ensure the kernel can compile |
| 58 | + using pixel_t = sycl::float4; |
| 59 | +#else |
| 60 | + using pixel_t = sycl::float3; |
| 61 | +#endif |
| 62 | + auto pixel = syclexp::fetch_image<pixel_t>(imgHandle, int(id[0])); |
| 63 | + outAcc[id] = pixel[0]; |
| 64 | + }); |
| 65 | + }); |
| 66 | + q.wait_and_throw(); |
| 67 | + |
| 68 | + } catch (const sycl::exception &ex) { |
| 69 | + const std::string_view errMsg(ex.what()); |
| 70 | + if (ctxt.get_backend() == sycl::backend::ext_oneapi_cuda) { |
| 71 | + if (errMsg.find("UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT") != |
| 72 | + std::string::npos) { |
| 73 | + std::cout << "CUDA doesn't support 3-channel formats, test passed." |
| 74 | + << std::endl; |
| 75 | + return 0; |
| 76 | + } |
| 77 | + } |
| 78 | + std::cerr << "Unexpected SYCL exception: " << errMsg << "\n"; |
| 79 | + return 1; |
| 80 | + } catch (...) { |
| 81 | + std::cerr << "Unknown exception caught!\n"; |
| 82 | + return 2; |
| 83 | + } |
| 84 | + |
| 85 | + bool validated = true; |
| 86 | + for (int i = 0; i < width; i++) { |
| 87 | + bool mismatch = false; |
| 88 | + if (out[i] != expected[i]) { |
| 89 | + mismatch = true; |
| 90 | + validated = false; |
| 91 | + } |
| 92 | + |
| 93 | + if (mismatch) { |
| 94 | +#ifdef VERBOSE_PRINT |
| 95 | + std::cout << "Result mismatch! Expected: " << expected[i] |
| 96 | + << ", Actual: " << out[i] << std::endl; |
| 97 | +#else |
| 98 | + break; |
| 99 | +#endif |
| 100 | + } |
| 101 | + } |
| 102 | + if (validated) { |
| 103 | + std::cout << "Test passed!" << std::endl; |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + std::cout << "Test failed!" << std::endl; |
| 108 | + return 3; |
| 109 | +} |
0 commit comments