Class: Khiva::Array
- Inherits:
-
Object
- Object
- Khiva::Array
- 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
- #copy ⇒ Object (also: #dup, #clone)
- #dims ⇒ Object
- #display ⇒ Object
- #element_size ⇒ Object
-
#initialize(obj, type: nil) ⇒ Array
constructor
A new instance of Array.
- #to_a ⇒ Object
- #to_ptr ⇒ Object
- #type ⇒ Object
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 52 53 54 |
# 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!*")] result = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP) FFI.call(:create_array, data, ndims, dims, result, TYPES.index(type)) @ptr = result end ObjectSpace.define_finalizer(self, self.class.finalize(@ptr)) end |
Class Method Details
Instance Method Details
#copy ⇒ Object Also known as: dup, clone
126 127 128 129 130 |
# File 'lib/khiva/array.rb', line 126 def copy result = Utils.create_ptr FFI.call(:copy, @ptr, result) self.class.new(result) end |
#dims ⇒ Object
95 96 97 98 99 |
# File 'lib/khiva/array.rb', line 95 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 |
#display ⇒ Object
56 57 58 |
# File 'lib/khiva/array.rb', line 56 def display FFI.call(:display, @ptr) end |
#element_size ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/khiva/array.rb', line 111 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_a ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/khiva/array.rb', line 82 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_ptr ⇒ Object
107 108 109 |
# File 'lib/khiva/array.rb', line 107 def to_ptr @ptr end |