Class: BOAST::Index

Inherits:
Expression show all
Defined in:
lib/BOAST/Algorithm.rb

Instance Attribute Summary collapse

Attributes inherited from Expression

#operand1, #operand2, #operator

Instance Method Summary collapse

Methods inherited from Expression

#!, #*, #+, #-, #-@, #/, #<, #==, #===, #>, #>=, #dereference, parens, #struct_reference

Constructor Details

#initialize(source, indexes) ⇒ Index

Returns a new instance of Index.



353
354
355
356
# File 'lib/BOAST/Algorithm.rb', line 353

def initialize(source, indexes)
  @source = source
  @indexes = indexes
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



352
353
354
# File 'lib/BOAST/Algorithm.rb', line 352

def indexes
  @indexes
end

#sourceObject (readonly)

Returns the value of attribute source.



351
352
353
# File 'lib/BOAST/Algorithm.rb', line 351

def source
  @source
end

Instance Method Details



460
461
462
463
464
465
466
467
# File 'lib/BOAST/Algorithm.rb', line 460

def print(final=true)
  s=""
  s += " "*BOAST::get_indent_level if final
  s += self.to_str
  s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
  BOAST::get_output.puts s if final
  return s
end

#to_sObject



362
363
364
# File 'lib/BOAST/Algorithm.rb', line 362

def to_s
  self.to_str
end

#to_strObject



365
366
367
368
# File 'lib/BOAST/Algorithm.rb', line 365

def to_str
  return self.to_str_fortran if BOAST::get_lang == FORTRAN
  return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
end

#to_str_cObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/BOAST/Algorithm.rb', line 419

def to_str_c
  return to_str_texture if @source.texture
  dim = @source.dimension.first
  if dim.val2 then
    start = dim.val1
  else
    start = BOAST::get_array_start
  end
  sub = "#{@indexes.first} - #{start}"
  i=1
  ss = ""
  @source.dimension[0..-2].each{ |d|
    if d.size then
      ss += " * (#{d.size})"
    elsif d.val2 then
      ss += " * (#{d.val2} - (#{d.val1}) + 1)"
    else
      raise "Unkwown dimension size!"
    end
    dim = @source.dimension[i]
    if dim.val2 then
      start = dim.val1
    else
      start = BOAST::get_array_start
    end
    sub += " + (#{@indexes[i]} - (#{start}))"+ss
    i+=1
  }
  if BOAST::get_replace_constants then
    begin
#         puts sub
     indx = eval(sub)
     indx = indx.to_i
#         puts indx
     return "#{@source.constant[indx]}"
    rescue Exception => e
    end
  end
  s = "#{@source}[" + sub + "]"
  return s
end

#to_str_fortranObject



369
370
371
372
373
# File 'lib/BOAST/Algorithm.rb', line 369

def to_str_fortran
  s = ""
  s += "#{@source}(#{@indexes.join(", ")})"
  return s
end

#to_str_textureObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/BOAST/Algorithm.rb', line 374

def to_str_texture
  raise "Unsupported language #{BOAST::get_lang} for texture!" if not [CL, CUDA].include?( BOAST::get_lang )
  raise "Write is unsupported for textures!" if not ( @source.constant or @source.direction == :in )
  dim_number = 1
  if @source.dimension then
    dim_number == @source.dimension.size
  end
  raise "Unsupported number of dimension: #{dim_number}!" if dim_number > 3
  s = ""
  if BOAST::get_lang == CL then
    s += "as_#{@source.type.decl}("
    s += "read_imageui(#{@source}, #{@source.sampler}, "
    if dim_number == 1 then
      s += "int2(#{@indexes[0]},0)"
    else
      if dim_number == 2 then
        s += "int2("
      else
        s += "int3("
      end
      s += "#{@indexes.join(", ")})"
    end
    s += ")"
    if @source.type.size == 4 then
      s += ".x"
    elsif @source.type.size == 8 then
      s += ".xy"
    end
    s += ")"
  else
    s += "tex#{dim_number}Dfetch(#{@source},"
    if dim_number == 1 then
      s += "#{@indexes[0]}"
    else
      if dim_number == 2 then
        s += "int2("
      else
        s += "int3("
      end
      s += "#{@indexes.join(", ")})"
    end
    s += ")"
  end
  return s
end

#to_varObject



358
359
360
# File 'lib/BOAST/Algorithm.rb', line 358

def to_var
  return @source.copy("#{self}", :const => nil, :constant => nil, :dim => nil, :dimension => nil)
end