aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda/src/impl/mod.rs
blob: d4105541d3f3a97ff1b5c36a7b9bd9ea717f7982 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::{
    cuda::{CUctx_st, CUdevice, CUdeviceptr, CUfunc_st, CUmod_st, CUresult, CUstream_st},
    r#impl::device::Device,
};
use std::{
    ffi::c_void,
    mem::{self, ManuallyDrop},
    os::raw::c_int,
    ptr,
    sync::Mutex,
    sync::TryLockError,
};

#[cfg(test)]
#[macro_use]
pub mod test;
pub mod context;
pub mod device;
pub mod export_table;
pub mod function;
pub mod memory;
pub mod module;
#[cfg_attr(windows, path = "os_win.rs")]
#[cfg_attr(not(windows), path = "os_unix.rs")]
pub(crate) mod os;
pub mod stream;

#[cfg(debug_assertions)]
pub fn unimplemented() -> CUresult {
    unimplemented!()
}

#[cfg(not(debug_assertions))]
pub fn unimplemented() -> CUresult {
    CUresult::CUDA_ERROR_NOT_SUPPORTED
}

pub trait HasLivenessCookie: Sized {
    const COOKIE: usize;
    const LIVENESS_FAIL: CUresult;

    fn try_drop(&mut self) -> Result<(), CUresult>;
}

// This struct is a best-effort check if wrapped value has been dropped,
// while it's inherently safe, its use coming from FFI is very unsafe
#[repr(C)]
pub struct LiveCheck<T: HasLivenessCookie> {
    cookie: usize,
    data: ManuallyDrop<T>,
}

impl<T: HasLivenessCookie> LiveCheck<T> {
    pub fn new(data: T) -> Self {
        LiveCheck {
            cookie: T::COOKIE,
            data: ManuallyDrop::new(data),
        }
    }

    fn destroy_impl(this: *mut Self) -> Result<(), CUresult> {
        let mut ctx_box = ManuallyDrop::new(unsafe { Box::from_raw(this) });
        ctx_box.try_drop()?;
        unsafe { ManuallyDrop::drop(&mut ctx_box) };
        Ok(())
    }

    unsafe fn ptr_from_inner(this: *mut T) -> *mut Self {
        let outer_ptr = (this as *mut u8).sub(mem::size_of::<usize>());
        outer_ptr as *mut Self
    }

    pub unsafe fn as_ref_unchecked(&self) -> &T {
        &self.data
    }

    pub fn as_option_mut(&mut self) -> Option<&mut T> {
        if self.cookie == T::COOKIE {
            Some(&mut self.data)
        } else {
            None
        }
    }

    pub fn as_result(&self) -> Result<&T, CUresult> {
        if self.cookie == T::COOKIE {
            Ok(&self.data)
        } else {
            Err(T::LIVENESS_FAIL)
        }
    }

    pub fn as_result_mut(&mut self) -> Result<&mut T, CUresult> {
        if self.cookie == T::COOKIE {
            Ok(&mut self.data)
        } else {
            Err(T::LIVENESS_FAIL)
        }
    }

    #[must_use]
    pub fn try_drop(&mut self) -> Result<(), CUresult> {
        if self.cookie == T::COOKIE {
            self.cookie = 0;
            self.data.try_drop()?;
            unsafe { ManuallyDrop::drop(&mut self.data) };
            return Ok(());
        }
        Err(T::LIVENESS_FAIL)
    }
}

impl<T: HasLivenessCookie> Drop for LiveCheck<T> {
    fn drop(&mut self) {
        self.cookie = 0;
    }
}

pub trait CudaRepr: Sized {
    type Impl: Sized;
}

impl<T: CudaRepr> CudaRepr for *mut T {
    type Impl = *mut T::Impl;
}

pub trait Decuda<To> {
    fn decuda(self: Self) -> To;
}

