Module: Ignis::CUDA::RuntimeAPI

Defined in:
lib/nvruby/cuda/runtime_api.rb

Overview

CUDA Runtime API — Fiddle-only hot-path bindings.

Rule: This file uses ONLY Fiddle for hot-path calls (malloc, free, memcpy, stream, event, sync). FFI struct calls (cudaGetDeviceProperties) live in device_props.rb (Rule 4: never mix FFI and Fiddle in the same file).

Cross-platform: Uses Ignis::Platform to resolve cudart path on Windows/Linux.

Constant Summary collapse

MEMCPY_HOST_TO_HOST =

CUDA memory copy directions

0
MEMCPY_HOST_TO_DEVICE =
1
MEMCPY_DEVICE_TO_HOST =
2
MEMCPY_DEVICE_TO_DEVICE =
3
MEMCPY_DEFAULT =
4
DEVICE_SCHEDULE_AUTO =

CUDA device flags

0
DEVICE_SCHEDULE_SPIN =
1
DEVICE_SCHEDULE_YIELD =
2
DEVICE_SCHEDULE_BLOCKING_SYNC =
4
DEVICE_MAP_HOST =
8
DEVICE_LMEM_RESIZE_TO_MAX =
16
HOST_ALLOC_DEFAULT =

CUDA host alloc flags

0
HOST_ALLOC_PORTABLE =
1
HOST_ALLOC_MAPPED =
2
HOST_ALLOC_WRITECOMBINED =
4
CUDART_LIB =

Resolve CUDA runtime library path at load time. Uses Ignis::Platform if available, falls back to OS detection.

if defined?(Ignis::Platform)
  Ignis::Platform.cudart_path
elsif RUBY_PLATFORM.match?(/mswin|mingw|cygwin/i)
  File.join('C:', 'Program Files', 'NVIDIA GPU Computing Toolkit',
            'CUDA', 'v13.0', 'bin', 'cudart64_130.dll')
else
  'libcudart.so.13'
end

Class Attribute Summary collapse

Backward-Compatible Shims collapse

Class Method Summary collapse

Class Attribute Details

.handleFiddle::Handle? (readonly)

Returns the loaded DLL handle.

Returns:

  • (Fiddle::Handle, nil)

    the loaded DLL handle



54
55
56
# File 'lib/nvruby/cuda/runtime_api.rb', line 54

def handle
  @handle
end

Class Method Details

.check_status!(status, context = 'CUDA operation') ⇒ void

This method returns an undefined value.

Check CUDA status and raise error if not success.

Parameters:

  • status (Integer)
  • context (String) (defaults to: 'CUDA operation')

Raises:



402
403
404
405
406
407
408
409
410
411
412
# File 'lib/nvruby/cuda/runtime_api.rb', line 402

def check_status!(status, context = 'CUDA operation')
  return if status.zero?

  ensure_loaded!
  error_name = get_error_name(status)
  error_string = get_error_string(status)
  raise CudaRuntimeError.new(
    "#{context}: #{error_name} - #{error_string}",
    cuda_code: status
  )
end

.cudaDeviceGetAttribute(ptr_out, attr_id, device) ⇒ Integer

Returns status.

Parameters:

  • ptr_out (Fiddle::Pointer)

    pointer-to-int

  • attr_id (Integer)
  • device (Integer)

Returns:

  • (Integer)

    status



544
545
546
547
# File 'lib/nvruby/cuda/runtime_api.rb', line 544

def cudaDeviceGetAttribute(ptr_out, attr_id, device)
  ensure_loaded!
  @functions[:cudaDeviceGetAttribute].call(ptr_out, attr_id, device)
end

.cudaDeviceGetDefaultMemPool(pool_ptr, device) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



745
746
747
748
# File 'lib/nvruby/cuda/runtime_api.rb', line 745

def cudaDeviceGetDefaultMemPool(pool_ptr, device)
  ensure_loaded!
  @functions[:cudaDeviceGetDefaultMemPool]&.call(pool_ptr, device) || 0
end

.cudaDeviceResetInteger

Returns status.

Returns:

  • (Integer)

    status



521
522
523
524
# File 'lib/nvruby/cuda/runtime_api.rb', line 521

