Class: MetaCL::DSL::ExpressionApplicator
- Inherits:
-
Object
- Object
- MetaCL::DSL::ExpressionApplicator
- Defined in:
- lib/metacl/dsl/expression_applicator.rb
Constant Summary collapse
- @@counter =
0
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#init_code ⇒ Object
readonly
Returns the value of attribute init_code.
Class Method Summary collapse
Instance Method Summary collapse
- #code_generation ⇒ Object
-
#initialize(program, expr, result_matrix, options = {}) ⇒ ExpressionApplicator
constructor
A new instance of ExpressionApplicator.
- #prepare_tree ⇒ Object
-
#pt_vars_gen ⇒ Object
pt means prepare_tree.
Constructor Details
#initialize(program, expr, result_matrix, options = {}) ⇒ ExpressionApplicator
Returns a new instance of ExpressionApplicator.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 7 def initialize(program, expr, result_matrix, = {}) @@counter += 1 @program = program @expr = if expr.kind_of? Logic::Node expr.deep_clone elsif expr.kind_of? Symbol Logic::Node.new :data, nil, nil, name: expr elsif expr.kind_of? Numeric Logic::Node.new :const, nil, nil, data: expr end @result_matrix = program.resources.matrices_hash[result_matrix] @left_border = [:from] || [0, 0] @right_border = [:to] || [@result_matrix.size_n, @result_matrix.size_m] @var_letter = 't' prepare_tree code_generation @init_code = if @program.platform == :cl Templates::KernelInit.render(@left_border, @right_border, @expr.params.code || '', @result_matrix, @expr.objects, @@counter, @program.platform) else '' end @code = Templates::ExpressionApplicatorWrapper.render(@left_border, @right_border, @expr.params.code || '', @expr.objects, @result_matrix, @@counter, @program.platform) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 5 def code @code end |
#init_code ⇒ Object (readonly)
Returns the value of attribute init_code.
5 6 7 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 5 def init_code @init_code end |
Class Method Details
.construct(program, expr, result_matrix, options = {}) ⇒ Object
35 36 37 38 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 35 def self.construct(program, expr, result_matrix, = {}) e = ExpressionApplicator.new(program, expr, result_matrix, ) [e.code, e.init_code] end |
Instance Method Details
#code_generation ⇒ Object
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 101 102 103 104 105 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 73 def code_generation @expr.walk do |node| case node.type when :operator left_var = node.left_child.params.var right_var = node.right_child.params.var left_code = node.left_child.params.code || '' right_code = node.right_child.params.code || '' node.params.code = left_code + right_code + "float #{node.params.var} = #{left_var} #{node.params.type} #{right_var};\n" when :aggregator code = node.left_child.params.code || '' subresult_var = node.left_child.params.var iterator = node.params.index from, to = node.params.from, node.params.to type = 'float' var = node.params.var operator = node.params.type node.params.code = Templates::Aggregator.render(@program.platform, code: code, subresult_var: subresult_var, iterator: iterator, from: from, to: to, type: type, var: var, operator: operator ) + "\n" end end @expr.params.code ||= '' @expr.params.code += "#{@result_matrix.name}[i*#{@result_matrix.size_m} + j] = #{@expr.params.var};\n" end |
#prepare_tree ⇒ Object
40 41 42 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 40 def prepare_tree pt_vars_gen end |
#pt_vars_gen ⇒ Object
pt means prepare_tree
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/metacl/dsl/expression_applicator.rb', line 46 def pt_vars_gen vars_count = 0 @expr.walk do |node| case node.type when :data index_i = node.params.index_i || 'i' index_j = node.params.index_j || 'j' data = @program.resources[node.params.name] node.params.object = data case data.klass when :matrix node.params.var = "#{data.name}[(#{index_i})*#{data.size_m} + (#{index_j})]" when :array node.params.var = "#{data.name}[#{index_i}]" when :numeric node.params.var = data.name end when :operator, :aggregator vars_count += 1 node.params.var = @var_letter + vars_count.to_s when :const node.params.var = node.params.data.to_s end end end |