Class: BOAST::For

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(i, b, e, s = 1, &block) ⇒ For

Returns a new instance of For.



1562
1563
1564
1565
1566
1567
1568
# File 'lib/BOAST/Algorithm.rb', line 1562

def initialize(i, b, e, s=1, &block)
  @iterator = i
  @begin = b
  @end = e
  @step = s
  @block = block
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



1554
1555
1556
# File 'lib/BOAST/Algorithm.rb', line 1554

def begin
  @begin
end

#endObject (readonly)

Returns the value of attribute end.



1555
1556
1557
# File 'lib/BOAST/Algorithm.rb', line 1555

def end
  @end
end

#iteratorObject (readonly)

Returns the value of attribute iterator.



1553
1554
1555
# File 'lib/BOAST/Algorithm.rb', line 1553

def iterator
  @iterator
end

#stepObject (readonly)

Returns the value of attribute step.



1556
1557
1558
# File 'lib/BOAST/Algorithm.rb', line 1556

def step
  @step
end

Class Method Details

.parens(*args, &block) ⇒ Object



1558
1559
1560
# File 'lib/BOAST/Algorithm.rb', line 1558

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

Instance Method Details

#close(final = true) ⇒ Object



1646
1647
1648
1649
# File 'lib/BOAST/Algorithm.rb', line 1646

def close(final=true)
  return self.close_fortran(final) if BOAST::get_lang == FORTRAN
  return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
end

#close_c(final = true) ⇒ Object



1650
1651
1652
1653
1654
1655
1656
1657
# File 'lib/BOAST/Algorithm.rb', line 1650

def close_c(final=true)
  s = ""
  BOAST::decrement_indent_level      
  s += " "*BOAST::get_indent_level if final
  s += "}"
  BOAST::get_output.puts s if final
  return s
end

#close_fortran(final = true) ⇒ Object



1658
1659
1660
1661
1662
1663
1664
1665
# File 'lib/BOAST/Algorithm.rb', line 1658

def close_fortran(final=true)
  s = ""
  BOAST::decrement_indent_level      
  s += " "*BOAST::get_indent_level if final
  s += "enddo"
  BOAST::get_output.puts s if final
  return s
end


1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
# File 'lib/BOAST/Algorithm.rb', line 1631

def print(*args)
  final = true
  s=""
  s += " "*BOAST::get_indent_level if final
  s += self.to_str
  BOAST::increment_indent_level      
  BOAST::get_output.puts s if final
  if @block then
    s += "\n"
    @block.call(*args)
    s += self.close
  end
  return s
end

#to_sObject



1569
1570
1571
# File 'lib/BOAST/Algorithm.rb', line 1569

def to_s
  self.to_str
end

#to_strObject



1572
1573
1574
1575
# File 'lib/BOAST/Algorithm.rb', line 1572

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



1582
1583
1584
1585
1586
# File 'lib/BOAST/Algorithm.rb', line 1582

def to_str_c
  s = ""
  s += "for(#{@iterator}=#{@begin}; #{@iterator}<=#{@end}; #{@iterator}+=#{@step}){"
  return s
end

#to_str_fortranObject



1576
1577
1578
1579
1580
1581
# File 'lib/BOAST/Algorithm.rb', line 1576

def to_str_fortran
  s = ""
  s += "do #{@iterator}=#{@begin}, #{@end}"
  s += ", #{@step}" if @step != 1
  return s
end

#unroll(*args) ⇒ Object



1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
# File 'lib/BOAST/Algorithm.rb', line 1588

def unroll(*args)
  raise "Block not given!" if not @block
  BOAST::push_env( :replace_constants => true )
  begin
    if @begin.kind_of?(Variable) then
      start = @begin.constant
    elsif @begin.kind_of?(Expression) then
      start = eval "#{@begin}"
    else
      start = @begin.to_i
    end
    if @end.kind_of?(Variable) then
      e = @end.constant
    elsif @end.kind_of?(Expression) then
      e = eval "#{@end}"
    else
      e = @end.to_i
    end
    if @step.kind_of?(Variable) then
      step = @step.constant
    elsif @step.kind_of?(Expression) then
      step = eval "#{@step}"
    else
      step = @step.to_i
    end
    raise "Invalid bounds (not constants)!" if not ( start and e and step )
  rescue Exception => ex
    if not ( start and e and step ) then
      BOAST::pop_env( :replace_constants )
      return self.print(*args) if not ( start and e and step )
    end
  end
  BOAST::pop_env( :replace_constants )
  range = start..e
  @iterator.force_replace_constant = true
  range.step(step) { |i|
    @iterator.constant = i
    @block.call(*args)
  }
  @iterator.force_replace_constant = false
  @iterator.constant = nil
end