barracuda 1.2 → 1.3

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.
@@ -0,0 +1,38 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../ext/')
2
+
3
+ require "test/unit"
4
+ require "barracuda"
5
+
6
+ include Barracuda
7
+
8
+ class TestBuffer < Test::Unit::TestCase
9
+ def test_buffer_create_invalid_data
10
+ assert_raise(TypeError) { Buffer.new("xyz") }
11
+ end
12
+
13
+ def test_buffer_create_with_array
14
+ b = Buffer.new([1, 2, 3, 4, 5])
15
+ assert_equal [1, 2, 3, 4, 5], b
16
+ end
17
+
18
+ def test_buffer_create_with_size
19
+ b = Buffer.new(80)
20
+ assert_equal 80, b.size
21
+ end
22
+
23
+ def test_buffer_mark_dirty
24
+ b = Buffer.new([4, 2, 3])
25
+ b.mark_dirty
26
+ assert b.dirty?
27
+ end
28
+
29
+ def test_buffer_from_array
30
+ b = Array.new(80).outvar
31
+ assert_kind_of Buffer, b
32
+ end
33
+
34
+ def test_outvar_buffer
35
+ b = Buffer.new(8)
36
+ assert b.outvar?
37
+ end
38
+ end
@@ -0,0 +1,206 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../ext/')
2
+
3
+ require "test/unit"
4
+ require "barracuda"
5
+
6
+ include Barracuda
7
+
8
+ class TestProgram < Test::Unit::TestCase
9
+ def test_program_create_invalid_code
10
+ assert_raise(Barracuda::SyntaxError) { Program.new "fib { SYNTAXERROR }" }
11
+ end
12
+
13
+ def test_program_create
14
+ assert_nothing_raised { Program.new "__kernel fib(int x) { return 0; }"}
15
+ end
16
+
17
+ def test_program_compile
18
+ p = Program.new
19
+ assert_nothing_raised { p.compile "__kernel fib(int x) { }" }
20
+ end
21
+
22
+ def test_kernel_run
23
+ p = Program.new("__kernel x_y_z(int x) { }")
24
+ assert_raise(ArgumentError) { p.x_y_z }
25
+ end
26
+
27
+ def test_kernel_missing
28
+ p = Program.new("__kernel x_y_z(int x) { }")
29
+ assert_raise(NoMethodError) { p.not_x_y_z }
30
+ end
31
+
32
+ def test_program_implicit_array_buffer
33
+ p = Program.new <<-'eof'
34
+ __kernel copy(__global int *out, __global int *in) {
35
+ int i = get_global_id(0);
36
+ out[i] = in[i] + 1;
37
+ }
38
+ eof
39
+
40
+ out = Buffer.new(3)
41
+ p.copy(out, [1, 2, 3])
42
+ assert_equal [2, 3, 4], out
43
+ end
44
+
45
+ def test_program_types
46
+ arr = (1..5).to_a
47
+ outarr = arr.map {|x| x + 1 }
48
+ p = Program.new
49
+
50
+ TYPES.keys.each do |type|
51
+ # FIXME These types are currently broken (unimplemented in opencl?)
52
+ next if type == :bool
53
+
54
+ p.compile <<-eof
55
+ __kernel run(__global #{type} *out, __global #{type} *in) {
56
+ int id = get_global_id(0);
57
+ out[id] = in[id] + 1;
58
+ }
59
+ eof
60
+
61
+ out = Buffer.new(arr.size).to_type(type)
62
+ p.run(out, arr.to_type(type))
63
+ assert_equal({type => outarr}, {type => out})
64
+ end
65
+ end
66
+
67
+ def test_program_int_input_buffer
68
+ p = Program.new <<-'eof'
69
+ __kernel run(__global int* out, __global int* in) {
70
+ int id = get_global_id(0);
71
+ out[id] = in[id] + 1;
72
+ }
73
+ eof
74
+
75
+ input = (1..256).to_a
76
+ out = Buffer.new(input.size).to_type(:int)
77
+ p.run(out, input)
78
+ assert_equal input.map {|x| x + 1 }, out
79
+ end
80
+
81
+ def test_program_float_buffer
82
+ p = Program.new <<-'eof'
83
+ __kernel run(__global float* out, __global int* in) {
84
+ int id = get_global_id(0);
85
+ out[id] = (float)in[id] + 0.5;
86
+ }
87
+ eof
88
+
89
+ input = (1..256).to_a
90
+ out = Buffer.new(input.size).to_type(:float)
91
+ p.run(out, input)
92
+ assert_equal input.map {|x| x.to_f + 0.5 }, out
93
+ end
94
+
95
+ def test_program_set_times
96
+ p = Program.new <<-'eof'
97
+ __kernel sum(__global int* out, __global int* in) {
98
+ int id = get_global_id(0);
99
+ atom_add(out, in[id]);
100
+ }
101
+ eof
102
+
103
+ input = (1..517).to_a
104
+ sum = input.inject(0) {|acc, el| acc + el }
105
+ out = Buffer.new(1)
106
+ p.sum(out, input, :times => input.size)
107
+ assert_equal sum, out[0]
108
+ end
109
+
110
+ def test_program_largest_buffer_is_input
111
+ p = Program.new <<-'eof'
112
+ __kernel sum(__global int* out, __global int* in) {
113
+ int id = get_global_id(0);
114
+ atom_add(out, in[id]);
115
+ }
116
+ eof
117
+
118
+ input = (1..517).to_a
119
+ sum = input.inject(0) {|acc, el| acc + el }
120
+ out = Buffer.new(1)
121
+ p.sum(out, input)
122
+ assert_equal sum, out[0]
123
+ end
124
+
125
+ def test_program_invalid_times
126
+ p = Program.new("__kernel sum(int x) { }")
127
+ assert_raise(ArgumentError) { p.sum(:times => "hello") }
128
+ assert_raise(ArgumentError) { p.sum(:time => 1) }
129
+ end
130
+
131
+ def test_program_invalid_args
132
+ p = Program.new("__kernel sum(int x, __global int *y) { }")
133
+ assert_raise(ArgumentError) { p.sum(1, 2) }
134
+ assert_raise(ArgumentError) { p.sum(1, Buffer.new(1), 3) }
135
+ end
136
+
137
+ def test_program_vectors
138
+ p = Program.new <<-'eof'
139
+ __kernel copy_to_out(__global float4 *out, __global float4 *vec) {
140
+ out[0].x = vec[0].x + 0.5;
141
+ out[0].y = vec[0].y + 0.5;
142
+ out[0].z = vec[0].z + 0.5;
143
+ out[0].w = vec[0].w + 0.5;
144
+ }
145
+ eof
146
+
147
+ out = Buffer.new(4).to_type(:float)
148
+ p.copy_to_out(out, [2.5, 2.5, 2.5, 2.5])
149
+ assert_equal [3, 3, 3, 3], out
150
+ end
151
+
152
+ def test_program_no_total
153
+ p = Program.new <<-'eof'
154
+ __kernel copy(__global int *out, __global int *in) {
155
+ int i = get_global_id(0);
156
+ out[i] = in[i] + 1;
157
+ }
158
+ eof
159
+
160
+ out = Buffer.new(3)
161
+ p.copy(out, (1..3).to_a)
162
+ assert_equal (2..4).to_a, out
163
+
164
+ out = Buffer.new(50446)
165
+ p.copy(out, (1..50446).to_a)
166
+ assert_equal (2..50447).to_a, out
167
+ end
168
+
169
+ def test_program_returns_outvars_as_array
170
+ p = Program.new <<-'eof'
171
+ __kernel outbufs(__global int *a, __global int *b, int x, __global int *c) {
172
+ int i = get_global_id(0);
173
+ a[i] = x+1; b[i] = x+2; c[i] = x+3;
174
+ }
175
+ eof
176
+
177
+ a, b, c = Buffer.new(10), Buffer.new(10), Buffer.new(10)
178
+ result = p.outbufs(a, b, 5, c)
179
+ assert_equal [a, b, c], result
180
+ end
181
+
182
+ def test_program_returns_buffer_for_single_outvar
183
+ p = Program.new <<-'eof'
184
+ __kernel outbufs(__global int *a, __global int *b) {
185
+ int i = get_global_id(0);
186
+ a[i] = b[i];
187
+ }
188
+ eof
189
+
190
+ a, b = Buffer.new(5), (1..5).to_a
191
+ result = p.outbufs(a, b)
192
+ assert_equal a, result
193
+ end
194
+
195
+ def test_program_outvar
196
+ p = Program.new <<-'eof'
197
+ __kernel add5(__global int *data) {
198
+ int i = get_global_id(0);
199
+ data[i] = data[i] + 5;
200
+ }
201
+ eof
202
+
203
+ data = [1, 2, 3].outvar
204
+ assert_equal [6, 7, 8], p.add5(data)
205
+ end
206
+ end
@@ -0,0 +1,68 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../ext/')
2
+
3
+ require "test/unit"
4
+ require "barracuda"
5
+
6
+ include Barracuda
7
+
8
+ class TestDataTypes < Test::Unit::TestCase
9
+ def test_default_fixnum_type
10
+ assert_equal :int, 2.data_type
11
+ end
12
+
13
+ def test_default_bignum_type
14
+ assert_equal :long, (2**64).data_type
15
+ end
16
+
17
+ def test_default_float_type
18
+ assert_equal :float, 2.5.data_type
19
+ end
20
+
21
+ def test_default_array_type
22
+ assert_equal :int, [2].data_type
23
+ assert_equal :float, [2.5, 2.6].data_type
24
+ end
25
+
26
+ def test_set_array_type
27
+ assert_equal :uchar, [2].to_type(:uchar).data_type
28
+ end
29
+
30
+ def test_set_buffer_type
31
+ assert_equal :uchar, Buffer.new([2]).to_type(:uchar).data_type
32
+ end
33
+
34
+ def test_conversion_to_buffer_maintains_data_type
35
+ o = [2].to_type(:uchar)
36
+ assert_equal :uchar, Buffer.new(o).data_type
37
+ end
38
+
39
+ def test_set_data_type_fixnum
40
+ assert_equal :char, 2.to_type(:char).data_type
41
+ assert_equal :int, 2.data_type
42
+ end
43
+
44
+ def test_set_data_type
45
+ [2**64, 2.5, [2]].each do |v|
46
+ assert_equal :char, v.to_type(:char).data_type
47
+ end
48
+ end
49
+
50
+ def test_set_invalid_data_type
51
+ assert_raise(ArgumentError) { 1.to_type(:unknown) }
52
+ end
53
+
54
+ def test_invalid_array_data_type
55
+ assert_raise(TypeError) { [Object.new].data_type }
56
+ assert_raise(TypeError) { ['x'].data_type }
57
+ assert_raise(TypeError) { [].data_type }
58
+ end
59
+
60
+ def test_object_data_type
61
+ assert_nil Object.new.data_type
62
+ end
63
+
64
+ def test_type_class
65
+ assert_equal :long, Type.new(1).long.data_type
66
+ assert_equal :uchar, Type(1).uchar.data_type
67
+ end
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barracuda
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.2"
4
+ version: "1.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loren Segal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-03 00:00:00 -04:00
12
+ date: 2009-09-06 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,9 +25,10 @@ files:
25
25
  - ext/barracuda.c
