Class: Ignis::CUDA::Event

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

Overview

CUDA event for timing and synchronization.

Refactored to use Fiddle-based RuntimeAPI methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocking: false, disable_timing: false) ⇒ Event

Returns a new instance of Event.

Parameters:

  • blocking (Boolean) (defaults to: false)

    If true, CPU will block on synchronize

  • disable_timing (Boolean) (defaults to: false)

    If true, timing is disabled for better performance



146
147
148
149
150
151
152
153
154
# File 'lib/nvruby/cuda/stream.rb', line 146

def initialize(blocking: false, disable_timing: false)
  @blocking = blocking
  @disable_timing = disable_timing
  @handle = create_event
  @destroyed = false

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

Instance Attribute Details

#handleFiddle::Pointer (readonly)

Returns Event handle.

Returns:

  • (Fiddle::Pointer)

    Event handle



142
143
144
# File 'lib/nvruby/cuda/stream.rb', line 142

def handle
  @handle
end

Class Method Details

.elapsed_time(start_event, end_event) ⇒ Float

Calculate elapsed time between two events.

Parameters:

  • start_event (Event)

    Start event

  • end_event (Event)

    End event

Returns:

  • (Float)

    Elapsed time in milliseconds



190
191
192
193
# File 'lib/nvruby/cuda/stream.rb', line 190

def self.elapsed_time(start_event, end_event)
  RuntimeAPI.ensure_loaded!
  RuntimeAPI.event_elapsed_time(start_event.handle, end_event.handle)
end

.release_finalizer(handle) ⇒ Proc

Create a finalizer for event cleanup.

Parameters:

  • handle (Fiddle::Pointer)

Returns:

  • (Proc)


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

def release_finalizer(handle)
  handle_addr = handle.to_i
  proc do
    begin
      RuntimeAPI.ensure_loaded!
      RuntimeAPI.event_destroy(Fiddle::Pointer.new(handle_addr))
    rescue StandardError
      # Silently ignore errors during finalization
    end
  end
end

Instance Method Details

#destroy!void

This method returns an undefined value.

Destroy the event.



197
198
199
200
201
202
203
204
205
# File 'lib/nvruby/cuda/stream.rb', line 197

def destroy!
  return if @destroyed

  RuntimeAPI.ensure_loaded!
  RuntimeAPI.event_destroy(@handle)

  @destroyed = true
  ObjectSpace.undefine_finalizer(self)
end

#destroyed?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/nvruby/cuda/stream.rb', line 157

def destroyed?
  @destroyed
end

#record(stream: nil) ⇒ void

This method returns an undefined value.

Record the event on a stream.

Parameters:

  • stream (Stream, nil) (defaults to: nil)

    Stream to record on (nil for default)

Raises:



164
165
166
167
168
169
170
# File 'lib/nvruby/cuda/stream.rb', line 164

def record(stream: nil)
  raise InvalidOperationError, 'Event has been destroyed' if @destroyed

  RuntimeAPI.ensure_loaded!
  stream_handle = stream&.to_ptr || Fiddle::Pointer.new(0)
  RuntimeAPI.event_record(@handle, stream_handle)
end

#synchronizevoid

This method returns an undefined value.

Wait for the event to complete.



174
175
176
177
178
179
# File 'lib/nvruby/cuda/stream.rb', line 174

def synchronize
  raise InvalidOperationError, 'Event has been destroyed' if @destroyed

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

#to_ptrFiddle::Pointer

Returns:

  • (Fiddle::Pointer)


182
183
184
# File 'lib/nvruby/cuda/stream.rb', line 182

def to_ptr
  @handle
end