Class: BOAST::For

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

Constant Summary collapse

ANNOTATIONS =
[ :iterator, :first, :last, :step, :operator ]

Instance Attribute Summary collapse

Attributes inherited from ControlStructure

#args

Instance Method Summary collapse

Methods included from Annotation

#annotate_array, #annotate_indepth?, #annotate_scalar, #annotate_var, #annotation, #annotation_identifier

Methods included from PrivateStateAccessor

#address_size, #address_size=, #annotate, #annotate=, #annotate?, #annotate_indepth_list, #annotate_indepth_list=, #annotate_level, #annotate_level=, #annotate_list, #annotate_list=, #architecture, #architecture=, #array_start, #array_start=, #chain_code, #chain_code=, #chain_code?, #debug, #debug=, #debug?, #debug_kernel_source, #debug_kernel_source=, #debug_kernel_source?, #debug_source, #debug_source=, #debug_source?, #decl_module, #decl_module=, #decl_module?, #default_align, #default_align=, #default_int_signed, #default_int_signed=, #default_int_signed?, #default_int_size, #default_int_size=, #default_real_size, #default_real_size=, #default_type, #default_type=, #disable_openmp, #disable_openmp=, #disable_openmp?, #executable, #executable=, #executable?, #ffi, #ffi=, #ffi?, #fortran_line_length, #fortran_line_length=, #get_address_size, #get_annotate, #get_annotate_indepth_list, #get_annotate_level, #get_annotate_list, #get_architecture, #get_array_start, #get_chain_code, #get_debug, #get_debug_kernel_source, #get_debug_source, #get_decl_module, #get_default_align, #get_default_int_signed, #get_default_int_size, #get_default_real_size, #get_default_type, #get_disable_openmp, #get_executable, #get_ffi, #get_fortran_line_length, #get_indent_increment, #get_indent_level, #get_keep_temp, #get_lang, #get_model, #get_optimizer_log, #get_optimizer_log_file, #get_output, #get_replace_constants, #get_use_vla, #get_verbose, #indent_increment, #indent_increment=, #indent_level, #indent_level=, #keep_temp, #keep_temp=, #keep_temp?, #lang, #lang=, #model, #model=, #optimizer_log, #optimizer_log=, #optimizer_log?, #optimizer_log_file, #optimizer_log_file=, #output, #output=, #replace_constants, #replace_constants=, #replace_constants?, #set_address_size, #set_annotate, #set_annotate_indepth_list, #set_annotate_level, #set_annotate_list, #set_architecture, #set_array_start, #set_chain_code, #set_debug, #set_debug_kernel_source, #set_debug_source, #set_decl_module, #set_default_align, #set_default_int_signed, #set_default_int_size, #set_default_real_size, #set_default_type, #set_disable_openmp, #set_executable, #set_ffi, #set_fortran_line_length, #set_indent_increment, #set_indent_level, #set_keep_temp, #set_lang, #set_model, #set_optimizer_log, #set_optimizer_log_file, #set_output, #set_replace_constants, #set_use_vla, #set_verbose, #use_vla, #use_vla=, #use_vla?, #verbose, #verbose=, #verbose?

Methods inherited from ControlStructure

#[], inherited

Methods included from Inspectable

#inspect

Constructor Details

#initialize(iterator, first, last, options = {}, &block) ⇒ For

Creates a new instance of the For construct.

Parameters:

  • iterator (Variable)
  • first (#to_s)

    iteration start

  • last (#to_s)

    iteration stop (inclusive)

  • options (Hash) (defaults to: {})

    contains named options

  • block (Proc, nil)

    if given, will be evaluated when #pr is called

Options Hash (options):

  • :step (#to_s)

    spcifies the increment in the for loop

  • :openmp (Boolean, Hash)

    specifies if an OpenMP For pragma has to be generated. If a Hash is specified it conatins the OpenMP clauses and their values.

  • :unroll (Boolean)

    specifies if #pr must try to unroll the loop

  • :args (Array<Object>)

    arguments to be passed to the block. Will be superseded by those provided by #pr



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/BOAST/Language/For.rb', line 34

def initialize(iterator, first, last, options={}, &block)
  super()
  default_options = {:step => 1}
  default_options.update( options )
  @options = options
  @iterator = iterator
  @first = first
  @last = last
  @step = default_options[:step]
  @operator = "<="
  @block = block
  @openmp = default_options[:openmp]
  @unroll = default_options[:unroll]
  @args = default_options[:args]
  if @openmp then
    if @openmp.kind_of?(Hash) then
      @openmp = OpenMP::For(@openmp)
    else
      @openmp = OpenMP::For({})
    end
  end
  push_env( :replace_constants => true ) {
    begin
      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?
    end
  }
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



12
13
14
# File 'lib/BOAST/Language/For.rb', line 12

def block
  @block
end

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#iteratorObject (readonly)

Returns the value of attribute iterator.



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

def iterator
  @iterator
end

#lastObject (readonly)

Returns the value of attribute last.



10
11
12
# File 'lib/BOAST/Language/For.rb', line 10

def last
  @last
end

#stepObject (readonly)

Returns the value of attribute step.



11
12
13
# File 'lib/BOAST/Language/For.rb', line 11

def step
  @step
end

Instance Method Details

#closeself

Closes the For construct (keyword, closing bracket in C like languages). The result is printed to the BOAST output.

Returns:

  • (self)


188
189
190
191
192
193
194
195
196
# File 'lib/BOAST/Language/For.rb', line 188

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

#openself

Opens the For construct (keyword, iterator, bounds, step, opening bracket in C like languages). The result is printed to the BOAST output.

Returns:

  • (self)


159
160
161
162
163
164
165
166
167
# File 'lib/BOAST/Language/For.rb', line 159

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

#pr(*args, &block) ⇒ self

Prints the For construct to the BOAST output (see #open). If a block is provided during initialization, it will be printed and the construct will be closed (see #close).

Parameters:

  • args (Array<Object>)

    any number of arguments to pass to the block

  • block (Proc)

    an optional block to be evaluated. Supersede the one given at initialization

Returns:

  • (self)


174
175
176
177
178
179
180
181
182
183
184
# File 'lib/BOAST/Language/For.rb', line 174

def pr(*args, &block)
  args = @args if args.length == 0 and @args
  block = @block unless block
  return pr_unroll(*args, &block) if unroll?
  open
  if block then
    block.call(*args)
    close
  end
  return self
end

#to_sObject

Returns a string representation of the For construct.



90
91
92
93
# File 'lib/BOAST/Language/For.rb', line 90

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

#unroll(flag = true) ⇒ For

Creates a copy of this For construct with the unroll option set and returns it if it is different from the current unroll flag.

Parameters:

  • flag (Boolean) (defaults to: true)

    specifying if the For should be unrolled or not

Returns:



98
99
100
101
102
103
104
105
106
# File 'lib/BOAST/Language/For.rb', line 98

def unroll( flag = true )
  if flag ^ @unroll then
    opts = @options.clone
    opts[:unroll] = flag
    return For::new(@iterator, @first, @last, opts, &block)
  else
    return self
  end
end

#unroll=(val) ⇒ Object

Sets the unroll attribute to val.



20
21
22
# File 'lib/BOAST/Language/For.rb', line 20

def unroll=(val)
  @unroll = val
end

#unroll?Boolean

returns the Boolean evaluation of the unroll attribute.

Returns:

  • (Boolean)


15
16
17
# File 'lib/BOAST/Language/For.rb', line 15

def unroll?
  return !!@unroll
end