Class: Ignis::Memory::HostMemoryResource Abstract

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

Overview

This class is abstract.

Abstract base class for host memory resources

Direct Known Subclasses

PinnedHostMemoryResource

Constant Summary collapse

ALIGNMENT =

Minimum alignment for all allocations

64

Instance Method Summary collapse

Constructor Details

#initializevoid



11
12
13
# File 'lib/nvruby/memory/pinned_host_memory_resource.rb', line 11

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#allocate(bytes) ⇒ FFI::Pointer

Allocate host memory

Parameters:

  • bytes (Integer)

    Number of bytes to allocate

Returns:

  • (FFI::Pointer)

    Host pointer

Raises:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nvruby/memory/pinned_host_memory_resource.rb', line 19

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

  aligned_bytes = align_up(bytes)

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

#deallocate(ptr, bytes) ⇒ void

This method returns an undefined value.

Deallocate host memory

Parameters:

  • ptr (FFI::Pointer)

    Host pointer to free

  • bytes (Integer)

    Size of allocation



35
36
37
38
39
40
41
42
43
44
# File 'lib/nvruby/memory/pinned_host_memory_resource.rb', line 35

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

  aligned_bytes = align_up(bytes)

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