|
| 1 | +/// \file |
| 2 | +/// \ingroup tutorial_heterogeneous |
| 3 | +/// Introductory SYCL examples inside ROOT’s interpreter. |
| 4 | +/// |
| 5 | +/// \note This tutorial requires ROOT to be built with **SYCL support** enabled. |
| 6 | +/// Configure CMake with: |
| 7 | +/// `-Dexperimental_adaptivecpp=ON` |
| 8 | +/// to enable SYCL support in the interpreter. |
| 9 | +/// |
| 10 | +/// This tutorial contains two examples: |
| 11 | +/// - `sycladd()`: a minimal kernel computing a sum of two integers |
| 12 | +/// - `syclvectoradd()`: adding two vectors |
| 13 | +/// |
| 14 | +/// \macro_code |
| 15 | +/// |
| 16 | +/// \author Devajith Valaparambil Sreeramaswamy (CERN) |
| 17 | + |
| 18 | +#include <sycl/sycl.hpp> |
| 19 | +#include <iostream> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +void sycladd() |
| 23 | +{ |
| 24 | + sycl::queue q{sycl::cpu_selector_v}; // Use openMP CPU backend |
| 25 | + std::cout << "Running on: " << q.get_device().get_info<sycl::info::device::name>() << "\n"; |
| 26 | + |
| 27 | + int a = 2, b = 3; |
| 28 | + int *sum = sycl::malloc_shared<int>(1, q); |
| 29 | + |
| 30 | + q.single_task([=] { *sum = a + b; }).wait(); |
| 31 | + std::cout << "Sum = " << *sum << '\n'; |
| 32 | + |
| 33 | + sycl::free(sum, q); |
| 34 | +} |
| 35 | + |
| 36 | +void syclvectoradd() |
| 37 | +{ |
| 38 | + sycl::queue q{sycl::default_selector_v}; // Portable backend choice |
| 39 | + const size_t N = 16; |
| 40 | + std::vector<int> A(N, 1), B(N, 2), C(N); |
| 41 | + |
| 42 | + int *a = sycl::malloc_shared<int>(N, q); |
| 43 | + int *b = sycl::malloc_shared<int>(N, q); |
| 44 | + int *c = sycl::malloc_shared<int>(N, q); |
| 45 | + |
| 46 | + std::copy(A.begin(), A.end(), a); |
| 47 | + std::copy(B.begin(), B.end(), b); |
| 48 | + |
| 49 | + q.parallel_for(N, [=](sycl::id<1> i) { c[i] = a[i] + b[i]; }).wait(); |
| 50 | + |
| 51 | + std::copy(c, c + N, C.begin()); |
| 52 | + |
| 53 | + std::cout << "C[0] = " << C[0] << ", C[N-1] = " << C[N - 1] << "\n"; |
| 54 | + |
| 55 | + sycl::free(a, q); |
| 56 | + sycl::free(b, q); |
| 57 | + sycl::free(c, q); |
| 58 | +} |
| 59 | + |
| 60 | +void syclintro() |
| 61 | +{ |
| 62 | + sycladd(); |
| 63 | + syclvectoradd(); |
| 64 | +} |
0 commit comments