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
49
50
51
52
53
54
55
56
# 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', 'float16'
    'float'
  when 'uint32'
    'uint'
  when 'int64'
    'int' # 'long' - NArray does not support 64bit int types
  when 'uint64'
    'uint'  # 'ulong' - NArray does not support 64bit int types
  when 'int32', 'int'
    'int'
  when 'uint16'
    'ushort'
  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

#max_value_for(dtype) ⇒ Object



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 79

def max_value_for(dtype)
  case dtype.to_s
  when 'float64'
    'DBL_MAX'
  when 'float32', 'float', 'float16'
    'FLT_MAX'
  when 'int32', 'int'
    'INT_MAX'
  when 'uint32', 'uint16'
    '0'
  when 'int16'
    'SHRT_MAX'
  when 'int8'
    '256'
  when 'boolean'
    '1'
  else
    raise "unknown dtype #{dtype}"
  end
end

#min_value_for(dtype) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 58

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

#operator_to_c(op) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tensor_stream/opencl/opencl_template_helper.rb', line 100

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