Class: CooCoo::CUDA::Vector

Inherits:
Math::AbstractVector show all
Defined in:
lib/coo-coo/cuda/vector.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Math::AbstractVector

#minmax_normalize, rand, #zero

Constructor Details

#initialize(length, initial_value = 0.0, &block) ⇒ Vector

Returns a new instance of Vector.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Class Method Details

.[](value, max_size = nil, default_value = 0.0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



103
104
105
106
# File 'lib/coo-coo/cuda/vector.rb', line 103

def self._load(args)
  arr = args.unpack('E*')
  self[arr]
end

.identity(w, h) ⇒ Object



60
61
62
# File 'lib/coo-coo/cuda/vector.rb', line 60

def self.identity(w, h)
  self[DeviceBuffer.identity(w, h)]
end

.ones(length) ⇒ Object



52
53
54
# File 'lib/coo-coo/cuda/vector.rb', line 52

def self.ones(length)
  self.new(length, 1.0)
end

.zeros(length) ⇒ Object



48
49
50
# File 'lib/coo-coo/cuda/vector.rb', line 48

def self.zeros(length)
  self.new(length, 0.0)
end

Instance Method Details

#*(other) ⇒ Vector

Calls the equivalent of #* on each element of self against other.

Parameters:

Returns:



211
# File 'lib/coo-coo/cuda/vector.rb', line 211

bin_op('*')

#**(other) ⇒ Vector

Calls the equivalent of #** on each element of self against other.

Parameters:

Returns:



213
# File 'lib/coo-coo/cuda/vector.rb', line 213

bin_op('**')

#+(other) ⇒ Vector

Calls the equivalent of #+ on each element of self against other.

Parameters:

Returns:



209
# File 'lib/coo-coo/cuda/vector.rb', line 209

bin_op('+')

#-(other) ⇒ Vector

Calls the equivalent of #- on each element of self against other.

Parameters:

Returns:



210
# File 'lib/coo-coo/cuda/vector.rb', line 210

bin_op('-')

#-@Vector

Negates every element in the vector.

Returns:



219
220
221
# File 'lib/coo-coo/cuda/vector.rb', line 219

def -@
  self * -1.0
end

#/(other) ⇒ Vector

Calls the equivalent of #/ on each element of self against other.

Parameters:

Returns:



212
# File 'lib/coo-coo/cuda/vector.rb', line 212

bin_op('/')

#<(other) ⇒ Vector

Calls the equivalent of #< on each element of self against other.

Parameters:

Returns:



205
# File 'lib/coo-coo/cuda/vector.rb', line 205

bin_op('<')

#<=(other) ⇒ Vector

Calls the equivalent of #<= on each element of self against other.

Parameters:

Returns:



206
# File 'lib/coo-coo/cuda/vector.rb', line 206

bin_op('<=')

#==(other) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# 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

#>(other) ⇒ Vector

Calls the equivalent of #> on each element of self against other.

Parameters:

Returns:



208
# File 'lib/coo-coo/cuda/vector.rb', line 208

bin_op('>')

#>=(other) ⇒ Vector

Calls the equivalent of #>= on each element of self against other.

Parameters:

Returns:



207
# File 'lib/coo-coo/cuda/vector.rb', line 207

bin_op('>=')

#[](i, len = nil) ⇒ Object



112
113
114
115
116
117
118
119
# 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) ⇒ Object



121
122
123
124
125
126
127
# 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) ⇒ Object



99
100
101
# File 'lib/coo-coo/cuda/vector.rb', line 99

def _dump(depth)
  @elements.to_a.pack('E*')
end

#absVector

Calls the equivalent of Math.abs on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.abs over self.



265
# File 'lib/coo-coo/cuda/vector.rb', line 265

f :abs

#acosVector

Calls the equivalent of Math.acos on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.acos over self.



274
# File 'lib/coo-coo/cuda/vector.rb', line 274

f :acos

#acoshVector

Calls the equivalent of Math.acosh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.acosh over self.



280
# File 'lib/coo-coo/cuda/vector.rb', line 280

f :acosh

#append(other) ⇒ Object



72
73
74
75
76
77
# 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

#asinVector

Calls the equivalent of Math.asin on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.asin over self.



272
# File 'lib/coo-coo/cuda/vector.rb', line 272

f :asin

#asinhVector

Calls the equivalent of Math.asinh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.asinh over self.



278
# File 'lib/coo-coo/cuda/vector.rb', line 278

f :asinh

#atanVector

Calls the equivalent of Math.atan on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.atan over self.



276
# File 'lib/coo-coo/cuda/vector.rb', line 276

f :atan

#atanhVector

Calls the equivalent of Math.atanh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.atanh over self.



282
# File 'lib/coo-coo/cuda/vector.rb', line 282

f :atanh

#averageObject



160
161
162
# File 'lib/coo-coo/cuda/vector.rb', line 160

def average
  @elements.sum / size
end

#ceilVector

Calls the equivalent of Math.ceil on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.ceil over self.



283
# File 'lib/coo-coo/cuda/vector.rb', line 283

f :ceil

#cloneObject



68
69
70
# File 'lib/coo-coo/cuda/vector.rb', line 68

def clone
  self.class.new(self.size).set(@elements)
end

#coerce(other) ⇒ Object



79
80
81
82
83
84
85
# 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) ⇒ Vector

Calls the equivalent of #collect_equal? on each element of self against other.

Parameters:

Returns:



214
# File 'lib/coo-coo/cuda/vector.rb', line 214

bin_op('collect_equal?')

#collect_infinite?Vector

Calls the equivalent of Math.collect_infinite? on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.collect_infinite? over self.



287
# File 'lib/coo-coo/cuda/vector.rb', line 287

f :collect_infinite?, :collect_inf

#collect_nan?Vector

Calls the equivalent of Math.collect_nan? on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.collect_nan? over self.



286
# File 'lib/coo-coo/cuda/vector.rb', line 286

f :collect_nan?, :collect_nan

#collect_not_equal?(other) ⇒ Vector

Calls the equivalent of #collect_not_equal? on each element of self against other.

Parameters:

Returns:



215
# File 'lib/coo-coo/cuda/vector.rb', line 215

bin_op('collect_not_equal?')

#cosVector

Calls the equivalent of Math.cos on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.cos over self.



273
# File 'lib/coo-coo/cuda/vector.rb', line 273

f :cos

#coshVector

Calls the equivalent of Math.cosh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.cosh over self.



279
# File 'lib/coo-coo/cuda/vector.rb', line 279

f :cosh

#diagflatObject



64
65
66
# File 'lib/coo-coo/cuda/vector.rb', line 64

def diagflat
  self.class[@elements.diagflat]
end

#dot(w, h, other, ow = nil, oh = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
# 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) ⇒ Object



129
130
131
# File 'lib/coo-coo/cuda/vector.rb', line 129

def each(&block)
  @elements.each(&block)
end

#each_slice(n, &block) ⇒ Object



137
138
139
140
141
142
143
# 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) ⇒ Object



133
134
135
# File 'lib/coo-coo/cuda/vector.rb', line 133

def each_with_index(&block)
  @elements.each.with_index(&block)
end

#expVector

Calls the equivalent of Math.exp on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.exp over self.



266
# File 'lib/coo-coo/cuda/vector.rb', line 266

f :exp

#floorVector

Calls the equivalent of Math.floor on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.floor over self.



284
# File 'lib/coo-coo/cuda/vector.rb', line 284

f :floor

#infinite?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/coo-coo/cuda/vector.rb', line 293

def infinite?
  collect_infinite?.sum > 0
end

#inspectObject



91
92
93
# File 'lib/coo-coo/cuda/vector.rb', line 91

def inspect
  to_a.inspect
end

#lengthObject



235
236
237
# File 'lib/coo-coo/cuda/vector.rb', line 235

def length
  @elements.size
end

#logVector

Calls the equivalent of Math.log on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.log over self.



267
# File 'lib/coo-coo/cuda/vector.rb', line 267

f :log

#log10Vector

Calls the equivalent of Math.log10 on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.log10 over self.



268
# File 'lib/coo-coo/cuda/vector.rb', line 268

f :log10

#log2Vector

Calls the equivalent of Math.log2 on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.log2 over self.



269
# File 'lib/coo-coo/cuda/vector.rb', line 269

f :log2

#magnitudeObject



168
169
170
# File 'lib/coo-coo/cuda/vector.rb', line 168

def magnitude
  ::Math.sqrt(magnitude_squared)
end

#magnitude_squaredObject



164
165
166
# File 'lib/coo-coo/cuda/vector.rb', line 164

def magnitude_squared
  (self * self).sum
end

#maxFloat

Returns maximum value of self.

Returns:

  • (Float)

    maximum value of self



154
# File 'lib/coo-coo/cuda/vector.rb', line 154

delegate :min, :max, :minmax, :sum, :to => :elements

#minFloat

Returns minimum value of self.

Returns:

  • (Float)

    minimum value of self



154
# File 'lib/coo-coo/cuda/vector.rb', line 154

delegate :min, :max, :minmax, :sum, :to => :elements

#minmax[Float, Float]

Returns #min and #max values of self.

Returns:

  • ([Float, Float])

    #min and #max values of self



154
# File 'lib/coo-coo/cuda/vector.rb', line 154

delegate :min, :max, :minmax, :sum, :to => :elements

#nan?Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/coo-coo/cuda/vector.rb', line 289

def nan?
  collect_nan?.sum > 0
end

#normalizeObject



172
173
174
# File 'lib/coo-coo/cuda/vector.rb', line 172

def normalize
  self / magnitude
end

#null?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/coo-coo/cuda/vector.rb', line 108

def null?
  @elements.null?
end

#roundVector

Calls the equivalent of Math.round on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.round over self.



285
# File 'lib/coo-coo/cuda/vector.rb', line 285

f :round

#set(values) ⇒ Object



38
39
40
41
42
43
44
45
46
# 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) ⇒ Object

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
# 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

#sinVector

Calls the equivalent of Math.sin on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.sin over self.



271
# File 'lib/coo-coo/cuda/vector.rb', line 271

f :sin

#sinhVector

Calls the equivalent of Math.sinh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.sinh over self.



277
# File 'lib/coo-coo/cuda/vector.rb', line 277

f :sinh

#sizeObject



231
232
233
# File 'lib/coo-coo/cuda/vector.rb', line 231

def size
  @elements.size
end

#slice_2d(*args) ⇒ Object



297
298
299
# File 'lib/coo-coo/cuda/vector.rb', line 297

def slice_2d(*args)
  self.class[@elements.slice_2d(*args)]
end

#sqrtVector

Calls the equivalent of Math.sqrt on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.sqrt over self.



270
# File 'lib/coo-coo/cuda/vector.rb', line 270

f :sqrt

#sumFloat

Reduces the vector with #+.

Returns:

  • (Float)

    the sum of self



154
# File 'lib/coo-coo/cuda/vector.rb', line 154

delegate :min, :max, :minmax, :sum, :to => :elements

#tanVector

Calls the equivalent of Math.tan on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.tan over self.



275
# File 'lib/coo-coo/cuda/vector.rb', line 275

f :tan

#tanhVector

Calls the equivalent of Math.tanh on each element of self.

Returns:

  • (Vector)

    the equivalent of Math.tanh over self.



281
# File 'lib/coo-coo/cuda/vector.rb', line 281

f :tanh

#to_aObject



95
96
97
# File 'lib/coo-coo/cuda/vector.rb', line 95

def to_a
  @elements.to_a
end

#to_sObject



87
88
89
# File 'lib/coo-coo/cuda/vector.rb', line 87

def to_s
  '[' + each.collect(&:to_f).join(', ') + ']'
end

#zerosObject



56
57
58
# File 'lib/coo-coo/cuda/vector.rb', line 56

def zeros
  self.zeros(size)
end