Class: Ignis::CUDA::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/nvruby/cuda/stream.rb

Overview

CUDA stream for asynchronous operations.

Refactored to use Fiddle-based RuntimeAPI methods.

Direct Known Subclasses

DefaultStream

Constant Summary collapse

DEFAULT =

Default stream singleton

:default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(synchronous: false) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • synchronous (Boolean) (defaults to: false)

    If true, creates a synchronous stream



18
19
20
21
22
23
24
25
26
27
# File 'lib/nvruby/cuda/stream.rb', line 18

def initialize(synchronous: false)
  @synchronous = synchronous
  @handle = create_stream
  @destroyed = false

  unless @synchronous
    captured_handle = @handle
    ObjectSpace.define_finalizer(self, self.class.release_finalizer(captured_handle))
  end
end

Instance Attribute Details

#handleFiddle::Pointer (readonly)

Returns Stream handle.

Returns:

  • (Fiddle::Pointer)

    Stream handle



12
13
14
# File 'lib/nvruby/cuda/stream.rb', line 12

def handle
  @handle
end

Class Method Details

.defaultStream

Get the default (null) stream.

Returns:



86
87
88
# File 'lib/nvruby/cuda/stream.rb', line 86

def default
  @default_stream ||= DefaultStream.new
end

.release_finalizer(handle) ⇒ Proc

Create a finalizer for stream cleanup.

Parameters:

  • handle (Fiddle::Pointer)

Returns:

  • (Proc)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nvruby/cuda/stream.rb', line 93

def release_finalizer(handle)
  handle_addr = handle.to_i
  proc do
    next if handle_addr.zero?

    begin
      RuntimeAPI.ensure_loaded!
      RuntimeAPI.stream_destroy(Fiddle::Pointer.new(handle_addr))
    rescue StandardError
      # Silently ignore errors during finalization
    end
  end
end

Instance Method Details

#completed?Boolean

Query if stream has completed.

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/nvruby/cuda/stream.rb', line 56

def completed?
  return true if @destroyed || default?

  RuntimeAPI.ensure_loaded!
  RuntimeAPI.stream_query(@handle)
end

#default?Boolean

Check if stream is the default stream.

Returns:

  • (Boolean)


36
37
38
# File 'lib/nvruby/cuda/stream.rb', line 36

def default?
  @handle.nil? || @handle.to_i.zero?
end

#destroy!void

This method returns an undefined value.

Destroy the stream.



65
66
67
68
69
70
71
72
73
# File 'lib/nvruby/cuda/stream.rb', line 65

def destroy!
  return if @destroyed || default?

  RuntimeAPI.ensure_loaded!
  RuntimeAPI.stream_destroy(@handle)

  @destroyed = true
  ObjectSpace.undefine_finalizer(self)
end

#destroyed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/nvruby/cuda/stream.rb', line 41

def destroyed?
  @destroyed
end

#synchronizevoid

This method returns an undefined value.

Synchronize the stream (wait for all operations to complete).



47
48
49
50
51
52
# File 'lib/nvruby/cuda/stream.rb', line 47

def synchronize
  return if @destroyed || default?

  RuntimeAPI.ensure_loaded!
  RuntimeAPI.stream_synchronize(@handle)
end

#to_ptrFiddle::Pointer

Returns for interop.

Returns:

  • (Fiddle::Pointer)

    for interop



30
31
32
# File 'lib/nvruby/cuda/stream.rb', line 30

def to_ptr
  @handle
end

#to_sString

Returns:

  • (String)


76
77
78
79
80
81
# File 'lib/nvruby/cuda/stream.rb', line 76

def to_s
  return 'CudaStream[default]' if default?
  return 'CudaStream[destroyed]' if @destroyed

  "CudaStream[0x#{@handle.to_i.to_s(16)}]"
end