Class: BOAST::Variable

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Variable.



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/BOAST/Algorithm.rb', line 577

def initialize(name,type,hash={})
  @name = name
  @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]
  @force_replace_constant = false
  if not hash[:replace_constant].nil? then
    @replace_constant = hash[:replace_constant]
  else
    @replace_constant = true
  end
  if @texture and BOAST::get_lang == CL then
    @sampler = Variable::new("sampler_#{name}", BOAST::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)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



556
557
558
559
# File 'lib/BOAST/Algorithm.rb', line 556

def method_missing(m, *a, &b)
  return self.struct_reference(type.members[m.to_s]) if type.members[m.to_s]
  return self.orig_method_missing(m, *a, &b)
end

Instance Attribute Details

#allocateObject (readonly)

Returns the value of attribute allocate.



568
569
570
# File 'lib/BOAST/Algorithm.rb', line 568

def allocate
  @allocate
end

#constantObject

Returns the value of attribute constant.



567
568
569
# File 'lib/BOAST/Algorithm.rb', line 567

def constant
  @constant
end

#dimensionObject (readonly)

Returns the value of attribute dimension.



570
571
572
# File 'lib/BOAST/Algorithm.rb', line 570

def dimension
  @dimension
end

#directionObject

Returns the value of attribute direction.



566
567
568
# File 'lib/BOAST/Algorithm.rb', line 566

def direction
  @direction
end

#force_replace_constantObject

Returns the value of attribute force_replace_constant.



575
576
577
# File 'lib/BOAST/Algorithm.rb', line 575

def force_replace_constant
  @force_replace_constant
end

#localObject (readonly)

Returns the value of attribute local.



571
572
573
# File 'lib/BOAST/Algorithm.rb', line 571

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



565
566
567
# File 'lib/BOAST/Algorithm.rb', line 565

def name
  @name
end

#replace_constantObject

Returns the value of attribute replace_constant.



574
575
576
# File 'lib/BOAST/Algorithm.rb', line 574

def replace_constant
  @replace_constant
end

#samplerObject (readonly)

Returns the value of attribute sampler.



573
574
575
# File 'lib/BOAST/Algorithm.rb', line 573

def sampler
  @sampler
end

#textureObject (readonly)

Returns the value of attribute texture.



572
573
574
# File 'lib/BOAST/Algorithm.rb', line 572

def texture
  @texture
end

#typeObject (readonly)

Returns the value of attribute type.



569
570
571
# File 'lib/BOAST/Algorithm.rb', line 569

def type
  @type
end

Class Method Details

.parens(*args, &block) ⇒ Object



561
562
563
# File 'lib/BOAST/Algorithm.rb', line 561

def self.parens(*args,&block)
  return self::new(*args,&block)
end

Instance Method Details

#!Object



661
662
663
# File 'lib/BOAST/Algorithm.rb', line 661

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

#*(x) ⇒ Object



649
650
651
# File 'lib/BOAST/Algorithm.rb', line 649

def *(x)
  return Expression::new("*",self,x)
end

#+(x) ⇒ Object



645
646
647
# File 'lib/BOAST/Algorithm.rb', line 645

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

#-(x) ⇒ Object



657
658
659
# File 'lib/BOAST/Algorithm.rb', line 657

def -(x)
  return Expression::new("-",self,x)
end

#-@Object



665
666
667
# File 'lib/BOAST/Algorithm.rb', line 665

def -@
  return Expression::new("-",nil,self)
end

#/(x) ⇒ Object



653
654
655
# File 'lib/BOAST/Algorithm.rb', line 653

def /(x)
  return Expression::new("/",self,x)
end

#<(x) ⇒ Object



637
638
639
# File 'lib/BOAST/Algorithm.rb', line 637

def <(x)
  return Expression::new("<",self,x)
end

#==(x) ⇒ Object



629
630
631
# File 'lib/BOAST/Algorithm.rb', line 629

def ==(x)
  return Expression::new("==",self,x)
end

#===(x) ⇒ Object



625
626
627
# File 'lib/BOAST/Algorithm.rb', line 625

def ===(x)
  return Expression::new("=",self,x)
end

#>(x) ⇒ Object



633
634
635
# File 'lib/BOAST/Algorithm.rb', line 633

def >(x)
  return Expression::new(">",self,x)
end

#>=(x) ⇒ Object



641
642
643
# File 'lib/BOAST/Algorithm.rb', line 641

def >=(x)
  return Expression::new(">=",self,x)
end

#[](*args) ⇒ Object



686
687
688
# File 'lib/BOAST/Algorithm.rb', line 686

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

#addressObject



669
670
671
# File 'lib/BOAST/Algorithm.rb', line 669

def address
  return Expression::new("&",nil,self)
end

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



