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
//! CUDA [Device] and [Context]
//!
//! [Device]:  https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE.html
//! [Context]: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__CTX.html

use crate::{error::*, *};
use cuda::*;
use std::sync::{Arc, Once};

pub use accel_derive::Contexted;

/// Handler for device and its primary context
#[derive(Debug, PartialEq, PartialOrd)]
pub struct Device {
    device: CUdevice,
}

impl Device {
    /// Initializer for CUDA Driver API
    fn init() {
        static DRIVER_API_INIT: Once = Once::new();
        DRIVER_API_INIT.call_once(|| unsafe {
            ffi_call!(cuda::cuInit, 0).expect("Initialization of CUDA Driver API failed");
        });
    }

    /// Get number of available GPUs
    pub fn get_count() -> Result<usize> {
        Self::init();
        let mut count: i32 = 0;
        unsafe {
            ffi_call!(cuDeviceGetCount, &mut count as *mut i32)?;
        }
        Ok(count as usize)
    }

    pub fn nth(id: usize) -> Result<Self> {
        let count = Self::get_count()?;
        if id >= count {
            return Err(AccelError::DeviceNotFound { id, count });
        }
        let device = unsafe { ffi_new!(cuDeviceGet, id as i32)? };
        Ok(Device { device })
    }

    /// Get total memory of GPU
    pub fn total_memory(&self) -> Result<usize> {
        let mut mem = 0;
        unsafe {
            ffi_call!(cuDeviceTotalMem_v2, &mut mem as *mut _, self.device)?;
        }
        Ok(mem)
    }

    /// Get name of GPU
    pub fn get_name(&self) -> Result<String> {
        let mut bytes: Vec<u8> = vec![0_u8; 1024];
        unsafe {
            ffi_call!(
                cuDeviceGetName,
                bytes.as_mut_ptr() as *mut i8,
                1024,
                self.device
            )?;
        }
        Ok(String::from_utf8(bytes).expect("GPU name is not UTF8"))
    }

    /// Create a new CUDA context on this device.
    ///
    /// ```
    /// # use accel::*;
    /// let device = Device::nth(0).unwrap();
    /// let ctx = device.create_context();
    /// ```
    pub fn create_context(&self) -> Context {
        let ptr = unsafe {
            ffi_new!(
                cuCtxCreate_v2,
                CUctx_flags_enum::CU_CTX_SCHED_AUTO as u32,
                self.device
            )
        }
        .expect("Failed to create a new context");
        if ptr.is_null() {
            panic!("Cannot crate a new context");
        }
        let ptr_new = ctx_pop().unwrap();
        assert_eq!(ptr, ptr_new);
        Arc::new(ContextOwned { ptr })
    }
}

/// Push to the context stack of this thread
fn ctx_push(ptr: CUcontext) -> Result<()> {
    unsafe { ffi_call!(cuCtxPushCurrent_v2, ptr) }?;
    Ok(())
}

/// Pop from the context stack of this thread
fn ctx_pop() -> Result<CUcontext> {
    let ptr = unsafe { ffi_new!(cuCtxPopCurrent_v2) }?;
    if ptr.is_null() {
        panic!("No current context");
    }
    Ok(ptr)
}

/// Get API version
fn ctx_version(ptr: CUcontext) -> Result<u32> {
    let mut version: u32 = 0;
    unsafe { ffi_call!(cuCtxGetApiVersion, ptr, &mut version as *mut _) }?;
    Ok(version)
}

/// Block until all tasks in this context to be complete.
fn ctx_sync(ptr: CUcontext) -> Result<()> {
    ctx_push(ptr)?;
    unsafe { ffi_call!(cuCtxSynchronize) }?;
    let ptr_new = ctx_pop()?;
    assert_eq!(ptr, ptr_new);
    Ok(())
}

/// Object with CUDA context
pub trait Contexted {
    fn guard(&self) -> Result<ContextGuard>;
    fn sync(&self) -> Result<()>;
    fn version(&self) -> Result<u32>;
    /// Get a reference
    ///
    /// This is **NOT** a Rust reference, i.e. you can drop owned context while the reference exists.
    /// The reference becomes expired after owned context is released, and it will cause a runtime error.
    ///
    fn get_ref(&self) -> ContextRef;
}