def cudaDeviceReset
  ensure_loaded!
  @functions[:cudaDeviceReset].call
end

.cudaDeviceSetMemPool(device, pool) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



763
764
765
766
# File 'lib/nvruby/cuda/runtime_api.rb', line 763

def cudaDeviceSetMemPool(device, pool)
  ensure_loaded!
  @functions[:cudaDeviceSetMemPool]&.call(device, pool) || 0
end

.cudaDeviceSynchronizeInteger

Returns status.

Returns:

  • (Integer)

    status



515
516
517
518
# File 'lib/nvruby/cuda/runtime_api.rb', line 515

def cudaDeviceSynchronize
  ensure_loaded!
  @functions[:cudaDeviceSynchronize].call
end

.cudaEventCreate(ptr_out) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



631
632
633
634
# File 'lib/nvruby/cuda/runtime_api.rb', line 631

def cudaEventCreate(ptr_out)
  ensure_loaded!
  @functions[:cudaEventCreate].call(ptr_out)
end

.cudaEventDestroy(event) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



637
638
639
640
# File 'lib/nvruby/cuda/runtime_api.rb', line 637

def cudaEventDestroy(event)
  ensure_loaded!
  @functions[:cudaEventDestroy].call(event)
end

.cudaEventElapsedTime(ms_ptr, start_event, end_event) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



655
656
657
658
# File 'lib/nvruby/cuda/runtime_api.rb', line 655

def cudaEventElapsedTime(ms_ptr, start_event, end_event)
  ensure_loaded!
  @functions[:cudaEventElapsedTime].call(ms_ptr, start_event, end_event)
end

.cudaEventRecord(event, stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



643
644
645
646
# File 'lib/nvruby/cuda/runtime_api.rb', line 643

def cudaEventRecord(event, stream)
  ensure_loaded!
  @functions[:cudaEventRecord].call(event, stream)
end

.cudaEventSynchronize(event) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



649
650
651
652
# File 'lib/nvruby/cuda/runtime_api.rb', line 649

def cudaEventSynchronize(event)
  ensure_loaded!
  @functions[:cudaEventSynchronize].call(event)
end

.cudaFree(ptr) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)

Returns:

  • (Integer)

    status



559
560
561
562
# File 'lib/nvruby/cuda/runtime_api.rb', line 559

def cudaFree(ptr)
  ensure_loaded!
  @functions[:cudaFree].call(ptr)
end

.cudaFreeAsync(ptr, stream) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)
  • stream (Fiddle::Pointer)

Returns:

  • (Integer)

    status



720
721
722
723
# File 'lib/nvruby/cuda/runtime_api.rb', line 720

def cudaFreeAsync(ptr, stream)
  ensure_loaded!
  @functions[:cudaFreeAsync].call(ptr, stream)
end

.cudaFreeHost(ptr) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)

Returns:

  • (Integer)

    status



607
608
609
610
# File 'lib/nvruby/cuda/runtime_api.rb', line 607

def cudaFreeHost(ptr)
  ensure_loaded!
  @functions[:cudaFreeHost].call(ptr)
end

.cudaGetDevice(ptr_out) ⇒ Integer

Returns status.

Parameters:

  • ptr_out (Fiddle::Pointer, FFI::Pointer)

    pointer-to-int

Returns:

  • (Integer)

    status



535
536
537
538
# File 'lib/nvruby/cuda/runtime_api.rb', line 535

def cudaGetDevice(ptr_out)
  ensure_loaded!
  @functions[:cudaGetDevice].call(ptr_out)
end

.cudaGetDeviceCount(ptr_out) ⇒ Integer

Returns status.

Parameters:

  • ptr_out (Fiddle::Pointer, FFI::Pointer)

    pointer-to-pointer

Returns:

  • (Integer)

    status



528
529
530
531
# File 'lib/nvruby/cuda/runtime_api.rb', line 528

def cudaGetDeviceCount(ptr_out)
  ensure_loaded!
  @functions[:cudaGetDeviceCount].call(ptr_out)
end

.cudaGetDeviceProperties(prop_ptr, device) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



775
776
777
778
# File 'lib/nvruby/cuda/runtime_api.rb', line 775

