Class: CooCoo::CUDA::HostBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/cuda/host_buffer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#sizeObject (readonly)

Returns the value of attribute size.



25
26
27
# File 'lib/coo-coo/cuda/host_buffer.rb', line 25

def size
  @size
end

#typeObject (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_sizeObject



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

#getObject



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_aObject



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_ptrObject



78
79
80
# File 'lib/coo-coo/cuda/host_buffer.rb', line 78

def to_ptr
  @buffer
end

#type_convertorObject



102
103
104
# File 'lib/coo-coo/cuda/host_buffer.rb', line 102

def type_convertor
  @type_convertor ||= TYPE_CONVERTOR[@type]
end

#type_readerObject



106
107
108
# File 'lib/coo-coo/cuda/host_buffer.rb', line 106

def type_reader
  @type_reader ||= TYPE_GETTER[@type]
end

#type_sizeObject



55
56
57
# File 'lib/coo-coo/cuda/host_buffer.rb', line 55

def type_size
  ::FFI.type_size(@type)
end

#type_writerObject



98
99
100
# File 'lib/coo-coo/cuda/host_buffer.rb', line 98

def type_writer
  @type_writer ||= TYPE_WRITER[@type]
end