600
601
602
603
604
605
606
# File 'lib/BOAST/Algorithm.rb', line 600

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

#decl(final = true, device = false) ⇒ Object



758
759
760
761
# File 'lib/BOAST/Algorithm.rb', line 758

def decl(final=true,device=false)
  return self.decl_fortran(final) if BOAST::get_lang == FORTRAN
  return self.decl_c(final, device) if [C, CL, CUDA].include?( BOAST::get_lang )
end

#decl_c(final = true, device = false) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/BOAST/Algorithm.rb', line 701

def decl_c(final=true, device=false)
  return decl_texture(final) if @texture
  s = ""
  s += self.indent if final
  s += "const " if @constant or @direction == :in
  s += "__global " if @direction and @dimension and not (@hash[:register] or @hash[:private] or @local) and BOAST::get_lang == CL
  s += "__local " if @local and BOAST::get_lang == CL
  s += "__shared__ " if @local and not device and BOAST::get_lang == CUDA
  s += @type.decl
  if(@dimension and not @constant and not @allocate and (not @local or (@local and device))) 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.reverse.join("*")
     s +="]"
  end 
  s += " = #{@constant}" if @constant
  s += self.finalize if final
  BOAST::get_output.print s if final
  return s
end

#decl_fortran(final = true) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/BOAST/Algorithm.rb', line 790

def decl_fortran(final=true)
  s = ""
  s += self.indent if final
  s += @type.decl
  s += ", intent(#{@direction})" if @direction 
  s += ", parameter" if @constant
  if(@dimension) then
    s += ", dimension("
    dim = @dimension[0].to_str
    if dim then
      s += dim
      @dimension[1..-1].each { |d|
         s += ", "
         s += d
      }
    else
      s += ":"
    end
    s += ")"
  end
  s += " :: #{@name}"
  if @constant
    s += " = #{@constant}"
    s += "_wp" if not @dimension and @type and @type.size == 8
  end
  s += self.finalize if final
  BOAST::get_output.print s if final
  return s
end

#decl_texture(final = true) ⇒ Object



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/BOAST/Algorithm.rb', line 763

def decl_texture(final=true)
  raise "Unsupported language #{BOAST::get_lang} for texture!" if not [CL, CUDA].include?( BOAST::get_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 = ""
  s += self.indent if final
  if BOAST::get_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
  s += self.finalize if final
  BOAST::get_output.print s if final
  return s
end

#dereferenceObject



673
674
675
# File 'lib/BOAST/Algorithm.rb', line 673

def dereference
  return Expression::new("*",nil,self)
end

#finalizeObject



694
695
696
697
698
699
# File 'lib/BOAST/Algorithm.rb', line 694

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

#header(lang = C, final = true) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/BOAST/Algorithm.rb', line 728

def header(lang=C,final=true)
  return decl_texture(final) if @texture
  s = ""
  s += self.indent if final
  s += "const " if @constant or @direction == :in
  s += "__global " if @direction and @dimension and BOAST::get_lang == CL
  s += "__local " if @local and BOAST::get_lang == CL
  s += "__shared__ " if @local and BOAST::get_lang == CUDA
  s += @type.decl
  if(@dimension and not @constant and not @local) then
    s += " *"
  end
  if not @dimension and lang == FORTRAN then
    s += " *"
  end
  s += " #{@name}"
  if(@dimension and @constant) then
    s += "[]"
  end
  if(@dimension and @local) then
     s +="["
     s += @dimension.reverse.join("*")
     s +="]"
  end 
  s += " = #{@constant}" if @constant
  s += self.finalize if final
  BOAST::get_output.print s if final
  return s
end

#incObject



682
683
684
# File 'lib/BOAST/Algorithm.rb', line 682

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

#indentObject



690
691
692
# File 'lib/BOAST/Algorithm.rb', line 690

def indent
   return " "*BOAST::get_indent_level
end

#orig_method_missingObject



554
# File 'lib/BOAST/Algorithm.rb', line 554

alias_method :orig_method_missing, :method_missing

#struct_reference(x) ⇒ Object



677
678
679
680
# File 'lib/BOAST/Algorithm.rb', line 677

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

#to_sObject



608
609
610
# File 'lib/BOAST/Algorithm.rb', line 608

def to_s
  self.to_str
end

#to_strObject



612
613
614
615
616
617
618
619
# File 'lib/BOAST/Algorithm.rb', line 612

def to_str
  if @force_replace_constant or ( @replace_constant and @constant and BOAST::get_replace_constants and not @dimension ) then
    s = @constant.to_s 
    s += "_wp" if BOAST::get_lang == FORTRAN and @type and @type.size == 8
    return s
  end
  return @name.to_str
end

#to_varObject



621
622
623
# File 'lib/BOAST/Algorithm.rb', line 621

def to_var
  return self
end