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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
use super::{context, transmute_lifetime, transmute_lifetime_mut, CUresult, GlobalState};
use crate::cuda;
use cuda::{CUdevice_attribute, CUuuid_st};
use ocl_core::{ClDeviceIdPtr, ContextProperties, DeviceType};
use std::{
cmp,
collections::HashSet,
ffi::c_void,
mem,
os::raw::{c_char, c_int, c_uint},
ptr,
sync::atomic::{AtomicU32, Ordering},
};
const PROJECT_URL_SUFFIX_SHORT: &'static str = " [ZLUDA]";
const PROJECT_URL_SUFFIX_LONG: &'static str = " [github.com/vosen/ZLUDA]";
#[repr(transparent)]
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct Index(pub c_int);
pub struct Device {
pub index: Index,
pub ocl_base: ocl_core::DeviceId,
pub default_queue: ocl_core::CommandQueue,
pub ocl_context: ocl_core::Context,
pub primary_context: context::Context,
pub allocations: HashSet<*mut c_void>,
pub is_amd: bool,
pub name: String,
}
unsafe impl Send for Device {}
impl Device {
pub fn new(
platform: ocl_core::PlatformId,
ocl_dev: ocl_core::DeviceId,
idx: usize,
is_amd: bool,
) -> Result<Self, CUresult> {
let mut props = ocl_core::ContextProperties::new();
props.set_platform(platform);
let ctx = ocl_core::create_context(Some(&props), &[ocl_dev], None, None)?;
let queue = ocl_core::create_command_queue(&ctx, ocl_dev, None)?;
let primary_context =
context::Context::new(context::ContextData::new(0, true, ptr::null_mut())?);
let props = ocl_core::get_device_info(ocl_dev, ocl_core::DeviceInfo::Name)?;
let name = if let ocl_core::DeviceInfoResult::Name(name) = props {
Ok(name)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}?;
Ok(Self {
index: Index(idx as c_int),
ocl_base: ocl_dev,
default_queue: queue,
ocl_context: ctx,
primary_context,
allocations: HashSet::new(),
is_amd,
name,
})
}
pub fn late_init(&mut self) {
self.primary_context.as_option_mut().unwrap().device = self as *mut _;
}
}
pub fn get_count(count: *mut c_int) -> Result<(), CUresult> {
let len = GlobalState::lock(|state| state.devices.len())?;
unsafe { *count = len as c_int };
Ok(())
}
pub fn get(device: *mut Index, ordinal: c_int) -> Result<(), CUresult> {
if device == ptr::null_mut() || ordinal < 0 {
return Err(CUresult::CUDA_ERROR_INVALID_VALUE);
}
let len = GlobalState::lock(|state| state.devices.len())?;
if ordinal < (len as i32) {
unsafe { *device = Index(ordinal) };
Ok(())
} else {
Err(CUresult::CUDA_ERROR_INVALID_VALUE)
}
}
pub fn get_name(name: *mut c_char, len: i32, dev_idx: Index) -> Result<(), CUresult> {
if name == ptr::null_mut() || len < 0 {
return Err(CUresult::CUDA_ERROR_INVALID_VALUE);
}
let name_string = GlobalState::lock_device(dev_idx, |dev| dev.name.clone())?;
let mut dst_null_pos = cmp::min((len - 1) as usize, name_string.len());
unsafe { std::ptr::copy_nonoverlapping(name_string.as_ptr() as *const _, name, dst_null_pos) };
if name_string.len() + PROJECT_URL_SUFFIX_LONG.len() < (len as usize) {
unsafe {
std::ptr::copy_nonoverlapping(
PROJECT_URL_SUFFIX_LONG.as_ptr(),
name.add(name_string.len()) as *mut _,
PROJECT_URL_SUFFIX_LONG.len(),
)
};
dst_null_pos += PROJECT_URL_SUFFIX_LONG.len();
} else if name_string.len() + PROJECT_URL_SUFFIX_SHORT.len() < (len as usize) {
unsafe {
std::ptr::copy_nonoverlapping(
PROJECT_URL_SUFFIX_SHORT.as_ptr(),
name.add(name_string.len()) as *mut _,
PROJECT_URL_SUFFIX_SHORT.len(),
)
};
dst_null_pos += PROJECT_URL_SUFFIX_SHORT.len();
}
unsafe { *(name.add(dst_null_pos)) = 0 };
Ok(())
}
pub fn total_mem_v2(bytes: *mut usize, dev_idx: Index) -> Result<(), CUresult> {
if bytes == ptr::null_mut() {
return Err(CUresult::CUDA_ERROR_INVALID_VALUE);
}
let mem_size = GlobalState::lock_device(dev_idx, |dev| {
let props = ocl_core::get_device_info(dev.ocl_base, ocl_core::DeviceInfo::GlobalMemSize)?;
if let ocl_core::DeviceInfoResult::GlobalMemSize(mem_size) = props {
Ok(mem_size)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??;
unsafe { *bytes = mem_size as usize };
Ok(())
}
impl CUdevice_attribute {
fn get_static_value(self) -> Option<i32> {
match self {
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_GPU_OVERLAP => Some(1),
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT => Some(1),
// TODO: go back to this once we have more funcitonality implemented
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR => Some(8),
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR => Some(0),
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_CAN_MAP_HOST_MEMORY => Some(1),
_ => None,
}
}
}
pub fn get_attribute(
pi: *mut i32,
attrib: CUdevice_attribute,
dev_idx: Index,
) -> Result<(), CUresult> {
if pi == ptr::null_mut() {
return Err(CUresult::CUDA_ERROR_INVALID_VALUE);
}
if let Some(value) = attrib.get_static_value() {
unsafe { *pi = value };
return Ok(());
}
let value = match attrib {
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_INTEGRATED => {
GlobalState::lock_device(dev_idx, |dev| if dev.is_amd { 0i32 } else { 1i32 })?
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_ASYNC_ENGINE_COUNT => 1,
// Streaming Multiprocessor corresponds roughly to a sub-slice (thread group can't cross either)
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT => {
GlobalState::lock_device(dev_idx, |dev| {
let props =
ocl_core::get_device_info(dev.ocl_base, ocl_core::DeviceInfo::MaxComputeUnits)?;
if let ocl_core::DeviceInfoResult::MaxComputeUnits(count) = props {
Ok(count as i32)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??
}
// I honestly don't know how to answer this query
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR => {
GlobalState::lock_device(dev_idx, |dev| {
if !dev.is_amd {
7 // correct for GEN9
} else {
4i32 * 32 // probably correct for RDNA
}
})?
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK => {
GlobalState::lock_device(dev_idx, |dev| {
let props = ocl_core::get_device_info(
dev.ocl_base,
ocl_core::DeviceInfo::MaxWorkGroupSize,
)?;
if let ocl_core::DeviceInfoResult::MaxWorkGroupSize(size) = props {
Ok(size as i32)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X => {
GlobalState::lock_device(dev_idx, |dev| {
let props = ocl_core::get_device_info(
dev.ocl_base,
ocl_core::DeviceInfo::MaxWorkItemSizes,
)?;
if let ocl_core::DeviceInfoResult::MaxWorkItemSizes(sizes) = props {
Ok(sizes)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??[0] as i32
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y => {
GlobalState::lock_device(dev_idx, |dev| {
let props = ocl_core::get_device_info(
dev.ocl_base,
ocl_core::DeviceInfo::MaxWorkItemSizes,
)?;
if let ocl_core::DeviceInfoResult::MaxWorkItemSizes(sizes) = props {
Ok(sizes)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??[1] as i32
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z => {
GlobalState::lock_device(dev_idx, |dev| {
let props = ocl_core::get_device_info(
dev.ocl_base,
ocl_core::DeviceInfo::MaxWorkItemSizes,
)?;
if let ocl_core::DeviceInfoResult::MaxWorkItemSizes(sizes) = props {
Ok(sizes)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})??[2] as i32
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK => {
GlobalState::lock_device(dev_idx, |dev| {
let props =
ocl_core::get_device_info(dev.ocl_base, ocl_core::DeviceInfo::LocalMemSize)?;
if let ocl_core::DeviceInfoResult::LocalMemSize(size) = props {
Ok(size)
} else {
Err(CUresult::CUDA_ERROR_UNKNOWN)
}
})?? as i32
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_WARP_SIZE => 32,
_ => {
// TODO: support more attributes for CUDA runtime
/*
return Err(l0::Error(
l0::sys::ze_result_t::ZE_RESULT_ERROR_UNSUPPORTED_FEATURE,
))
*/
0
}
};
unsafe { *pi = value };
Ok(())
}
pub fn get_uuid(uuid: *mut CUuuid_st, dev_idx: Index) -> Result<(), CUresult> {
unsafe {
*uuid = CUuuid_st {
bytes: mem::zeroed(),
}
};
Ok(())
}
// TODO: add support if Level 0 exposes it
pub fn get_luid(
luid: *mut c_char,
dev_node_mask: *mut c_uint,
_dev_idx: Index,
) -> Result<(), CUresult> {
unsafe { ptr::write_bytes(luid, 0u8, 8) };
unsafe { *dev_node_mask = 0 };
Ok(())
}
pub fn primary_ctx_get_state(
dev_idx: Index,
flags: *mut u32,
active: *mut i32,
) -> Result<(), CUresult> {
let (is_active, flags_value) = GlobalState::lock_device(dev_idx, |dev| {
// This is safe because primary context can't be dropped
let ctx_ptr = &mut dev.primary_context as *mut _;
let flags_ptr =
(&unsafe { dev.primary_context.as_ref_unchecked() }.flags) as *const AtomicU32;
let is_active = context::CONTEXT_STACK
.with(|stack| stack.borrow().last().map(|x| *x))
.map(|current| current == ctx_ptr)
.unwrap_or(false);
let flags_value = unsafe { &*flags_ptr }.load(Ordering::Relaxed);
Ok::<_, CUresult>((is_active, flags_value))
})??;
unsafe { *active = if is_active { 1 } else { 0 } };
unsafe { *flags = flags_value };
Ok(())
}
pub fn primary_ctx_retain(
pctx: *mut *mut context::Context,
dev_idx: Index,
) -> Result<(), CUresult> {
let ctx_ptr = GlobalState::lock_device(dev_idx, |dev| &mut dev.primary_context as *mut _)?;
unsafe { *pctx = ctx_ptr };
Ok(())
}
// TODO: allow for retain/reset/release of primary context
pub(crate) fn primary_ctx_release_v2(_dev_idx: Index) -> CUresult {
CUresult::CUDA_SUCCESS
}
|