BOAST 0.9995 → 0.9996

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/BOAST/Case.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  module BOAST
2
2
 
3
- class Case < BOAST::ControlStructure
4
- include BOAST::Inspectable
5
- extend BOAST::Functor
3
+ class Case < ControlStructure
6
4
 
7
5
  attr_reader :expression
8
6
  attr_reader :constants_list
@@ -44,10 +42,10 @@ module BOAST
44
42
  }
45
43
 
46
44
  @@strings = {
47
- BOAST::C => @@c_strings,
48
- BOAST::CL => @@c_strings,
49
- BOAST::CUDA => @@c_strings,
50
- BOAST::FORTRAN => @@f_strings
45
+ C => @@c_strings,
46
+ CL => @@c_strings,
47
+ CUDA => @@c_strings,
48
+ FORTRAN => @@f_strings
51
49
  }
52
50
 
53
51
  eval token_string_generator( * %w{switch expr})
@@ -60,49 +58,49 @@ module BOAST
60
58
  s = ""
61
59
  if block_number then
62
60
  if block_number != 0 then
63
- s += BOAST::indent + break_string + "\n" if break_string
64
- BOAST::decrement_indent_level
61
+ s += indent + break_string + "\n" if break_string
62
+ decrement_indent_level
65
63
  end
66
- s += BOAST::indent
64
+ s += indent
67
65
  if @constants_list[block_number] and @constants_list[block_number].size > 0 then
68
66
  s += case_string(@constants_list[block_number])
69
67
  else
70
68
  s += default_string
71
69
  end
72
70
  else
73
- s += BOAST::indent
71
+ s += indent
74
72
  s += switch_string(@expression)
75
73
  end
76
- BOAST::increment_indent_level
74
+ increment_indent_level
77
75
  return s
78
76
  end
79
77
 
80
78
  def open
81
- BOAST::get_output.puts self.to_s
79
+ output.puts to_s
82
80
  return self
83
81
  end
84
82
 
85
- def print(*args)
86
- self.open
83
+ def pr(*args)
84
+ open
87
85
  if @blocks.size > 0 then
88
86
  @blocks.each_index { |indx|
89
- s = self.to_s(indx)
90
- BOAST::get_output.puts s
87
+ s = to_s(indx)
88
+ output.puts s
91
89
  @blocks[indx].call(*args)
92
90
  }
93
- self.close
91
+ close
94
92
  end
95
93
  return self
96
94
  end
97
95
 
98
96
  def close
99
97
  s = ""
100
- s += BOAST::indent + break_string + "\n" if break_string
101
- BOAST::decrement_indent_level
102
- s += BOAST::indent
98
+ s += indent + break_string + "\n" if break_string
99
+ decrement_indent_level
100
+ s += indent
103
101
  s += end_string
104
- BOAST::decrement_indent_level
105
- BOAST::get_output.puts s
102
+ decrement_indent_level
103
+ output.puts s
106
104
  return self
107
105
  end
108
106
 
@@ -1,13 +1,21 @@
1
1
  module BOAST
2
2
 
3
3
  class ControlStructure
4
+ include PrivateStateAccessor
5
+ include Inspectable
6
+
7
+ def self.inherited(child)
8
+ child.extend Functor
9
+ end
10
+
4
11
  def self.token_string_generator(name, *args)
5
12
  s = <<EOF
6
13
  def #{name}_string(#{args.join(",")})
