sgc-ruby-cuda 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.yardopts +2 -0
  2. data/COPYING +674 -0
  3. data/README.rdoc +106 -0
  4. data/Rakefile +76 -0
  5. data/doc/devel.rdoc +77 -0
  6. data/doc/features.rdoc +55 -0
  7. data/lib/cuda/driver/context.rb +236 -0
  8. data/lib/cuda/driver/cu.rb +60 -0
  9. data/lib/cuda/driver/device.rb +155 -0
  10. data/lib/cuda/driver/deviceptr.rb +69 -0
  11. data/lib/cuda/driver/error.rb +182 -0
  12. data/lib/cuda/driver/event.rb +124 -0
  13. data/lib/cuda/driver/ffi-cu.rb +620 -0
  14. data/lib/cuda/driver/function.rb +293 -0
  15. data/lib/cuda/driver/init.rb +45 -0
  16. data/lib/cuda/driver/memory.rb +134 -0
  17. data/lib/cuda/driver/module.rb +142 -0
  18. data/lib/cuda/driver/rubycu.rb +37 -0
  19. data/lib/cuda/driver/stream.rb +128 -0
  20. data/lib/cuda/driver/version.rb +42 -0
  21. data/lib/cuda/runtime/cuda.rb +65 -0
  22. data/lib/cuda/runtime/device.rb +175 -0
  23. data/lib/cuda/runtime/error.rb +197 -0
  24. data/lib/cuda/runtime/event.rb +117 -0
  25. data/lib/cuda/runtime/ffi-cuda.rb +588 -0
  26. data/lib/cuda/runtime/function.rb +161 -0
  27. data/lib/cuda/runtime/memory.rb +110 -0
  28. data/lib/cuda/runtime/rubycuda.rb +34 -0
  29. data/lib/cuda/runtime/stream.rb +126 -0
  30. data/lib/cuda/runtime/thread.rb +81 -0
  31. data/lib/cuda/runtime/version.rb +51 -0
  32. data/lib/ffi/prettystruct.rb +32 -0
  33. data/lib/helpers/flags.rb +82 -0
  34. data/lib/helpers/interface/ienum.rb +45 -0
  35. data/lib/helpers/klass.rb +45 -0
  36. data/lib/memory/buffer.rb +125 -0
  37. data/lib/memory/interface/ibuffer.rb +63 -0
  38. data/lib/memory/pointer.rb +72 -0
  39. data/lib/rubycu.rb +1 -0
  40. data/lib/rubycuda.rb +1 -0
  41. data/test/bad.ptx +0 -0
  42. data/test/memory/test_buffer.rb +93 -0
  43. data/test/rubycu/test_cucontext.rb +148 -0
  44. data/test/rubycu/test_cudevice.rb +69 -0
  45. data/test/rubycu/test_cudeviceptr.rb +43 -0
  46. data/test/rubycu/test_cuevent.rb +81 -0
  47. data/test/rubycu/test_cufunction.rb +165 -0
  48. data/test/rubycu/test_cumemory.rb +113 -0
  49. data/test/rubycu/test_cumodule.rb +114 -0
  50. data/test/rubycu/test_custream.rb +77 -0
  51. data/test/rubycu/test_cuversion.rb +39 -0
  52. data/test/rubycu/testbase.rb +107 -0
  53. data/test/rubycuda/test_cudadevice.rb +125 -0
  54. data/test/rubycuda/test_cudaerror.rb +48 -0
  55. data/test/rubycuda/test_cudaevent.rb +78 -0
  56. data/test/rubycuda/test_cudafunction.rb +106 -0
  57. data/test/rubycuda/test_cudamemory.rb +90 -0
  58. data/test/rubycuda/test_cudastream.rb +72 -0
  59. data/test/rubycuda/test_cudathread.rb +69 -0
  60. data/test/rubycuda/test_cudaversion.rb +41 -0
  61. data/test/rubycuda/testbase.rb +67 -0
  62. data/test/vadd.cu +21 -0
  63. data/version.rb +1 -0
  64. metadata +180 -0
