Class: Khiva::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/khiva/array.rb

Constant Summary collapse

TYPES =
[:f32, :c32, :f64, :c64, :b8, :s32, :u32, :u8, :s64, :u64, :s16, :u16]
TYPE_FORMAT =

TODO support complex

{
  f32: "f",
  c32: "",
  f64: "d",
  c64: "",
  b8: "c",
  s32: "l",
  u32: "L",
  u8: "C",
  s64: "q",
  u64: "Q",
  s16: "s",
  u16: "S"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, type: nil) ⇒ Array

Returns a new instance of Array.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/khiva/array.rb', line 20

def initialize(obj, type: nil)
  if obj.is_a?(Fiddle::Pointer)
    @ptr = obj
  else
    # TODO make more performant for Numo
    obj = obj.to_a
    dims = []
    o = obj
    4.times do
      break unless o.is_a?(::Array)
      dims << o.size
      o = o.first
    end
    dims.reverse!

    flat_obj = obj.flatten(dims.size)

    # TODO check each dimension
    expected_size = dims.inject(1, &:*)
    raise Error, "Unexpected size" if flat_obj.size != expected_size

    # TODO check integer range
    type ||= flat_obj.all? { |v| v.is_a?(Integer) } ? :s64 : :f64

    data = Fiddle::Pointer[flat_obj.pack("#{TYPE_FORMAT[type]}*")]
    ndims = dims.size
    dims = Fiddle::Pointer[dims.pack("q!*")]
    @ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
    FFI.call(:create_array, data, ndims, dims, @ptr, TYPES.index(type))
    ObjectSpace.define_finalizer(@ptr, self.class.finalize(@ptr.to_i))
  end
end

Class Method Details

.finalize(addr) ⇒ Object



131
132
133
134
# File 'lib/khiva/array.rb', line 131

def self.finalize(addr)
  # must use proc instead of stabby lambda
  proc { FFI.call(:delete_array, addr) }
end

Instance Method Details

#copyObject Also known as: dup, clone



123
124
125
126
127
# File 'lib/khiva/array.rb', line 123

def copy
  result = Utils.create_ptr
  FFI.call(:copy, @ptr, result)
  self.class.new(result)
end

#dimsObject



92
93
94
95
96
# File 'lib/khiva/array.rb', line 92

def dims
  dims = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG_LONG * 4)
  FFI.call(:get_dims, @ptr, dims)
  dims.to_s(dims.size).unpack("q!*")
end

#displayObject



53
54
55
# File 'lib/khiva/array.rb', line 53

def display
  FFI.call(:display, @ptr)
end

#element_sizeObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/khiva/array.rb', line 108

def element_size
  case type
  when :b8, :u8
    1
  when :s16, :u16
    2
  when :f32, :c32, :s32, :u32
    4
  when :f64, :c64, :s64, :u64
    8
  else
    raise Error, "Unknown type: #{type}"
  end
end

#to_aObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/khiva/array.rb', line 79

def to_a
  d = dims
  d.pop while d.last == 1 && d.size > 1
  d.reverse!

  elements = dims.inject(1, &:*)
  data = Fiddle::Pointer.malloc(elements * element_size)
  FFI.call(:get_data, @ptr, data)
  result = data.to_s(data.size).unpack("#{TYPE_FORMAT[type]}*")
  result.map! { |r| r > 0 ? true : false } if type == :b8
  Utils.reshape(result, d)
end

#to_ptrObject



104
105
106
# File 'lib/khiva/array.rb', line 104

def to_ptr
  @ptr
end

#typeObject



98
99
100
101
102
# File 'lib/khiva/array.rb', line 98

def type
  type = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
  FFI.call(:get_type, @ptr, type)
  TYPES[type.to_s(type.size).unpack1("i")]
end