|
| 1 | +// |
| 2 | +// Copyright (C) 2026 Intel Corporation |
| 3 | +// |
| 4 | +// Under the MIT License or the Apache License v2.0. |
| 5 | +// See LICENSE-MIT and LICENSE-APACHE for license information. |
| 6 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 7 | +// |
| 8 | + |
| 9 | +use oneapi_rs::{buffer::Buffer, kernel_bundle::{KernelArgument, KernelArgumentList}, queue::Queue, usm::{SharedAllocator, UsmAllocator}}; |
| 10 | + |
| 11 | +static IOTA_SRC: &str = r#" |
| 12 | +#include <sycl/sycl.hpp> |
| 13 | +namespace syclext = sycl::ext::oneapi; |
| 14 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 15 | +
|
| 16 | +extern "C" |
| 17 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 18 | +void iota(float start, float *ptr) { |
| 19 | + size_t id = syclext::this_work_item::get_nd_item<1>().get_global_linear_id(); |
| 20 | + ptr[id] = start + static_cast<float>(id); |
| 21 | +} |
| 22 | +"#; |
| 23 | + |
| 24 | +struct IotaArgs<'a> { |
| 25 | + start: f32, |
| 26 | + buffer: &'a mut Buffer<f32, UsmAllocator<SharedAllocator>> |
| 27 | +} |
| 28 | + |
| 29 | +unsafe impl<'a> KernelArgumentList<2> for IotaArgs<'a> { |
| 30 | + unsafe fn as_raw_arg_list(&self) -> [&[u8]; 2] { |
| 31 | + return [ |
| 32 | + unsafe { self.start.as_raw_arg() }, |
| 33 | + unsafe { self.buffer.as_raw_arg() } |
| 34 | + ] |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn main() { |
| 39 | + let mut queue = Queue::new(); |
| 40 | + let mut buffer = queue.alloc_shared::<f32>(1024).wait(); |
| 41 | + |
| 42 | + let kernel = queue.get_context() |
| 43 | + .create_kernel_bundle_from_source(IOTA_SRC) |
| 44 | + .build() |
| 45 | + .get_kernel("iota"); |
| 46 | + |
| 47 | + unsafe { queue.launch(&kernel, IotaArgs { start: 3.14, buffer: &mut buffer }) }.wait(); |
| 48 | + |
| 49 | + for e in buffer.iter() { |
| 50 | + print!("{e} "); |
| 51 | + } |
| 52 | + println!(); |
| 53 | +} |
0 commit comments