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