BOAST 0.9994 → 0.9995

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,10 @@
1
1
  module BOAST
2
2
 
3
3
  class Operator
4
+ def Operator.inspect
5
+ return "#{self.name}"
6
+ end
7
+
4
8
  def Operator.get_vector_name(type)
5
9
  case BOAST::get_architecture
6
10
  when X86
@@ -401,6 +405,7 @@ module BOAST
401
405
 
402
406
  class Ternary
403
407
  include BOAST::Arithmetic
408
+ include BOAST::Inspectable
404
409
 
405
410
  def self.parens(*args,&block)
406
411
  return self::new(*args,&block)
@@ -417,24 +422,22 @@ module BOAST
417
422
  end
418
423
 
419
424
  def to_s
420
- self.to_str
421
- end
422
-
423
- def to_str
424
425
  raise "Ternary operator unsupported in FORTRAN!" if BOAST::get_lang == BOAST::FORTRAN
425
- return self.to_str_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
426
+ return self.to_s_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
426
427
  end
427
- def to_str_c
428
+
429
+ def to_s_c
428
430
  s = ""
429
431
  s += "(#{@operand1} ? #{@operand2} : #{@operand3})"
430
432
  end
431
- def print(final=true)
433
+
434
+ def print
432
435
  s=""
433
- s += " "*BOAST::get_indent_level if final
434
- s += self.to_str
435
- s += ";" if final and [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
436
- BOAST::get_output.puts s if final
437
- return s
436
+ s += BOAST::indent
437
+ s += self.to_s
438
+ s += ";" if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
439
+ BOAST::get_output.puts s
440
+ return self
438
441
  end
439
442
  end
440
443
  end
data/lib/BOAST/Parens.rb CHANGED
@@ -23,9 +23,9 @@ end
23
23
 
24
24
  module BOAST
25
25
 
26
- def BOAST.register_funccall(name)
26
+ def self.register_funccall(name)
27
27
  s =<<EOF
28
- def BOAST.#{name}(*args)
28
+ def self.#{name}(*args)
29
29
  return BOAST::FuncCall("#{name}", *args)
30
30
  end
31
31
  EOF
data/lib/BOAST/Pragma.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  module BOAST
2
2
 
3
3
  class Pragma
4
- def self.parens(*args,&block)
5
- return self::new(*args,&block)
6
- end
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
7
6
 
8
7
  attr_reader :name
9
8
  attr_reader :options
@@ -14,10 +13,6 @@ module BOAST
14
13
  end
15
14
 
16
15
  def to_s
17
- self.to_str
18
- end
19
-
20
- def to_str
21
16
  s = ""
22
17
  if BOAST::get_lang == FORTRAN then
23
18
  s += "$!"
@@ -30,11 +25,11 @@ module BOAST
30
25
  return s
31
26
  end
32
27
 
33
- def print(final = true)
28
+ def print
34
29
  s=""
35
- s += self.to_str
36
- BOAST::get_output.puts s if final
37
- return s
30
+ s += self.to_s
31
+ BOAST::get_output.puts s
32
+ return self
38
33
  end
39
34
  end
40
35
 
@@ -1,14 +1,15 @@
1
1
  module BOAST
2
+
2
3
  class Procedure
3
- def self.parens(*args,&block)
4
- return self::new(*args,&block)
5
- end
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
6
6
 
7
7
  attr_reader :name
8
8
  attr_reader :parameters
9
9
  attr_reader :constants
10
10
  attr_reader :properties
11
11
  attr_reader :headers
12
+
12
13
  def initialize(name, parameters=[], constants=[], properties={}, &block)
13
14
  @name = name
14
15
  @parameters = parameters
@@ -19,7 +20,7 @@ module BOAST
19
20
  @headers = [] if not @headers
20
21
  end
21
22
 
22
- def header(lang=C,final=true)
23
+ def boast_header_s( lang=C )
23
24
  s = ""
24
25
  headers.each { |h|
25
26
  s += "#include <#{h}>\n"
@@ -43,10 +44,10 @@ module BOAST
43
44
  end
44
45
  s += "#{@name}#{trailer}("
45
46
  if parameters.first then
46
- s += parameters.first.header(lang,false)
47
+ s += parameters.first.boast_header(lang)
47
48
  parameters[1..-1].each { |p|
48
49
  s += ", "
49
- s += p.header(lang,false)
50
+ s += p.boast_header(lang)
50
51
  }
51
52
  end
52
53
  if lang == CUDA then
@@ -54,11 +55,16 @@ module BOAST
54
55
  s += "size_t *block_number, size_t *block_size"
55
56
  end
56
57
  s += ")"
57
- s += ";\n" if final
58
- BOAST::get_output.print s if final
59
58
  return s
60
59
  end
61
60
 
61
+ def boast_header(lang=C)
62
+ s = boast_header_s(lang)
63
+ s += ";\n"
64
+ BOAST::get_output.print s
65
+ return self
66
+ end
67
+
62
68
  def call(*parameters)
63
69
  prefix = ""
64
70
  prefix += "call " if BOAST::get_lang==FORTRAN
@@ -66,23 +72,22 @@ module BOAST
66
72
  f.prefix = prefix
67
73
  return f
68
74
  end
69
- def decl(final=true)
70
- return self.decl_fortran(final) if BOAST::get_lang==FORTRAN
71
- return self.decl_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
72
- end
73
- def close(final=true)
74
- return self.close_fortran(final) if BOAST::get_lang==FORTRAN
75
- return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
75
+
76
+ def close
77
+ return self.close_fortran if BOAST::get_lang==FORTRAN
78
+ return self.close_c if [C, CL, CUDA].include?( BOAST::get_lang )
76
79
  end
77
- def close_c(final=true)
80
+
81
+ def close_c
78
82
  BOAST::decrement_indent_level
79
83
  s = ""
80
84
  s += " return #{@properties[:return]};\n" if @properties[:return]
81
85
  s += "}"
82
- BOAST::get_output.puts s if final
83
- return s
86
+ BOAST::get_output.puts s
87
+ return self
84
88
  end
85
- def close_fortran(final=true)
89
+
90
+ def close_fortran
86
91
  BOAST::decrement_indent_level
87
92
  s = ""
88
93
  if @properties[:return] then
@@ -91,23 +96,30 @@ module BOAST
91
96
  else
92
97
  s += "END SUBROUTINE #{@name}"
93
98
  end
94
- BOAST::get_output.puts s if final
95
- return s
99
+ BOAST::get_output.puts s
100
+ return self
96
101
  end
97
102
 
98
- def print(final=true)
99
- s = self.decl
103
+ def print
104
+ self.open
100
105
  if @block then
101
106
  @block.call
102
- s += self.close
107
+ self.close
103
108
  end
104
- return s
109
+ return self
110
+ end
111
+
112
+ def decl
113
+ return self.decl_fortran if BOAST::get_lang==FORTRAN
114
+ return self.decl_c if [C, CL, CUDA].include?( BOAST::get_lang )
115
+ end
116
+
117
+ def decl_fortran
118
+ raise "Interfaces are not implemented in FORTRAN yet!"
105
119
  end
106
120
 
107
- def decl_c(final=true)
121
+ def decl_c_s
108
122
  s = ""
109
- # s += self.header(BOAST::get_lang,false)
110
- # s += ";\n"
111
123
  if BOAST::get_lang == CL then
112
124
  if @properties[:local] then
113
125
  s += "static "
@@ -139,22 +151,37 @@ module BOAST
139
151
  end
140
152
  s += "#{@name}("
141
153
  if parameters.first then
142
- s += parameters.first.decl(false, @properties[:local])
154
+ s += parameters.first.decl_c_s(@properties[:local])
143
155
  parameters[1..-1].each { |p|
144
- s += ", "+p.decl(false, @properties[:local])
156
+ s += ", "+p.decl_c_s(@properties[:local])
145
157
  }
146
158
  end
147
- s += "){\n"
159
+ s += ")"
160
+ return s
161
+ end
162
+
163
+ def decl_c
164
+ s = decl_c_s + ";"
165
+ BOAST::get_output.puts s
166
+ return self
167
+ end
168
+
169
+ def open
170
+ return self.open_fortran if BOAST::get_lang==FORTRAN
171
+ return self.open_c if [C, CL, CUDA].include?( BOAST::get_lang )
172
+ end
173
+
174
+ def open_c
175
+ s = decl_c_s + "{"
176
+ BOAST::get_output.puts s
148
177
  BOAST::increment_indent_level
149
178
  constants.each { |c|
150
- s += " "*BOAST::get_indent_level
151
- s += c.decl(false)
152
- s += ";\n"
179
+ c.decl
153
180
  }
154
- BOAST::get_output.print s if final
155
- return s
181
+ return self
156
182
  end
157
- def decl_fortran(final=true)
183
+
184
+ def open_fortran
158
185
  s = ""
159
186
  if @properties[:return] then
160
187
  s += "#{@properties[:return].type.decl} FUNCTION "
@@ -162,27 +189,18 @@ module BOAST
162
189
  s += "SUBROUTINE "
163
190
  end
164
191
  s += "#{@name}("
165
- if parameters.first then
166
- s += parameters.first
167
- parameters[1..-1].each { |p|
168
- s += ", "+p
169
- }
170
- end
192
+ s += parameters.join(", ")
171
193
  s += ")\n"
172
194
  BOAST::increment_indent_level
173
- s += " "*BOAST::get_indent_level + "integer, parameter :: wp=kind(1.0d0)\n"
195
+ s += BOAST::indent + "integer, parameter :: wp=kind(1.0d0)"
196
+ BOAST::get_output.puts s
174
197
  constants.each { |c|
175
- s += " "*BOAST::get_indent_level
176
- s += c.decl(false)
177
- s += "\n"
198
+ c.decl
178
199
  }
179
200
  parameters.each { |p|
180
- s += " "*BOAST::get_indent_level
181
- s += p.decl(false)
182
- s += "\n"
201
+ p.decl
183
202
  }
184
- BOAST::get_output.print s if final
185
- return s
203
+ return self
186
204
  end
187
205
  end
188
206
 
@@ -1,13 +1,13 @@
1
1
  module BOAST
2
2
 
3
3
  class Dimension
4
- def self.parens(*args,&block)
5
- return self::new(*args,&block)
6
- end
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
7
6
 
8
7
  attr_reader :val1
9
8
  attr_reader :val2
10
9
  attr_reader :size
10
+
11
11
  def initialize(v1=nil,v2=nil)
12
12
  @size = nil
13
13
  @val1 = nil
@@ -22,7 +22,7 @@ module BOAST
22
22
  end
23
23
  end
24
24
 
25
- def to_str
25
+ def to_s
26
26
  s = ""
27
27
  if @val2 then
28
28
  if BOAST::get_lang == BOAST::FORTRAN then
@@ -39,24 +39,22 @@ module BOAST
39
39
  end
40
40
  return s
41
41
  end
42
- def to_s
43
- self.to_str
44
- end
45
42
  end
46
43
 
47
44
  class ConstArray < Array
45
+ include BOAST::Inspectable
46
+
48
47
  def initialize(array,type = nil)
49
48
  super(array)
50
49
  @type = type::new if type
51
50
  end
51
+
52
52
  def to_s
53
- self.to_str
54
- end
55
- def to_str
56
- return self.to_str_fortran if BOAST::get_lang == BOAST::FORTRAN
57
- return self.to_str_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
53
+ return self.to_s_fortran if BOAST::get_lang == BOAST::FORTRAN
54
+ return self.to_s_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
58
55
  end
59
- def to_str_fortran
56
+
57
+ def to_s_fortran
60
58
  s = ""
61
59
  return s if self.first.nil?
62
60
  s += "(/ &\n"
@@ -68,7 +66,8 @@ module BOAST
68
66
  }
69
67
  s += " /)"
70
68
  end
71
- def to_str_c
69
+
70
+ def to_s_c
72
71
  s = ""
73
72
  return s if self.first.nil?
74
73
  s += "{\n"
@@ -82,6 +81,8 @@ module BOAST
82
81
 
83
82
  class Variable
84
83
  include BOAST::Arithmetic
84
+ include BOAST::Inspectable
85
+ extend BOAST::Functor
85
86
 
86
87
  alias_method :orig_method_missing, :method_missing
87
88
 
@@ -92,10 +93,6 @@ module BOAST
92
93
  return self.orig_method_missing(m, *a, &b)
93
94
  end
94
95
 
95
- def self.parens(*args,&block)
96
- return self::new(*args,&block)
97
- end
98
-
99
96
  attr_reader :name
100
97
  attr_accessor :direction
101
98
  attr_accessor :constant
@@ -130,17 +127,18 @@ module BOAST
130
127
  @sampler = nil
131
128
  end
132
129
  @type = type::new(hash)
133
- @hash = hash
130
+ @options = hash
134
131
  if (@direction == :out or @direction == :inout) and not @dimension then
135
132
  @scalar_output = true
136
133
  else
137
134
  @scalar_output = false
138
135
  end
136
+ @dimension = [@dimension].flatten if @dimension
139
137
  end
140
138
 
141
139
  def copy(name=nil,options={})
142
140
  name = @name if not name
143
- h = @hash.clone
141
+ h = @options.clone
144
142
  options.each { |k,v|
145
143
  h[k] = v
146
144
  }
@@ -158,10 +156,6 @@ module BOAST
158
156
  end
159
157
 
160
158
  def to_s
161
- self.to_str
162
- end
163
-
164
- def to_str
165
159
  if @force_replace_constant or ( @replace_constant and @constant and BOAST::get_replace_constants and not @dimension ) then
166
160
  s = @constant.to_s
167
161
  s += "_wp" if BOAST::get_lang == BOAST::FORTRAN and @type and @type.size == 8
@@ -200,10 +194,6 @@ module BOAST
200
194
  return Index::new(self,args)
201
195
  end
202
196
 
203
- def indent
204
- return " "*BOAST::get_indent_level
205
- end
206
-
207
197
  def finalize
208
198
  s = ""
209
199
  s += ";" if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
@@ -211,12 +201,31 @@ module BOAST
211
201
  return s
212
202
  end
213
203
 
214
- def decl_c(final=true, device=false)
215
- return decl_texture(final) if @texture
204
+ def boast_header(lang=C)
205
+ return decl_texture_s if @texture
206
+ s = ""
207
+ s += "const " if @constant or @direction == :in
208
+ s += @type.decl
209
+ if @dimension then
210
+ s += " *"
211
+ end
212
+ if not @dimension and ( lang == BOAST::FORTRAN or @direction == :out or @direction == :inout ) then
213
+ s += " *"
214
+ end
215
+ s += " #{@name}"
216
+ return s
217
+ end
218
+
219
+ def decl
220
+ return self.decl_fortran if BOAST::get_lang == BOAST::FORTRAN
221
+ return self.decl_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
222
+ end
223
+
224
+ def decl_c_s(device = false)
225
+ return decl_texture_s if @texture
216
226
  s = ""
217
- s += self.indent if final
218
227
  s += "const " if @constant or @direction == :in
219
- s += "__global " if @direction and @dimension and not (@hash[:register] or @hash[:private] or @local) and BOAST::get_lang == BOAST::CL
228
+ s += "__global " if @direction and @dimension and not (@options[:register] or @options[:private] or @local) and BOAST::get_lang == BOAST::CL
220
229
  s += "__local " if @local and BOAST::get_lang == BOAST::CL
221
230
  s += "__shared__ " if @local and not device and BOAST::get_lang == BOAST::CUDA
222
231
  s += @type.decl
@@ -243,47 +252,10 @@ module BOAST
243
252
  s +="]"
244
253
  end
245
254
  s += " = #{@constant}" if @constant
246
- s += self.finalize if final
247
- BOAST::get_output.print s if final
248
255
  return s
249
256
  end
250
257
 
251
- def header(lang=C,final=true)
252
- return decl_texture(final) if @texture
253
- s = ""
254
- s += self.indent if final
255
- s += "const " if @constant or @direction == :in
256
- s += "__global " if @direction and @dimension and BOAST::get_lang == BOAST::CL
257
- s += "__local " if @local and BOAST::get_lang == BOAST::CL
258
- s += "__shared__ " if @local and BOAST::get_lang == BOAST::CUDA
259
- s += @type.decl
260
- if(@dimension and not @constant and not @local) then
261
- s += " *"
262
- end
263
- if not @dimension and ( lang == BOAST::FORTRAN or @direction == :out or @direction == :inout ) then
264
- s += " *"
265
- end
266
- s += " #{@name}"
267
- if(@dimension and @constant) then
268
- s += "[]"
269
- end
270
- if(@dimension and @local) then
271
- s +="["
272
- s += @dimension.reverse.join("*")
273
- s +="]"
274
- end
275
- s += " = #{@constant}" if @constant
276
- s += self.finalize if final
277
- BOAST::get_output.print s if final
278
- return s
279
- end
280
-
281
- def decl(final=true,device=false)
282
- return self.decl_fortran(final) if BOAST::get_lang == BOAST::FORTRAN
283
- return self.decl_c(final, device) if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
284
- end
285
-
286
- def decl_texture(final=true)
258
+ def decl_texture_s
287
259
  raise "Unsupported language #{BOAST::get_lang} for texture!" if not [BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
288
260
  raise "Write is unsupported for textures!" if not (@constant or @direction == :in)
289
261
  dim_number = 1
@@ -292,7 +264,6 @@ module BOAST
292
264
  end
293
265
  raise "Unsupported number of dimension: #{dim_number}!" if dim_number > 3
294
266
  s = ""
295
- s += self.indent if final
296
267
  if BOAST::get_lang == BOAST::CL then
297
268
  s += "__read_only "
298
269
  if dim_number < 3 then
@@ -304,26 +275,32 @@ module BOAST
304
275
  s += "texture<#{@type.decl}, cudaTextureType#{dim_number}D, cudaReadModeElementType> "
305
276
  end
306
277
  s += @name
307
- s += self.finalize if final
308
- BOAST::get_output.print s if final
309
278
  return s
310
279
  end
311
280
 
281
+ def decl_c
282
+ s = ""
283
+ s += BOAST::indent
284
+ s += self.decl_c_s
285
+ s += self.finalize
286
+ BOAST::get_output.print s
287
+ return self
288
+ end
312
289
 
313
- def decl_fortran(final=true)
290
+
291
+ def decl_fortran
314
292
  s = ""
315
- s += self.indent if final
293
+ s += BOAST::indent
316
294
  s += @type.decl
317
295
  s += ", intent(#{@direction})" if @direction
318
296
  s += ", parameter" if @constant
319
297
  if(@dimension) then
320
298
  s += ", dimension("
321
- dim = @dimension[0].to_str
299
+ dim = @dimension[0].to_s
322
300
  if dim then
323
301
  s += dim
324
302
  @dimension[1..-1].each { |d|
325
- s += ", "
326
- s += d
303
+ s += ", #{d}"
327
304
  }
328
305
  else
329
306
  s += "*"
@@ -335,9 +312,9 @@ module BOAST
335
312
  s += " = #{@constant}"
336
313
  s += "_wp" if not @dimension and @type and @type.size == 8
337
314
  end
338
- s += self.finalize if final
339
- BOAST::get_output.print s if final
340
- return s
315
+ s += self.finalize
316
+ BOAST::get_output.print s
317
+ return self
341
318
  end
342
319
 
343
320
  end
data/lib/BOAST/While.rb CHANGED
@@ -1,65 +1,65 @@
1
1
  module BOAST
2
2
 
3
- class While
4
- def self.parens(*args,&block)
5
- return self::new(*args,&block)
6
- end
3
+ class While < BOAST::ControlStructure
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
7
6
 
8
7
  attr_reader :condition
8
+
9
9
  def initialize(condition, &block)
10
10
  @condition = condition
11
11
  @block = block
12
12
  end
13
+
14
+ @@c_strings = {
15
+ :while => '"while (#{cond}) {"',
16
+ :end => '"}"'
17
+ }
18
+
19
+ @@f_strings = {
20
+ :while => '"do while (#{cond})"',
21
+ :end => '"end do"'
22
+ }
23
+
24
+ @@strings = {
25
+ BOAST::C => @@c_strings,
26
+ BOAST::CL => @@c_strings,
27
+ BOAST::CUDA => @@c_strings,
28
+ BOAST::FORTRAN => @@f_strings
29
+ }
30
+
31
+ eval token_string_generator( * %w{while cond} )
32
+ eval token_string_generator( * %w{end} )
33
+
13
34
  def to_s
14
- self.to_str
15
- end
16
- def to_str
17
- return self.to_str_fortran if BOAST::get_lang == FORTRAN
18
- return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
19
- end
20
- def to_str_fortran
21
- s = ""
22
- s += "do while( #{@condition} )"
23
- return s
35
+ return while_string(@condition)
24
36
  end
25
- def to_str_c
26
- s = ""
27
- s += "while(#{@condition}){"
28
- return s
37
+
38
+ def open
39
+ s=""
40
+ s += BOAST::indent
41
+ s += self.to_s
42
+ BOAST::get_output.puts s
43
+ BOAST::increment_indent_level
44
+ return self
29
45
  end
46
+
30
47
  def print(*args)
31
- final = true
32
- s=""
33
- s += " "*BOAST::get_indent_level if final
34
- s += self.to_str
35
- BOAST::increment_indent_level
36
- BOAST::get_output.puts s if final
48
+ self.open
37
49
  if @block then
38
- s += "\n"
39
50
  @block.call(*args)
40
- s += self.close
51
+ self.close
41
52
  end
42
- return s
53
+ return self
43
54
  end
44
- def close(final=true)
45
- return self.close_fortran(final) if BOAST::get_lang == FORTRAN
46
- return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
47
- end
48
- def close_c(final=true)
49
- s = ""
55
+
56
+ def close
50
57
  BOAST::decrement_indent_level
51
- s += " "*BOAST::get_indent_level if final
52
- s += "}"
53
- BOAST::get_output.puts s if final
54
- return s
55
- end
56
- def close_fortran(final=true)
57
58
  s = ""
58
- BOAST::decrement_indent_level
59
- s += " "*BOAST::get_indent_level if final
60
- s += "end do"
61
- BOAST::get_output.puts s if final
62
- return s
59
+ s += BOAST::indent
60
+ s += end_string
61
+ BOAST::get_output.puts s
62
+ return self
63
63
  end
64
64
 
65
65
  end