Class: CooCoo::CUDA::HostBuffer
- Defined in:
- lib/coo-coo/cuda/host_buffer.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #byte_size ⇒ Object
- #each(&block) ⇒ Object
- #get ⇒ Object
-
#initialize(size, type = :double) ⇒ HostBuffer
constructor
A new instance of HostBuffer.
- #resize(new_size) ⇒ Object
- #set(values) ⇒ Object
- #to_a ⇒ Object
- #to_ptr ⇒ Object
- #type_convertor ⇒ Object
- #type_reader ⇒ Object
- #type_size ⇒ Object
- #type_writer ⇒ Object
Constructor Details
#initialize(size, type = :double) ⇒ HostBuffer
Returns a new instance of HostBuffer.
39 40 41 42 43 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 39 def initialize(size, type = :double) @size = size @type = type @buffer = ::FFI::MemoryPointer.new(type, size) end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
25 26 27 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 25 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 25 def type @type end |
Class Method Details
.[](other, length = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 27 def self.[](other, length = nil) if other.kind_of?(self) return other.resize(length || other.size) elsif other.respond_to?(:each_with_index) return self.new(length || other.size).set(other) elsif other.kind_of?(Numeric) return self.new(length || 1).set(other) else return self[other.to_enum, length] end end |
Instance Method Details
#[](index) ⇒ Object
86 87 88 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 86 def [](index) @buffer[index].send(type_reader) end |
#[]=(index, value) ⇒ Object
82 83 84 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 82 def []=(index, value) @buffer[index].send(type_writer, type_convertor.call(value)) end |
#byte_size ⇒ Object
51 52 53 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 51 def byte_size @size * type_size end |
#each(&block) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 90 def each(&block) return to_enum(__method__) unless block_given? size.times do |i| block.call(self[i]) end end |
#get ⇒ Object
74 75 76 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 74 def get @buffer end |
#resize(new_size) ⇒ Object
45 46 47 48 49 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 45 def resize(new_size) return self if @size == new_size self.class.new(new_size).set(self.each) end |
#set(values) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 59 def set(values) if values.respond_to?(:each_with_index) values.each_with_index do |v, i| break if i >= size self[i] = v end else size.times do |i| self[i] = values end end self end |
#to_a ⇒ Object
110 111 112 113 114 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 110 def to_a @size.times.collect do |i| self[i] end end |
#to_ptr ⇒ Object
78 79 80 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 78 def to_ptr @buffer end |
#type_convertor ⇒ Object
102 103 104 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 102 def type_convertor @type_convertor ||= TYPE_CONVERTOR[@type] end |
#type_reader ⇒ Object
106 107 108 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 106 def type_reader @type_reader ||= TYPE_GETTER[@type] end |
#type_size ⇒ Object
55 56 57 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 55 def type_size ::FFI.type_size(@type) end |
#type_writer ⇒ Object
98 99 100 |
# File 'lib/coo-coo/cuda/host_buffer.rb', line 98 def type_writer @type_writer ||= TYPE_WRITER[@type] end |