Class: Ignis::JIT::Compiler

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

Overview

JIT Compiler with multi-level caching Provides runtime compilation of CUDA C++ source to executable kernels Based on nvmath-python caching strategy:

  • Thread-local cache for fast path (no locking)

  • Shared cache with Mutex for cross-thread reuse

  • Cache key: source code hash + compute capability

Constant Summary collapse

DEFAULT_OPTIONS =

Default compilation options

[
  "--fmad=true",
  "--std=c++17",
  "-default-device"
].freeze
THREAD_LOCAL_CACHE_KEY =

Thread-local storage key for kernel pointer cache

:nvruby_jit_kernel_cache

Class Method Summary collapse

Class Method Details

.cache_statsHash

Get cache statistics

Returns:

  • (Hash)

    Statistics about cache usage



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nvruby/jit/compiler.rb', line 81

def cache_stats
  @cache_mutex.synchronize do
    compiled_count = @compiled_code_cache.values.sum { |h| h.size }
    module_count = @kernel_module_cache.values.sum { |h| h.size }

    {
      compiled_kernels: compiled_count,
      loaded_modules: module_count,
      compute_capabilities: @compiled_code_cache.keys,
      devices_with_modules: @kernel_module_cache.keys
    }
  end
end

.clear_cache!void

This method returns an undefined value.

Clear all caches



64
65
66
67
68
69
70
71
# File 'lib/nvruby/jit/compiler.rb', line 64

def clear_cache!
  @cache_mutex.synchronize do
    @compiled_code_cache.clear
    @kernel_module_cache.clear
  end
  clear_thread_local_cache!
  Ignis.logger.info("JIT compiler caches cleared")
end

.clear_thread_local_cache!void

This method returns an undefined value.

Clear thread-local cache only



75
76
77
# File 'lib/nvruby/jit/compiler.rb', line 75

def clear_thread_local_cache!
  Thread.current[THREAD_LOCAL_CACHE_KEY] = nil
end

.compile(source, kernel_name, device_id: 0, options: []) ⇒ Kernel

Compile CUDA source code and return an executable Kernel

Parameters:

  • source (String)

    CUDA C++ source code

  • kernel_name (String)

    Name of the kernel function

  • device_id (Integer) (defaults to: 0)

    Target device (for compute capability)

  • options (Array<String>) (defaults to: [])

    Additional compilation options

Returns:

  • (Kernel)

    Executable kernel

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nvruby/jit/compiler.rb', line 37

def compile(source, kernel_name, device_id: 0, options: [])
  source_hash = compute_source_hash(source, kernel_name)
  cc = get_device_cc(device_id)

  kernel_ptr = get_cached_kernel(device_id, source_hash)
  return kernel_ptr if kernel_ptr

  @cache_mutex.synchronize do
    kernel_ptr = get_kernel_module_cached(source, source_hash, kernel_name, device_id, cc, options)
    cache_kernel_pointer(device_id, source_hash, kernel_ptr)
    kernel_ptr
  end
end

.compile_only(source, kernel_name, compute_capability:, options: []) ⇒ CompiledKernel

Compile to CompiledKernel without loading (for caching/serialization)

Parameters:

  • source (String)

    CUDA C++ source code

  • kernel_name (String)

    Name of the kernel function

  • compute_capability (Integer)

    Target compute capability (e.g., 89)

  • options (Array<String>) (defaults to: [])

    Additional compilation options

Returns:

Raises:



58
59
60
# File 'lib/nvruby/jit/compiler.rb', line 58

def compile_only(source, kernel_name, compute_capability:, options: [])
  compile_to_cubin(source, kernel_name, compute_capability, options)
end

.version_infoHash

Get version information

Returns:

  • (Hash)

    NVRTC and driver version info



97
98
99
100
101
102
# File 'lib/nvruby/jit/compiler.rb', line 97

def version_info
  {
    nvrtc: NVRTCBindings.version,
    cuda_driver: Ignis.cuda_version
  }
end