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,51 @@
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 'cuda/runtime/ffi-cuda'
26
+
27
+
28
+ module SGC
29
+ module Cuda
30
+
31
+ # @return [Integer] The CUDA driver version.
32
+ def driver_version
33
+ p = FFI::MemoryPointer.new(:int)
34
+ status = API::cudaDriverGetVersion(p)
35
+ Pvt::handle_error(status, "Failed to query the CUDA driver version.")
36
+ p.read_int
37
+ end
38
+ module_function :driver_version
39
+
40
+
41
+ # @return [Integer] The CUDA Runtime version.
42
+ def runtime_version
43
+ p = FFI::MemoryPointer.new(:int)
44
+ status = API::cudaRuntimeGetVersion(p)
45
+ Pvt::handle_error(status, "Failed to query the CUDA Runtime version.")
46
+ p.read_int
47
+ end
48
+ module_function :runtime_version
49
+
50
+ end # module
51
+ end # module
@@ -0,0 +1,32 @@
1
+ require 'ffi'
2
+
3
+
4
+ module FFI
5
+
6
+ # This class is obtained from ffi-tk (https://github.com/Tass/ffi-tk).
7
+ class PrettyStruct < FFI::Struct
8
+ ACCESSOR_CODE = <<-CODE
9
+ def {name}; self[{sym}]; end
10
+ def {name}=(value) self[{sym}] = value; end
11
+ CODE
12
+
13
+ def self.layout(*kvs)
14
+ kvs.each_slice(2) do |key, value|
15
+ eval ACCESSOR_CODE.gsub(/\{(.*?)\}/, '{name}' => key, '{sym}' => ":#{key}")
16
+ end
17
+
18
+ super
19
+ end
20
+
21
+ def members
22
+ layout.members
23
+ end
24
+
25
+ def inspect
26
+ kvs = members.zip(values)
27
+ kvs.map!{|key, value| "%s=%s" % [key, value.inspect] }
28
+ "<%s %s>" % [self.class, kvs.join(' ')]
29
+ end
30
+ end
31
+
32
+ end # module
@@ -0,0 +1,82 @@
1
+ #
2
+ # Copyright (c) 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
+ module SGC
26
+ module Helper
27
+
28
+ # Provide methods for evaluating the composite value of flags.
29
+ # _self_ which _include/extend_ this module should implement {IEnum} interface.
30
+ module FlagsValue
31
+
32
+ # @param [Symbol, Integer, Array<Symbol, Integer>] *flags The list of flags to include in the returning value.
33
+ # @raise [ArgumentError] Invalid symbol or value.
34
+ # @return [Integer] The composite value of the _flags_ with respect to _self_.
35
+ #
36
+ # @example Compute the composite value of flags with multiple symbols and integers.
37
+ # CUEventFlags.symbols #=> [:DEFAULT, :BLOCKING_SYNC, :DISABLE_TIMING]
38
+ # CUEventFlags[:DEFAULT] #=> 0
39
+ # CUEventFlags[0] #=> :DEFAULT
40
+ # CUEventFlags[:BLOCKING_SYNC] #=> 1
41
+ # CUEventFlags[1] #=> :BLOCKING_SYNC
42
+ # CUEventFlags.value(:DISABLE_TIMING) #=> 2
43
+ # CUEventFlags.value(:BLOCKING_SYNC, :DISABLE_TIMING) #=> 3
44
+ # CUEventFlags.value([:BLOCKING_SYNC, :DISABLE_TIMING]) #=> 3
45
+ # CUEventFlags.value([1, :DISABLE_TIMING]) #=> 3
46
+ def value(*flags)
47
+ flags.empty? == false or raise ArgumentError, "No flags is provided. Expect Array<Symbol, Integer>, Symbol or Integer."
48
+
49
+ f = 0
50
+ flags.flatten.each do |x|
51
+ case x
52
+ when Symbol
53
+ v = self[x] or Pvt::raise_invalid_symbol(x)
54
+ f |= v
55
+ when Integer
56
+ self[x] or Pvt::raise_invalid_value(x)
57
+ f |= x
58
+ else
59
+ raise ArgumentError, "Invalid flags: #{x.to_s}. Expect Symbol or Integer in the flags array."
60
+ end
61
+ end
62
+ f
63
+ end
64
+
65
+
66
+ module Pvt
67
+
68
+ def raise_invalid_symbol(symbol)
69
+ raise ArgumentError, "Invalid flags symbol: #{symbol.to_s}. Expect symbol in #{self.symbols.to_s}."
70
+ end
71
+
72
+
73
+ def raise_invalid_value(value)
74
+ raise ArgumentError, "Invalid flags value: #{value.to_s}."
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end # module
82
+ end # module
@@ -0,0 +1,45 @@
1
+ #
2
+ # Copyright (c) 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
+ module SGC
26
+ module Helper
27
+
28
+ # @abstract An Enum interface.
29
+ # An enum maps a symbol to a value, and a value to a symbol.
30
+ module IEnum
31
+
32
+ # @return [Array] The list of valid symbols of this enum.
33
+ def symbols; raise NotImplementedError; end
34
+
35
+ # @param [Symbol, Object] key A symbol or value to use as a key to map.
36
+ # @return [Symbol, Object] The symbol or value that the _key_ maps to.
37
+ # * If the _key_ is a symbol, return the corresponding value.
38
+ # * If the _key_ is a value, return the corresponding symbol.
39
+ # * Return nil if the _key_ is invalid.
40
+ def [](key); raise NotImplementedError; end
41
+
42
+ end
43
+
44
+ end # module
45
+ end # module
@@ -0,0 +1,45 @@
1
+ #
2
+ # Copyright (c) 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
+ module SGC
26
+ module Helper
27
+
28
+ # @param [Class] klass The name of the class to obtain.
29
+ # @return [String] The class name of the class without module string.
30
+ #
31
+ # @example Obtain the class name of the class.
32
+ # module A
33
+ # module B
34
+ # class C
35
+ # end
36
+ # end
37
+ # end
38
+ # classname(A::B::C) #=> "C"
39
+ def classname(klass)
40
+ klass.name.gsub(/.*\:\:/, "")
41
+ end
42
+ module_function :classname
43
+
44
+ end # module
45
+ end # module
@@ -0,0 +1,125 @@
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 'ffi'
26
+
27
+ require 'memory/interface/ibuffer'
28
+ require 'memory/pointer'
29
+
30
+
31
+ module SGC
32
+ module Memory
33
+
34
+ # A memory buffer class which implements {IBuffer} interface.
35
+ # @see IBuffer
36
+ # @see IBuffer::ClassMethods
37
+ class Buffer
38
+
39
+ include IBuffer
40
+
41
+
42
+ # @param [Symbol] type A symbol corresponds to a supported C data type, e.g. :int, :long, :float.
43
+ # @param [Integer] size The number of elements.
44
+ # @return A buffer with _size_ elements of _type_.
45
+ def initialize(type, size)
46
+ @@reads[type] && @@writes[type] or raise "Invalid buffer element type."
47
+
48
+ @reader = @@reads[type]
49
+ @writer = @@writes[type]
50
+ @ptr = FFI::MemoryPointer.new(type, size)
51
+ @size = size
52
+ end
53
+
54
+
55
+ # @param [Integer] index The index (0..size-1) of the element to return.
56
+ # @return The element at _index_ of this buffer.
57
+ def [](index)
58
+ assert_index(index)
59
+ @ptr[index].send(@reader)
60
+ end
61
+
62
+
63
+ # Set the element at _index_ of this buffer to _value_.
64
+ # @param [Integer] index The index (0..size-1) of the element to set.
65
+ # @param [Object] value The value to set to.
66
+ # @return _value_.
67
+ def []=(index, value)
68
+ assert_index(index)
69
+ @ptr[index].send(@writer, value)
70
+ value
71
+ end
72
+
73
+
74
+ # @return [Integer] The number of elements in this buffer.
75
+ def size
76
+ @size
77
+ end
78
+
79
+
80
+ # @return [Integer] The size of an element in this buffer in bytes.
81
+ def element_size
82
+ @ptr.type_size
83
+ end
84
+
85
+
86
+ # @private
87
+ def ptr
88
+ @ptr
89
+ end
90
+
91
+
92
+ # @private
93
+ def to_api
94
+ @ptr
95
+ end
96
+
97
+
98
+ # @param [Integer] index The index to an element in this buffer.
99
+ # @return [MemoryPointer] A memory pointer pointing to the _index_ element.
100
+ def offset(index)
101
+ assert_index(index)
102
+ MemoryPointer.new(@ptr[index])
103
+ end
104
+
105
+
106
+ # @param [Symbol] type A symbol corresponds to a supported C data type, e.g. :int, :long, :float.
107
+ # @return [Integer] The size of an element of _type_.
108
+ def self.element_size(type)
109
+ @@sizes[type]
110
+ end
111
+
112
+ protected
113
+
114
+ def assert_index(i)
115
+ i >= 0 && i < @size or raise IndexError, "Invalid index to buffer: index = #{i}. Expect index in 0..#{@size-1}"
116
+ end
117
+
118
+ @@reads = { int: :read_int, long: :read_long, float: :read_float } # @private
119
+ @@writes = { int: :write_int, long: :write_long, float: :write_float } # @private
120
+ @@sizes = { int: 4, long: FFI::TypeDefs[:long].size, float: 4 } # @private
121
+
122
+ end
123
+
124
+ end # module
125
+ end # module
@@ -0,0 +1,63 @@
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
+ module SGC
26
+ module Memory
27
+
28
+ # @abstract A memory buffer interface.
29
+ # A buffer stores elements of the same C data type.
30
+ module IBuffer
31
+
32
+ # @param [Integer] index The index (0..size-1) of the element to return.
33
+ # @return The element at _index_ of this buffer.
34
+ def [](index); raise NotImplementedError; end
35
+
36
+ # Set the element at _index_ of this buffer to _value_.
37
+ # @param [Integer] index The index (0..size-1) of the element to set.
38
+ # @param [Object] value The value to set to.
39
+ # @return _value_.
40
+ def []=(index, value); raise NotImplementedError; end
41
+
42
+ # @return [Integer] The number of elements in this buffer.
43
+ def size; raise NotImplementedError; end
44
+
45
+ # @return [Integer] The size of an element in this buffer in bytes.
46
+ def element_size; raise NotImplementedError; end
47
+
48
+ # A set of methods automatically extended by the classes which _include_ _IBuffer_.
49
+ module ClassMethods
50
+ # @param [Symbol] type A symbol corresponds to a supported C data type, e.g. :int, :long, :float.
51
+ # @return [Integer] The size of an element of _type_.
52
+ def element_size(type); raise NotImplementedError; end
53
+ end
54
+
55
+ # @private
56
+ def self.included(base)
57
+ base.extend(ClassMethods)
58
+ end
59
+
60
+ end
61
+
62
+ end # module
63
+ end # module