class CooCoo::CUDA::Vector

Public Class Methods

[](value, max_size = nil, default_value = 0.0) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 23
def self.[](value, max_size = nil, default_value = 0.0)
  if value.kind_of?(DeviceBuffer)
    v = new(nil)
    v.instance_variable_set('@elements', value)
    v
  else
    if value.respond_to?(:each)
      max_size ||= value.size
    else
      max_size ||= 1
    end
    new(max_size, default_value).set(value)
  end
end
_load(args) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 103
def self._load(args)
  arr = args.unpack('E*')
  self[arr]
end
identity(w, h) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 60
def self.identity(w, h)
  self[DeviceBuffer.identity(w, h)]
end
new(length, initial_value = 0.0, &block) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 8
def initialize(length, initial_value = 0.0, &block)
  if length != nil && length <= 0
    raise ArgumentError.new("Invalid Vector size")
  elsif length != nil
    @elements = DeviceBuffer.create(length, initial_value)
    if block
      @elements.size.times.each_slice(1024).with_index do |slice, slice_idx|
        @elements[slice_idx * 1024, 1024] = slice.collect do |i|
          block.call(i)
        end
      end
    end
  end
end
ones(length) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 52
def self.ones(length)
  self.new(length, 1.0)
end
zeros(length) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 48
def self.zeros(length)
  self.new(length, 0.0)
end

Private Class Methods

bin_op(op) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 187
      def self.bin_op(op)
        class_eval <<-EOT
        def #{op}(other)
          if other.kind_of?(self.class)
            self.class[@elements.send(:#{op}, other.elements)]
          else
            self.class[@elements.send(:#{op}, other)]
          end
        end
EOT
      end
f(name, real_name = nil) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 251
      def self.f(name, real_name = nil)
        class_eval <<-EOT
        def #{name}
          self.class[@elements.send(:#{real_name || name})]
        end
EOT
      end

Public Instance Methods

-@() click to toggle source

Negates every element in the vector. @return [Vector]

# File lib/coo-coo/cuda/vector.rb, line 219
def -@
  self * -1.0
end
==(other) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 239
def ==(other)
  if other.kind_of?(self.class)
    @elements == other.elements
  elsif other != nil
    b, a = coerce(other)
    self == b
  else
    false
  end
end
[](i, len = nil) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 112
def [](i, len = nil)
  v = @elements[i, len]
  if len
    self.class[v]
  else
    v
  end
end
[]=(i, v, l = nil) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 121
def []=(i, v, l = nil)
  if l == nil
    @elements[i] = v
  else
    @elements[i, v] = l
  end
end
_dump(depth) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 99
def _dump(depth)
  @elements.to_a.pack('E*')
end
append(other) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 72
def append(other)
  b = self.class.new(size + other.size)
  b[0, size] = self
  b[size, other.size] = other
  b
end
average() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 160
def average
  @elements.sum / size
end
clone() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 68
def clone
  self.class.new(self.size).set(@elements)
end
coerce(other) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 79
def coerce(other)
  if other.respond_to?(:each)
    return self.class[other], self
  else
    return self.class.new(self.size, other), self
  end
end
collect_equal?(n) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 223
def collect_equal?(n)
  if n.kind_of?(self.class)
    self.class[@elements.collect_equal?(n.elements)]
  else
    self.class[@elements.collect_equal?(n)]
  end
end
diagflat() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 64
def diagflat
  self.class[@elements.diagflat]
end
dot(w, h, other, ow = nil, oh = nil) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 176
def dot(w, h, other, ow = nil, oh = nil)
  if other.kind_of?(self.class)
    self.class[@elements.dot(w, h, other.elements, ow, oh)]
  elsif other.respond_to?(:each)
    dot(w, h, self.class[other.each], ow, oh)
  else
    raise ArgumentError.new("argument is not a #{self.class} or Enumerator")
  end
end
each(&block) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 129
def each(&block)
  @elements.each(&block)
end
each_slice(n, &block) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 137
def each_slice(n, &block)
  return to_enum(__method__, n) unless block
  
  @elements.each_slice(n) do |slice|
    block.call(self.class[slice])
  end
end
each_with_index(&block) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 133
def each_with_index(&block)
  @elements.each.with_index(&block)
end
infinite?() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 293
def infinite?
  collect_infinite?.sum > 0
end
inspect() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 91
def inspect
  to_a.inspect
end
length() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 235
def length
  @elements.size
end
magnitude() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 168
def magnitude
  ::Math.sqrt(magnitude_squared)
end
magnitude_squared() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 164
def magnitude_squared
  (self * self).sum
end
nan?() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 289
def nan?
  collect_nan?.sum > 0
end
normalize() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 172
def normalize
  self / magnitude
end
null?() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 108
def null?
  @elements.null?
end
set(values) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 38
def set(values)
  if values.kind_of?(self.class)
    @elements.set(values.elements)
  else
    @elements.set(values)
  end
  
  self
end
set2d!(width, src, src_width, x, y) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 301
def set2d!(width, src, src_width, x, y)
  raise ArgumentError.new("src's size #{src.size} must be divisible by src_width #{src_width}") if src.respond_to?(:each) && src.size % src_width > 0
  
  src = src.elements if src.kind_of?(self.class)
  @elements.set2d!(width, src, src_width, x, y)
  self
end
size() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 231
def size
  @elements.size
end
slice_2d(*args) click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 297
def slice_2d(*args)
  self.class[@elements.slice_2d(*args)]
end
sum() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 156
def sum
  @elements.sum
end
to_a() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 95
def to_a
  @elements.to_a
end
to_s() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 87
def to_s
  '[' + each.collect(&:to_f).join(', ') + ']'
end
zeros() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 56
def zeros
  self.zeros(size)
end

Protected Instance Methods

elements() click to toggle source
# File lib/coo-coo/cuda/vector.rb, line 310
def elements
  @elements
end