def cudaGetDeviceProperties(prop_ptr, device)
  ensure_loaded!
  @functions[:cudaGetDeviceProperties]&.call(prop_ptr, device) || 0
end

.cudaGetErrorName(status) ⇒ String

Parameters:

  • status (Integer)

Returns:

  • (String)


680
681
682
# File 'lib/nvruby/cuda/runtime_api.rb', line 680

def cudaGetErrorName(status)
  get_error_name(status)
end

.cudaGetErrorString(status) ⇒ String

Parameters:

  • status (Integer)

Returns:

  • (String)


674
675
676
# File 'lib/nvruby/cuda/runtime_api.rb', line 674

def cudaGetErrorString(status)
  get_error_string(status)
end

.cudaGetLastErrorInteger

Returns status.

Returns:

  • (Integer)

    status



661
662
663
664
# File 'lib/nvruby/cuda/runtime_api.rb', line 661

def cudaGetLastError
  ensure_loaded!
  @functions[:cudaGetLastError].call
end

.cudaHostAlloc(ptr_ptr, size, flags) ⇒ Integer

Returns status.

Parameters:

  • ptr_ptr (Fiddle::Pointer)

    pointer-to-pointer

  • size (Integer)
  • flags (Integer)

Returns:

  • (Integer)

    status



600
601
602
603
# File 'lib/nvruby/cuda/runtime_api.rb', line 600

def cudaHostAlloc(ptr_ptr, size, flags)
  ensure_loaded!
  @functions[:cudaHostAlloc].call(ptr_ptr, size, flags)
end

.cudaHostRegister(ptr, size, flags) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)

    host pointer to register

  • size (Integer)

    bytes

  • flags (Integer)

Returns:

  • (Integer)

    status



688
689
690
691
# File 'lib/nvruby/cuda/runtime_api.rb', line 688

def cudaHostRegister(ptr, size, flags)
  ensure_loaded!
  @functions[:cudaHostRegister]&.call(ptr, size, flags) || 0
end

.cudaHostUnregister(ptr) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)

    host pointer to unregister

Returns:

  • (Integer)

    status



695
696
697
698
# File 'lib/nvruby/cuda/runtime_api.rb', line 695

def cudaHostUnregister(ptr)
  ensure_loaded!
  @functions[:cudaHostUnregister]&.call(ptr) || 0
end

.cudaMalloc(ptr_ptr, size) ⇒ Integer

Returns status.

Parameters:

  • ptr_ptr (Fiddle::Pointer)

    pointer-to-pointer

  • size (Integer)

Returns:

  • (Integer)

    status



552
553
554
555
# File 'lib/nvruby/cuda/runtime_api.rb', line 552

def cudaMalloc(ptr_ptr, size)
  ensure_loaded!
  @functions[:cudaMalloc].call(ptr_ptr, size)
end

.cudaMallocAsync(ptr_out, size, stream) ⇒ Integer

Returns status.

Parameters:

  • ptr_out (Fiddle::Pointer)
  • size (Integer)
  • stream (Fiddle::Pointer)

Returns:

  • (Integer)

    status



712
713
714
715
# File 'lib/nvruby/cuda/runtime_api.rb', line 712

def cudaMallocAsync(ptr_out, size, stream)
  ensure_loaded!
  @functions[:cudaMallocAsync].call(ptr_out, size, stream)
end

