1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
flat_mod!(raw, complex, range);
pub mod map;

#[cfg(feature = "cl1_1")]
flat_mod!(slice, chunks);

use crate::prelude::{Context, RawEvent, RawKernel, Result};
use blaze_proc::docfg;

#[cfg(feature = "svm")]
use crate::svm::{SvmBox, SvmPointer, SvmVec};

pub mod flags;
pub mod rect;

pub unsafe trait KernelPointer<T: Sync> {
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()>;
    fn complete(&self, event: &RawEvent) -> Result<()>;
}

unsafe impl<T: Copy + Sync, C: Context> KernelPointer<T> for Buffer<T, C> {
    #[inline(always)]
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        _wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()> {
        kernel.set_argument::<opencl_sys::cl_mem, _>(idx, self.id_ref())
    }

    #[inline(always)]
    fn complete(&self, _event: &RawEvent) -> Result<()> {
        Ok(())
    }
}

unsafe impl<T: Copy + Sync, C: Context> KernelPointer<T> for rect::RectBuffer2D<T, C> {
    #[inline(always)]
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        _wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()> {
        kernel.set_argument::<opencl_sys::cl_mem, _>(idx, self.id_ref())
    }

    #[inline(always)]
    fn complete(&self, _event: &RawEvent) -> Result<()> {
        Ok(())
    }
}

#[docfg(feature = "svm")]
unsafe impl<T: Sync, C: Context> KernelPointer<T> for SvmBox<[T], C> {
    #[inline]
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()> {
        kernel.set_svm_argument::<T, Self>(idx, self)?;

        if Box::allocator(self).is_coarse() {
            let evt = Box::allocator(self).unmap(SvmPointer::<T>::as_ptr(self) as *mut _, None)?;
            wait.push(evt)
        }

        Ok(())
    }

    #[inline]
    fn complete(&self, event: &RawEvent) -> Result<()> {
        if Box::allocator(self).is_coarse() {
            let alloc = Box::allocator(self);
            let size = core::mem::size_of::<T>() * SvmPointer::<T>::len(self);
            let ptr = self.as_ptr() as *const T as usize;

            unsafe {
                alloc.map_blocking::<{ opencl_sys::CL_MAP_READ | opencl_sys::CL_MAP_WRITE }>(
                    ptr as *mut _,
                    size,
                    Some(core::slice::from_ref(event)),
                )?;
            }
        }

        Ok(())
    }
}

#[docfg(feature = "svm")]
unsafe impl<T: Sync, C: Context> KernelPointer<T> for SvmBox<T, C> {
    #[inline]
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()> {
        kernel.set_svm_argument::<T, Self>(idx, self)?;

        if Box::allocator(self).is_coarse() {
            let evt = Box::allocator(self).unmap(SvmPointer::<T>::as_ptr(self) as *mut _, None)?;
            wait.push(evt)
        }

        Ok(())
    }

    #[inline]
    fn complete(&self, event: &RawEvent) -> Result<()> {
        if Box::allocator(self).is_coarse() {
            let alloc = Box::allocator(self);
            let size = core::mem::size_of::<T>();
            let ptr = self.as_ptr() as *const T as usize;

            unsafe {
                alloc.map_blocking::<{ opencl_sys::CL_MAP_READ | opencl_sys::CL_MAP_WRITE }>(
                    ptr as *mut _,
                    size,
                    Some(core::slice::from_ref(event)),
                )?;
            }
        }

        Ok(())
    }
}

#[docfg(feature = "svm")]
unsafe impl<T: Sync, C: Context> KernelPointer<T> for SvmVec<T, C> {
    #[inline]
    unsafe fn set_arg(
        &self,
        kernel: &mut RawKernel,
        wait: &mut Vec<RawEvent>,
        idx: u32,
    ) -> Result<()> {
        kernel.set_svm_argument::<T, Self>(idx, self)?;

        if Vec::allocator(self).is_coarse() {
            let evt = Vec::allocator(self).unmap(SvmPointer::<T>::as_ptr(self) as *mut _, None)?;
            wait.push(evt)
        }

        Ok(())
    }

    #[inline]
    fn complete(&self, event: &RawEvent) -> Result<()> {
        if Vec::allocator(self).is_coarse() {
            let alloc = Vec::allocator(self);
            let size = core::mem::size_of::<T>() * SvmPointer::<T>::len(self);
            let ptr = self.as_ptr() as *const T as usize;

            unsafe {
                alloc.map_blocking::<{ opencl_sys::CL_MAP_READ | opencl_sys::CL_MAP_WRITE }>(
                    ptr as *mut _,
                    size,
                    Some(core::slice::from_ref(event)),
                )?;
            }
        }

        Ok(())
    }
}

/*
unsafe impl<T: Sync, P: KernelPointer<T>> KernelPointer<MaybeUninit<T>> for P {
    #[inline(always)]
    unsafe fn set_arg (&self, kernel: &mut RawKernel, wait: &mut Vec<RawEvent>, idx: u32) -> Result<()> {
        <Self as KernelPointer<T>>::set_arg(&self, kernel, wait, idx)
    }

    #[inline(always)]
    fn complete (&self, event: &RawEvent) -> Result<()> {
        <Self as KernelPointer<T>>::complete(&self, event)
    }
}
*/

// Whenever [this](https://github.com/rust-lang/rust/issues/48869) is fixed, this will be the generic implementation

/*#[docfg(feature = "svm")]
unsafe impl<T: Sync, P: SvmPointer<T>> KernelPointer<T> for P where P::Context: 'static + Send + Clone {
    #[inline]
    unsafe fn set_arg (&self, kernel: &mut RawKernel, wait: &mut Vec<RawEvent>, idx: u32) -> Result<()> {
        kernel.set_svm_argument(idx, self)?;

        if self.allocator().is_coarse() {
            let evt = self.allocator().unmap(self.as_ptr() as *mut _, WaitList::EMPTY)?;
            wait.push(evt)
        }

        Ok(())
    }

    #[inline]
    fn complete (&self, event: &RawEvent) -> Result<()> {
        if self.allocator().is_coarse() {
            let size = core::mem::size_of::<T>() * self.len();

            unsafe {
                let _ = self.allocator().map::<&RawEvent, {CL_MAP_READ | CL_MAP_WRITE}>(self.as_ptr(), size, event)?;
            }
        }

        Ok(())
    }
}*/