/// Owend handler for CUDA context
#[derive(Debug, PartialEq)]
pub struct ContextOwned {
    ptr: CUcontext,
}

pub type Context = Arc<ContextOwned>;

impl Drop for ContextOwned {
    fn drop(&mut self) {
        if let Err(e) = unsafe { ffi_call!(cuCtxDestroy_v2, self.ptr) } {
            log::error!("Context remove failed: {:?}", e);
        }
    }
}

unsafe impl Send for ContextOwned {}
unsafe impl Sync for ContextOwned {}

impl Contexted for Context {
    fn sync(&self) -> Result<()> {
        ctx_sync(self.ptr)
    }

    fn version(&self) -> Result<u32> {
        ctx_version(self.ptr)
    }

    fn guard(&self) -> Result<ContextGuard> {
        ctx_push(self.ptr)?;
        Ok(ContextGuard { ptr: self.ptr })
    }

    fn get_ref(&self) -> ContextRef {
        ContextRef { ptr: self.ptr }
    }
}

/// Non-Owend handler for CUDA context
///
/// The validity of reference is checked dynamically.
/// CUDA APIs (e.g. [cuPointerGetAttribute]) allow us to get a pointer to CUDA context,
/// but its validity cannot be assured by Rust lifetime system.
///
/// [cuPointerGetAttribute]: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__UNIFIED.html#group__CUDA__UNIFIED_1g0c28ed0aff848042bc0533110e45820c
///
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ContextRef {
    ptr: CUcontext,
}

impl ContextRef {
    pub(crate) fn from_ptr(ptr: CUcontext) -> Self {
        assert!(!ptr.is_null());
        ContextRef { ptr }
    }
}

unsafe impl Send for ContextRef {}
unsafe impl Sync for ContextRef {}

impl Contexted for ContextRef {
    fn sync(&self) -> Result<()> {
        ctx_sync(self.ptr)
    }

    fn version(&self) -> Result<u32> {
        ctx_version(self.ptr)
    }

    fn guard(&self) -> Result<ContextGuard> {
        ctx_push(self.ptr)?;
        Ok(ContextGuard { ptr: self.ptr })
    }

    fn get_ref(&self) -> ContextRef {
        self.clone()
    }
}

impl std::cmp::PartialEq<ContextRef> for ContextOwned {
    fn eq(&self, ctx: &ContextRef) -> bool {
        self.ptr == ctx.ptr
    }
}

impl std::cmp::PartialEq<ContextOwned> for ContextRef {
    fn eq(&self, ctx: &ContextOwned) -> bool {
        self.ptr == ctx.ptr
    }
}

/// RAII handler for using CUDA context
///
/// As described in [CUDA Programming Guide], library using CUDA should push context before using
/// it, and then pop it.
///
/// [CUDA Programming Guide]: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#context
pub struct ContextGuard {
    ptr: CUcontext,
}

impl Drop for ContextGuard {
    fn drop(&mut self) {
        match ctx_pop() {
            Ok(ptr) => {
                if ptr != self.ptr {
                    log::error!("Poped context is different from pushed: {:?}", ptr);
                }
            }
            Err(e) => {
                log::error!("Failed to pop context: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_count() -> Result<()> {
        Device::get_count()?;
        Ok(())
    }

    #[test]
    fn get_zeroth() -> Result<()> {
        Device::nth(0)?;
        Ok(())
    }

    #[test]
    fn out_of_range() -> Result<()> {
        assert!(Device::nth(129).is_err());
        Ok(())
    }

    #[test]
    fn create() -> Result<()> {
        let device = Device::nth(0)?;
        let ctx = device.create_context();
        dbg!(&ctx);
        Ok(())
    }

    #[should_panic]
    #[test]
    fn expired_context_ref() {
        let device = Device::nth(0).unwrap();
        let ctx = device.create_context();
        let ctx_ref = ctx.get_ref();
        drop(ctx);
        let _version = ctx_ref.version().unwrap(); // ctx has been expired
    }

    #[should_panic]
    #[test]
    fn expired_contexted_call() {
        let device = Device::nth(0).unwrap();
        let ctx = device.create_context();
        let ctx_ref = ctx.get_ref();
        drop(ctx);
        unsafe { contexted_call!(&ctx_ref, cuCtxSynchronize) }.unwrap();
    }
}