impl<T: CudaRepr> Decuda<*mut T::Impl> for *mut T {
    fn decuda(self: Self) -> *mut T::Impl {
        self as *mut _
    }
}

impl From<l0::sys::ze_result_t> for CUresult {
    fn from(result: l0::sys::ze_result_t) -> Self {
        match result {
            l0::sys::ze_result_t::ZE_RESULT_SUCCESS => CUresult::CUDA_SUCCESS,
            l0_sys::ze_result_t::ZE_RESULT_ERROR_UNINITIALIZED => {
                CUresult::CUDA_ERROR_NOT_INITIALIZED
            }
            l0_sys::ze_result_t::ZE_RESULT_ERROR_INVALID_ENUMERATION
            | l0_sys::ze_result_t::ZE_RESULT_ERROR_INVALID_ARGUMENT
            | l0_sys::ze_result_t::ZE_RESULT_ERROR_INVALID_GROUP_SIZE_DIMENSION
            | l0_sys::ze_result_t::ZE_RESULT_ERROR_INVALID_GLOBAL_WIDTH_DIMENSION => {
                CUresult::CUDA_ERROR_INVALID_VALUE
            }
            l0_sys::ze_result_t::ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY => {
                CUresult::CUDA_ERROR_OUT_OF_MEMORY
            }
            l0_sys::ze_result_t::ZE_RESULT_ERROR_UNSUPPORTED_FEATURE => {
                CUresult::CUDA_ERROR_NOT_SUPPORTED
            }
            _ => CUresult::CUDA_ERROR_UNKNOWN,
        }
    }
}

impl<T> From<TryLockError<T>> for CUresult {
    fn from(_: TryLockError<T>) -> Self {
        CUresult::CUDA_ERROR_ILLEGAL_STATE
    }
}

impl From<ocl_core::Error> for CUresult {
    fn from(result: ocl_core::Error) -> Self {
        match result {
            _ => CUresult::CUDA_ERROR_UNKNOWN,
        }
    }
}

pub trait Encuda {
    type To: Sized;
    fn encuda(self: Self) -> Self::To;
}

impl Encuda for CUresult {
    type To = CUresult;
    fn encuda(self: Self) -> Self::To {
        self
    }
}

impl Encuda for l0::sys::ze_result_t {
    type To = CUresult;
    fn encuda(self: Self) -> Self::To {
        self.into()
    }
}

impl Encuda for () {
    type To = CUresult;
    fn encuda(self: Self) -> Self::To {
        CUresult::CUDA_SUCCESS
    }
}

impl<T1: Encuda<To = CUresult>, T2: Encuda<To = CUresult>> Encuda for Result<T1, T2> {
    type To = CUresult;
    fn encuda(self: Self) -> Self::To {
        match self {
            Ok(e) => e.encuda(),
            Err(e) => e.encuda(),
        }
    }
}

lazy_static! {
    static ref GLOBAL_STATE: Mutex<Option<GlobalState>> = Mutex::new(None);
}

struct GlobalState {
    devices: Vec<Device>,
    global_heap: *mut c_void,
    platform: ocl_core::PlatformId,
}

unsafe impl Send for GlobalState {}

impl GlobalState {
    fn lock<T>(f: impl FnOnce(&mut GlobalState) -> T) -> Result<T, CUresult> {
        let mut mutex = GLOBAL_STATE
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let global_state = mutex.as_mut().ok_or(CUresult::CUDA_ERROR_ILLEGAL_STATE)?;
        Ok(f(global_state))
    }

