Class: Ignis::CUDA::Device

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

Overview

Represents a CUDA GPU device.

Uses RuntimeAPI (Fiddle-based) for hot-path attribute queries and DeviceProperties (FFI::Struct) for full device property reads.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Device

Returns a new instance of Device.

Parameters:

  • index (Integer)

    Device index



52
53
54
55
56
57
58
# File 'lib/nvruby/cuda/device.rb', line 52

def initialize(index)
  @index = index
  @attribute_cache = {}
  @name = nil
  @total_memory = nil
  @props_cache = nil
end

Instance Attribute Details

#indexInteger (readonly)

Returns Device index.

Returns:

  • (Integer)

    Device index



49
50
51
# File 'lib/nvruby/cuda/device.rb', line 49

def index
  @index
end

Class Method Details

.available?Boolean

Check if any CUDA device is available.

Returns:

  • (Boolean)


254
255
256
257
258
# File 'lib/nvruby/cuda/device.rb', line 254

def available?
  count.positive?
rescue CudaRuntimeError
  false
end

.countInteger

Get the number of CUDA devices.

Returns:

  • (Integer)


228
229
230
231
# File 'lib/nvruby/cuda/device.rb', line 228

def count
  RuntimeAPI.ensure_loaded!
  RuntimeAPI.get_device_count
end

.currentDevice

Get the current device.

Returns:



241
242
243
244
# File 'lib/nvruby/cuda/device.rb', line 241

def current
  RuntimeAPI.ensure_loaded!
  new(RuntimeAPI.get_device)
end

.defaultDevice

Get the default device from configuration.

Returns:



248
249
250
# File 'lib/nvruby/cuda/device.rb', line 248

def default
  new(Ignis.configuration.default_device)
end

.listArray<Device>

List all available devices.

Returns:



235
236
237
# File 'lib/nvruby/cuda/device.rb', line 235

def list
  count.times.map { |i| new(i) }
end

Instance Method Details

#clock_rateInteger

Returns Clock rate in kHz.

Returns:

  • (Integer)

    Clock rate in kHz



128
129
130
# File 'lib/nvruby/cuda/device.rb', line 128

def clock_rate
  get_attribute(DeviceAttribute::CLOCK_RATE)
end

#compute_capabilityString

Returns Compute capability as “major.minor”.

Returns:

  • (String)

    Compute capability as “major.minor”



90
91
92
# File 'lib/nvruby/cuda/device.rb', line 90

def compute_capability
  "#{compute_capability_major}.#{compute_capability_minor}"
end

#compute_capability_majorInteger

Returns Major compute capability.

Returns:

  • (Integer)

    Major compute capability



95
96
97
# File 'lib/nvruby/cuda/device.rb', line 95

def compute_capability_major
  get_attribute(DeviceAttribute::COMPUTE_CAPABILITY_MAJOR)
end

#compute_capability_minorInteger

Returns Minor compute capability.

Returns:

  • (Integer)

    Minor compute capability



100
101
102
# File 'lib/nvruby/cuda/device.rb', line 100

def compute_capability_minor
  get_attribute(DeviceAttribute::COMPUTE_CAPABILITY_MINOR)
end

#concurrent_kernels?Boolean

Returns Whether concurrent kernels are supported.

Returns:

  • (Boolean)

    Whether concurrent kernels are supported



153
154
155
# File 'lib/nvruby/cuda/device.rb', line 153

def concurrent_kernels?
  get_attribute(DeviceAttribute::CONCURRENT_KERNELS) != 0
end

#cooperative_launch?Boolean

Returns Whether cooperative launch is supported.

Returns:

  • (Boolean)

    Whether cooperative launch is supported



168
169
170
# File 'lib/nvruby/cuda/device.rb', line 168

def cooperative_launch?
  get_attribute(DeviceAttribute::COOPERATIVE_LAUNCH) != 0
end

#ecc_enabled?Boolean

Returns Whether ECC memory is enabled.

Returns:

  • (Boolean)

    Whether ECC memory is enabled



148
149
150
# File 'lib/nvruby/cuda/device.rb', line 148

def ecc_enabled?
  get_attribute(DeviceAttribute::ECC_ENABLED) != 0
end

#free_memoryInteger

Returns Free memory in bytes.

Returns:

  • (Integer)

    Free memory in bytes



181
182
183
# File 'lib/nvruby/cuda/device.rb', line 181

def free_memory
  memory_info[:free_bytes]
end

#inspectString

Returns Detailed inspection.

Returns:

  • (String)

    Detailed inspection



220
221
222
223
# File 'lib/nvruby/cuda/device.rb', line 220

def inspect
  "#<Ignis::CUDA::Device:#{object_id} index=#{@index} name=#{name.inspect} " \
    "compute=#{compute_capability} memory=#{total_memory}>"
end

#l2_cache_sizeInteger

Returns L2 cache size in bytes.

Returns:

  • (Integer)

    L2 cache size in bytes



143
144
145
# File 'lib/nvruby/cuda/device.rb', line 143

def l2_cache_size
  get_attribute(DeviceAttribute::L2_CACHE_SIZE)
end

#managed_memory?Boolean

Returns Whether managed memory is supported.

Returns:

  • (Boolean)

    Whether managed memory is supported



163
164
165
# File 'lib/nvruby/cuda/device.rb', line 163

def managed_memory?
  get_attribute(DeviceAttribute::MANAGED_MEMORY) != 0
end

#max_grid_sizeArray<Integer>

Returns Max grid size [x, y, z].

