|
| 1 | +// REQUIRES: aspect-ext_oneapi_bindless_images |
| 2 | +// REQUIRES: aspect-ext_oneapi_bindless_sampled_image_fetch_1d_usm |
| 3 | +// UNSUPPORTED: target-amd |
| 4 | +// UNSUPPORTED-INTENDED: Sampled fetch not currently supported on AMD |
| 5 | + |
| 6 | +// RUN: %{build} -o %t.out |
| 7 | +// RUN: %{run-unfiltered-devices} env NEOReadDebugKeys=1 UseBindlessMode=1 UseExternalAllocatorForSshAndDsh=1 %t.out |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <sycl/detail/core.hpp> |
| 11 | +#include <sycl/ext/oneapi/bindless_images.hpp> |
| 12 | +#include <sycl/usm.hpp> |
| 13 | + |
| 14 | +class kernel_sampled_fetch; |
| 15 | + |
| 16 | +// Uncomment to print additional test information |
| 17 | +// #define VERBOSE_PRINT |
| 18 | + |
| 19 | +int main() { |
| 20 | + sycl::device dev; |
| 21 | + sycl::queue q(dev); |
| 22 | + auto ctxt = q.get_context(); |
| 23 | + |
| 24 | + // Declare image size, and expected output and actual output vectors |
| 25 | + constexpr size_t width = 32; |
| 26 | + constexpr size_t widthInBytes = width * sizeof(float); |
| 27 | + std::vector<float> out(width); |
| 28 | + std::vector<float> expected(width); |
| 29 | + for (int i = 0; i < width; ++i) { |
| 30 | + expected[i] = static_cast<float>(i); |
| 31 | + } |
| 32 | + |
| 33 | + namespace syclexp = sycl::ext::oneapi::experimental; |
| 34 | + |
| 35 | + try { |
| 36 | + // Extension: image descriptor |
| 37 | + syclexp::image_descriptor desc({width}, 1, sycl::image_channel_type::fp32); |
| 38 | + |
| 39 | + // Extension: Image creation requires a sampler, but it will have no effect |
| 40 | + // on the result, as we will use `fetch_image` in the kernel. |
| 41 | + syclexp::bindless_image_sampler samp( |
| 42 | + sycl::addressing_mode::repeat, |
| 43 | + sycl::coordinate_normalization_mode::normalized, |
| 44 | + sycl::filtering_mode::linear); |
| 45 | + |
| 46 | + // Allocate Host USM and initialize with expected data |
| 47 | + float *imgMem = sycl::malloc_host<float>(width, q); |
| 48 | + memcpy(imgMem, expected.data(), widthInBytes); |
| 49 | + |
| 50 | + // Extension: create the image backed by Host USM and return the handle |
| 51 | + auto imgHandle = syclexp::create_image(imgMem, 0, samp, desc, q); |
| 52 | + |
| 53 | + // Create a buffer to output the result from `fetch_image` |
| 54 | + sycl::buffer outBuf(out.data(), sycl::range{width}); |
| 55 | + q.submit([&](sycl::handler &cgh) { |
| 56 | + sycl::accessor outAcc{outBuf, cgh, sycl::write_only}; |
| 57 | + |
| 58 | + cgh.parallel_for<kernel_sampled_fetch>(width, [=](sycl::id<1> id) { |
| 59 | + // Extension: fetch data from sampled image handle |
| 60 | + outAcc[id] = syclexp::fetch_image<float>(imgHandle, int(id[0])); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + q.wait_and_throw(); |
| 65 | + |
| 66 | + // Extension: cleanup |
| 67 | + syclexp::destroy_image_handle(imgHandle, dev, ctxt); |
| 68 | + sycl::free(imgMem, ctxt); |
| 69 | + } catch (sycl::exception e) { |
| 70 | + std::cerr << "SYCL exception caught! : " << e.what() << "\n"; |
| 71 | + return 1; |
| 72 | + } catch (...) { |
| 73 | + std::cerr << "Unknown exception caught!\n"; |
| 74 | + return 2; |
| 75 | + } |
| 76 | + |
| 77 | + // collect and validate output |
| 78 | + bool validated = true; |
| 79 | + for (int i = 0; i < width; i++) { |
| 80 | + bool mismatch = false; |
| 81 | + if (out[i] != expected[i]) { |
| 82 | + mismatch = true; |
| 83 | + validated = false; |
| 84 | + } |
| 85 | + |
| 86 | + if (mismatch) { |
| 87 | +#ifdef VERBOSE_PRINT |
| 88 | + std::cout << "Result mismatch! Expected: " << expected[i] |
| 89 | + << ", Actual: " << out[i] << std::endl; |
| 90 | +#else |
| 91 | + break; |
| 92 | +#endif |
| 93 | + } |
| 94 | + } |
| 95 | + if (validated) { |
| 96 | + std::cout << "Test passed!" << std::endl; |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + std::cout << "Test failed!" << std::endl; |
| 101 | + return 3; |
| 102 | +} |
0 commit comments