Class: Ignis::Memory::DeviceMemoryResource Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/nvruby/memory/device_memory_resource.rb

Overview

This class is abstract.

Abstract base class for device memory resources

Modeled after RMM’s device_memory_resource interface

Constant Summary collapse

ALIGNMENT =

Minimum alignment for all allocations (256 bytes per RMM standard)

256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_index: nil) ⇒ void

Parameters:

  • device_index (Integer) (defaults to: nil)

    GPU device index (default: current device)



16
17
18
19
# File 'lib/nvruby/memory/device_memory_resource.rb', line 16

def initialize(device_index: nil)
  @device_index = device_index || Ignis.configuration.default_device
  @mutex = Mutex.new
end

Instance Attribute Details

#device_indexInteger (readonly)

Returns Device index this resource manages.

Returns:

  • (Integer)

    Device index this resource manages



12
13
14
# File 'lib/nvruby/memory/device_memory_resource.rb', line 12

def device_index
  @device_index
end

Instance Method Details

#allocate(bytes, stream: nil) ⇒ FFI::Pointer

Allocate device memory with optional stream ordering

Parameters:

  • bytes (Integer)

    Number of bytes to allocate

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

    Optional stream for async allocation

Returns:

  • (FFI::Pointer)

    Device pointer

Raises:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nvruby/memory/device_memory_resource.rb', line 26

def allocate(bytes, stream: nil)
  raise ArgumentError, "bytes must be positive, got #{bytes}" if bytes <= 0

  aligned_bytes = align_up(bytes)

  @mutex.synchronize do
    ptr = do_allocate(aligned_bytes, stream)
    Stats.record_allocation(aligned_bytes)
    ptr
  end
end

#deallocate(ptr, bytes, stream: nil) ⇒ void

This method returns an undefined value.

Deallocate device memory with optional stream ordering

Parameters:

  • ptr (FFI::Pointer)

    Device pointer to free

  • bytes (Integer)

    Size of allocation

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

    Optional stream for async deallocation



43
44
45
46
47
48
49
50
51
52
# File 'lib/nvruby/memory/device_memory_resource.rb', line 43

def deallocate(ptr, bytes, stream: nil)
  return if ptr.null?

  aligned_bytes = align_up(bytes)

  @mutex.synchronize do
    do_deallocate(ptr, aligned_bytes, stream)
    Stats.record_deallocation(aligned_bytes)
  end
end

#inspectString

Returns:

  • (String)


73
74
75
# File 'lib/nvruby/memory/device_memory_resource.rb', line 73

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} device=#{@device_index}>"
end

#is_equal?(other) ⇒ Boolean

Check if memory from this resource can be deallocated by another

Parameters:

Returns:

  • (Boolean)


63
64
65
# File 'lib/nvruby/memory/device_memory_resource.rb', line 63

def is_equal?(other)
  self.class == other.class && @device_index == other.device_index
end

#supports_streams?Boolean

Check if this resource supports stream-ordered allocation

Returns:

  • (Boolean)


56
57
58
# File 'lib/nvruby/memory/device_memory_resource.rb', line 56

def supports_streams?
  false
end

#to_sString

Returns:

  • (String)


68
69
70
# File 'lib/nvruby/memory/device_memory_resource.rb', line 68

def to_s
  "#{self.class.name}[device=#{@device_index}]"
end