|
| 1 | +// REQUIRES: aspect-ext_oneapi_bindless_images |
| 2 | +// REQUIRES: aspect-ext_oneapi_bindless_images_sample_1d_usm |
| 3 | + |
| 4 | +// UNSUPPORTED: hip |
| 5 | +// UNSUPPORTED-INTENDED: Host USM backed image support is not yet enabled in UR |
| 6 | +// adapter. Also, when provionally enabled, the test crashes upon image |
| 7 | +// creation, whereas Device USM backed images do not crash. This issue is |
| 8 | +// undetermined. |
| 9 | + |
| 10 | +// RUN: %{build} -o %t.out |
| 11 | +// RUN: %{run-unfiltered-devices} env NEOReadDebugKeys=1 UseBindlessMode=1 UseExternalAllocatorForSshAndDsh=1 %t.out |
| 12 | + |
| 13 | +#include <cmath> |
| 14 | +#include <iostream> |
| 15 | +#include <sycl/detail/core.hpp> |
| 16 | + |
| 17 | +#include <sycl/ext/oneapi/bindless_images.hpp> |
| 18 | +#include <sycl/usm.hpp> |
| 19 | + |
| 20 | +// Uncomment to print additional test information |
| 21 | +// #define VERBOSE_PRINT |
| 22 | + |
| 23 | +class sample_host_usm_image_kernel; |
| 24 | + |
| 25 | +int main() { |
| 26 | + |
| 27 | + sycl::device dev; |
| 28 | + sycl::queue q(dev); |
| 29 | + auto ctxt = q.get_context(); |
| 30 | + |
| 31 | + // declare image data |
| 32 | + size_t width = 32; |
| 33 | + size_t widthInBytes = width * sizeof(float); |
| 34 | + std::vector<float> out(width); |
| 35 | + std::vector<float> expected(width); |
| 36 | + for (int i = 0; i < width; ++i) { |
| 37 | + expected[i] = static_cast<float>(i); |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + sycl::ext::oneapi::experimental::bindless_image_sampler samp( |
| 42 | + sycl::addressing_mode::clamp, |
| 43 | + sycl::coordinate_normalization_mode::normalized, |
| 44 | + sycl::filtering_mode::linear); |
| 45 | + |
| 46 | + // Extension: image descriptor |
| 47 | + sycl::ext::oneapi::experimental::image_descriptor desc( |
| 48 | + {width}, 1, sycl::image_channel_type::fp32); |
| 49 | + |
| 50 | + // Host USM allocation |
| 51 | + float *imgMem = sycl::malloc_host<float>(width, ctxt); |
| 52 | + |
| 53 | + if (imgMem == nullptr) { |
| 54 | + std::cerr << "Error allocating host USM!" << std::endl; |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + // Initialize input data |
| 59 | + for (int i = 0; i < width; ++i) { |
| 60 | + imgMem[i] = static_cast<float>(i); |
| 61 | + } |
| 62 | + |
| 63 | + // Extension: create the image and return the handle |
| 64 | + sycl::ext::oneapi::experimental::sampled_image_handle imgHandle = |
| 65 | + sycl::ext::oneapi::experimental::create_image(imgMem, 0 /* pitch */, |
| 66 | + samp, desc, dev, ctxt); |
| 67 | + |
| 68 | + sycl::buffer<float, 1> buf((float *)out.data(), sycl::range<1>{width}); |
| 69 | + q.submit([&](sycl::handler &cgh) { |
| 70 | + auto outAcc = |
| 71 | + buf.get_access<sycl::access_mode::write>(cgh, sycl::range<1>{width}); |
| 72 | + |
| 73 | + cgh.parallel_for<sample_host_usm_image_kernel>( |
| 74 | + sycl::nd_range<1>{{width}, {width}}, [=](sycl::nd_item<1> it) { |
| 75 | + size_t dim0 = it.get_local_id(0); |
| 76 | + |
| 77 | + // Normalize coordinates -- +0.5 to look towards centre of pixel |
| 78 | + float fdim0 = float(dim0 + 0.5f) / (float)width; |
| 79 | + |
| 80 | + // Extension: sample image data from handle |
| 81 | + float px = sycl::ext::oneapi::experimental::sample_image<float>( |
| 82 | + imgHandle, (float)fdim0); |
| 83 | + |
| 84 | + outAcc[sycl::id<1>{dim0}] = px; |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + q.wait_and_throw(); |
| 89 | + |
| 90 | + // Extension: cleanup |
| 91 | + sycl::ext::oneapi::experimental::destroy_image_handle(imgHandle, dev, ctxt); |
| 92 | + sycl::free(imgMem, ctxt); |
| 93 | + } catch (sycl::exception e) { |
| 94 | + std::cerr << "SYCL exception caught! : " << e.what() << "\n"; |
| 95 | + return 1; |
| 96 | + } catch (...) { |
| 97 | + std::cerr << "Unknown exception caught!\n"; |
| 98 | + return 2; |
| 99 | + } |
| 100 | + |
| 101 | + // collect and validate output |
| 102 | + bool validated = true; |
| 103 | + for (int i = 0; i < width; i++) { |
| 104 | + bool mismatch = false; |
| 105 | + if (out[i] != expected[i]) { |
| 106 | + mismatch = true; |
| 107 | + validated = false; |
| 108 | + } |
| 109 | + |
| 110 | + if (mismatch) { |
| 111 | +#ifdef VERBOSE_PRINT |
| 112 | + std::cout << "Result mismatch! Expected: " << expected[i] |
| 113 | + << ", Actual: " << out[i] << std::endl; |
| 114 | +#else |
| 115 | + break; |
| 116 | +#endif |
| 117 | + } |
| 118 | + } |
| 119 | + if (validated) { |
| 120 | + std::cout << "Test passed!" << std::endl; |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + std::cout << "Test failed!" << std::endl; |
| 125 | + return 3; |
| 126 | +} |
0 commit comments