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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
use cuda_types::*;
use hip_runtime_sys::*;
use std::{mem, ptr};
use super::context;
const PROJECT_SUFFIX: &[u8] = b" [ZLUDA]\0";
pub const COMPUTE_CAPABILITY_MAJOR: i32 = 8;
pub const COMPUTE_CAPABILITY_MINOR: i32 = 8;
pub(crate) fn compute_capability(major: &mut i32, minor: &mut i32, _dev: hipDevice_t) -> CUresult {
*major = COMPUTE_CAPABILITY_MAJOR;
*minor = COMPUTE_CAPABILITY_MINOR;
Ok(())
}
pub(crate) fn get(device: *mut hipDevice_t, ordinal: i32) -> hipError_t {
unsafe { hipDeviceGet(device, ordinal) }
}
#[allow(warnings)]
trait DeviceAttributeNames {
const hipDeviceAttributeGpuOverlap: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeDeviceOverlap;
const hipDeviceAttributeMaximumTexture1DWidth: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture1DWidth;
const hipDeviceAttributeMaximumTexture2DWidth: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture2DWidth;
const hipDeviceAttributeMaximumTexture2DHeight: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture2DHeight;
const hipDeviceAttributeMaximumTexture3DWidth: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture3DWidth;
const hipDeviceAttributeMaximumTexture3DHeight: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture3DHeight;
const hipDeviceAttributeMaximumTexture3DDepth: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxTexture3DDepth;
const hipDeviceAttributeGlobalMemoryBusWidth: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMemoryBusWidth;
const hipDeviceAttributeMaxThreadsPerMultiprocessor: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMaxThreadsPerMultiProcessor;
const hipDeviceAttributeAsyncEngineCount: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeConcurrentKernels;
const hipDeviceAttributePciDomainId: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributePciDomainID;
const hipDeviceAttributeMultiGpuBoard: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeIsMultiGpuBoard;
const hipDeviceAttributeMultiGpuBoardGroupId: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeMultiGpuBoardGroupID;
const hipDeviceAttributeMaxSharedMemoryPerBlockOptin: hipDeviceAttribute_t =
hipDeviceAttribute_t::hipDeviceAttributeSharedMemPerBlockOptin;
}
impl DeviceAttributeNames for hipDeviceAttribute_t {}
macro_rules! remap_attribute {
($attrib:expr => $([ $($word:expr)* ]),*,) => {
match $attrib {
$(
paste::paste! { CUdevice_attribute:: [< CU_DEVICE_ATTRIBUTE $(_ $word:upper)* >] } => {
paste::paste! { hipDeviceAttribute_t:: [< hipDeviceAttribute $($word:camel)* >] }
}
)*
_ => return Err(hipErrorCode_t::NotSupported)
}
}
}
pub(crate) fn get_attribute(
pi: &mut i32,
attrib: CUdevice_attribute,
dev_idx: hipDevice_t,
) -> hipError_t {
match attrib {
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_WARP_SIZE => {
*pi = 32;
return Ok(());
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_TCC_DRIVER => {
*pi = 0;
return Ok(());
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR => {
*pi = COMPUTE_CAPABILITY_MAJOR;
return Ok(());
}
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR => {
*pi = COMPUTE_CAPABILITY_MINOR;
return Ok(());
}
_ => {}
}
let attrib = remap_attribute! {
attrib =>
[MAX THREADS PER BLOCK],
[MAX BLOCK DIM X],
[MAX BLOCK DIM Y],
[MAX BLOCK DIM Z],
[MAX GRID DIM X],
[MAX GRID DIM Y],
[MAX GRID DIM Z],
[MAX SHARED MEMORY PER BLOCK],
[TOTAL CONSTANT MEMORY],
//[WARP SIZE],
[MAX PITCH],
[MAX REGISTERS PER BLOCK],
[CLOCK RATE],
[TEXTURE ALIGNMENT],
[GPU OVERLAP],
[MULTIPROCESSOR COUNT],
[KERNEL EXEC TIMEOUT],
[INTEGRATED],
[CAN MAP HOST MEMORY],
[COMPUTE MODE],
[MAXIMUM TEXTURE1D WIDTH],
[MAXIMUM TEXTURE2D WIDTH],
[MAXIMUM TEXTURE2D HEIGHT],
[MAXIMUM TEXTURE3D WIDTH],
[MAXIMUM TEXTURE3D HEIGHT],
[MAXIMUM TEXTURE3D DEPTH],
//[MAXIMUM TEXTURE2D LAYERED WIDTH],
//[MAXIMUM TEXTURE2D LAYERED HEIGHT],
//[MAXIMUM TEXTURE2D LAYERED LAYERS],
//[MAXIMUM TEXTURE2D ARRAY WIDTH],
//[MAXIMUM TEXTURE2D ARRAY HEIGHT],
//[MAXIMUM TEXTURE2D ARRAY NUMSLICES],
[SURFACE ALIGNMENT],
[CONCURRENT KERNELS],
[ECC ENABLED],
[PCI BUS ID],
[PCI DEVICE ID],
//[TCC DRIVER],
[MEMORY CLOCK RATE],
[GLOBAL MEMORY BUS WIDTH],
[L2 CACHE SIZE],
[MAX THREADS PER MULTIPROCESSOR],
[ASYNC ENGINE COUNT],
[UNIFIED ADDRESSING],
//[MAXIMUM TEXTURE1D LAYERED WIDTH],
//[MAXIMUM TEXTURE1D LAYERED LAYERS],
//[CAN TEX2D GATHER],
//[MAXIMUM TEXTURE2D GATHER WIDTH],
//[MAXIMUM TEXTURE2D GATHER HEIGHT],
//[MAXIMUM TEXTURE3D WIDTH ALTERNATE],
//[MAXIMUM TEXTURE3D HEIGHT ALTERNATE],
//[MAXIMUM TEXTURE3D DEPTH ALTERNATE],
[PCI DOMAIN ID],
[TEXTURE PITCH ALIGNMENT],
//[MAXIMUM TEXTURECUBEMAP WIDTH],
//[MAXIMUM TEXTURECUBEMAP LAYERED WIDTH],
//[MAXIMUM TEXTURECUBEMAP LAYERED LAYERS],
//[MAXIMUM SURFACE1D WIDTH],
//[MAXIMUM SURFACE2D WIDTH],
//[MAXIMUM SURFACE2D HEIGHT],
//[MAXIMUM SURFACE3D WIDTH],
//[MAXIMUM SURFACE3D HEIGHT],
//[MAXIMUM SURFACE3D DEPTH],
//[MAXIMUM SURFACE1D LAYERED WIDTH],
//[MAXIMUM SURFACE1D LAYERED LAYERS],
//[MAXIMUM SURFACE2D LAYERED WIDTH],
//[MAXIMUM SURFACE2D LAYERED HEIGHT],
//[MAXIMUM SURFACE2D LAYERED LAYERS],
//[MAXIMUM SURFACECUBEMAP WIDTH],
//[MAXIMUM SURFACECUBEMAP LAYERED WIDTH],
//[MAXIMUM SURFACECUBEMAP LAYERED LAYERS],
//[MAXIMUM TEXTURE1D LINEAR WIDTH],
//[MAXIMUM TEXTURE2D LINEAR WIDTH],
//[MAXIMUM TEXTURE2D LINEAR HEIGHT],
//[MAXIMUM TEXTURE2D LINEAR PITCH],
//[MAXIMUM TEXTURE2D MIPMAPPED WIDTH],
//[MAXIMUM TEXTURE2D MIPMAPPED HEIGHT],
//[COMPUTE CAPABILITY MAJOR],
//[COMPUTE CAPABILITY MINOR],
//[MAXIMUM TEXTURE1D MIPMAPPED WIDTH],
[STREAM PRIORITIES SUPPORTED],
[GLOBAL L1 CACHE SUPPORTED],
[LOCAL L1 CACHE SUPPORTED],
[MAX SHARED MEMORY PER MULTIPROCESSOR],
[MAX REGISTERS PER MULTIPROCESSOR],
[MANAGED MEMORY],
[MULTI GPU BOARD],
[MULTI GPU BOARD GROUP ID],
[HOST NATIVE ATOMIC SUPPORTED],
[SINGLE TO DOUBLE PRECISION PERF RATIO],
[PAGEABLE MEMORY ACCESS],
[CONCURRENT MANAGED ACCESS],
[COMPUTE PREEMPTION SUPPORTED],
[CAN USE HOST POINTER FOR REGISTERED MEM],
//[CAN USE STREAM MEM OPS],
[COOPERATIVE LAUNCH],
[COOPERATIVE MULTI DEVICE LAUNCH],
[MAX SHARED MEMORY PER BLOCK OPTIN],
//[CAN FLUSH REMOTE WRITES],
[HOST REGISTER SUPPORTED],
[PAGEABLE MEMORY ACCESS USES HOST PAGE TABLES],
[DIRECT MANAGED MEM ACCESS FROM HOST],
//[VIRTUAL ADDRESS MANAGEMENT SUPPORTED],
[VIRTUAL MEMORY MANAGEMENT SUPPORTED],
//[HANDLE TYPE POSIX FILE DESCRIPTOR SUPPORTED],
//[HANDLE TYPE WIN32 HANDLE SUPPORTED],
//[HANDLE TYPE WIN32 KMT HANDLE SUPPORTED],
//[MAX BLOCKS PER MULTIPROCESSOR],
//[GENERIC COMPRESSION SUPPORTED],
//[MAX PERSISTING L2 CACHE SIZE],
//[MAX ACCESS POLICY WINDOW SIZE],
//[GPU DIRECT RDMA WITH CUDA VMM SUPPORTED],
//[RESERVED SHARED MEMORY PER BLOCK],
//[SPARSE CUDA ARRAY SUPPORTED],
//[READ ONLY HOST REGISTER SUPPORTED],
//[TIMELINE SEMAPHORE INTEROP SUPPORTED],
[MEMORY POOLS SUPPORTED],
//[GPU DIRECT RDMA SUPPORTED],
//[GPU DIRECT RDMA FLUSH WRITES OPTIONS],
//[GPU DIRECT RDMA WRITES ORDERING],
//[MEMPOOL SUPPORTED HANDLE TYPES],
//[CLUSTER LAUNCH],
//[DEFERRED MAPPING CUDA ARRAY SUPPORTED],
//[CAN USE 64 BIT STREAM MEM OPS],
//[CAN USE STREAM WAIT VALUE NOR],
//[DMA BUF SUPPORTED],
//[IPC EVENT SUPPORTED],
//[MEM SYNC DOMAIN COUNT],
//[TENSOR MAP ACCESS SUPPORTED],
//[HANDLE TYPE FABRIC SUPPORTED],
//[UNIFIED FUNCTION POINTERS],
//[NUMA CONFIG],
//[NUMA ID],
//[MULTICAST SUPPORTED],
//[MPS ENABLED],
//[HOST NUMA ID],
};
unsafe { hipDeviceGetAttribute(pi, attrib, dev_idx) }
}
pub(crate) fn get_uuid(uuid: *mut hipUUID, device: hipDevice_t) -> hipError_t {
unsafe { hipDeviceGetUuid(uuid, device) }
}
pub(crate) fn get_uuid_v2(uuid: *mut hipUUID, device: hipDevice_t) -> hipError_t {
get_uuid(uuid, device)
}
pub(crate) fn get_luid(
luid: *mut ::core::ffi::c_char,
device_node_mask: &mut ::core::ffi::c_uint,
dev: hipDevice_t,
) -> hipError_t {
let luid = unsafe {
luid.cast::<[i8; 8]>()
.as_mut()
.ok_or(hipErrorCode_t::InvalidValue)
}?;
let mut properties = unsafe { mem::zeroed() };
unsafe { hipGetDevicePropertiesR0600(&mut properties, dev) }?;
*luid = properties.luid;
*device_node_mask = properties.luidDeviceNodeMask;
Ok(())
}
pub(crate) fn get_name(
name: *mut ::core::ffi::c_char,
len: ::core::ffi::c_int,
dev: hipDevice_t,
) -> cuda_types::CUresult {
unsafe { hipDeviceGetName(name, len, dev) }?;
let len = len as usize;
let buffer = unsafe { std::slice::from_raw_parts(name, len) };
let first_zero = buffer.iter().position(|c| *c == 0);
let first_zero = if let Some(x) = first_zero {
x
} else {
return Ok(());
};
if (first_zero + PROJECT_SUFFIX.len()) > len {
return Ok(());
}
unsafe {
ptr::copy_nonoverlapping(
PROJECT_SUFFIX.as_ptr() as _,
name.add(first_zero),
PROJECT_SUFFIX.len(),
)
};
Ok(())
}
pub(crate) fn total_mem_v2(bytes: *mut usize, dev: hipDevice_t) -> hipError_t {
unsafe { hipDeviceTotalMem(bytes, dev) }
}
pub(crate) fn get_properties(prop: &mut cuda_types::CUdevprop, dev: hipDevice_t) -> hipError_t {
let mut hip_props = unsafe { mem::zeroed() };
unsafe { hipGetDevicePropertiesR0600(&mut hip_props, dev) }?;
prop.maxThreadsPerBlock = hip_props.maxThreadsPerBlock;
prop.maxThreadsDim = hip_props.maxThreadsDim;
prop.maxGridSize = hip_props.maxGridSize;
prop.totalConstantMemory = clamp_usize(hip_props.totalConstMem);
prop.SIMDWidth = 32;
prop.memPitch = clamp_usize(hip_props.memPitch);
prop.regsPerBlock = hip_props.regsPerBlock;
prop.clockRate = hip_props.clockRate;
prop.textureAlign = clamp_usize(hip_props.textureAlignment);
Ok(())
}
pub(crate) fn get_count(count: &mut ::core::ffi::c_int) -> hipError_t {
unsafe { hipGetDeviceCount(count) }
}
fn clamp_usize(x: usize) -> i32 {
usize::min(x, i32::MAX as usize) as i32
}
pub(crate) fn primary_context_retain(
pctx: &mut CUcontext,
hip_dev: hipDevice_t,
) -> Result<(), CUerror> {
let (ctx, raw_ctx) = context::get_primary(hip_dev)?;
{
let mut mutable_ctx = ctx.mutable.lock().map_err(|_| CUerror::UNKNOWN)?;
mutable_ctx.ref_count += 1;
}
*pctx = raw_ctx;
Ok(())
}
pub(crate) fn primary_context_release(hip_dev: hipDevice_t) -> Result<(), CUerror> {
let (ctx, _) = context::get_primary(hip_dev)?;
{
let mut mutable_ctx = ctx.mutable.lock().map_err(|_| CUerror::UNKNOWN)?;
if mutable_ctx.ref_count == 0 {
return Err(CUerror::INVALID_CONTEXT);
}
mutable_ctx.ref_count -= 1;
if mutable_ctx.ref_count == 0 {
// TODO: drop all children
}
}
Ok(())
}
|