Skip to main content

sycl_rs/
range.rs

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
9use crate::Result;
10use sycl_rs_sys::types;
11
12use crate::{
13    event::Event,
14    kernel::{Kernel, KernelArgumentList},
15    private::Sealed,
16    queue::Queue,
17};
18
19/// `Range` is a 1D, 2D or 3D vector that defines the iteration domain of either a single work-group
20/// in a parallel dispatch, or the overall dimensions of the dispatch.
21pub type Range<const DIMENSIONS: usize = 1> = [u64; DIMENSIONS];
22
23/// The `NdRange` struct defines the iteration domain of both the work-groups and the overall
24/// dispatch.
25///
26/// An `NdRange` comprises two [`Range`] parameters: the whole range over which the kernel is to be
27/// executed and the range of each work group.
28#[derive(Clone, Copy, Debug, Hash)]
29pub struct NdRange<const DIMENSIONS: usize = 1> {
30    pub group_size: Range<DIMENSIONS>,
31    pub local_size: Range<DIMENSIONS>,
32}
33
34impl<const DIMENSIONS: usize> NdRange<DIMENSIONS> {
35    pub fn new(group_size: Range<DIMENSIONS>, local_size: Range<DIMENSIONS>) -> Self {
36        Self {
37            group_size,
38            local_size,
39        }
40    }
41}
42
43/// [`NdRange`] types which are limited to 1, 2 or 3 dimensions.
44pub trait ValidDimension: Sealed {
45    /// # Safety
46    ///
47    /// Each argument must match the launched SYCL kernel's signature, including its size,
48    /// layout, and alignment.
49    unsafe fn launch<const ARGC: usize>(
50        &self,
51        queue: &mut Queue,
52        kernel: &Kernel,
53        args: impl KernelArgumentList<ARGC>,
54    ) -> Result<Event>;
55}
56
57impl Sealed for NdRange<1> {}
58impl ValidDimension for NdRange<1> {
59    unsafe fn launch<const ARGC: usize>(
60        &self,
61        queue: &mut Queue,
62        kernel: &Kernel,
63        args: impl KernelArgumentList<ARGC>,
64    ) -> Result<Event> {
65        unsafe {
66            sycl_rs_sys::queue::ffi::launch_1d(
67                &mut queue.0,
68                types::ffi::Range1 {
69                    data: self.group_size,
70                },
71                types::ffi::Range1 {
72                    data: self.local_size,
73                },
74                &kernel.0,
75                &args.as_raw_arg_list(),
76            )
77        }
78        .map(Into::into)
79    }
80}
81
82impl Sealed for NdRange<2> {}
83impl ValidDimension for NdRange<2> {
84    unsafe fn launch<const ARGC: usize>(
85        &self,
86        queue: &mut Queue,
87        kernel: &Kernel,
88        args: impl KernelArgumentList<ARGC>,
89    ) -> Result<Event> {
90        unsafe {
91            sycl_rs_sys::queue::ffi::launch_2d(
92                &mut queue.0,
93                types::ffi::Range2 {
94                    data: self.group_size,
95                },
96                types::ffi::Range2 {
97                    data: self.local_size,
98                },
99                &kernel.0,
100                &args.as_raw_arg_list(),
101            )
102        }
103        .map(Into::into)
104    }
105}
106
107impl Sealed for NdRange<3> {}
108impl ValidDimension for NdRange<3> {
109    unsafe fn launch<const ARGC: usize>(
110        &self,
111        queue: &mut Queue,
112        kernel: &Kernel,
113        args: impl KernelArgumentList<ARGC>,
114    ) -> Result<Event> {
115        unsafe {
116            sycl_rs_sys::queue::ffi::launch_3d(
117                &mut queue.0,
118                types::ffi::Range3 {
119                    data: self.group_size,
120                },
121                types::ffi::Range3 {
122                    data: self.local_size,
123                },
124                &kernel.0,
125                &args.as_raw_arg_list(),
126            )
127        }
128        .map(Into::into)
129    }
130}