|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) Codeplay Software Ltd. |
| 4 | + * |
| 5 | + * Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 6 | + * Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 7 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * SYCLcompat API |
| 16 | + * |
| 17 | + * helloworld.cpp |
| 18 | + * |
| 19 | + * Description: |
| 20 | + * Checks that the SYCLcompat example program compiles and runs |
| 21 | + **************************************************************************/ |
| 22 | + |
| 23 | +// RUN: %{build} -o %t.out |
| 24 | +// RUN: %{run} %t.out |
| 25 | + |
| 26 | +#include <sycl/detail/core.hpp> |
| 27 | + |
| 28 | +// The example uses specific headers but the user can |
| 29 | +// simple include <syclcompat/syclcompat.hpp> to get all the |
| 30 | +// functionality with a single header |
| 31 | + |
| 32 | +#include <syclcompat/device.hpp> |
| 33 | +#include <syclcompat/id_query.hpp> |
| 34 | +#include <syclcompat/launch.hpp> |
| 35 | +#include <syclcompat/memory.hpp> |
| 36 | + |
| 37 | +#include <cstdlib> |
| 38 | +#include <iostream> |
| 39 | + |
| 40 | +#define CHECK_MEMORY(ptr) \ |
| 41 | + if ((ptr) == nullptr) { \ |
| 42 | + std::cerr << "Failed to allocate memory: " << (#ptr) << "\n"; \ |
| 43 | + exit(EXIT_FAILURE); \ |
| 44 | + } |
| 45 | + |
| 46 | +/** |
| 47 | + * Slope intercept form of a straight line equation: Y = m * X + b |
| 48 | + */ |
| 49 | +template <int BLOCK_SIZE> |
| 50 | +void slope_intercept(float *Y, float *X, float m, float b, size_t n) { |
| 51 | + |
| 52 | + // Block index |
| 53 | + size_t bx = syclcompat::work_group_id::x(); |
| 54 | + // Thread index |
| 55 | + size_t tx = syclcompat::local_id::x(); |
| 56 | + |
| 57 | + size_t i = bx * BLOCK_SIZE + tx; |
| 58 | + // or i = syclcompat::global_id::x(); |
| 59 | + if (i < n) |
| 60 | + Y[i] = m * X[i] + b; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Program main |
| 65 | + */ |
| 66 | +int main(int argc, char **argv) { |
| 67 | + std::cout << "Simple Kernel example" << "\n"; |
| 68 | + |
| 69 | + constexpr size_t n_points = 32; |
| 70 | + constexpr float m = 1.5f; |
| 71 | + constexpr float b = 0.5f; |
| 72 | + |
| 73 | + int block_size = 32; |
| 74 | + if (block_size > syclcompat::get_current_device() |
| 75 | + .get_info<sycl::info::device::max_work_group_size>()) { |
| 76 | + block_size = 16; |
| 77 | + } |
| 78 | + |
| 79 | + std::cout << "block_size = " << block_size << ", n_points = " << n_points |
| 80 | + << "\n"; |
| 81 | + |
| 82 | + // Allocate host memory for vectors X and Y |
| 83 | + size_t mem_size = n_points * sizeof(float); |
| 84 | + float *h_X = (float *)syclcompat::malloc_host(mem_size); |
| 85 | + float *h_Y = (float *)syclcompat::malloc_host(mem_size); |
| 86 | + CHECK_MEMORY(h_X); |
| 87 | + CHECK_MEMORY(h_Y); |
| 88 | + |
| 89 | + // Alternative templated allocation for the expected output |
| 90 | + float *h_expected = syclcompat::malloc_host<float>(n_points); |
| 91 | + CHECK_MEMORY(h_expected); |
| 92 | + |
| 93 | + // Initialize host memory & expected output |
| 94 | + for (size_t i = 0; i < n_points; i++) { |
| 95 | + h_X[i] = i + 1; |
| 96 | + h_expected[i] = m * h_X[i] + b; |
| 97 | + } |
| 98 | + |
| 99 | + // Allocate device memory |
| 100 | + float *d_X = (float *)syclcompat::malloc(mem_size); |
| 101 | + float *d_Y = (float *)syclcompat::malloc(mem_size); |
| 102 | + CHECK_MEMORY(d_X); |
| 103 | + CHECK_MEMORY(d_Y); |
| 104 | + |
| 105 | + // copy host memory to device |
| 106 | + syclcompat::memcpy(d_X, h_X, mem_size); |
| 107 | + |
| 108 | + size_t threads = block_size; |
| 109 | + size_t grid = n_points / block_size; |
| 110 | + |
| 111 | + std::cout << "Computing result using SYCL Kernel... "; |
| 112 | + if (block_size == 16) { |
| 113 | + syclcompat::launch<slope_intercept<16>>(grid, threads, d_Y, d_X, m, b, |
| 114 | + n_points); |
| 115 | + } else { |
| 116 | + syclcompat::launch<slope_intercept<32>>(grid, threads, d_Y, d_X, m, b, |
| 117 | + n_points); |
| 118 | + } |
| 119 | + syclcompat::wait(); |
| 120 | + std::cout << "DONE" << "\n"; |
| 121 | + |
| 122 | + // Async copy result from device to host |
| 123 | + syclcompat::memcpy_async(h_Y, d_Y, mem_size).wait(); |
| 124 | + |
| 125 | + // Check output |
| 126 | + for (size_t i = 0; i < n_points; i++) { |
| 127 | + if (std::abs(h_Y[i] - h_expected[i]) >= 1e-6) { |
| 128 | + std::cerr << "Mismatch at index " << i << ": expected " << h_expected[i] |
| 129 | + << ", but got " << h_Y[i] << "\n"; |
| 130 | + exit(EXIT_FAILURE); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Clean up memory |
| 135 | + syclcompat::free(h_X); |
| 136 | + syclcompat::free(h_Y); |
| 137 | + syclcompat::free(h_expected); |
| 138 | + syclcompat::free(d_X); |
| 139 | + syclcompat::free(d_Y); |
| 140 | + |
| 141 | + return EXIT_SUCCESS; |
| 142 | +} |
0 commit comments