Class: Ignis::CUDA::Stream
- Inherits:
-
Object
- Object
- Ignis::CUDA::Stream
- Defined in:
- lib/nvruby/cuda/stream.rb
Overview
CUDA stream for asynchronous operations.
Refactored to use Fiddle-based RuntimeAPI methods.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT =
Default stream singleton
:default
Instance Attribute Summary collapse
-
#handle ⇒ Fiddle::Pointer
readonly
Stream handle.
Class Method Summary collapse
-
.default ⇒ Stream
Get the default (null) stream.
-
.release_finalizer(handle) ⇒ Proc
Create a finalizer for stream cleanup.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
Query if stream has completed.
-
#default? ⇒ Boolean
Check if stream is the default stream.
-
#destroy! ⇒ void
Destroy the stream.
- #destroyed? ⇒ Boolean
-
#initialize(synchronous: false) ⇒ Stream
constructor
A new instance of Stream.
-
#synchronize ⇒ void
Synchronize the stream (wait for all operations to complete).
-
#to_ptr ⇒ Fiddle::Pointer
For interop.
- #to_s ⇒ String
Constructor Details
#initialize(synchronous: false) ⇒ Stream
Returns a new instance of 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
#handle ⇒ Fiddle::Pointer (readonly)
Returns Stream handle.
12 13 14 |
# File 'lib/nvruby/cuda/stream.rb', line 12 def handle @handle end |
Class Method Details
.default ⇒ Stream
Get the default (null) stream.
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.
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.
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.
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
41 42 43 |
# File 'lib/nvruby/cuda/stream.rb', line 41 def destroyed? @destroyed end |
#synchronize ⇒ void
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_ptr ⇒ Fiddle::Pointer
Returns for interop.
30 31 32 |
# File 'lib/nvruby/cuda/stream.rb', line 30 def to_ptr @handle end |
#to_s ⇒ 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 |