    fn lock_device<T>(
        device::Index(dev_idx): device::Index,
        f: impl FnOnce(&'static mut device::Device) -> T,
    ) -> Result<T, CUresult> {
        if dev_idx < 0 {
            return Err(CUresult::CUDA_ERROR_INVALID_DEVICE);
        }
        Self::lock(|global_state| {
            if dev_idx >= global_state.devices.len() as c_int {
                Err(CUresult::CUDA_ERROR_INVALID_DEVICE)
            } else {
                Ok(f(unsafe {
                    transmute_lifetime_mut(&mut global_state.devices[dev_idx as usize])
                }))
            }
        })?
    }

    fn lock_current_context<F: FnOnce(&mut context::ContextData) -> R, R>(
        f: F,
    ) -> Result<R, CUresult> {
        Self::lock_current_context_unchecked(|ctx| Ok(f(ctx.as_result_mut()?)))?
    }

    fn lock_current_context_unchecked<F: FnOnce(&mut context::Context) -> R, R>(
        f: F,
    ) -> Result<R, CUresult> {
        context::CONTEXT_STACK.with(|stack| {
            stack
                .borrow_mut()
                .last_mut()
                .ok_or(CUresult::CUDA_ERROR_INVALID_CONTEXT)
                .map(|ctx| GlobalState::lock(|_| f(unsafe { &mut **ctx })))?
        })
    }

    fn lock_stream<T>(
        stream: *mut stream::Stream,
        f: impl FnOnce(&mut stream::StreamData) -> T,
    ) -> Result<T, CUresult> {
        if stream == ptr::null_mut()
            || stream == stream::CU_STREAM_LEGACY
            || stream == stream::CU_STREAM_PER_THREAD
        {
            Self::lock_current_context(|ctx| Ok(f(&mut ctx.default_stream)))?
        } else {
            Self::lock(|_| {
                let stream = unsafe { &mut *stream }.as_result_mut()?;
                Ok(f(stream))
            })?
        }
    }

    fn lock_enqueue(
        stream: *mut stream::Stream,
        f: impl FnOnce(&ocl_core::CommandQueue) -> Result<(), CUresult>,
    ) -> Result<(), CUresult> {
        Self::lock_stream(stream, |stream_data| {
            let l0_dev = unsafe { (*(*stream_data.context).device).base };
            let l0_ctx = unsafe { &mut (*(*stream_data.context).device).ocl_context };
            let cmd_list = unsafe { transmute_lifetime(&stream_data.cmd_list) };
            // TODO: make new_marker drop-safe
            let (new_event, new_marker) = stream_data.get_event(l0_dev, l0_ctx)?;
            stream_data.try_reuse_finished_events()?;
            let prev_event = stream_data.get_last_event();
            let prev_event_array = prev_event.map(|e| [e]);
            let empty = [];
            let prev_event_slice = prev_event_array.as_ref().map_or(&empty[..], |arr| &arr[..]);
            f(cmd_list, &new_event, prev_event_slice)?;
            stream_data.push_event((new_event, new_marker));
            Ok(())
        })?
    }

    fn lock_function<T>(
        func: *mut function::Function,
        f: impl FnOnce(&mut function::FunctionData) -> T,
    ) -> Result<T, CUresult> {
        if func == ptr::null_mut() {
            return Err(CUresult::CUDA_ERROR_INVALID_HANDLE);
        }
        Self::lock(|_| {
            let func = unsafe { &mut *func }.as_result_mut()?;
            Ok(f(func))
        })?
    }
}

// TODO: implement
fn is_intel_gpu_driver(_: &l0::Driver) -> bool {
    true
}

pub fn init() -> Result<(), CUresult> {
    let mut global_state = GLOBAL_STATE
        .lock()
        .map_err(|_| CUresult::CUDA_ERROR_UNKNOWN)?;
    if global_state.is_some() {
        return Ok(());
    }
    l0::init()?;
    let platforms = ocl_core::get_platform_ids()?;
    let (platform, device) = platforms
        .iter()
        .find_map(|plat| {
            let devices =
                ocl_core::get_device_ids(plat, Some(ocl_core::DeviceType::GPU), None).ok()?;
            for dev in devices {
                let vendor = ocl_core::get_device_info(dev, ocl_core::DeviceInfo::VendorId).ok()?;
                if let ocl_core::DeviceInfoResult::VendorId(0x8086) = vendor {
                    let dev_type =
                        ocl_core::get_device_info(dev, ocl_core::DeviceInfo::Type).ok()?;
                    if let ocl_core::DeviceInfoResult::Type(ocl_core::DeviceType::GPU) = dev_type {
                        return Some((plat.clone(), dev));
                    }
                }
            }
            None
        })
        .ok_or(CUresult::CUDA_ERROR_UNKNOWN)?;
    let drivers = l0::Driver::get()?;
    let devices = match drivers.into_iter().find(is_intel_gpu_driver) {
        None => return Err(CUresult::CUDA_ERROR_UNKNOWN),
        Some(driver) => driver
            .devices()?
            .into_iter()
            .enumerate()
            .map(|(idx, l0_dev)| device::Device::new(&driver, l0_dev, device, idx).unwrap())
            .collect::<Vec<_>>(),
    };
    let global_heap = unsafe { os::heap_create() };
    if global_heap == ptr::null_mut() {
        return Err(CUresult::CUDA_ERROR_OUT_OF_MEMORY);
    }
    *global_state = Some(GlobalState {
        devices,
        global_heap,
        platform,
    });
    drop(global_state);
    Ok(())
}

macro_rules! stringify_curesult {
    ($x:ident => [ $($variant:ident),+ ]) => {
        match $x {
            $(
                CUresult::$variant => Some(concat!(stringify!($variant), "\0")),
            )+
            _ => None
        }
    }
}

pub(crate) fn get_error_string(error: CUresult, str: *mut *const i8) -> CUresult {
    if str == ptr::null_mut() {
        return CUresult::CUDA_ERROR_INVALID_VALUE;
    }
    let text = stringify_curesult!(
        error => [
            CUDA_SUCCESS,
            CUDA_ERROR_INVALID_VALUE,
            CUDA_ERROR_OUT_OF_MEMORY,
            CUDA_ERROR_NOT_INITIALIZED,
            CUDA_ERROR_DEINITIALIZED,
            CUDA_ERROR_PROFILER_DISABLED,
            CUDA_ERROR_PROFILER_NOT_INITIALIZED,
            CUDA_ERROR_PROFILER_ALREADY_STARTED,
            CUDA_ERROR_PROFILER_ALREADY_STOPPED,
            CUDA_ERROR_NO_DEVICE,
            CUDA_ERROR_INVALID_DEVICE,
            CUDA_ERROR_INVALID_IMAGE,
            CUDA_ERROR_INVALID_CONTEXT,
            CUDA_ERROR_CONTEXT_ALREADY_CURRENT,
            CUDA_ERROR_MAP_FAILED,
            CUDA_ERROR_UNMAP_FAILED,
            CUDA_ERROR_ARRAY_IS_MAPPED,
            CUDA_ERROR_ALREADY_MAPPED,
            CUDA_ERROR_NO_BINARY_FOR_GPU,
            CUDA_ERROR_ALREADY_ACQUIRED,
            CUDA_ERROR_NOT_MAPPED,
            CUDA_ERROR_NOT_MAPPED_AS_ARRAY,
            CUDA_ERROR_NOT_MAPPED_AS_POINTER,
            CUDA_ERROR_ECC_UNCORRECTABLE,
            CUDA_ERROR_UNSUPPORTED_LIMIT,
            CUDA_ERROR_CONTEXT_ALREADY_IN_USE,
            CUDA_ERROR_PEER_ACCESS_UNSUPPORTED,
            CUDA_ERROR_INVALID_PTX,
            CUDA_ERROR_INVALID_GRAPHICS_CONTEXT,
            CUDA_ERROR_NVLINK_UNCORRECTABLE,
            CUDA_ERROR_JIT_COMPILER_NOT_FOUND,
            CUDA_ERROR_INVALID_SOURCE,
            CUDA_ERROR_FILE_NOT_FOUND,
            CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND,
            CUDA_ERROR_SHARED_OBJECT_INIT_FAILED,
            CUDA_ERROR_OPERATING_SYSTEM,
            CUDA_ERROR_INVALID_HANDLE,
            CUDA_ERROR_ILLEGAL_STATE,
            CUDA_ERROR_NOT_FOUND,
            CUDA_ERROR_NOT_READY,
            CUDA_ERROR_ILLEGAL_ADDRESS,
            CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES,
            CUDA_ERROR_LAUNCH_TIMEOUT,
            CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING,
            CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED,
            CUDA_ERROR_PEER_ACCESS_NOT_ENABLED,
            CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE,
            CUDA_ERROR_CONTEXT_IS_DESTROYED,
            CUDA_ERROR_ASSERT,
            CUDA_ERROR_TOO_MANY_PEERS,
            CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED,
            CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED,
            CUDA_ERROR_HARDWARE_STACK_ERROR,
            CUDA_ERROR_ILLEGAL_INSTRUCTION,
            CUDA_ERROR_MISALIGNED_ADDRESS,
            CUDA_ERROR_INVALID_ADDRESS_SPACE,
            CUDA_ERROR_INVALID_PC,
            CUDA_ERROR_LAUNCH_FAILED,
            CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE,
            CUDA_ERROR_NOT_PERMITTED,
            CUDA_ERROR_NOT_SUPPORTED,
            CUDA_ERROR_SYSTEM_NOT_READY,
            CUDA_ERROR_SYSTEM_DRIVER_MISMATCH,
            CUDA_ERROR_COMPAT_NOT_SUPPORTED_ON_DEVICE,
            CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED,
            CUDA_ERROR_STREAM_CAPTURE_INVALIDATED,
            CUDA_ERROR_STREAM_CAPTURE_MERGE,
            CUDA_ERROR_STREAM_CAPTURE_UNMATCHED,
            CUDA_ERROR_STREAM_CAPTURE_UNJOINED,
            CUDA_ERROR_STREAM_CAPTURE_ISOLATION,
            CUDA_ERROR_STREAM_CAPTURE_IMPLICIT,
            CUDA_ERROR_CAPTURED_EVENT,
            CUDA_ERROR_STREAM_CAPTURE_WRONG_THREAD,
            CUDA_ERROR_TIMEOUT,
            CUDA_ERROR_GRAPH_EXEC_UPDATE_FAILURE,
            CUDA_ERROR_UNKNOWN
        ]
    );
    match text {
        Some(text) => {
            unsafe { *str = text.as_ptr() as *const _ };
            CUresult::CUDA_SUCCESS
        }
        None => CUresult::CUDA_ERROR_INVALID_VALUE,
    }
}

unsafe fn transmute_lifetime<'a, 'b, T: ?Sized>(t: &'a T) -> &'b T {
    mem::transmute(t)
}

unsafe fn transmute_lifetime_mut<'a, 'b, T: ?Sized>(t: &'a mut T) -> &'b mut T {
    mem::transmute(t)
}

pub fn driver_get_version() -> c_int {
    i32::max_value()
}

impl<'a> CudaRepr for CUctx_st {
    type Impl = context::Context;
}

impl<'a> CudaRepr for CUdevice {
    type Impl = device::Index;
}

impl Decuda<device::Index> for CUdevice {
    fn decuda(self) -> device::Index {
        device::Index(self.0)
    }
}

impl<'a> CudaRepr for CUdeviceptr {
    type Impl = *mut c_void;
}

impl Decuda<*mut c_void> for CUdeviceptr {
    fn decuda(self) -> *mut c_void {
        self.0 as *mut _
    }
}

impl<'a> CudaRepr for CUmod_st {
    type Impl = module::Module;
}

impl<'a> CudaRepr for CUfunc_st {
    type Impl = function::Function;
}

impl<'a> CudaRepr for CUstream_st {
    type Impl = stream::Stream;
}