Class: OpenclTemplateHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/tensor_stream/evaluator/opencl_template_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ OpenclTemplateHelper



3
4
5
# File 'lib/tensor_stream/evaluator/opencl_template_helper.rb', line 3

def initialize(source)
  @source = source
end

Instance Method Details

#dtype_to_c_type(dtype) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/tensor_stream/evaluator/opencl_template_helper.rb', line 21

def dtype_to_c_type(dtype)
  case(dtype)
  when 'fp'
    'float'
  when 'int'
    'int'
  end
end

#generateObject



7
8
9
# File 'lib/tensor_stream/evaluator/opencl_template_helper.rb', line 7

def generate
  ERB.new(@source, nil, '%').result(binding)
end

#operator_to_c(op) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tensor_stream/evaluator/opencl_template_helper.rb', line 30

def operator_to_c(op)
  case(op)
  when 'less'
    '<'
  when 'less_equal'
    '<='
  when 'equal'
    '=='
  when 'greater'
    '>'
  when 'greater_equal'
    '>='
  when 'not_equal'
    '!='
  when 'logical_and'
    '&&'
  when 'div'
    '/'
  when 'add'
    '+'
  when 'sub'
    '-'
  when 'mul'
    '*'
  else
    raise "unsupported op #{op}"
  end
end

#render(template, locals = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/tensor_stream/evaluator/opencl_template_helper.rb', line 11

def render(template, locals = {})
  filename = File.join(File.dirname(__FILE__), 'kernels', "_#{template}")
  source = File.read(filename)
  current_scope = binding
  locals.each do |k,v|
    current_scope.local_variable_set(k.to_sym, v)
  end
  ERB.new(source, nil, '%').result(current_scope)
end