7
- return eval @@strings[BOAST::get_lang][:#{name}]
14
+ return eval @@strings[get_lang][:#{name}]
8
15
  end
9
16
  EOF
10
17
  end
18
+
11
19
  end
12
20
 
13
21
  end
@@ -1,11 +1,21 @@
1
1
  module BOAST
2
2
 
3
- class Sizet
4
- extend BOAST::VarFunctor
3
+ class DataType
4
+
5
+ include PrivateStateAccessor
6
+
7
+ def self.inherited(child)
8
+ child.extend( VarFunctor)
9
+ end
10
+
11
+ end
12
+
13
+ class Sizet < DataType
5
14
 
6
15
  attr_reader :signed
7
16
  attr_reader :size
8
17
  attr_reader :vector_length
18
+
9
19
  def initialize(hash={})
10
20
  if hash[:signed] != nil then
11
21
  @signed = hash[:signed]
@@ -19,7 +29,7 @@ module BOAST
19
29
  end
20
30
 
21
31
  def copy(options={})
22
- hash = self.to_hash
32
+ hash = to_hash
23
33
  options.each { |k,v|
24
34
  hash[k] = v
25
35
  }
@@ -27,17 +37,21 @@ module BOAST
27
37
  end
28
38
 
29
39
  def decl
30
- return "integer(kind=#{BOAST::get_default_int_size})" if BOAST::get_lang == FORTRAN
40
+ return "integer(kind=#{get_default_int_size})" if lang == FORTRAN
31
41
  if not @signed then
32
- return "size_t" if [C, CL, CUDA].include?( BOAST::get_lang )
42
+ return "size_t" if [C, CL, CUDA].include?( lang )
33
43
  else
34
- return "ptrdiff_t" if [C, CL, CUDA].include?( BOAST::get_lang )
44
+ return "ptrdiff_t" if [C, CL, CUDA].include?( lang )
35
45
  end
36
46
  end
47
+
48
+ def signed?
49
+ return !!signed
50
+ end
51
+
37
52
  end
38
53
 
39
- class Real
40
- extend BOAST::VarFunctor
54
+ class Real < DataType
41
55
 
42
56
  attr_reader :size
43
57
  attr_reader :signed
@@ -47,7 +61,7 @@ module BOAST
47
61
  attr_reader :setters
48
62
 
49
63
  def ==(t)
50
- return true if t.class == self.class and t.size == self.size and t.vector_length == self.vector_length
64
+ return true if t.class == self.class and t.size == size and t.vector_length == vector_length
51
65
  return false
52
66
  end
53
67
 
@@ -55,7 +69,7 @@ module BOAST
55
69
  if hash[:size] then
56
70
  @size = hash[:size]
57
71
  else
58
- @size = BOAST::get_default_real_size
72
+ @size = get_default_real_size
59
73
  end
60
74
  # @getters = {}
61
75
  # @setters = {}
@@ -72,6 +86,10 @@ module BOAST
72
86
  @signed = true
73
87
  end
74
88
 
89
+ def signed?
90
+ return !!signed
91
+ end
92
+
75
93
  def to_hash
76
94
  return { :size => @size, :vector_length => @vector_length }
77
95
  end
@@ -85,30 +103,29 @@ module BOAST
85
103
  end
86
104
 
87
105
  def decl
88
- return "real(kind=#{@size})" if BOAST::get_lang == FORTRAN
89
- if [C, CL, CUDA].include?( BOAST::get_lang ) and @vector_length == 1 then
106
+ return "real(kind=#{@size})" if lang == FORTRAN
107
+ if [C, CL, CUDA].include?( lang ) and @vector_length == 1 then
90
108
  return "float" if @size == 4
91
109
  return "double" if @size == 8
92
- elsif BOAST::get_lang == C and @vector_length > 1 then
93
- if BOAST::get_architecture == BOAST::X86 then
110
+ elsif lang == C and @vector_length > 1 then
111
+ if get_architecture == X86 then
94
112
  return "__m#{@total_size*8}" if @size == 4
95
113
  return "__m#{@total_size*8}d" if @size == 8
96
- elsif BOAST::get_architecture == BOAST::ARM then
114
+ elsif get_architecture == ARM then
97
115
  raise "Unsupported data type in NEON: double!" if @size == 8
98
116
  raise "Unsupported vector length in NEON: #{@total_size} (#{@size} x 8 x #{@vector_length})!" if @total_size * 8 != 64 or @total_size * 8 != 128
99
117
  return "float#{@size*8}x#{@vector_length}_t"
100
118
  else
101
119
  raise "Unsupported architecture!"
102
120
  end
103
- elsif [CL, CUDA].include?( BOAST::get_lang ) and @vector_length > 1 then
121
+ elsif [CL, CUDA].include?( lang ) and @vector_length > 1 then
104
122
  return "float#{@vector_length}" if @size == 4
105
123
  return "double#{@vector_length}" if @size == 8
106
124
  end
107
125
  end
108
126
  end
109
127
 
110
- class Int
111
- extend BOAST::VarFunctor
128
+ class Int < DataType
112
129
 
113
130
  attr_reader :size
114
131
  attr_reader :signed
@@ -116,7 +133,7 @@ module BOAST
116
133
  attr_reader :total_size
117
134
 
118
135
  def ==(t)
119
- return true if t.class == self.class and t.signed == self.signed and t.size == self.size and t.vector_length == self.vector_length
136
+ return true if t.class == self.class and t.signed == signed and t.size == size and t.vector_length == vector_length
120
137
  return false
121
138
  end
122
139
 
@@ -124,12 +141,12 @@ module BOAST
124
141
  if hash[:size] then
125
142
  @size = hash[:size]
126
143
  else
127
- @size = BOAST::get_default_int_size
144
+ @size = get_default_int_size
128
145
  end
129
146
  if hash[:signed] != nil then
130
147
  @signed = hash[:signed]
131
148
  else
132
- @signed = BOAST::get_default_int_signed
149
+ @signed = get_default_int_signed
133
150
  end
134
151
  # @getters = {}
135
152
  # @setters = {}
@@ -150,7 +167,7 @@ module BOAST
150
167
  end
151
168
 
152
169
  def copy(options={})
153
- hash = self.to_hash
170
+ hash = to_hash
154
171
  options.each { |k,v|
155
172
  hash[k] = v
156
173
  }
@@ -158,27 +175,27 @@ module BOAST
158
175
  end
159
176
 
160
177
  def signed?
161
- return @signed
178
+ return !!@signed
162
179
  end
163
180
 
164
181
  def decl
165
- return "integer(kind=#{@size})" if BOAST::get_lang == FORTRAN
166
- if BOAST::get_lang == C then
182
+ return "integer(kind=#{@size})" if lang == FORTRAN
183
+ if lang == C then
167
184
  if @vector_length == 1 then
168
185
  s = ""
169
186
  s += "u" if not @signed
170
187
  return s+"int#{8*@size}_t"
171
188
  elsif @vector_length > 1 then
172
- if BOAST::get_architecture == BOAST::X86 then
189
+ if get_architecture == X86 then
173
190
  return "__m#{@total_size*8}#{@total_size*8>64 ? "i" : ""}"
174
- elsif BOAST::get_architecture == BOAST::ARM then
191
+ elsif get_architecture == ARM then
175
192
  raise "Unsupported vector length in NEON: #{@total_size*8} (#{@size} x 8 x #{@vector_length})!" if @total_size * 8 != 64 and @total_size * 8 != 128
176
193
  return "#{ @signed ? "" : "u"}int#{@size*8}x#{@vector_length}_t"
177
194
  else
178
195
  raise "Unsupported architecture!"
179
196
  end
180
197
  end
181
- elsif BOAST::get_lang == CL then
198
+ elsif lang == CL then
182
199
  #char="cl_"
183
200
  char=""
184
201
  char += "u" if not @signed
@@ -198,7 +215,7 @@ module BOAST
198
215
  char += "#{@vector_length}"
199
216
  end
200
217
  return char
201
- elsif BOAST::get_lang == CUDA then
218
+ elsif lang == CUDA then
202
219
  if @vector_length > 1 then
203
220
  char=""
204
221
  char += "u" if not @signed
@@ -227,8 +244,7 @@ module BOAST
227
244
  end
228
245
  end
229
246
 
230
- class CStruct
231
- extend BOAST::VarFunctor
247
+ class CStruct < DataType
232
248
 
233
249
  attr_reader :name, :members, :members_array
234
250
 
@@ -244,61 +260,62 @@ module BOAST
244
260
  end
245
261
 
246
262
  def decl_c
247
- return "struct #{@name}" if [C, CL, CUDA].include?( BOAST::get_lang )
263
+ return "struct #{@name}" if [C, CL, CUDA].include?( lang )
248
264
  end
249
265
 
250
266
  def decl_fortran
251
- return "TYPE(#{@name})" if BOAST::get_lang == FORTRAN
267
+ return "TYPE(#{@name})" if lang == FORTRAN
252
268
  end
253
269
 
254
270
  def decl
255
- return self.decl_c if [C, CL, CUDA].include?( BOAST::get_lang )
256
- return self.decl_fortran if BOAST::get_lang == FORTRAN
271
+ return decl_c if [C, CL, CUDA].include?( lang )
272
+ return decl_fortran if lang == FORTRAN
257
273
  end
258
274
 
259
275
  def finalize
260
276
  s = ""
261
- s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
277
+ s += ";" if [C, CL, CUDA].include?( lang )
262
278
  s+="\n"
263
279
  return s
264
280
  end
265
281
 
266
282
  def define
267
- return define_c if [C, CL, CUDA].include?( BOAST::get_lang )
268
- return define_fortran if BOAST::get_lang == FORTRAN
283
+ return define_c if [C, CL, CUDA].include?( lang )
284
+ return define_fortran if lang == FORTRAN
269
285
  end
270
286
 
271
287
  def define_c
272
- s = BOAST::indent
273
- s += self.decl_c + " {"
274
- BOAST::get_output.puts s
288
+ s = indent
289
+ s += decl_c + " {"
290
+ output.puts s
275
291
  @members_array.each { |value|
276
292
  value.decl
277
293
  }
278
- s = BOAST::indent
294
+ s = indent
279
295
  s += "}"
280
- s += self.finalize
281
- BOAST::get_output.print s
296
+ s += finalize
297
+ output.print s
282
298
  return self
283
299
  end
284
300
 
285
301
  def define_fortran
286
- s = BOAST::indent
302
+ s = indent
287
303
  s += "TYPE :: #{@name}\n"
288
- BOAST::get_output.puts s
304
+ output.puts s
289
305
  @members_array.each { |value|
290
306
  value.decl
291
307
  }
292
- s = BOAST::indent
308
+ s = indent
293
309
  s += "END TYPE #{@name}"
294
- s += self.finalize
295
- BOAST::get_output.print s
310
+ s += finalize
311
+ output.print s
296
312
  return self
297
313
  end
298
314
 
299
315
  end
300
316
 
301
- class CustomType
317
+ class CustomType < DataType
318
+
302
319
  attr_reader :size, :name, :vector_length
303
320
  def initialize(hash={})
304
321
  @name = hash[:type_name]
@@ -308,9 +325,11 @@ module BOAST
308
325
  @vector_length = 1 if @vector_length.nil?
309
326
  @total_size = @vector_length*@size
310
327
  end
328
+
311
329
  def decl
312
- return "#{@name}" if [C, CL, CUDA].include?( BOAST::get_lang )
330
+ return "#{@name}" if [C, CL, CUDA].include?( lang )
313
331
  end
332
+
314
333
  end
315
334
 
316
335
  end
@@ -1,13 +1,17 @@
1
1
  module BOAST
2
2
 
3
- def self.Return(value)
3
+ module_function
4
+
5
+ def Return(value)
4
6
  return Expression("return",nil, value)
5
7
  end
6
8
 
7
9
  class Expression
8
- include BOAST::Arithmetic
9
- include BOAST::Inspectable
10
- extend BOAST::Functor
10
+ include PrivateStateAccessor
11
+ include Arithmetic
12
+ include Inspectable
13
+ extend Functor
14
+ include TypeTransition
11
15
 
12
16
  attr_reader :operator
13
17
  attr_reader :operand1
@@ -18,7 +22,7 @@ module BOAST
18
22
  @operand2 = operand2
19
23
  end
20
24
 
21
- def Expression.to_s_base(op1, op2, oper, return_type = nil)
25
+ def to_s_base(op1, op2, oper, return_type = nil)
22
26
  return oper.to_s(op1, op2, return_type) if not oper.kind_of?(String)
23
27
  s = ""
24
28
  if op1 then
@@ -27,7 +31,7 @@ module BOAST
27
31
  s += ")" if (oper == "*" or oper == "/")
28
32
  end
29
33
  s += " " unless oper == "++" or oper == "."
30
- s += oper unless ( oper == "&" and BOAST::get_lang == BOAST::FORTRAN )
34
+ s += oper unless ( oper == "&" and lang == FORTRAN )
31
35
  s += " " unless oper == "." or oper == "&" or ( oper == "*" and op1.nil? )
32
36
  if op2 then
33
37
  s += "(" if (oper == "*" or oper == "/" or oper == "-")
@@ -43,14 +47,14 @@ module BOAST
43
47
  op2 = nil
44
48
  op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
45
49
  if op1 and op2 then
46
- r_t, oper = BOAST::transition(op1, op2, @operator)
47
- res_exp = BOAST::Expression::to_s_base(op1, op2, oper, r_t)
50
+ r_t, oper = transition(op1, op2, @operator)
51
+ res_exp = to_s_base(op1, op2, oper, r_t)
48
52
  return r_t.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
49
53
  elsif op2
50
- res_exp = BOAST::Expression::to_s_base(@operand1, op2, @operator)
54
+ res_exp = to_s_base(@operand1, op2, @operator)
51
55
  return op2.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
52
56
  elsif op1
53
- res_exp = BOAST::Expression::to_s_base(op1, @operand2, @operator)
57
+ res_exp = to_s_base(op1, @operand2, @operator)
54
58
  return op1.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
55
59
  else
56
60
  STDERR.puts "#{@operand1} #{@operand2}"
@@ -65,7 +69,7 @@ module BOAST
65
69
  op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
66
70
  r_t = nil
67
71
  if op1 and op2 then
68
- r_t, oper = BOAST::transition(op1, op2, @operator)
72
+ r_t, oper = transition(op1, op2, @operator)
69
73
  else
70
74
  oper = @operator
71
75
  end
@@ -73,17 +77,18 @@ module BOAST
73
77
  op1 = @operand1 if op1.nil?
74
78
  op2 = @operand2 if op2.nil?
75
79
 
76
- return BOAST::Expression::to_s_base(op1, op2, oper, r_t)
80
+ return to_s_base(op1, op2, oper, r_t)
77
81
  end
78
82
 
79
- def print
83
+ def pr
80
84
  s=""
81
- s += BOAST::indent
82
- s += self.to_s
83
- s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
84
- BOAST::get_output.puts s
85
+ s += indent
86
+ s += to_s
87
+ s += ";" if [C, CL, CUDA].include?( lang )
88
+ output.puts s
85
89
  return self
86
90
  end
91
+
87
92
  end
88
93
 
89
94
  end
data/lib/BOAST/For.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  module BOAST
2
2
 
3
3
  class For < ControlStructure
4
- include BOAST::Inspectable
5
- extend BOAST::Functor
6
4
 
7
5
  attr_reader :iterator
8
6
  attr_reader :begin
@@ -14,11 +12,27 @@ module BOAST
14
12
  @begin = b
15
13
  @end = e
16
14
  @step = s
15
+ @operator = "<="
17
16
  @block = block
17
+ begin
18
+ push_env( :replace_constants => true )
19
+ if @step.kind_of?(Variable) then
20
+ step = @step.constant
21
+ elsif @step.kind_of?(Expression) then
22
+ step = eval "#{@step}"
23
+ else
24
+ step = @step.to_i
25
+ end
26
+ @operator = ">=" if step < 0
27
+ rescue
28
+ STDERR.puts "Warning could not determine sign of step (#{@step}) assuming positive" if [C, CL, CUDA].include?( lang ) and debug?
29
+ ensure
30
+ pop_env( :replace_constants )
31
+ end
18
32
  end
19
33
 
20
34
  @@c_strings = {
21
- :for => '"for (#{i} = #{b}; #{i} <= #{e}; #{i} += #{s}) {"',
35
+ :for => '"for (#{i} = #{b}; #{i} #{o} #{e}; #{i} += #{s}) {"',
22
36
  :end => '"}"'
23
37
  }
24
38
 
@@ -28,23 +42,23 @@ module BOAST
28
42
  }
29
43
 
30
44
  @@strings = {
31
- BOAST::C => @@c_strings,
32
- BOAST::CL => @@c_strings,
33
- BOAST::CUDA => @@c_strings,
34
- BOAST::FORTRAN => @@f_strings
45
+ C => @@c_strings,
46
+ CL => @@c_strings,
47
+ CUDA => @@c_strings,
48
+ FORTRAN => @@f_strings
35
49
  }
36
50
 
37
- eval token_string_generator( * %w{for i b e s})
51
+ eval token_string_generator( * %w{for i b e s o})
38
52
  eval token_string_generator( * %w{end})
39
53
 
40
54
  def to_s
41
- s = for_string(@iterator, @begin, @end, @step)
55
+ s = for_string(@iterator, @begin, @end, @step, @operator)
42
56
  return s
43
57
  end
44
58
 
45
59
  def unroll(*args)
46
60
  raise "Block not given!" if not @block
47
- BOAST::push_env( :replace_constants => true )
61
+ push_env( :replace_constants => true )
48
62
  begin
49
63
  if @begin.kind_of?(Variable) then
50
64
  start = @begin.constant
@@ -70,11 +84,11 @@ module BOAST
70
84
  raise "Invalid bounds (not constants)!" if not ( start and e and step )
71
85
  rescue Exception => ex
72
86
  if not ( start and e and step ) then
73
- BOAST::pop_env( :replace_constants )
74
- return self.print(*args) if not ( start and e and step )
87
+ pop_env( :replace_constants )
88
+ return pr(*args) if not ( start and e and step )
75
89
  end
76
90
  end
77
- BOAST::pop_env( :replace_constants )
91
+ pop_env( :replace_constants )
78
92
  range = start..e
79
93
  @iterator.force_replace_constant = true
80
94
  range.step(step) { |i|
@@ -87,28 +101,28 @@ module BOAST
87
101
 
88
102
  def open
89
103
  s=""
90
- s += BOAST::indent
91
- s += self.to_s
92
- BOAST::get_output.puts s
93
- BOAST::increment_indent_level
104
+ s += indent
105
+ s += to_s
106
+ output.puts s
107
+ increment_indent_level
94
108
  return self
95
109
  end
96
110
 
97
- def print(*args)
98
- self.open
111
+ def pr(*args)
112
+ open
99
113
  if @block then
100
114
  @block.call(*args)
101
- self.close
115
+ close
102
116
  end
103
117
  return self
104
118
  end
105
119
 
106
120
  def close
107
- BOAST::decrement_indent_level
121
+ decrement_indent_level
108
122
  s = ""
109
- s += BOAST::indent
123
+ s += indent
110
124
  s += end_string
111
- BOAST::get_output.puts s
125
+ output.puts s
112
126
  return self
113
127
  end
114
128
 
@@ -1,9 +1,10 @@
1
1
  module BOAST
2
2
 
3
3
  class FuncCall
4
- include BOAST::Arithmetic
5
- include BOAST::Inspectable
6
- extend BOAST::Functor
4
+ include PrivateStateAccessor
5
+ include Arithmetic
6
+ include Inspectable
7
+ extend Functor
7
8
 
8
9
  @return_type
9
10
  @options
@@ -35,8 +36,8 @@ module BOAST
35
36
  end
36
37
 
37
38
  def to_s
38
- return self.to_s_fortran if BOAST::get_lang == FORTRAN
39
- return self.to_s_c if [C, CL, CUDA].include?( BOAST::get_lang )
39
+ return to_s_fortran if lang == FORTRAN
40
+ return to_s_c if [C, CL, CUDA].include?( lang )
40
41
  end
41
42
 
42
43
  def to_s_fortran
@@ -51,12 +52,12 @@ module BOAST
51
52
  s += "#{func_name}(#{@args.join(", ")})"
52
53
  end
53
54
 
54
- def print
55
+ def pr
55
56
  s=""
56
- s += BOAST::indent
57
- s += self.to_s
58
- s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
59
- BOAST::get_output.puts s
57
+ s += indent
58
+ s += to_s
59
+ s += ";" if [C, CL, CUDA].include?( lang )
60
+ output.puts s
60
61
  return self
61
62
  end
62
63
  end