Class: OpenclTemplateHelper

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

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ OpenclTemplateHelper

Returns a new instance of OpenclTemplateHelper.



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

def initialize(source)
  @source = source
end

Instance Method Details

#dtype_to_c_type(dtype) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 31

def dtype_to_c_type(dtype)
  case dtype.to_s
  when 'float64'
    'double'
  when 'float32', 'float'
    'float'
  when 'int32', 'int'
    'int'
  when 'int16'
    'short'
  when 'uint8'
    'uchar'
  when 'boolean'
    'uchar'
  else
    raise "unknown dtype #{dtype}"
  end
end

#floating_point?(dtype) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 17

def floating_point?(dtype)
  TensorStream::Ops::FLOATING_POINT_TYPES.include?(dtype)
end

#generate(args = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 7

def generate(args = {})
  current_scope = binding

  args.each do |k, v|
    current_scope.local_variable_set(k.to_sym, v)
  end

  ERB.new(@source, nil, '%').result(current_scope)
end

#min_value_for(dtype) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 50

def min_value_for(dtype)
  case dtype.to_s
  when 'float64'
    'DBL_MIN'
  when 'float32', 'float'
    'FLT_MIN'
  when 'int32', 'int'
    'INT_MIN'
  when 'int16'
    'SHRT_MIN'
  when 'int8'
    '0'
  when 'boolean'
    '0'
  else
    raise "unknown dtype #{dtype}"
  end
end

#operator_to_c(op) ⇒ Object



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
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 69

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'
    '*'
  when 'mod'
    '%'
  else
    raise "unsupported op #{op}"
  end
end

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



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

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