.cudaMallocFromPoolAsync(ptr_ptr, size, pool, stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



739
740
741
742
# File 'lib/nvruby/cuda/runtime_api.rb', line 739

def cudaMallocFromPoolAsync(ptr_ptr, size, pool, stream)
  ensure_loaded!
  @functions[:cudaMallocFromPoolAsync]&.call(ptr_ptr, size, pool, stream) || 0
end

.cudaMallocHost(ptr_ptr, size) ⇒ Integer

Returns status.

Parameters:

  • ptr (Fiddle::Pointer)

    pointer-to-pointer

  • size (Integer)

Returns:

  • (Integer)

    status



703
704
705
706
# File 'lib/nvruby/cuda/runtime_api.rb', line 703

def cudaMallocHost(ptr_ptr, size)
  ensure_loaded!
  @functions[:cudaMallocHost]&.call(ptr_ptr, size) || cudaHostAlloc(ptr_ptr, size, 0)
end

.cudaMemcpy(dst, src, count, kind) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



565
566
567
568
569
# File 'lib/nvruby/cuda/runtime_api.rb', line 565

def cudaMemcpy(dst, src, count, kind)
  ensure_loaded!
  kind_int = resolve_memcpy_kind(kind)
  @functions[:cudaMemcpy].call(dst, src, count, kind_int)
end

.cudaMemcpyAsync(dst, src, count, kind, stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



572
573
574
575
576
# File 'lib/nvruby/cuda/runtime_api.rb', line 572

def cudaMemcpyAsync(dst, src, count, kind, stream)
  ensure_loaded!
  kind_int = resolve_memcpy_kind(kind)
  @functions[:cudaMemcpyAsync].call(dst, src, count, kind_int, stream)
end

.cudaMemGetInfo(free_ptr, total_ptr) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



591
592
593
594
# File 'lib/nvruby/cuda/runtime_api.rb', line 591

def cudaMemGetInfo(free_ptr, total_ptr)
  ensure_loaded!
  @functions[:cudaMemGetInfo].call(free_ptr, total_ptr)
end

.cudaMemPoolCreate(pool_ptr, props) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



769
770
771
772
# File 'lib/nvruby/cuda/runtime_api.rb', line 769

def cudaMemPoolCreate(pool_ptr, props)
  ensure_loaded!
  @functions[:cudaMemPoolCreate]&.call(pool_ptr, props) || 0
end

.cudaMemPoolDestroy(pool) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



733
734
735
736
# File 'lib/nvruby/cuda/runtime_api.rb', line 733

def cudaMemPoolDestroy(pool)
  ensure_loaded!
  @functions[:cudaMemPoolDestroy]&.call(pool) || 0
end

.cudaMemPoolGetAttribute(pool, attr, value_ptr) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



757
758
759
760
# File 'lib/nvruby/cuda/runtime_api.rb', line 757

def cudaMemPoolGetAttribute(pool, attr, value_ptr)
  ensure_loaded!
  @functions[:cudaMemPoolGetAttribute]&.call(pool, attr, value_ptr) || 0
end

.cudaMemPoolSetAttribute(pool, attr, value_ptr) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



751
752
753
754
# File 'lib/nvruby/cuda/runtime_api.rb', line 751

def cudaMemPoolSetAttribute(pool, attr, value_ptr)
  ensure_loaded!
  @functions[:cudaMemPoolSetAttribute]&.call(pool, attr, value_ptr) || 0
end

.cudaMemset(ptr, value, count) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



579
580
581
582
# File 'lib/nvruby/cuda/runtime_api.rb', line 579

def cudaMemset(ptr, value, count)
  ensure_loaded!
  @functions[:cudaMemset].call(ptr, value, count)
end

.cudaMemsetAsync(ptr, value, count, stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



585
586
587
588
# File 'lib/nvruby/cuda/runtime_api.rb', line 585

def cudaMemsetAsync(ptr, value, count, stream)
  ensure_loaded!
  @functions[:cudaMemsetAsync].call(ptr, value, count, stream)
end

.cudaPeekAtLastErrorInteger

Returns status.

Returns:

  • (Integer)

    status



667
668
669
670
# File 'lib/nvruby/cuda/runtime_api.rb', line 667

def cudaPeekAtLastError
  ensure_loaded!
  @functions[:cudaPeekAtLastError].call
end

.cudaRuntimeGetVersion(ptr_out) ⇒ Integer

Returns status.

Parameters:

  • ptr_out (Fiddle::Pointer)

    pointer to store version

Returns:

  • (Integer)

    status



727
728
729
730
# File 'lib/nvruby/cuda/runtime_api.rb', line 727

def cudaRuntimeGetVersion(ptr_out)
  ensure_loaded!
  @functions[:cudaRuntimeGetVersion].call(ptr_out)
end

.cudaSetDevice(device_id) ⇒ Integer

Returns CUDA status code.

Parameters:

  • device_id (Integer)

Returns:

  • (Integer)

    CUDA status code



509
510
511
512
# File 'lib/nvruby/cuda/runtime_api.rb', line 509

def cudaSetDevice(device_id)
  ensure_loaded!
  @functions[:cudaSetDevice].call(device_id)
end

.cudaStreamCreate(ptr_out) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



613
614
615
616
# File 'lib/nvruby/cuda/runtime_api.rb', line 613

def cudaStreamCreate(ptr_out)
  ensure_loaded!
  @functions[:cudaStreamCreate].call(ptr_out)
end

.cudaStreamDestroy(stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



619
620
621
622
# File 'lib/nvruby/cuda/runtime_api.rb', line 619

def cudaStreamDestroy(stream)
  ensure_loaded!
  @functions[:cudaStreamDestroy].call(stream)
end

.cudaStreamSynchronize(stream) ⇒ Integer

Returns status.

Returns:

  • (Integer)

    status



625
626
627
628
# File 'lib/nvruby/cuda/runtime_api.rb', line 625

def cudaStreamSynchronize(stream)
  ensure_loaded!
  @functions[:cudaStreamSynchronize].call(stream)
end

.device_get_attribute(attr_id, device) ⇒ Integer

Returns attribute value.

Parameters:

  • attr_id (Integer)

    CUDA device attribute ID

  • device (Integer)

    device index

Returns:

  • (Integer)

    attribute value



119
120
121
122
123
124
125
# File 'lib/nvruby/cuda/runtime_api.rb', line 119

def device_get_attribute(attr_id, device)
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  status = @functions[:cudaDeviceGetAttribute].call(ptr, attr_id, device)
  check_status!(status, 'cudaDeviceGetAttribute')
  ptr[0, Fiddle::SIZEOF_INT].unpack1('l')
end

.device_resetvoid

This method returns an undefined value.



110
111
112
113
114
# File 'lib/nvruby/cuda/runtime_api.rb', line 110

def device_reset
  ensure_loaded!
  status = @functions[:cudaDeviceReset].call
  check_status!(status, 'cudaDeviceReset')
end

.device_synchronizevoid

This method returns an undefined value.



103
104
105
106
107
# File 'lib/nvruby/cuda/runtime_api.rb', line 103

def device_synchronize
  ensure_loaded!
  status = @functions[:cudaDeviceSynchronize].call
  check_status!(status, 'cudaDeviceSynchronize')
end

.driver_versionInteger

Returns CUDA driver version.

Returns:

  • (Integer)

    CUDA driver version



389
390
391
392
393
394
395
# File 'lib/nvruby/cuda/runtime_api.rb', line 389

def driver_version
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  status = @functions[:cudaDriverGetVersion].call(ptr)
  check_status!(status, 'cudaDriverGetVersion')
  ptr[0, Fiddle::SIZEOF_INT].unpack1('l')
end

.ensure_loaded!void

This method returns an undefined value.

Ensure the CUDA runtime is loaded and all functions are bound.

Raises:

  • (Fiddle::DLError)

    if the library cannot be loaded



59
60
61
62
63
64
65
# File 'lib/nvruby/cuda/runtime_api.rb', line 59

def ensure_loaded!
  return if @loaded

  @handle = Fiddle::Handle.new(CUDART_LIB)
  attach_all_functions!
  @loaded = true
end

.event_createFiddle::Pointer

Create a CUDA event.

Returns:

  • (Fiddle::Pointer)

    event handle



293
294
295
296
297
298
299
# File 'lib/nvruby/cuda/runtime_api.rb', line 293

def event_create
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  status = @functions[:cudaEventCreate].call(ptr)
  check_status!(status, 'cudaEventCreate')
  Fiddle::Pointer.new(ptr[0, Fiddle::SIZEOF_VOIDP].unpack1('Q'))
end

.event_destroy(event) ⇒ void

This method returns an undefined value.

Destroy a CUDA event.

Parameters:

  • event (Fiddle::Pointer)


304
305
306
307
308
# File 'lib/nvruby/cuda/runtime_api.rb', line 304

def event_destroy(event)
  ensure_loaded!
  status = @functions[:cudaEventDestroy].call(event)
  check_status!(status, 'cudaEventDestroy')
end

.event_elapsed_time(start_event, end_event) ⇒ Float

Compute elapsed time between two events.

Parameters:

  • start_event (Fiddle::Pointer)
  • end_event (Fiddle::Pointer)

Returns:

  • (Float)

    elapsed time in milliseconds



333
334
335
336
337
338
339
# File 'lib/nvruby/cuda/runtime_api.rb', line 333

def event_elapsed_time(start_event, end_event)
  ensure_loaded!
  ms_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_FLOAT)
  status = @functions[:cudaEventElapsedTime].call(ms_ptr, start_event, end_event)
  check_status!(status, 'cudaEventElapsedTime')
  ms_ptr[0, Fiddle::SIZEOF_FLOAT].unpack1('e')
end

.event_record(event, stream) ⇒ void

This method returns an undefined value.

Record an event in a stream.

Parameters:

  • event (Fiddle::Pointer)
  • stream (Fiddle::Pointer)


314
315
316
317
318
# File 'lib/nvruby/cuda/runtime_api.rb', line 314

def event_record(event, stream)
  ensure_loaded!
  status = @functions[:cudaEventRecord].call(event, stream)
  check_status!(status, 'cudaEventRecord')
end

.event_synchronize(event) ⇒ void

This method returns an undefined value.

Block until event completes.

Parameters:

  • event (Fiddle::Pointer)


323
324
325
326
327
# File 'lib/nvruby/cuda/runtime_api.rb', line 323

def event_synchronize(event)
  ensure_loaded!
  status = @functions[:cudaEventSynchronize].call(event)
  check_status!(status, 'cudaEventSynchronize')
end

.free(ptr) ⇒ void

This method returns an undefined value.

Free device memory.

Parameters:

  • ptr (Fiddle::Pointer)

    device pointer to free



145
146
147
148
149
# File 'lib/nvruby/cuda/runtime_api.rb', line 145

def free(ptr)
  ensure_loaded!
  status = @functions[:cudaFree].call(ptr)
  check_status!(status, 'cudaFree')
end

.free_async(ptr, stream) ⇒ void

This method returns an undefined value.

Parameters:

  • ptr (Fiddle::Pointer)

    device pointer

  • stream (Fiddle::Pointer)

    CUDA stream



240
241
242
243
244
# File 'lib/nvruby/cuda/runtime_api.rb', line 240

def free_async(ptr, stream)
  ensure_loaded!
  status = @functions[:cudaFreeAsync].call(ptr, stream)
  check_status!(status, 'cudaFreeAsync')
end

.free_host(ptr) ⇒ void

This method returns an undefined value.

Free pinned host memory.

Parameters:

  • ptr (Fiddle::Pointer)


202
203
204
205
206
# File 'lib/nvruby/cuda/runtime_api.rb', line 202

def free_host(ptr)
  ensure_loaded!
  status = @functions[:cudaFreeHost].call(ptr)
  check_status!(status, 'cudaFreeHost')
end

.get_deviceInteger

Returns current device index.

Returns:

  • (Integer)

    current device index



86
87
88
89
90
91
92
# File 'lib/nvruby/cuda/runtime_api.rb', line 86

def get_device
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  status = @functions[:cudaGetDevice].call(ptr)
  check_status!(status, 'cudaGetDevice')
  ptr[0, Fiddle::SIZEOF_INT].unpack1('l')
end

.get_device_countInteger

Returns number of CUDA devices.

Returns:

  • (Integer)

    number of CUDA devices



77
78
79
80
81
82
83
# File 'lib/nvruby/cuda/runtime_api.rb', line 77

def get_device_count
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  status = @functions[:cudaGetDeviceCount].call(ptr)
  check_status!(status, 'cudaGetDeviceCount')
  ptr[0, Fiddle::SIZEOF_INT].unpack1('l')
end

.get_error_name(status) ⇒ String

Get error name for a status code.

Parameters:

  • status (Integer)

Returns:

  • (String)

    error name



369
370
371
372
373
# File 'lib/nvruby/cuda/runtime_api.rb', line 369

def get_error_name(status)
  ensure_loaded!
  ptr = @functions[:cudaGetErrorName].call(status)
  Fiddle::Pointer.new(ptr).to_s
end

.get_error_string(status) ⇒ String

Get error string for a status code.

Parameters:

  • status (Integer)

Returns:

  • (String)

    error description



360
361
362
363
364
# File 'lib/nvruby/cuda/runtime_api.rb', line 360

def get_error_string(status)
  ensure_loaded!
  ptr = @functions[:cudaGetErrorString].call(status)
  Fiddle::Pointer.new(ptr).to_s
end

.get_last_errorInteger

Returns last error code.

Returns:

  • (Integer)

    last error code



346
347
348
349
# File 'lib/nvruby/cuda/runtime_api.rb', line 346

def get_last_error
  ensure_loaded!
  @functions[:cudaGetLastError].call
end

.host_alloc(size, flags = HOST_ALLOC_DEFAULT) ⇒ Fiddle::Pointer

Allocate pinned host memory.

Parameters:

  • size (Integer)

    bytes

  • flags (Integer) (defaults to: HOST_ALLOC_DEFAULT)

    allocation flags

Returns:

  • (Fiddle::Pointer)

    host pointer



191
192
193
194
195
196
197
# File 'lib/nvruby/cuda/runtime_api.rb', line 191

def host_alloc(size, flags = HOST_ALLOC_DEFAULT)
  ensure_loaded!
  ptr_buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  status = @functions[:cudaHostAlloc].call(ptr_buf, size, flags)
  check_status!(status, "cudaHostAlloc(#{size})")
  Fiddle::Pointer.new(ptr_buf[0, Fiddle::SIZEOF_VOIDP].unpack1('Q'))
end

.loaded?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/nvruby/cuda/runtime_api.rb', line 68

def loaded?
  @loaded
end

.malloc(size) ⇒ Fiddle::Pointer

Allocate device memory.

Parameters:

  • size (Integer)

    bytes to allocate

Returns:

  • (Fiddle::Pointer)

    device pointer



134
135
136
137
138
139
140
# File 'lib/nvruby/cuda/runtime_api.rb', line 134

def malloc(size)
  ensure_loaded!
  ptr_buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  status = @functions[:cudaMalloc].call(ptr_buf, size)
  check_status!(status, "cudaMalloc(#{size})")
  Fiddle::Pointer.new(ptr_buf[0, Fiddle::SIZEOF_VOIDP].unpack1('Q'))
end

.malloc_async(size, stream) ⇒ Fiddle::Pointer

Returns device pointer.

Parameters:

  • size (Integer)

    bytes

  • stream (Fiddle::Pointer)

    CUDA stream

Returns:

  • (Fiddle::Pointer)

    device pointer



229
230
231
232
233
234
235
# File 'lib/nvruby/cuda/runtime_api.rb', line 229

def malloc_async(size, stream)
  ensure_loaded!
  ptr_buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  status = @functions[:cudaMallocAsync].call(ptr_buf, size, stream)
  check_status!(status, "cudaMallocAsync(#{size})")
  Fiddle::Pointer.new(ptr_buf[0, Fiddle::SIZEOF_VOIDP].unpack1('Q'))
end

.mem_get_infoHash

Query GPU memory info.

Returns:

  • (Hash)

    total_bytes:



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/nvruby/cuda/runtime_api.rb', line 210

def mem_get_info
  ensure_loaded!
  free_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T)
  total_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T)
  status = @functions[:cudaMemGetInfo].call(free_ptr, total_ptr)
  check_status!(status, 'cudaMemGetInfo')
  {
    free_bytes: free_ptr[0, Fiddle::SIZEOF_SIZE_T].unpack1('Q'),
    total_bytes: total_ptr[0, Fiddle::SIZEOF_SIZE_T].unpack1('Q')
  }
end

.memcpy(dst, src, count, kind) ⇒ void

This method returns an undefined value.

Copy memory.

Parameters:

  • dst (Fiddle::Pointer)

    destination

  • src (Fiddle::Pointer)

    source

  • count (Integer)

    bytes to copy

  • kind (Integer)

    copy direction constant



157
158
159
160
161
# File 'lib/nvruby/cuda/runtime_api.rb', line 157

def memcpy(dst, src, count, kind)
  ensure_loaded!
  status = @functions[:cudaMemcpy].call(dst, src, count, kind)
  check_status!(status, "cudaMemcpy(#{count} bytes, kind=#{kind})")
end

.memcpy_async(dst, src, count, kind, stream) ⇒ void

This method returns an undefined value.

Async memory copy.

Parameters:

  • dst (Fiddle::Pointer)
  • src (Fiddle::Pointer)
  • count (Integer)
  • kind (Integer)
  • stream (Fiddle::Pointer)

    CUDA stream



170
171
172
173
174
# File 'lib/nvruby/cuda/runtime_api.rb', line 170

def memcpy_async(dst, src, count, kind, stream)
  ensure_loaded!
  status = @functions[:cudaMemcpyAsync].call(dst, src, count, kind, stream)
  check_status!(status, "cudaMemcpyAsync(#{count} bytes)")
end

.memset(ptr, value, count) ⇒ void

This method returns an undefined value.

Set device memory.

Parameters:

  • ptr (Fiddle::Pointer)

    device pointer

  • value (Integer)

    byte value to set

  • count (Integer)

    bytes to set



181
182
183
184
185
# File 'lib/nvruby/cuda/runtime_api.rb', line 181

def memset(ptr, value, count)
  ensure_loaded!
  status = @functions[:cudaMemset].call(ptr, value, count)
  check_status!(status, "cudaMemset(#{count} bytes)")
end

.peek_at_last_errorInteger

Returns last error without clearing.

Returns:

  • (Integer)

    last error without clearing



352
353
354
355
# File 'lib/nvruby/cuda/runtime_api.rb', line 352

def peek_at_last_error
  ensure_loaded!
  @functions[:cudaPeekAtLastError].call
end

.runtime_versionInteger

Returns CUDA runtime version.

Returns:

  • (Integer)

    CUDA runtime version



380
381
382
383
384
385
386
# File 'lib/nvruby/cuda/runtime_api.rb', line 380

def runtime_version
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  status = @functions[:cudaRuntimeGetVersion].call(ptr)
  check_status!(status, 'cudaRuntimeGetVersion')
  ptr[0, Fiddle::SIZEOF_INT].unpack1('l')
end

.set_device(device) ⇒ void

This method returns an undefined value.

Parameters:

  • device (Integer)

    device index to set



96
97
98
99
100
# File 'lib/nvruby/cuda/runtime_api.rb', line 96

def set_device(device)
  ensure_loaded!
  status = @functions[:cudaSetDevice].call(device)
  check_status!(status, 'cudaSetDevice')
end

.stream_createFiddle::Pointer

Create a CUDA stream.

Returns:

  • (Fiddle::Pointer)

    stream handle



252
253
254
255
256
257
258
# File 'lib/nvruby/cuda/runtime_api.rb', line 252

def stream_create
  ensure_loaded!
  ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  status = @functions[:cudaStreamCreate].call(ptr)
  check_status!(status, 'cudaStreamCreate')
  Fiddle::Pointer.new(ptr[0, Fiddle::SIZEOF_VOIDP].unpack1('Q'))
end

.stream_destroy(stream) ⇒ void

This method returns an undefined value.

Destroy a CUDA stream.

Parameters:

  • stream (Fiddle::Pointer)


263
264
265
266
267
# File 'lib/nvruby/cuda/runtime_api.rb', line 263

def stream_destroy(stream)
  ensure_loaded!
  status = @functions[:cudaStreamDestroy].call(stream)
  check_status!(status, 'cudaStreamDestroy')
end

.stream_query(stream) ⇒ Boolean

Query stream completion status.

Parameters:

  • stream (Fiddle::Pointer)

Returns:

  • (Boolean)

    true if all work complete



281
282
283
284
285
# File 'lib/nvruby/cuda/runtime_api.rb', line 281

def stream_query(stream)
  ensure_loaded!
  status = @functions[:cudaStreamQuery].call(stream)
  status.zero?
end

.stream_synchronize(stream) ⇒ void

This method returns an undefined value.

Synchronize a stream (blocks until all commands complete).

Parameters:

  • stream (Fiddle::Pointer)


272
273
274
275
276
# File 'lib/nvruby/cuda/runtime_api.rb', line 272

def stream_synchronize(stream)
  ensure_loaded!
  status = @functions[:cudaStreamSynchronize].call(stream)
  check_status!(status, 'cudaStreamSynchronize')
end