@@ -0,0 +1,165 @@
1
+ #-----------------------------------------------------------------------
2
+ # Copyright (c) 2010-2011 Chung Shin Yee
3
+ #
4
+ # shinyee@speedgocomputing.com
5
+ # http://www.speedgocomputing.com
6
+ # http://github.com/xman/sgc-ruby-cuda
7
+ # http://rubyforge.org/projects/rubycuda
8
+ #
9
+ # This file is part of SGC-Ruby-CUDA.
10
+ #
11
+ # SGC-Ruby-CUDA is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation, either version 3 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # SGC-Ruby-CUDA is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with SGC-Ruby-CUDA. If not, see <http://www.gnu.org/licenses/>.
23
+ #-----------------------------------------------------------------------
24
+
25
+ require 'test/unit'
26
+ require_relative 'testbase'
27
+
28
+
29
+ class TestCUFunction < Test::Unit::TestCase
30
+
31
+ include CUTestBase
32
+
33
+
34
+ def test_function_set_param
35
+ da = CUDevice.malloc(1024)
36
+ db = CUDevice.malloc(1024)
37
+ dc = CUDevice.malloc(1024)
38
+ @func.param = []
39
+ @func.param = [da, db, dc, 10]
40
+
41
+ f = @func.param_setf(0, 2.5)
42
+ assert_instance_of(CUFunction, f)
43
+ f = @func.param_seti(4, 33)
44
+ assert_instance_of(CUFunction, f)
45
+ v = Buffer.new(:int, 1)
46
+ f = @func.param_setv(0, v, 4)
47
+ assert_instance_of(CUFunction, f)
48
+ f = @func.param_set_size(8)
49
+ assert_instance_of(CUFunction, f)
50
+
51
+ da.free
52
+ db.free
53
+ dc.free
54
+ end
55
+
56
+
57
+ def test_function_set_block_shape
58
+ @func.block_shape = 2
59
+ @func.block_shape = [2, 3]
60
+ @func.block_shape = 2, 3, 4
61
+ end
62
+
63
+
64
+ def test_function_set_shared_size
65
+ @func.shared_size = 0
66
+ @func.shared_size = 1024
67
+ end
68
+
69
+
70
+ def test_function_launch
71
+ assert_function_launch(10) do |f, params|
72
+ f.param = params
73
+ f.block_shape = 10
74
+ f.launch
75
+ end
76
+ end
77
+
78
+
79
+ def test_function_launch_grid
80
+ assert_function_launch(10) do |f, params|
81
+ f.param = params
82
+ f.block_shape = 10
83
+ f.launch_grid(1)
84
+ end
85
+
86
+ assert_function_launch(20) do |f, params|
87
+ f.param = params
88
+ f.block_shape = 5
89
+ f.launch_grid(4)
90
+ end
91
+ end
92
+
93
+
94
+ def test_function_launch_kernel
95
+ assert_function_launch(30) do |f, params|
96
+ f.launch_kernel(3, 1, 1, 10, 1, 1, 0, 0, params)
97
+ end
98
+ end
99
+
100
+
101
+ def test_function_launch_async
102
+ assert_nothing_raised do
103
+ assert_function_launch(10) do |f, params|
104
+ f.param = params
105
+ f.block_shape = 10
106
+ f.launch_grid_async(1, 0)
107
+ end
108
+
109
+ assert_function_launch(20) do |f, params|
110
+ f.param = params
111
+ f.block_shape = 5
112
+ f.launch_grid_async(4, 0)
113
+ end
114
+ end
115
+ end
116
+
117
+
118
+ def test_function_get_attribute
119
+ CUFunctionAttribute.symbols.each do |k|
120
+ v = @func.attribute(k)
121
+ assert_instance_of(Fixnum, v)
122
+ end
123
+ end
124
+
125
+
126
+ def test_function_set_cache_config
127
+ CUFunctionCache.symbols.each do |k|
128
+ @func.cache_config = k
129
+ end
130
+ end
131
+
132
+ private
133
+
134
+ def assert_function_launch(size)
135
+ type = :int
136
+ element_size = 4
137
+ nbytes = size*element_size
138
+ da = CUDevice.malloc(nbytes)
139
+ db = CUDevice.malloc(nbytes)
140
+ dc = CUDevice.malloc(nbytes)
141
+ ha = Buffer.new(type, size)
142
+ hb = Buffer.new(type, size)
143
+ hc = Array.new(size)
144
+ hd = Buffer.new(type, size)
145
+ (0...size).each { |i|
146
+ ha[i] = i
147
+ hb[i] = 1
148
+ hc[i] = ha[i] + hb[i]
149
+ hd[i] = 0
150
+ }
151
+ CUMemory.memcpy_htod(da, ha, nbytes)
152
+ CUMemory.memcpy_htod(db, hb, nbytes)
153
+
154
+ yield @func, [da, db, dc, size]
155
+
156
+ CUMemory.memcpy_dtoh(hd, dc, nbytes)
157
+ (0...size).each { |i|
158
+ assert_equal(hc[i], hd[i])
159
+ }
160
+ da.free
161
+ db.free
162
+ dc.free
163
+ end
164
+
165
+ end
@@ -0,0 +1,113 @@
1
+ #-----------------------------------------------------------------------
2
+ # Copyright (c) 2010-2011 Chung Shin Yee
3
+ #
4
+ # shinyee@speedgocomputing.com
5
+ # http://www.speedgocomputing.com
6
+ # http://github.com/xman/sgc-ruby-cuda
7
+ # http://rubyforge.org/projects/rubycuda
8
+ #
9
+ # This file is part of SGC-Ruby-CUDA.
10
+ #
11
+ # SGC-Ruby-CUDA is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation, either version 3 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # SGC-Ruby-CUDA is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with SGC-Ruby-CUDA. If not, see <http://www.gnu.org/licenses/>.
23
+ #-----------------------------------------------------------------------
24
+
25
+ require 'test/unit'
26
+ require_relative 'testbase'
27
+
28
+
29
+ class TestCUMemory < Test::Unit::TestCase
30
+
31
+ include CUTestBase
32
+
33
+
34
+ def test_memory_copy
35
+ type = :int
36
+ size = 16
37
+ element_size = 4
38
+ nbytes = size*element_size
39
+ b = Buffer.new(type, size)
40
+ c = Buffer.new(type, size)
41
+ d = Buffer.new(type, size)
42
+ e = Buffer.new(type, size)
43
+ p = CUDevice.malloc(nbytes)
44
+ q = CUDevice.malloc(nbytes)
45
+ r = CUDevice.malloc(nbytes)
46
+
47
+ (0...size).each do |i|
48
+ b[i] = i
49
+ c[i] = 0
50
+ d[i] = 0
51
+ e[i] = 0
52
+ end
53
+
54
+ CUMemory.memcpy_htod(p, b, nbytes)
55
+ CUMemory.memcpy_dtoh(c, p, nbytes)
56
+ (0...size).each do |i|
57
+ assert_equal(b[i], c[i])
58
+ end
59
+
60
+ (0...size).each do |i|
61
+ b[i] = 2*i
62
+ c[i] = 0
63
+ end
64
+ CUMemory.memcpy_htod_async(p, b, nbytes, 0)
65
+ CUMemory.memcpy_dtoh_async(c, p, nbytes, 0)
66
+ CUContext.synchronize
67
+ (0...size).each do |i|
68
+ assert_equal(b[i], c[i])
69
+ end
70
+
71
+ CUMemory.memcpy_dtod(q, p, nbytes)
72
+ CUContext.synchronize
73
+ CUMemory.memcpy_dtoh(d, q, nbytes)
74
+ (0...size).each do |i|
75
+ assert_equal(b[i], d[i])
76
+ end
77
+
78
+ CUMemory.memcpy_dtod_async(r, p, size*element_size, 0)
79
+ CUContext.synchronize
80
+ CUMemory.memcpy_dtoh(e, r, nbytes)
81
+ (0...size).each do |i|
82
+ assert_equal(b[i], e[i])
83
+ end
84
+
85
+ if false # FIXME: The memcpy is not working.
86
+ if @dev.attribute(:UNIFIED_ADDRESSING) > 0
87
+ (0...size).each do |i|
88
+ b[i] = i
89
+ c[i] = 0
90
+ d[i] = 0
91
+ e[i] = 0
92
+ end
93
+ CUMemory.memcpy(p, b, nbytes)
94
+ CUMemory.memcpy(c, p, nbytes)
95
+ (0...size).each do |i|
96
+ assert_equal(b[i], c[i])
97
+ end
98
+ end
99
+ end
100
+
101
+ p.free
102
+ q.free
103
+ r.free
104
+ end
105
+
106
+
107
+ def test_memory_get_info
108
+ info = CUMemory.mem_info
109
+ assert(info[:free] >= 0)
110
+ assert(info[:total] >= 0)
111
+ end
112
+
113
+ end
@@ -0,0 +1,114 @@
1
+ #-----------------------------------------------------------------------
2
+ # Copyright (c) 2010-2011 Chung Shin Yee
3
+ #
4
+ # shinyee@speedgocomputing.com
5
+ # http://www.speedgocomputing.com
6
+ # http://github.com/xman/sgc-ruby-cuda
7
+ # http://rubyforge.org/projects/rubycuda
8
+ #
9
+ # This file is part of SGC-Ruby-CUDA.
10
+ #
11
+ # SGC-Ruby-CUDA is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation, either version 3 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # SGC-Ruby-CUDA is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with SGC-Ruby-CUDA. If not, see <http://www.gnu.org/licenses/>.
23
+ #-----------------------------------------------------------------------
24
+
25
+ require 'test/unit'
26
+ require_relative 'testbase'
27
+
28
+
29
+ class TestCUModule < Test::Unit::TestCase
30
+
31
+ include CUTestBase
32
+
33
+
34
+ def test_module_load_unload
35
+ path = prepare_ptx
36
+ m = CUModule.new
37
+ m = m.load(path)
38
+ assert_instance_of(CUModule, m)
39
+ m = m.unload
40
+ assert_instance_of(CUModule, m)
41
+ end
42
+
43
+
44
+ def test_module_load_with_invalid_ptx
45
+ assert_raise(CUInvalidImageError) do
46
+ m = CUModule.new
47
+ m = m.load('test/bad.ptx')
48
+ end
49
+
50
+ assert_raise(CUFileNotFoundError) do
51
+ m = CUModule.new
52
+ m = m.load('test/notexists.ptx')
53
+ end
54
+ end
55
+
56
+
57
+ def test_module_load_data
58
+ path = prepare_ptx
59
+ m = CUModule.new
60
+ File.open(path) do |f|
61
+ str = f.read
62
+ r = m.load_data(str)
63
+ assert_instance_of(CUModule, r)
64
+ m.unload
65
+ end
66
+ end
67
+
68
+
69
+ def test_module_load_data_with_invalid_ptx
70
+ assert_raise(CUInvalidImageError) do
71
+ m = CUModule.new
72
+ str = "invalid ptx"
73
+ m.load_data(str)
74
+ end
75
+ end
76
+
77
+
78
+ def test_module_get_function
79
+ f = @mod.function('vadd')
80
+ assert_instance_of(CUFunction, f)
81
+ end
82
+
83
+
84
+ def test_module_get_function_with_invalid_name
85
+ assert_raise(CUInvalidValueError) do
86
+ f = @mod.function('')
87
+ end
88
+
89
+ assert_raise(CUReferenceNotFoundError) do
90
+ f = @mod.function('badname')
91
+ end
92
+ end
93
+
94
+
95
+ def test_module_get_global
96
+ devptr, nbytes = @mod.global('gvar')
97
+ assert_equal(4, nbytes)
98
+ u = Buffer.new(:int, 1)
99
+ CUMemory.memcpy_dtoh(u, devptr, nbytes)
100
+ assert_equal(1997, u[0])
101
+ end
102
+
103
+
104
+ def test_module_get_global_with_invalid_name
105
+ assert_raise(CUInvalidValueError) do
106
+ devptr, nbytes = @mod.global('')
107
+ end
108
+
109
+ assert_raise(CUReferenceNotFoundError) do
110
+ devptr, nbytes = @mod.global('badname')
111
+ end
112
+ end
113
+
114
+ end
@@ -0,0 +1,77 @@
1
+ #-----------------------------------------------------------------------
2
+ # Copyright (c) 2010-2011 Chung Shin Yee
3
+ #
4
+ # shinyee@speedgocomputing.com
5
+ # http://www.speedgocomputing.com
6
+ # http://github.com/xman/sgc-ruby-cuda
7
+ # http://rubyforge.org/projects/rubycuda
8
+ #
9
+ # This file is part of SGC-Ruby-CUDA.
10
+ #
11
+ # SGC-Ruby-CUDA is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation, either version 3 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # SGC-Ruby-CUDA is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with SGC-Ruby-CUDA. If not, see <http://www.gnu.org/licenses/>.
23
+ #-----------------------------------------------------------------------
24
+
25
+ require 'test/unit'
26
+ require_relative 'testbase'
27
+
28
+
29
+ class TestCUStream < Test::Unit::TestCase
30
+
31
+ include CUTestBase
32
+
33
+
34
+ def test_stream_create_destroy
35
+ s = CUStream.create
36
+ assert_instance_of(CUStream, s)
37
+ s = s.destroy
38
+ assert_nil(s)
39
+
40
+ s = CUStream.create(0)
41
+ assert_instance_of(CUStream, s)
42
+ s = s.destroy
43
+ assert_nil(s)
44
+ end
45
+
46
+
47
+ def test_stream_query
48
+ s = CUStream.create
49
+ b = s.query
50
+ assert(b)
51
+ s.destroy
52
+ end
53
+
54
+
55
+ def test_stream_synchronize
56
+ s = CUStream.create
57
+ s = s.synchronize
58
+ assert_instance_of(CUStream, s)
59
+ s.destroy
60
+ end
61
+
62
+
63
+ def test_stream_wait_event
64
+ s = CUStream.create
65
+ e = CUEvent.create
66
+ s = s.wait_event(e)
67
+ assert_instance_of(CUStream, s)
68
+ s = s.wait_event(e, 0)
69
+ assert_instance_of(CUStream, s)
70
+ s.destroy
71
+ s = CUStream.wait_event(e)
72
+ assert_nil(s)
73
+ s = CUStream.wait_event(e, 0)
74
+ assert_nil(s)
75
+ end
76
+
77
+ end