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
#![allow(non_upper_case_globals)]

use crate::extras::{MemInfo};
use crate::ffi::cuda_runtime_api::*;
use crate::ffi::driver_types::*;

use std::ffi::{CStr};
use std::mem::{size_of, zeroed};
use std::os::raw::{c_void, c_int, c_uint};
use std::ptr::{null_mut};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CudaError(pub cudaError_t);

impl CudaError {
  pub fn get_code(&self) -> u32 {
    let &CudaError(e) = self;
    e as _
  }

  pub fn get_string(&self) -> String {
    let raw_s = unsafe { cudaGetErrorString(self.0) };
    if raw_s.is_null() {
      return format!("(null)");
    }
    let cs = unsafe { CStr::from_ptr(raw_s) };
    let s = match cs.to_str() {
      Err(_) => "(invalid utf-8)",
      Ok(s) => s,
    };
    s.to_owned()
  }
}

pub type CudaResult<T=()> = Result<T, CudaError>;

pub fn get_driver_version() -> CudaResult<i32> {
  let mut version: c_int = -1;
  match unsafe { cudaDriverGetVersion(&mut version as *mut c_int) } {
    cudaError_cudaSuccess => {
      assert!(version >= 0);
      Ok(version)
    }
    e => Err(CudaError(e)),
  }
}

