Class: BOAST::Variable

Inherits:
Object
  • Object
show all
Extended by:
Functor
Includes:
Arithmetic, Inspectable, PrivateStateAccessor
Defined in:
lib/BOAST/Variable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Functor

extended

Methods included from Inspectable

#inspect

Methods included from Arithmetic

#!, #!=, #*, #+, #-, #-@, #/, #<, #<=, #==, #===, #>, #>=, #address, #and, #components, #or

Methods included from PrivateStateAccessor

private_boolean_state_accessor, private_state_accessor

Constructor Details

#initialize(name, type, hash = {}) ⇒ Variable

Returns a new instance of Variable.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/BOAST/Variable.rb', line 145

def initialize(name,type,hash={})
  @name = name.to_s
  @direction = hash[:direction] ? hash[:direction] : hash[:dir]
  @constant = hash[:constant] ? hash[:constant]  : hash[:const]
  @dimension = hash[:dimension] ? hash[:dimension] : hash[:dim]
  @local = hash[:local] ? hash[:local] : hash[:shared]
  @texture = hash[:texture]
  @allocate = hash[:allocate]
  @restrict = hash[:restrict]
  @force_replace_constant = false
  if not hash[:replace_constant].nil? then
    @replace_constant = hash[:replace_constant]
  else
    @replace_constant = true
  end
  if @texture and lang == CL then
    @sampler = Variable::new("sampler_#{name}", CustomType,:type_name => "sampler_t" ,:replace_constant => false, :constant => "CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST")
  else
    @sampler = nil
  end
  @type = type::new(hash)
  @options = hash
  if (@direction == :out or @direction == :inout) and not dimension? then
    @scalar_output = true
  else
    @scalar_output = false
  end
  @dimension = [@dimension].flatten if dimension?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/BOAST/Variable.rb', line 84

def method_missing(m, *a, &b)
  if @type.methods.include?(:members) and @type.members[m.to_s] then
    return struct_reference(type.members[m.to_s])
  elsif @type.methods.include?(:vector_length) and @type.vector_length > 1 and m.to_s[0] == 's' and lang == CL then
    required_set = m.to_s[1..-1].chars.to_a
    existing_set = [*('0'..'9'),*('a'..'z')].first(@type.vector_length)
    if required_set.length == required_set.uniq.length and (required_set - existing_set).empty? then
      return self.copy(name+"."+m.to_s, :vector_length => m.to_s[1..-1].length)
    else
      return orig_method_missing(m, *a, &b)
    end
  else
    return orig_method_missing(m, *a, &b)
  end
end

Instance Attribute Details

#allocateObject (readonly)

Returns the value of attribute allocate.



103
104
105
# File 'lib/BOAST/Variable.rb', line 103

def allocate
  @allocate
end

#constantObject

Returns the value of attribute constant.



102
103
104
# File 'lib/BOAST/Variable.rb', line 102

def constant
  @constant
end

#dimensionObject (readonly)

Returns the value of attribute dimension.



105
106
107
# File 'lib/BOAST/Variable.rb', line 105

def dimension
  @dimension
end

#directionObject

Returns the value of attribute direction.



101
102
103
# File 'lib/BOAST/Variable.rb', line 101

def direction
  @direction
end

#force_replace_constantObject

Returns the value of attribute force_replace_constant.



111
112
113
# File 'lib/BOAST/Variable.rb', line 111

def force_replace_constant
  @force_replace_constant
end

#localObject (readonly)

Returns the value of attribute local.



106
107
108
# File 'lib/BOAST/Variable.rb', line 106

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



100
101
102
# File 'lib/BOAST/Variable.rb', line 100

def name
  @name
end

#replace_constantObject

Returns the value of attribute replace_constant.



110
111
112
# File 'lib/BOAST/Variable.rb', line 110

def replace_constant
  @replace_constant
end

#restrictObject (readonly)

Returns the value of attribute restrict.



109
110
111
# File 'lib/BOAST/Variable.rb', line 109

def restrict
  @restrict
end

#samplerObject (readonly)

Returns the value of attribute sampler.



108
109
110
# File 'lib/BOAST/Variable.rb', line 108

def sampler
  @sampler
end

#textureObject (readonly)

Returns the value of attribute texture.



107
108
109
# File 'lib/BOAST/Variable.rb', line 107

def texture
  @texture
end

#typeObject (readonly)

Returns the value of attribute type.



104
105
106
# File 'lib/BOAST/Variable.rb', line 104

def type
  @type
end

Class Method Details

