Class: Ignis::JIT::Kernel

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

Overview

High-level kernel execution interface Provides easy-to-use launch configuration and argument marshaling

Defined Under Namespace

Classes: U64

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kernel_module) ⇒ Kernel

Create a new Kernel wrapper

Parameters:



19
20
21
22
23
24
# File 'lib/nvruby/jit/kernel.rb', line 19

def initialize(kernel_module)
  @kernel_module = kernel_module
  @launch_count = 0
  @total_launch_time = 0.0
  @mutex = Mutex.new
end

Instance Attribute Details

#kernel_moduleKernelModule (readonly)

Returns The underlying kernel module.

Returns:



9
10
11
# File 'lib/nvruby/jit/kernel.rb', line 9

def kernel_module
  @kernel_module
end

#launch_countInteger (readonly)

Returns Total number of times this kernel has been launched.

Returns:

  • (Integer)

    Total number of times this kernel has been launched



12
13
14
# File 'lib/nvruby/jit/kernel.rb', line 12

def launch_count
  @launch_count
end

#total_launch_timeFloat (readonly)

Returns Total time spent in kernel launches (seconds).

Returns:

  • (Float)

    Total time spent in kernel launches (seconds)



15
16
17
# File 'lib/nvruby/jit/kernel.rb', line 15

def total_launch_time
  @total_launch_time
end

Class Method Details

.calc_grid_size(total_elements, block_size: 256) ⇒ Array<Integer>

Calculate optimal grid size for a given element count

Parameters:

  • total_elements (Integer)

    Total number of elements to process

  • block_size (Integer) (defaults to: 256)

    Desired block size (threads per block)

Returns:

  • (Array<Integer>)

    Grid dimensions



89
90
91
92
# File 'lib/nvruby/jit/kernel.rb', line 89

def self.calc_grid_size(total_elements, block_size: 256)
  blocks = (total_elements + block_size - 1) / block_size
  [blocks]
end

Instance Method Details

#device_idInteger

Get the device this kernel is loaded on

Returns:

  • (Integer)


34
35
36
# File 'lib/nvruby/jit/kernel.rb', line 34

def device_id
  @kernel_module.device_id
end

#inspectString

Get detailed inspection

Returns:

  • (String)


116
117
118
119
120
121
# File 'lib/nvruby/jit/kernel.rb', line 116

def inspect
  "#<Ignis::JIT::Kernel:0x#{object_id.to_s(16)} " \
    "name=#{name.inspect} " \
    "device=#{device_id} " \
    "launches=#{@launch_count}>"
end

#launch(grid:, block:, args: [], shared_mem: 0, stream: nil) ⇒ self

Launch the kernel with specified configuration

Parameters:

  • grid (Array<Integer>, Integer)

    Grid dimensions (blocks)

  • block (Array<Integer>, Integer)

    Block dimensions (threads)

  • args (Array) (defaults to: [])

    Kernel arguments (NvArray, scalars, pointers)

  • shared_mem (Integer) (defaults to: 0)

    Dynamic shared memory in bytes

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

    CUDA stream for async execution

Returns:

  • (self)

    Returns self for chaining

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nvruby/jit/kernel.rb', line 47

def launch(grid:, block:, args: [], shared_mem: 0, stream: nil)
  raise InvalidOperationError, "Kernel module has been destroyed" if @kernel_module.destroyed?

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  Ignis.set_device(device_id)
  param_pointers = marshal_arguments(args)
  stream_ptr = extract_stream_pointer(stream)

  DriverAPIBindings.launch_kernel(
    @kernel_module.function_handle,
    grid_dim: normalize_dims(grid),
    block_dim: normalize_dims(block),
    kernel_params: param_pointers,
    shared_mem: shared_mem,
    stream: stream_ptr
  )

  @mutex.synchronize do
    @launch_count += 1
    @total_launch_time += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  end

  self
end

#launch_sync(grid:, block:, args: [], shared_mem: 0) ⇒ self

Synchronous kernel launch with automatic device synchronization

Parameters:

  • grid (Array<Integer>, Integer)

    Grid dimensions

  • block (Array<Integer>, Integer)

    Block dimensions

  • args (Array) (defaults to: [])

    Kernel arguments

  • shared_mem (Integer) (defaults to: 0)

    Dynamic shared memory

Returns:

  • (self)


79
80
81
82
83
# File 'lib/nvruby/jit/kernel.rb', line 79

def launch_sync(grid:, block:, args: [], shared_mem: 0)
  launch(grid: grid, block: block, args: args, shared_mem: shared_mem, stream: nil)
  Ignis.synchronize
  self
end

#nameString

Get the kernel function name

Returns:

  • (String)


28
29
30
# File 'lib/nvruby/jit/kernel.rb', line 28

def name
  @kernel_module.kernel_name
end

#statsHash

Get execution statistics

Returns:

  • (Hash)

    Statistics including launch count and timing



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nvruby/jit/kernel.rb', line 96

def stats
  @mutex.synchronize do
    {
      kernel_name: name,
      device_id: device_id,
      launch_count: @launch_count,
      total_launch_time: @total_launch_time,
      avg_launch_time: @launch_count.positive? ? @total_launch_time / @launch_count : 0.0
    }
  end
end

#to_sString

Get a string representation

Returns:

  • (String)


110
111
112
# File 'lib/nvruby/jit/kernel.rb', line 110

def to_s
  "#<Ignis::JIT::Kernel #{name} device=#{device_id} launches=#{@launch_count}>"
end