Class: Cumo::CUDA::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/cumo/cuda/event.rb

Overview

A CUDA event: a point in a stream that can be waited for, and timed.

start = Cumo::CUDA::Event.new.record
c = a.gemm(b)
stop = Cumo::CUDA::Event.new.record
stop.synchronize
Cumo::CUDA.get_elapsed_time(start, stop)   # => milliseconds

Constant Summary collapse

LIVE =
ObjectSpace::WeakMap.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocking: false, timing: true) ⇒ Event

blocking: makes synchronize sleep rather than spin; timing: false makes a lighter event that cannot be timed.



24
25
26
27
28
29
30
31
# File 'lib/cumo/cuda/event.rb', line 24

def initialize(blocking: false, timing: true)
  flags = Runtime::CUDA_EVENT_DEFAULT
  flags |= Runtime::CUDA_EVENT_BLOCKING_SYNC if blocking
  flags |= Runtime::CUDA_EVENT_DISABLE_TIMING unless timing
  @ptr = Runtime.cudaEventCreateWithFlags(flags)
  LIVE[@ptr] = self
  ObjectSpace.define_finalizer(self, self.class.finalizer(@ptr))
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



14
15
16
# File 'lib/cumo/cuda/event.rb', line 14

def ptr
  @ptr
end

Class Method Details

.finalizer(ptr) ⇒ Object

A handle may be given out again after a destroy, so an event that is still owned by a live object is not the one this finalizer was for.



18
19
20
# File 'lib/cumo/cuda/event.rb', line 18

def self.finalizer(ptr)
  proc { Runtime.cudaEventDestroy(ptr) rescue nil unless LIVE[ptr] }
end

Instance Method Details

#destroyObject



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

def destroy
  return if @ptr.nil?
  Runtime.cudaEventDestroy(@ptr)
  ObjectSpace.undefine_finalizer(self)
  @ptr = nil
end

#done?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cumo/cuda/event.rb', line 52

def done?
  Runtime.cudaEventQuery(handle)
end

#handleObject

The handle of an event that has not been destroyed.

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/cumo/cuda/event.rb', line 34

def handle
  raise ArgumentError, "the event is destroyed" if @ptr.nil?
  @ptr
end

#record(stream = Stream.current) ⇒ Object

Records the event on a stream, the current one by default, after everything queued on it so far.

Raises:

  • (TypeError)


41
42
43
44
45
# File 'lib/cumo/cuda/event.rb', line 41

def record(stream = Stream.current)
  raise TypeError, "record takes a Stream, got a #{stream.class}" unless stream.is_a?(Stream)
  Runtime.cudaEventRecord(handle, stream.handle)
  self
end

#synchronizeObject



47
48
49
50
# File 'lib/cumo/cuda/event.rb', line 47

def synchronize
  Runtime.cudaEventSynchronize(handle)
  self
end