.from_type(name, type, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/BOAST/Variable.rb', line 184

def self.from_type(name, type, options={})
  hash = type.to_hash
  options.each { |k,v|
    hash[k] = v
  }
  hash[:direction] = nil
  hash[:dir] = nil
  return Variable::new(name, type.class, hash)
end

Instance Method Details

#[](*args) ⇒ Object



228
229
230
# File 'lib/BOAST/Variable.rb', line 228

def [](*args)
  return Index::new(self,args)
end

#allocate?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/BOAST/Variable.rb', line 117

def allocate?
  !!@allocate
end

#boast_header(lang = C) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/BOAST/Variable.rb', line 239

def boast_header(lang=C)
  return decl_texture_s if texture?
  s = ""
  s += "const " if constant? or @direction == :in
  s += @type.decl
  if dimension? then
    s += " *"
  end
  if not dimension? and ( lang == FORTRAN or @direction == :out or @direction == :inout ) then
    s += " *"
  end
  s += " #{@name}"
  return s
end

#constant?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/BOAST/Variable.rb', line 113

def constant?
  !!@constant
end

#copy(name = nil, options = {}) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/BOAST/Variable.rb', line 175

def copy(name=nil,options={})
  name = @name if not name
  h = @options.clone
  options.each { |k,v|
    h[k] = v
  }
  return Variable::new(name, @type.class, h)
end

#declObject



254
255
256
257
# File 'lib/BOAST/Variable.rb', line 254

def decl
  return decl_fortran if lang == FORTRAN
  return decl_c if [C, CL, CUDA].include?( lang )
end

#decl_cObject



316
317
318
319
320
321
322
323
# File 'lib/BOAST/Variable.rb', line 316

def decl_c
  s = ""
  s += indent
  s += decl_c_s
  s += finalize
  output.print s
  return self
end

#decl_c_s(device = false) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/BOAST/Variable.rb', line 259

def decl_c_s(device = false)
  return decl_texture_s if texture?
  s = ""
  s += "const " if constant? or @direction == :in
  s += "__global " if @direction and dimension? and not (@options[:register] or @options[:private] or local?) and lang == CL
  s += "__local " if local? and lang == CL
  s += "__shared__ " if local? and not device and lang == CUDA
  s += @type.decl
  if dimension? and not constant? and not allocate? and (not local? or (local? and device)) then
    s += " *"
    if restrict? then
      if lang == CL
        s += " restrict"
      else
        s += " __restrict__"
      end
    end
  end
  if not dimension? and ( @direction == :out or @direction == :inout ) then
    s += " *"
  end
  s += " #{@name}"
  if dimension? and constant? then
    s += "[]"
  end
  if dimension? and ((local? and not device) or (allocate? and not constant?)) then
     s +="[("
     s += @dimension.collect{ |d| d.to_s }.reverse.join(")*(")
     s +=")]"
  end 
  s += " = #{@constant}" if constant?
  return s
end

#decl_fortranObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/BOAST/Variable.rb', line 326

def decl_fortran
  s = ""
  s += indent
  s += @type.decl
  s += ", intent(#{@direction})" if @direction 
  s += ", parameter" if constant?
  if dimension? then
    s += ", dimension("
    s += @dimension.collect { |d|
      dim = d.to_s
      if dim then
        dim.to_s
      else
        "*"
      end
    }.join(", ")
    s += ")"
  end
  s += " :: #{@name}"
  if constant? then
    s += " = #{@constant}"
    s += @type.suffix if not dimension? and @type
  end
  s += finalize
  output.print s
  return self
end

#decl_texture_sObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/BOAST/Variable.rb', line 293

def decl_texture_s
  raise "Unsupported language #{lang} for texture!" if not [CL, CUDA].include?( lang )
  raise "Write is unsupported for textures!" if not (constant? or @direction == :in)
  dim_number = 1
  if dimension? then
    dim_number == @dimension.size
  end
  raise "Unsupported number of dimension: #{dim_number}!" if dim_number > 3
  s = ""
  if lang == CL then
    s += "__read_only "
    if dim_number < 3 then
      s += "image2d_t " #from OCL 1.2+ image1d_t is defined
    else
      s += "image3d_t "
    end
  else
    s += "texture<#{@type.decl}, cudaTextureType#{dim_number}D, cudaReadModeElementType> "
  end
  s += @name
  return s
end

#dereferenceObject



213
214
215
216
217
# File 'lib/BOAST/Variable.rb', line 213

def dereference
  return copy("*(#{name})", :dimension => nil, :dim => nil, :direction => nil, :dir => nil) if [C, CL, CUDA].include?( lang )
  return self if lang == FORTRAN
  #return Expression::new("*",nil,self)
end

#dimension?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/BOAST/Variable.rb', line 141

def dimension?
  !!@dimension
end

#finalizeObject



232
233
234
235
236
237
# File 'lib/BOAST/Variable.rb', line 232

def finalize
   s = ""
   s += ";" if [C, CL, CUDA].include?( lang )
   s+="\n"
   return s
end

#force_replace_constant?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/BOAST/Variable.rb', line 137

def force_replace_constant?
  !!@force_replace_constant
end

#incObject



224
225
226
# File 'lib/BOAST/Variable.rb', line 224

def inc
  return Expression::new("++",self,nil)
end

#local?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/BOAST/Variable.rb', line 125

def local?
  !!@local
end

#orig_method_missingObject



82
# File 'lib/BOAST/Variable.rb', line 82

alias_method :orig_method_missing, :method_missing

#replace_constant?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/BOAST/Variable.rb', line 133

def replace_constant?
  !!@replace_constant
end

#restrict?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/BOAST/Variable.rb', line 129

def restrict?
  !!@restrict
end

#set(x) ⇒ Object



209
210
211
# File 'lib/BOAST/Variable.rb', line 209

def set(x)
  return Expression::new(Set, self, x)
end

#struct_reference(x) ⇒ Object



219
220
221
222
# File 'lib/BOAST/Variable.rb', line 219

def struct_reference(x)
  return x.copy(name+"."+x.name) if [C, CL, CUDA].include?( lang )
  return x.copy(name+"%"+x.name) if lang == FORTRAN
end

#texture?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/BOAST/Variable.rb', line 121

def texture?
  !!@texture
end

#to_sObject



194
195
196
197
198
199
200
201
202
203
# File 'lib/BOAST/Variable.rb', line 194

def to_s
  if force_replace_constant? or ( replace_constant? and constant? and replace_constants? and not dimension? ) then
    s = @constant.to_s + @type.suffix
    return s
  end
  if @scalar_output and [C, CL, CUDA].include?( lang ) then
    return "(*#{name})"
  end
  return @name
end

#to_varObject



205
206
207
# File 'lib/BOAST/Variable.rb', line 205

def to_var
  return self
end