Class: BOAST::For

Inherits:
ControlStructure show all
Defined in:
lib/BOAST/For.rb

Constant Summary collapse

@@c_strings =
{
  :for => '"for (#{i} = #{b}; #{i} #{o} #{e}; #{i} += #{s}) {"',
  :end => '"}"'
}
@@f_strings =
{
  :for => '"do #{i} = #{b}, #{e}, #{s}"',
  :end => '"end do"'
}
@@strings =
{
  C => @@c_strings,
  CL => @@c_strings,
  CUDA => @@c_strings,
  FORTRAN => @@f_strings
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ControlStructure

inherited, token_string_generator

Methods included from Inspectable

#inspect

Methods included from PrivateStateAccessor

private_boolean_state_accessor, private_state_accessor

Constructor Details

#initialize(i, b, e, s = 1, &block) ⇒ For

Returns a new instance of For.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/BOAST/For.rb', line 10

def initialize(i, b, e, s=1, &block)
  @iterator = i
  @begin = b
  @end = e
  @step = s
  @operator = "<="
  @block = block
  begin
    push_env( :replace_constants => true )
    if @step.kind_of?(Variable) then
      step = @step.constant
    elsif @step.kind_of?(Expression) then
      step = eval "#{@step}"
    else
      step = @step.to_i
    end
    @operator = ">=" if step < 0
  rescue
    STDERR.puts "Warning could not determine sign of step (#{@step}) assuming positive" if [C, CL, CUDA].include?( lang ) and debug?
  ensure
    pop_env( :replace_constants )
  end
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



6
7
8
# File 'lib/BOAST/For.rb', line 6

def begin
  @begin
end

#endObject (readonly)

Returns the value of attribute end.



7
8
9
# File 'lib/BOAST/For.rb', line 7

def end
  @end
end

#iteratorObject (readonly)

Returns the value of attribute iterator.



5
6
7
# File 'lib/BOAST/For.rb', line 5

def iterator
  @iterator
end

#stepObject (readonly)

Returns the value of attribute step.



8
9
10
# File 'lib/BOAST/For.rb', line 8

def step
  @step
end

Instance Method Details

#closeObject



120
121
122
123
124
125
126
127
# File 'lib/BOAST/For.rb', line 120

def close
  decrement_indent_level      
  s = ""
  s += indent
  s += end_string
  output.puts s
  return self
end

#openObject



102
103
104
105
106
107
108
109
# File 'lib/BOAST/For.rb', line 102

def open
  s=""
  s += indent
  s += to_s
  output.puts s
  increment_indent_level      
  return self
end

#pr(*args) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/BOAST/For.rb', line 111

def pr(*args)
  open
  if @block then
    @block.call(*args)
    close
  end
  return self
end

#to_sObject



54
55
56
57
# File 'lib/BOAST/For.rb', line 54

def to_s
  s = for_string(@iterator, @begin, @end, @step, @operator)
  return s
end

#unroll(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/BOAST/For.rb', line 59

def unroll(*args)
  raise "Block not given!" if not @block
  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
      pop_env( :replace_constants )
      return pr(*args) if not ( start and e and step )
    end
  end
  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