Returns:

  • (Array<Integer>)

    Max grid size [x, y, z]



119
120
121
122
123
124
125
# File 'lib/nvruby/cuda/device.rb', line 119

def max_grid_size
  [
    get_attribute(DeviceAttribute::MAX_GRID_DIM_X),
    get_attribute(DeviceAttribute::MAX_GRID_DIM_Y),
    get_attribute(DeviceAttribute::MAX_GRID_DIM_Z)
  ]
end

#max_threads_dimArray<Integer>

Returns Max threads per dimension [x, y, z].

Returns:

  • (Array<Integer>)

    Max threads per dimension [x, y, z]



110
111
112
113
114
115
116
# File 'lib/nvruby/cuda/device.rb', line 110

def max_threads_dim
  [
    get_attribute(DeviceAttribute::MAX_BLOCK_DIM_X),
    get_attribute(DeviceAttribute::MAX_BLOCK_DIM_Y),
    get_attribute(DeviceAttribute::MAX_BLOCK_DIM_Z)
  ]
end

#max_threads_per_blockInteger

Returns Max threads per block.

Returns:

  • (Integer)

    Max threads per block



105
106
107
# File 'lib/nvruby/cuda/device.rb', line 105

def max_threads_per_block
  get_attribute(DeviceAttribute::MAX_THREADS_PER_BLOCK)
end

#memory_bus_widthInteger

Returns Memory bus width in bits.

Returns:

  • (Integer)

    Memory bus width in bits



138
139
140
# File 'lib/nvruby/cuda/device.rb', line 138

def memory_bus_width
  get_attribute(DeviceAttribute::GLOBAL_MEMORY_BUS_WIDTH)
end

#memory_clock_rateInteger

Returns Memory clock rate in kHz.

Returns:

  • (Integer)

    Memory clock rate in kHz



133
134
135
# File 'lib/nvruby/cuda/device.rb', line 133

def memory_clock_rate
  get_attribute(DeviceAttribute::MEMORY_CLOCK_RATE)
end

#memory_infoHash

Get available and total memory via RuntimeAPI (Fiddle hot path).

Returns:

  • (Hash)

    total_bytes:



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

def memory_info
  RuntimeAPI.ensure_loaded!
  RuntimeAPI.set_device(@index)
  RuntimeAPI.mem_get_info
end

#multiprocessor_countInteger

Returns Number of streaming multiprocessors.

Returns:

  • (Integer)

    Number of streaming multiprocessors



80
81
82
# File 'lib/nvruby/cuda/device.rb', line 80

def multiprocessor_count
  get_attribute(DeviceAttribute::MULTIPROCESSOR_COUNT)
end

#nameString

Returns Device name.

Returns:

  • (String)

    Device name



61
62
63
64
65
# File 'lib/nvruby/cuda/device.rb', line 61

def name
  return @name if @name

  @name = properties[:name]
end

#propertiesHash

Get full device properties via DeviceProperties (FFI::Struct). Cached after first call.

Returns:

  • (Hash)

    device property summary



188
189
190
# File 'lib/nvruby/cuda/device.rb', line 188

def properties
  @props_cache ||= DeviceProperties.summary(@index)
end

#reset!void

This method returns an undefined value.

Reset this device.



208
209
210
211
# File 'lib/nvruby/cuda/device.rb', line 208

def reset!
  set_current!
  RuntimeAPI.device_reset
end

#set_current!void

This method returns an undefined value.

Set this device as the current device.



194
195
196
197
# File 'lib/nvruby/cuda/device.rb', line 194

def set_current!
  RuntimeAPI.ensure_loaded!
  RuntimeAPI.set_device(@index)
end

#shared_memory_per_blockInteger

Returns Shared memory per block in bytes.

Returns:

  • (Integer)

    Shared memory per block in bytes



75
76
77
# File 'lib/nvruby/cuda/device.rb', line 75

def shared_memory_per_block
  get_attribute(DeviceAttribute::MAX_SHARED_MEMORY_PER_BLOCK)
end

#synchronizevoid

This method returns an undefined value.

Synchronize this device.



201
202
203
204
# File 'lib/nvruby/cuda/device.rb', line 201

def synchronize
  set_current!
  RuntimeAPI.device_synchronize
end

#to_sString

Returns Human-readable device description.

Returns:

  • (String)

    Human-readable device description



214
215
216
217
# File 'lib/nvruby/cuda/device.rb', line 214

def to_s
  mem_mb = total_memory / (1024 * 1024)
  "Device[#{@index}]: #{name} (CC #{compute_capability}, #{mem_mb} MB)"
end

#total_memoryInteger

Returns Total global memory in bytes.

Returns:

  • (Integer)

    Total global memory in bytes



68
69
70
71
72
# File 'lib/nvruby/cuda/device.rb', line 68

def total_memory
  return @total_memory if @total_memory

  @total_memory = memory_info[:total_bytes]
end

#unified_addressing?Boolean

Returns Whether unified addressing is supported.

Returns:

  • (Boolean)

    Whether unified addressing is supported



158
159
160
# File 'lib/nvruby/cuda/device.rb', line 158

def unified_addressing?
  get_attribute(DeviceAttribute::UNIFIED_ADDRESSING) != 0
end

#warp_sizeInteger

Returns Warp size (typically 32).

Returns:

  • (Integer)

    Warp size (typically 32)



85
86
87
# File 'lib/nvruby/cuda/device.rb', line 85

def warp_size
  get_attribute(DeviceAttribute::WARP_SIZE)
end