Class: Ignis::JIT::KernelModule

Inherits:
Object
  • Object
show all
Defined in:
lib/nvruby/jit/kernel_module.rb

Overview

Represents a loaded CUDA module on a specific device Manages the CUmodule handle and CUfunction extraction Handles automatic cleanup via finalizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiled_kernel, device_id: 0) ⇒ KernelModule

Create a new KernelModule by loading compiled code onto a device

Parameters:

  • compiled_kernel (CompiledKernel)

    The compiled kernel to load

  • device_id (Integer) (defaults to: 0)

    Device to load onto

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nvruby/jit/kernel_module.rb', line 28

def initialize(compiled_kernel, device_id: 0)
  @compiled_kernel = compiled_kernel
  @device_id = device_id
  @loaded_at = Time.now
  @destroyed = false
  @mutex = Mutex.new

  load_module!
  extract_function!

  setup_finalizer
end

Instance Attribute Details

#compiled_kernelCompiledKernel (readonly)

Returns The compiled kernel this module was loaded from.

Returns:

  • (CompiledKernel)

    The compiled kernel this module was loaded from



10
11
12
# File 'lib/nvruby/jit/kernel_module.rb', line 10

def compiled_kernel
  @compiled_kernel
end

#device_idInteger (readonly)

Returns Device ID this module is loaded on.

Returns:

  • (Integer)

    Device ID this module is loaded on



13
14
15
# File 'lib/nvruby/jit/kernel_module.rb', line 13

def device_id
  @device_id
end

#function_handleFFI::Pointer (readonly)

Returns The CUfunction handle.

Returns:

  • (FFI::Pointer)

    The CUfunction handle



19
20
21
# File 'lib/nvruby/jit/kernel_module.rb', line 19

def function_handle
  @function_handle
end

#loaded_atTime (readonly)

Returns When this module was loaded.

Returns:

  • (Time)

    When this module was loaded



22
23
24
# File 'lib/nvruby/jit/kernel_module.rb', line 22

def loaded_at
  @loaded_at
end

#module_handleFFI::Pointer (readonly)

Returns The CUmodule handle.

Returns:

  • (FFI::Pointer)

    The CUmodule handle



16
17
18
# File 'lib/nvruby/jit/kernel_module.rb', line 16

def module_handle
  @module_handle
end

Instance Method Details

#destroy!void

This method returns an undefined value.

Unload the module from GPU memory



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nvruby/jit/kernel_module.rb', line 61

def destroy!
  @mutex.synchronize do
    return if @destroyed

    if @module_handle && !@module_handle.null?
      DriverAPIBindings.unload_module(@module_handle)
    end

    @module_handle = nil
    @function_handle = nil
    @destroyed = true

    Ignis.logger.debug("Unloaded kernel module: #{kernel_name} from device #{@device_id}")
  end
end

#destroyed?Boolean

Check if this module has been destroyed

Returns:

  • (Boolean)


49
50
51
# File 'lib/nvruby/jit/kernel_module.rb', line 49

def destroyed?
  @mutex.synchronize { @destroyed }
end

#inspectString

Get detailed inspection

Returns:

  • (String)


85
86
87
88
89
90
91
# File 'lib/nvruby/jit/kernel_module.rb', line 85

def inspect
  status = @destroyed ? "destroyed" : "loaded"
  "#<Ignis::JIT::KernelModule:0x#{object_id.to_s(16)} " \
    "kernel=#{kernel_name.inspect} " \
    "device=#{@device_id} " \
    "status=#{status}>"
end

#kernel_nameString

Get the kernel function name

Returns:

  • (String)


43
44
45
# File 'lib/nvruby/jit/kernel_module.rb', line 43

def kernel_name
  @compiled_kernel.kernel_name
end

#to_kernelKernel

Create a Kernel instance for execution

Returns:

  • (Kernel)

    Executable kernel wrapper



55
56
57
# File 'lib/nvruby/jit/kernel_module.rb', line 55

def to_kernel
  Kernel.new(self)
end

#to_sString

Get a string representation

Returns:

  • (String)


79
80
81
# File 'lib/nvruby/jit/kernel_module.rb', line 79

def to_s
  "#<Ignis::JIT::KernelModule #{kernel_name} device=#{@device_id}>"
end