26
26
  - ext/extconf.rb
27
27
  - benchmarks/normalize.rb
28
- - benchmarks/sort.rb
29
28
  - benchmarks/to_float.rb
30
- - test/test_barracuda.rb
29
+ - test/test_buffer.rb
30
+ - test/test_program.rb
31
+ - test/test_types.rb
31
32
  - LICENSE
32
33
  - README.md
33
34
  - Rakefile
@@ -60,4 +61,6 @@ signing_key:
60
61
  specification_version: 3
61
62
  summary: Barracuda is a wrapper library for OpenCL/CUDA GPGPU programming
62
63
  test_files:
63
- - test/test_barracuda.rb
64
+ - test/test_buffer.rb
65
+ - test/test_program.rb
66
+ - test/test_types.rb
@@ -1,29 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../ext')
2
-
3
- require 'barracuda'
4
- require 'benchmark'
5
-
6
- include Barracuda
7
-
8
- prog = Program.new <<-'eof'
9
- __kernel sort(__global int *out, __global int *in, int total) {
10
- int i, final_index = 0, extra = 0;
11
- int id = get_global_id(0);
12
- int my_value = in[id];
13
- for (i = 0; i < total; i++) {
14
- if (in[i] < my_value) final_index++;
15
- if (in[i] == my_value && i < id) extra++;
16
- }
17
- out[final_index+extra] = my_value;
18
- }
19
- eof
20
-
21
- max = 10000
22
- arr = (1..max).map { (rand * max).to_i }
23
- output = OutputBuffer.new(:int, arr.size)
24
-
25
- Benchmark.bm do |x|
26
- x.report("cpu") { arr.sort }
27
- x.report("gpu") { prog.sort(output, arr, arr.size) }
28
- end
29
-
@@ -1,291 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../ext/')
2
-
3
- require "test/unit"
4
- require "barracuda"
5
-
6
- include Barracuda
7
-
8
- class TestDataTypes < Test::Unit::TestCase
9
- def test_default_fixnum_type
10
- assert_equal :int, 2.data_type
11
- end
12
-
13
- def test_default_bignum_type
14
- assert_equal :long, (2**64).data_type
15
- end
16
-
17
- def test_default_float_type
18
- assert_equal :float, 2.5.data_type
19
- end
20
-
21
- def test_default_array_type
22
- assert_equal :int, [2].data_type
23
- assert_equal :float, [2.5, 2.6].data_type
24
- end
25
-
26
- def test_set_data_type_fixnum
27
- assert_equal :char, 2.to_type(:char).data_type
28
- assert_equal :int, 2.data_type
29
- end
30
-
31
- def test_set_data_type
32
- [2**64, 2.5, [2]].each do |v|
33
- assert_equal :char, v.to_type(:char).data_type
34
- end
35
- end
36
-
37
- def test_set_invalid_data_type
38
- assert_raise(ArgumentError) { 1.to_type(:unknown) }
39
- end
40
-
41
- def test_invalid_array_data_type
42
- assert_raise(RuntimeError) { [Object.new].data_type }
43
- assert_raise(RuntimeError) { ['x'].data_type }
44
- assert_raise(RuntimeError) { [].data_type }
45
- end
46
-
47
- def test_object_data_type
48
- assert_nil Object.new.data_type
49
- end
50
-
51
- def test_type_class
52
- assert_equal :long, Type.new(1).long.data_type
53
- assert_equal :uchar, Type(1).uchar.data_type
54
- end
55
- end
56
-
57
- class TestBuffer < Test::Unit::TestCase
58
- def test_buffer_create_no_data
59
- assert_raise(ArgumentError) { Buffer.new }
60
- end
61
-
62
- def test_buffer_create_invalid_data
63
- assert_raise(RuntimeError) { Buffer.new("xyz") }
64
- end
65
-
66
- def test_buffer_create_with_array
67
- b = Buffer.new([1, 2, 3, 4, 5])
68
- assert_equal [1, 2, 3, 4, 5], b.data
69
- end
70
-
71
- def test_buffer_create_with_splat
72
- b = Buffer.new(1.0, 2.0, 3.0)
73
- assert_equal [1.0, 2.0, 3.0], b.data
74
- end
75
-
76
- def test_buffer_set_data
77
- b = Buffer.new(1)
78
- b.data = [1, 2, 3]
79
- assert_equal 3, b.data.size
80
- end
81
-
82
- def test_buffer_read
83
- b = Buffer.new(4, 2, 3)
84
- b.data[0] = 1
85
- b.read
86
- assert_equal [4,2,3], b.data
87
- end
88
-
89
- def test_buffer_write
90
- b = Buffer.new(1, 2, 3)
91
- b.data[0] = 4
92
- b.write
93
- b.read
94
- assert_equal [4,2,3], b.data
95
- end
96
-
97
- def test_buffer_size_changed
98
- b = Buffer.new(1, 2, 3)
99
- b.data << 4
100
- b.size_changed
101
- b.read
102
- assert_equal [1,2,3,4], b.data
103
- end
104
- end
105
-
106
- class TestOutputBuffer < Test::Unit::TestCase
107
- def test_create_output_buffer_valid_types
108
- TYPES.keys.each do |type|
109
- assert_nothing_raised { OutputBuffer.new(type.to_s, 5) }
110
- end
111
- end
112
-
113
- def test_create_output_buffer_with_invalid_type
114
- assert_raise(ArgumentError) { OutputBuffer.new(:CHAR, 5) }
115
- end
116
-
117
- def test_create_output_buffer_with_invalid_size
118
- assert_raise(ArgumentError) { OutputBuffer.new(:int, 'x') }
119
- end
120
- end
121
-
122
- class TestProgram < Test::Unit::TestCase
123
- def test_program_create_invalid_code
124
- assert_raise(Barracuda::SyntaxError) { Program.new "fib { SYNTAXERROR }" }
125
- end
126
-
127
- def test_program_create
128
- assert_nothing_raised { Program.new "__kernel fib(int x) { return 0; }"}
129
- end
130
-
131
- def test_program_compile
132
- p = Program.new
133
- assert_nothing_raised { p.compile "__kernel fib(int x) { }" }
134
- end
135
-
136
- def test_kernel_run
137
- p = Program.new("__kernel x_y_z(int x) { }")
138
- assert_raise(ArgumentError) { p.x_y_z }
139
- end
140
-
141
- def test_kernel_missing
142
- p = Program.new("__kernel x_y_z(int x) { }")
143
- assert_raise(NoMethodError) { p.not_x_y_z }
144
- end
145
-
146
- def test_program_implicit_array_buffer
147
- p = Program.new <<-'eof'
148
- __kernel copy(__global int *out, __global int *in, int total) {
149
- int i = get_global_id(0);
150
- if (i < total) out[i] = in[i] + 1;
151
- }
152
- eof
153
-
154
- out = OutputBuffer.new(:int, 3)
155
- p.copy(out, [1, 2, 3], 3)
156
- assert_equal [2, 3, 4], out.data
157
- end
158
-
159
- def test_program_types
160
- arr = (1..5).to_a
161
- outarr = arr.map {|x| x + 1 }
162
- p = Program.new
163
-
164
- TYPES.keys.each do |type|
165
- # FIXME These types are currently broken (unimplemented in opencl?)
166
- next if type == :bool
167
- next if type == :double
168
- next if type == :size_t
169
- next if type == :ptrdiff_t
170
- next if type == :intptr_t
171
- next if type == :uintptr_t
172
-
173
- p.compile <<-eof
174
- __kernel run(__global #{type} *out, __global #{type} *in, int total) {
175
- int id = get_global_id(0);
176
- if (id < total) out[id] = in[id] + 1;
177
- }
178
- eof
179
-
180
- out = OutputBuffer.new(type, arr.size)
181
- p.run(out, arr.to_type(type), arr.size)
182
- assert_equal({type => outarr}, {type => out.data})
183
- end
184
- end
185
-
186
- def test_program_int_input_buffer
187
- p = Program.new <<-'eof'
188
- __kernel run(__global int* out, __global int* in, int total) {
189
- int id = get_global_id(0);
190
- if (id < total) out[id] = in[id] + 1;
191
- }
192
- eof
193
-
194
- arr = (1..256).to_a
195
- _in = Buffer.new(arr)
196
- out = OutputBuffer.new(:int, arr.size)
197
- p.run(out, _in, arr.size)
198
- assert_equal arr.map {|x| x + 1 }, out.data
199
- end
200
-
201
- def test_program_float_buffer
202
- p = Program.new <<-'eof'
203
- __kernel run(__global float* out, __global int* in, int total) {
204
- int id = get_global_id(0);
205
- if (id < total) out[id] = (float)in[id] + 0.5;
206
- }
207
- eof
208
-
209
- arr = (1..256).to_a
210
- _in = Buffer.new(arr)
211
- out = OutputBuffer.new(:float, arr.size)
212
- p.run(out, _in, arr.size)
213
- assert_equal arr.map {|x| x.to_f + 0.5 }, out.data
214
- end
215
-
216
- def test_program_set_times
217
- p = Program.new <<-'eof'
218
- __kernel sum(__global int* out, __global int* in, int total) {
219
- int id = get_global_id(0);
220
- if (id < total) atom_add(out, in[id]);
221
- }
222
- eof
223
-
224
- arr = (1..517).to_a
225
- sum = arr.inject(0) {|acc, el| acc + el }
226
- _in = Buffer.new(arr)
227
- out = OutputBuffer.new(:int, 1)
228
- p.sum(out, _in, arr.size, :times => arr.size)
229
- assert_equal sum, out.data[0]
230
- end
231
-
232
- def test_program_largest_buffer_is_input
233
- p = Program.new <<-'eof'
234
- __kernel sum(__global int* out, __global int* in, int total) {
235
- int id = get_global_id(0);
236
- if (id < total) atom_add(out, in[id]);
237
- }
238
- eof
239
-
240
- arr = (1..517).to_a
241
- sum = arr.inject(0) {|acc, el| acc + el }
242
- _in = Buffer.new(arr)
243
- out = OutputBuffer.new(:int, 1)
244
- p.sum(out, _in, arr.size)
245
- assert_equal sum, out.data[0]
246
- end
247
-
248
- def test_program_invalid_times
249
- p = Program.new("__kernel sum(int x) { }")
250
- assert_raise(ArgumentError) { p.sum(:times => "hello") }
251
- assert_raise(ArgumentError) { p.sum(:time => 1) }
252
- end
253
-
254
- def test_program_invalid_args
255
- p = Program.new("__kernel sum(int x, __global int *y) { }")
256
- assert_raise(ArgumentError) { p.sum(1, 2) }
257
- assert_raise(ArgumentError) { p.sum(1, OutputBuffer.new(:int, 1), 3) }
258
- end
259
-
260
- def test_program_vectors
261
- p = Program.new <<-'eof'
262
- __kernel copy_to_out(__global float4 *out, __global float4 *vec) {
263
- out[0].x = vec[0].x + 0.5;
264
- out[0].y = vec[0].y + 0.5;
265
- out[0].z = vec[0].z + 0.5;
266
- out[0].w = vec[0].w + 0.5;
267
- }
268
- eof
269
-
270
- out = OutputBuffer.new(:float, 4)
271
- p.copy_to_out(out, [2.5, 2.5, 2.5, 2.5])
272
- assert_equal [3, 3, 3, 3], out.data
273
- end
274
-
275
- def test_program_no_total
276
- p = Program.new <<-'eof'
277
- __kernel copy(__global int *out, __global int *in) {
278
- int i = get_global_id(0);
279
- out[i] = in[i] + 1;
280
- }
281
- eof
282
-
283
- out = OutputBuffer.new(:int, 3)
284
- p.copy(out, (1..3).to_a)
285
- assert_equal (2..4).to_a, out.data
286
-
287
- out = OutputBuffer.new(:int, 50446)
288
- p.copy(out, (1..50446).to_a)
289
- assert_equal (2..50447).to_a, out.data
290
- end
291
- end