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
|
use super::{
stream::{self, CU_STREAM_LEGACY},
CUresult, GlobalState,
};
use std::{
ffi::c_void,
mem::{self, size_of},
ptr,
};
pub fn alloc_v2(dptr: *mut *mut c_void, bytesize: usize) -> Result<(), CUresult> {
let ptr = GlobalState::lock_stream(CU_STREAM_LEGACY, |stream_data| {
let dev = unsafe { &mut *(*stream_data.context).device };
let queue = stream_data.cmd_list.as_ref().unwrap();
let ptr = unsafe {
ocl_core::ffi::clSVMAlloc(
dev.ocl_context.as_ptr(),
ocl_core::ffi::CL_MEM_READ_WRITE,
bytesize,
0,
)
};
// CUDA does the same thing and e.g. GeekBench relies on this behavior
let mut event = ptr::null_mut();
let err = unsafe {
ocl_core::ffi::clEnqueueSVMMemFill(
queue.as_ptr(),
ptr,
&0u8 as *const u8 as *const c_void,
1,
bytesize,
0,
ptr::null(),
&mut event,
)
};
assert_eq!(err, 0);
let err = unsafe { ocl_core::ffi::clWaitForEvents(1, &mut event) };
assert_eq!(err, 0);
dev.allocations.insert(ptr);
Ok::<_, CUresult>(ptr)
})??;
unsafe { *dptr = ptr };
Ok(())
}
pub fn copy_v2(dst: *mut c_void, src: *const c_void, bytesize: usize) -> Result<(), CUresult> {
GlobalState::lock_stream(stream::CU_STREAM_LEGACY, |stream_data| {
let dev = unsafe { &*(*stream_data.context).device };
let queue = stream_data.cmd_list.as_ref().unwrap();
let err = unsafe {
ocl_core::ffi::clEnqueueSVMMemcpy(
queue.as_ptr(),
1,
dst,
src,
bytesize,
0,
ptr::null(),
ptr::null_mut(),
)
};
assert_eq!(err, 0);
Ok(())
})?
}
pub fn free_v2(ptr: *mut c_void) -> Result<(), CUresult> {
GlobalState::lock_current_context(|ctx| {
let dev = unsafe { &mut *ctx.device };
unsafe { ocl_core::ffi::clSVMFree(dev.ocl_context.as_ptr(), ptr) };
dev.allocations.remove(&ptr);
Ok(())
})?
}
pub(crate) fn set_d32_v2(dst: *mut c_void, mut ui: u32, n: usize) -> Result<(), CUresult> {
GlobalState::lock_stream(stream::CU_STREAM_LEGACY, move |stream_data| {
let dev = unsafe { &*(*stream_data.context).device };
let queue = stream_data.cmd_list.as_ref().unwrap();
let pattern_size = mem::size_of_val(&ui);
let mut event = ptr::null_mut();
let err = unsafe {
ocl_core::ffi::clEnqueueSVMMemFill(
queue.as_ptr(),
dst,
&ui as *const _ as *const _,
pattern_size,
pattern_size * n,
0,
ptr::null(),
&mut event,
)
};
assert_eq!(err, 0);
let err = unsafe { ocl_core::ffi::clWaitForEvents(1, &mut event) };
assert_eq!(err, 0);
Ok(())
})?
}
pub(crate) fn set_d8_v2(dst: *mut c_void, mut uc: u8, n: usize) -> Result<(), CUresult> {
GlobalState::lock_stream(stream::CU_STREAM_LEGACY, move |stream_data| {
let dev = unsafe { &*(*stream_data.context).device };
let queue = stream_data.cmd_list.as_ref().unwrap();
let pattern_size = mem::size_of_val(&uc);
let mut event = ptr::null_mut();
let err = unsafe {
ocl_core::ffi::clEnqueueSVMMemFill(
queue.as_ptr(),
dst,
&uc as *const _ as *const _,
pattern_size,
pattern_size * n,
0,
ptr::null(),
&mut event,
)
};
assert_eq!(err, 0);
let err = unsafe { ocl_core::ffi::clWaitForEvents(1, &mut event) };
assert_eq!(err, 0);
Ok(())
})?
}
#[cfg(test)]
mod test {
use super::super::test::CudaDriverFns;
use super::super::CUresult;
use std::ptr;
cuda_driver_test!(alloc_without_ctx);
fn alloc_without_ctx<T: CudaDriverFns>() {
assert_eq!(T::cuInit(0), CUresult::CUDA_SUCCESS);
let mut mem = ptr::null_mut();
assert_eq!(
T::cuMemAlloc_v2(&mut mem, std::mem::size_of::<usize>()),
CUresult::CUDA_ERROR_INVALID_CONTEXT
);
assert_eq!(mem, ptr::null_mut());
}
cuda_driver_test!(alloc_with_ctx);
fn alloc_with_ctx<T: CudaDriverFns>() {
assert_eq!(T::cuInit(0), CUresult::CUDA_SUCCESS);
let mut ctx = ptr::null_mut();
assert_eq!(T::cuCtxCreate_v2(&mut ctx, 0, 0), CUresult::CUDA_SUCCESS);
let mut mem = ptr::null_mut();
assert_eq!(
T::cuMemAlloc_v2(&mut mem, std::mem::size_of::<usize>()),
CUresult::CUDA_SUCCESS
);
assert_ne!(mem, ptr::null_mut());
assert_eq!(T::cuCtxDestroy_v2(ctx), CUresult::CUDA_SUCCESS);
}
cuda_driver_test!(free_without_ctx);
fn free_without_ctx<T: CudaDriverFns>() {
assert_eq!(T::cuInit(0), CUresult::CUDA_SUCCESS);
let mut ctx = ptr::null_mut();
assert_eq!(T::cuCtxCreate_v2(&mut ctx, 0, 0), CUresult::CUDA_SUCCESS);
let mut mem = ptr::null_mut();
assert_eq!(
T::cuMemAlloc_v2(&mut mem, std::mem::size_of::<usize>()),
CUresult::CUDA_SUCCESS
);
assert_ne!(mem, ptr::null_mut());
assert_eq!(T::cuCtxDestroy_v2(ctx), CUresult::CUDA_SUCCESS);
assert_eq!(T::cuMemFree_v2(mem), CUresult::CUDA_ERROR_INVALID_VALUE);
}
}
|