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    unsafe fn launch<const ARGC: usize>(
46        &self,
47        queue: &mut Queue,
48        kernel: &Kernel,
49        args: impl KernelArgumentList<ARGC>,
50    ) -> Result<Event>;
51}
52
53impl Sealed for NdRange<1> {}
54impl ValidDimension for NdRange<1> {
55    unsafe fn launch<const ARGC: usize>(
56        &self,
57        queue: &mut Queue,
58        kernel: &Kernel,
59        args: impl KernelArgumentList<ARGC>,
60    ) -> Result<Event> {
61        unsafe {
62            sycl_rs_sys::queue::ffi::launch_1d(
63                &mut queue.0,
64                types::ffi::Range1 {
65                    data: self.group_size,
66                },
67                types::ffi::Range1 {
68                    data: self.local_size,
69                },
70                &kernel.0,
71                &args.as_raw_arg_list(),
72            )
73        }
74        .map(Into::into)
75    }
76}
77
78impl Sealed for NdRange<2> {}
79impl ValidDimension for NdRange<2> {
80    unsafe fn launch<const ARGC: usize>(
81        &self,
82        queue: &mut Queue,
83        kernel: &Kernel,
84        args: impl KernelArgumentList<ARGC>,
85    ) -> Result<Event> {
86        unsafe {
87            sycl_rs_sys::queue::ffi::launch_2d(
88                &mut queue.0,
89                types::ffi::Range2 {
90                    data: self.group_size,
91                },
92                types::ffi::Range2 {
93                    data: self.local_size,
94                },
95                &kernel.0,
96                &args.as_raw_arg_list(),
97            )
98        }
99        .map(Into::into)
100    }
101}
102
103impl Sealed for NdRange<3> {}
104impl ValidDimension for NdRange<3> {
105    unsafe fn launch<const ARGC: usize>(
106        &self,
107        queue: &mut Queue,
108        kernel: &Kernel,
109        args: impl KernelArgumentList<ARGC>,
110    ) -> Result<Event> {
111        unsafe {
112            sycl_rs_sys::queue::ffi::launch_3d(
113                &mut queue.0,
114                types::ffi::Range3 {
115                    data: self.group_size,
116                },
117                types::ffi::Range3 {
118                    data: self.local_size,
119                },
120                &kernel.0,
121                &args.as_raw_arg_list(),
122            )
123        }
124        .map(Into::into)
125    }
126}