Class: FFTW3::AlignedMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/fftw3/aligned_memory.rb

Constant Summary collapse

REAL_SIZES =
{
  double: 8, float: 4, long_double: 16, quad: 16
}.freeze
COMPLEX_SIZES =
REAL_SIZES.transform_values { |v| v * 2 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count, type: :complex, precision: :double) ⇒ AlignedMemory

Returns a new instance of AlignedMemory.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fftw3/aligned_memory.rb', line 15

def initialize(count, type: :complex, precision: :double)
  @precision = precision
  @type = type
  @count = count

  sizes = (type == :complex) ? COMPLEX_SIZES : REAL_SIZES
  @element_size = sizes.fetch(precision)
  @size = count * @element_size

  ffi = FFTW3::FFI.module_for(precision)
  pfx = FFTW3::FFI.prefix_for(precision)

  @pointer = case type
             when :complex
               ffi.send(:"#{pfx}alloc_complex", count)
             when :real
               ffi.send(:"#{pfx}alloc_real", count)
             else
               ffi.send(:"#{pfx}malloc", @size)
             end

  raise FFTW3::Error::PlanError, "fftw_malloc returned NULL" if @pointer.null?

  @freed_flag = [false]
  @free_func = ffi.method(:"#{pfx}free")
  ObjectSpace.define_finalizer(self, self.class.release(@pointer, @free_func, @freed_flag))
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



7
8
9
# File 'lib/fftw3/aligned_memory.rb', line 7

def pointer
  @pointer
end

#precisionObject (readonly)

Returns the value of attribute precision.



7
8
9
# File 'lib/fftw3/aligned_memory.rb', line 7

def precision
  @precision
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/fftw3/aligned_memory.rb', line 7

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/fftw3/aligned_memory.rb', line 7

def type
  @type
end

Class Method Details

.release(ptr, free_func, freed_flag) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/fftw3/aligned_memory.rb', line 135

def self.release(ptr, free_func, freed_flag)
  proc {
    unless freed_flag[0] || ptr.null?
      free_func.call(ptr)
      freed_flag[0] = true
    end
  }
end

Instance Method Details

#free!Object



57
58
59
60
61
62
# File 'lib/fftw3/aligned_memory.rb', line 57

def free!
  return if @freed_flag[0]

  @free_func.call(@pointer)
  @freed_flag[0] = true
end

#readObject



50
51
52
53
54
55
# File 'lib/fftw3/aligned_memory.rb', line 50

def read
  case @type
  when :real    then read_real
  when :complex then read_complex
  end
end

#write(data) ⇒ Object



43
44
45
46
47
48
# File 'lib/fftw3/aligned_memory.rb', line 43

def write(data)
  case @type
  when :real    then write_real(data)
  when :complex then write_complex(data)
  end
end