pub fn get_runtime_version() -> CudaResult<i32> {
  let mut version: c_int = -1;
  match unsafe { cudaRuntimeGetVersion(&mut version as *mut c_int) } {
    cudaError_cudaSuccess => {
      assert!(version >= 0);
      Ok(version)
    }
    e => Err(CudaError(e)),
  }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CudaDevice(pub i32);

impl CudaDevice {
  /// Count the number of devices.
  ///
  /// Corresponds to `cudaGetDeviceCount`.
  pub fn count() -> CudaResult<i32> {
    let mut count: c_int = 0;
    match unsafe { cudaGetDeviceCount(&mut count as *mut c_int) } {
      cudaError_cudaSuccess => {
        assert!(count >= 0);
        Ok(count)
      }
      e => Err(CudaError(e)),
    }
  }

  /// Reset the current device.
  ///
  /// Corresponds to `cudaDeviceReset`.
  pub fn reset_current() -> CudaResult {
    match unsafe { cudaDeviceReset() } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  /// Synchronize all work on the current device.
  ///
  /// Corresponds to `cudaDeviceSynchronize`.
  pub fn synchronize_current() -> CudaResult {
    match unsafe { cudaDeviceSynchronize() } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  /// Set flags for the current device.
  ///
  /// Corresponds to `cudaSetDeviceFlags`.
  pub fn set_flags_current(flags: u32) -> CudaResult {
    match unsafe { cudaSetDeviceFlags(flags as c_uint) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  /// Query the current device.
  ///
  /// Corresponds to `cudaGetDevice`.
  pub fn get_current() -> CudaResult<CudaDevice> {
    let mut curr_dev: c_int = 0;
    match unsafe { cudaGetDevice(&mut curr_dev as *mut c_int) } {
      cudaError_cudaSuccess => Ok(CudaDevice(curr_dev)),
      e => Err(CudaError(e)),
    }
  }

  /// Set the current device.
  ///
  /// Corresponds to `cudaSetDevice`.
  pub fn set_current(&self) -> CudaResult {
    match unsafe { cudaSetDevice(self.0 as c_int) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  /// Query the `cudaDeviceProp` properties struct for the given device.
  ///
  /// Corresponds to `cudaGetDeviceProperties`.
  pub fn get_properties(&self) -> CudaResult<cudaDeviceProp> {
    let mut prop: cudaDeviceProp = unsafe { zeroed() };
    match unsafe { cudaGetDeviceProperties(&mut prop as *mut cudaDeviceProp, self.0 as c_int) } {
      cudaError_cudaSuccess => Ok(prop),
      e => Err(CudaError(e)),
    }
  }

  /// Query the given attribute for the given device.
  ///
  /// Corresponds to `cudaGetDeviceAttribute`.
  pub fn get_attribute(&self, attr: cudaDeviceAttr) -> CudaResult<i32> {
    let mut value: c_int = 0;
    match unsafe { cudaDeviceGetAttribute(&mut value as *mut c_int, attr, self.0 as c_int) } {
      cudaError_cudaSuccess => Ok(value as i32),
      e => Err(CudaError(e)),
    }
  }

  /// Check whether peer device access to `peer_dev` can be enabled.
  ///
  /// Corresponds to `cudaDeviceCanAccessPeer`.
  pub fn can_access_peer(&self, peer_dev: i32) -> CudaResult<bool> {
    let mut access: c_int = 0;
    match unsafe { cudaDeviceCanAccessPeer(&mut access as *mut c_int, self.0 as c_int, peer_dev as c_int) } {
      cudaError_cudaSuccess => Ok(access != 0),
      e => Err(CudaError(e)),
    }
  }

  /// Enable peer device access from the current device to `peer_dev`.
  /// Returns whether or not peer device access was previously enabled.
  ///
  /// Corresponds to `cudaDeviceEnablePeerAccess`.
  pub fn enable_peer_access_current(peer_dev: i32) -> CudaResult<bool> {
    match unsafe { cudaDeviceEnablePeerAccess(peer_dev as c_int, 0) } {
      cudaError_cudaSuccess => Ok(false),
      cudaError_cudaErrorPeerAccessAlreadyEnabled => Ok(true),
      e => Err(CudaError(e)),
    }
  }

  /// Disable peer device access from the current device to `peer_dev`.
  /// Returns whether or not peer device access was previously enabled.
  ///
  /// Corresponds to `cudaDeviceDisablePeerAccess`.
  pub fn disable_peer_access_current(peer_dev: i32) -> CudaResult<bool> {
    match unsafe { cudaDeviceDisablePeerAccess(peer_dev as c_int) } {
      cudaError_cudaSuccess => Ok(true),
      cudaError_cudaErrorPeerAccessNotEnabled => Ok(false),
      e => Err(CudaError(e)),
    }
  }

  /// Returns the free and total device memory in bytes for the current device.
  ///
  /// Corresponds to `cudaMemGetInfo`.
  pub fn get_mem_info_current() -> CudaResult<MemInfo> {
    let mut free: usize = 0;
    let mut total: usize = 0;
    match unsafe { cudaMemGetInfo(&mut free as *mut _, &mut total as *mut _) } {
      cudaError_cudaSuccess => Ok(MemInfo{free, total}),
      e => Err(CudaError(e)),
    }
  }
}

#[derive(Debug)]
pub struct CudaStream {
  ptr:  cudaStream_t,
}

unsafe impl Send for CudaStream {}
unsafe impl Sync for CudaStream {}

impl Drop for CudaStream {
  fn drop(&mut self) {
    if !self.ptr.is_null() {
      match unsafe { cudaStreamDestroy(self.ptr) } {
        cudaError_cudaSuccess => {}
        cudaError_cudaErrorCudartUnloading => {
          // NB(20160308): Sometimes drop() is called while the global runtime
          // is shutting down; suppress these errors.
        }
        e => {
          let err = CudaError(e);
          panic!("FATAL: CudaStream::drop() failed: {:?} ({})",
              err, err.get_string());
        }
      }
    }
  }
}

impl CudaStream {
  pub fn default() -> CudaStream {
    CudaStream{ptr: null_mut()}
  }

  pub fn create_current() -> CudaResult<CudaStream> {
    let mut ptr: cudaStream_t = null_mut();
    match unsafe { cudaStreamCreate(&mut ptr as *mut cudaStream_t) } {
      cudaError_cudaSuccess => Ok(CudaStream{ptr: ptr}),
      e => Err(CudaError(e)),
    }
  }

  pub fn as_raw(&self) -> cudaStream_t {
    self.ptr
  }

  pub fn ptr_eq(&self, other: &CudaStream) -> bool {
    self.ptr == other.ptr
  }

  pub fn add_callback(&mut self, callback: extern "C" fn (stream: cudaStream_t, status: cudaError_t, user_data: *mut c_void), user_data: *mut c_void) -> CudaResult {
    match unsafe { cudaStreamAddCallback(self.ptr, Some(callback), user_data, 0) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  pub fn synchronize(&mut self) -> CudaResult {
    match unsafe { cudaStreamSynchronize(self.ptr) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  pub fn wait_event(&mut self, event: &mut CudaEvent) -> CudaResult {
    match unsafe { cudaStreamWaitEvent(self.ptr, event.as_raw(), 0) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e))
    }
  }
}

#[derive(Clone, Copy, Debug)]
pub enum CudaEventStatus {
  Complete,
  NotReady,
}

#[derive(Debug)]
pub struct CudaEvent {
  ptr:  cudaEvent_t,
}

unsafe impl Send for CudaEvent {}
unsafe impl Sync for CudaEvent {}

impl Drop for CudaEvent {
  fn drop(&mut self) {
    if !self.ptr.is_null() {
      match unsafe { cudaEventDestroy(self.ptr) } {
        cudaError_cudaSuccess => {}
        cudaError_cudaErrorCudartUnloading => {
          // NB(20160308): Sometimes drop() is called while the global runtime
          // is shutting down; suppress these errors.
        }
        e => {
          let err = CudaError(e);
          panic!("FATAL: CudaEvent::drop() failed: {:?} ({})",
              err, err.get_string());
        }
      }
    }
  }
}

impl CudaEvent {
  pub fn create_current() -> CudaResult<CudaEvent> {
    let mut ptr = null_mut() as cudaEvent_t;
    match unsafe { cudaEventCreate(&mut ptr as *mut cudaEvent_t) } {
      cudaError_cudaSuccess => Ok(CudaEvent{ptr: ptr}),
      e => Err(CudaError(e)),
    }
  }

  pub fn blocking_current() -> CudaResult<CudaEvent> {
    Self::create_current_with_flags(0x01)
  }

  pub fn fastest_current() -> CudaResult<CudaEvent> {
    Self::create_current_with_flags(0x02)
  }

  pub fn create_current_with_flags(flags: u32) -> CudaResult<CudaEvent> {
    let mut ptr = null_mut() as cudaEvent_t;
    match unsafe { cudaEventCreateWithFlags(&mut ptr as *mut cudaEvent_t, flags) } {
      cudaError_cudaSuccess => Ok(CudaEvent{ptr: ptr}),
      e => Err(CudaError(e)),
    }
  }

  pub fn as_raw(&self) -> cudaEvent_t {
    self.ptr
  }

  pub fn ptr_eq(&self, other: &CudaEvent) -> bool {
    self.ptr == other.ptr
  }

  pub fn query(&mut self) -> CudaResult<CudaEventStatus> {
    match unsafe { cudaEventQuery(self.ptr) } {
      cudaError_cudaSuccess => Ok(CudaEventStatus::Complete),
      cudaError_cudaErrorNotReady => Ok(CudaEventStatus::NotReady),
      e => Err(CudaError(e)),
    }
  }

  pub fn record(&mut self, stream: &mut CudaStream) -> CudaResult {
    match unsafe { cudaEventRecord(self.ptr, stream.as_raw()) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }

  pub fn synchronize(&mut self) -> CudaResult {
    match unsafe { cudaEventSynchronize(self.ptr) } {
      cudaError_cudaSuccess => Ok(()),
      e => Err(CudaError(e)),
    }
  }
}

pub fn cuda_alloc_device(size: usize) -> CudaResult<*mut u8> {
  let mut dptr: *mut c_void = null_mut();
  match unsafe { cudaMalloc(&mut dptr as *mut *mut c_void, size) } {
    cudaError_cudaSuccess => Ok(dptr as *mut u8),
    e => Err(CudaError(e)),
  }
}

pub fn cuda_alloc_host(size: usize) -> CudaResult<*mut u8> {
  let mut ptr: *mut c_void = null_mut();
  match unsafe { cudaMallocHost(&mut ptr as *mut *mut c_void, size) } {
    cudaError_cudaSuccess => Ok(ptr as *mut u8),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_free_device(dptr: *mut u8) -> CudaResult {
  match cudaFree(dptr as *mut c_void) {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_free_host(ptr: *mut u8) -> CudaResult {
  match cudaFreeHost(ptr as *mut c_void) {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_memset(dptr: *mut u8, value: i32, size: usize) -> CudaResult {
  match cudaMemset(dptr as *mut c_void, value, size) {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_memset_async(dptr: *mut u8, value: i32, size: usize, stream: &mut CudaStream) -> CudaResult {
  match cudaMemsetAsync(dptr as *mut c_void, value, size, stream.as_raw()) {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

#[derive(Clone, Copy, Debug)]
pub enum CudaMemcpyKind {
  HostToHost,
  HostToDevice,
  DeviceToHost,
  DeviceToDevice,
  Unified,
}

impl CudaMemcpyKind {
  pub fn to_raw(&self) -> cudaMemcpyKind {
    match *self {
      CudaMemcpyKind::HostToHost      => cudaMemcpyKind_cudaMemcpyHostToHost,
      CudaMemcpyKind::HostToDevice    => cudaMemcpyKind_cudaMemcpyHostToDevice,
      CudaMemcpyKind::DeviceToHost    => cudaMemcpyKind_cudaMemcpyDeviceToHost,
      CudaMemcpyKind::DeviceToDevice  => cudaMemcpyKind_cudaMemcpyDeviceToDevice,
      CudaMemcpyKind::Unified         => cudaMemcpyKind_cudaMemcpyDefault,
    }
  }
}

pub unsafe fn cuda_memcpy<T>(
    dst: *mut T,
    src: *const T,
    len: usize,
    kind: CudaMemcpyKind) -> CudaResult
where T: Copy + 'static
{
  match cudaMemcpy(
      dst as *mut c_void,
      src as *const c_void,
      len * size_of::<T>(),
      kind.to_raw())
  {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_memcpy_async<T>(
    dst: *mut T,
    src: *const T,
    len: usize,
    kind: CudaMemcpyKind,
    stream: &mut CudaStream) -> CudaResult
where T: Copy + 'static
{
  match cudaMemcpyAsync(
      dst as *mut c_void,
      src as *const c_void,
      len * size_of::<T>(),
      kind.to_raw(),
      stream.as_raw())
  {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_memcpy_2d_async<T>(
    dst: *mut T,
    dst_pitch_bytes: usize,
    src: *const T,
    src_pitch_bytes: usize,
    width: usize,
    height: usize,
    kind: CudaMemcpyKind,
    stream: &mut CudaStream) -> CudaResult
where T: Copy + 'static
{
  let width_bytes = width * size_of::<T>();
  assert!(width_bytes <= dst_pitch_bytes);
  assert!(width_bytes <= src_pitch_bytes);
  match cudaMemcpy2DAsync(
      dst as *mut c_void,
      dst_pitch_bytes,
      src as *const c_void,
      src_pitch_bytes,
      width_bytes,
      height,
      kind.to_raw(),
      stream.as_raw())
  {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}

pub unsafe fn cuda_memcpy_peer_async<T>(
    dst: *mut T,
    dst_device_idx: i32,
    src: *const T,
    src_device_idx: i32,
    len: usize,
    stream: &mut CudaStream) -> CudaResult
where T: Copy + 'static
{
  match cudaMemcpyPeerAsync(
      dst as *mut c_void,
      dst_device_idx,
      src as *const c_void,
      src_device_idx,
      len * size_of::<T>(),
      stream.as_raw())
  {
    cudaError_cudaSuccess => Ok(()),
    e => Err(CudaError(e)),
  }
}