sgc-ruby-cuda 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +2 -0
- data/COPYING +674 -0
- data/README.rdoc +106 -0
- data/Rakefile +76 -0
- data/doc/devel.rdoc +77 -0
- data/doc/features.rdoc +55 -0
- data/lib/cuda/driver/context.rb +236 -0
- data/lib/cuda/driver/cu.rb +60 -0
- data/lib/cuda/driver/device.rb +155 -0
- data/lib/cuda/driver/deviceptr.rb +69 -0
- data/lib/cuda/driver/error.rb +182 -0
- data/lib/cuda/driver/event.rb +124 -0
- data/lib/cuda/driver/ffi-cu.rb +620 -0
- data/lib/cuda/driver/function.rb +293 -0
- data/lib/cuda/driver/init.rb +45 -0
- data/lib/cuda/driver/memory.rb +134 -0
- data/lib/cuda/driver/module.rb +142 -0
- data/lib/cuda/driver/rubycu.rb +37 -0
- data/lib/cuda/driver/stream.rb +128 -0
- data/lib/cuda/driver/version.rb +42 -0
- data/lib/cuda/runtime/cuda.rb +65 -0
- data/lib/cuda/runtime/device.rb +175 -0
- data/lib/cuda/runtime/error.rb +197 -0
- data/lib/cuda/runtime/event.rb +117 -0
- data/lib/cuda/runtime/ffi-cuda.rb +588 -0
- data/lib/cuda/runtime/function.rb +161 -0
- data/lib/cuda/runtime/memory.rb +110 -0
- data/lib/cuda/runtime/rubycuda.rb +34 -0
- data/lib/cuda/runtime/stream.rb +126 -0
- data/lib/cuda/runtime/thread.rb +81 -0
- data/lib/cuda/runtime/version.rb +51 -0
- data/lib/ffi/prettystruct.rb +32 -0
- data/lib/helpers/flags.rb +82 -0
- data/lib/helpers/interface/ienum.rb +45 -0
- data/lib/helpers/klass.rb +45 -0
- data/lib/memory/buffer.rb +125 -0
- data/lib/memory/interface/ibuffer.rb +63 -0
- data/lib/memory/pointer.rb +72 -0
- data/lib/rubycu.rb +1 -0
- data/lib/rubycuda.rb +1 -0
- data/test/bad.ptx +0 -0
- data/test/memory/test_buffer.rb +93 -0
- data/test/rubycu/test_cucontext.rb +148 -0
- data/test/rubycu/test_cudevice.rb +69 -0
- data/test/rubycu/test_cudeviceptr.rb +43 -0
- data/test/rubycu/test_cuevent.rb +81 -0
- data/test/rubycu/test_cufunction.rb +165 -0
- data/test/rubycu/test_cumemory.rb +113 -0
- data/test/rubycu/test_cumodule.rb +114 -0
- data/test/rubycu/test_custream.rb +77 -0
- data/test/rubycu/test_cuversion.rb +39 -0
- data/test/rubycu/testbase.rb +107 -0
- data/test/rubycuda/test_cudadevice.rb +125 -0
- data/test/rubycuda/test_cudaerror.rb +48 -0
- data/test/rubycuda/test_cudaevent.rb +78 -0
- data/test/rubycuda/test_cudafunction.rb +106 -0
- data/test/rubycuda/test_cudamemory.rb +90 -0
- data/test/rubycuda/test_cudastream.rb +72 -0
- data/test/rubycuda/test_cudathread.rb +69 -0
- data/test/rubycuda/test_cudaversion.rb +41 -0
- data/test/rubycuda/testbase.rb +67 -0
- data/test/vadd.cu +21 -0
- data/version.rb +1 -0
- metadata +180 -0
data/README.rdoc
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
== Welcome to SGC-Ruby-CUDA
|
2
|
+
|
3
|
+
SGC-Ruby-CUDA implements Ruby bindings to Nvidia CUDA SDK. It provides easy
|
4
|
+
access to CUDA-enabled GPU from a Ruby program.
|
5
|
+
|
6
|
+
SGC-Ruby-CUDA is incomplete in many ways. Currently, it supports only some
|
7
|
+
crucial CUDA Driver and Runtime API functions. We hope to expand the coverage
|
8
|
+
as much as possible.
|
9
|
+
|
10
|
+
SGC-Ruby-CUDA is tested on 64bit Fedora 14. We are looking forward to support
|
11
|
+
Mac OSX. We have not tested it against 32bit Linux OS and Windows. We certainly
|
12
|
+
wish to improve the code base to support multiple platforms in the future.
|
13
|
+
We also welcome CUDA users to test it against their working environments.
|
14
|
+
|
15
|
+
Current development will focus on supporting CUDA Toolkit 4.0.
|
16
|
+
|
17
|
+
Check out {file:doc/features.rdoc} for the supported CUDA features.
|
18
|
+
Also see {file:doc/devel.rdoc} for the latest development plan.
|
19
|
+
|
20
|
+
|
21
|
+
Fedora and the Infinity design logo are trademarks of Red Hat, Inc.
|
22
|
+
|
23
|
+
Linux is a registered trademark of Linus Torvalds.
|
24
|
+
|
25
|
+
NVIDIA, the NVIDIA logo, CUDA, GeForce, Quadro, and Tesla are trademarks or
|
26
|
+
registered trademarks of NVIDIA Corporation in the U.S. and other countries.
|
27
|
+
|
28
|
+
Windows is a registered trademark of Microsoft Corporation in the United States
|
29
|
+
and other countries.
|
30
|
+
|
31
|
+
|
32
|
+
== Design philosophy
|
33
|
+
|
34
|
+
The Ruby bindings, Ruby CUDA API, which deal directly with Nvidia CUDA SDK C/C++
|
35
|
+
bindings retain the CUDA Driver and Runtime API in a systematic way whenever
|
36
|
+
possible. This facilitates developers familiar with CUDA C who should be able to
|
37
|
+
use SGC-Ruby-CUDA with minimum effort. At times, we may design the API to be
|
38
|
+
more Ruby-like and let go unnecessary API structure in modern programming
|
39
|
+
language.
|
40
|
+
|
41
|
+
We use Ruby-FFI as a bridge to call the CUDA C API. Ruby classes and methods to
|
42
|
+
are built on top. The use of Ruby-FFI would ease the support of multiple Ruby
|
43
|
+
interpretors.
|
44
|
+
|
45
|
+
|
46
|
+
== Prerequisites
|
47
|
+
|
48
|
+
* Ruby (Tested with Ruby 1.9.2)
|
49
|
+
* Ruby-FFI (Tested with Ruby-FFI 1.0.7)
|
50
|
+
* CUDA Toolkit (Tested with CUDA Toolkit 4.0)
|
51
|
+
* C++ compiler (Tested with GCC 4.6.0 but requires simple patch to CUDA Toolkit)
|
52
|
+
* Yard (Tested with Yard 0.6.7, required for generating documentations)
|
53
|
+
|
54
|
+
|
55
|
+
== How to get SGC-Ruby-CUDA
|
56
|
+
|
57
|
+
The SGC-Ruby-CUDA git repository can be found in the following:
|
58
|
+
|
59
|
+
http://github.com/xman/sgc-ruby-cuda
|
60
|
+
git://rubyforge.org/rubycuda.git
|
61
|
+
|
62
|
+
The master branch can be checked out with the following command:
|
63
|
+
|
64
|
+
git clone git://github.com/xman/sgc-ruby-cuda.git sgc-ruby-cuda
|
65
|
+
git clone git://rubyforge.org/rubycuda.git sgc-ruby-cuda
|
66
|
+
|
67
|
+
The devel* branches may be checked out for a peep on the latest development.
|
68
|
+
However, they are highly volatile and _rebase_ may be applied for cleaner
|
69
|
+
commits. The devel* branches serve as grace period before commits move into
|
70
|
+
mainstream or master branches which are considered stable commits. This would
|
71
|
+
minimize unnecessary fixing commits.
|
72
|
+
|
73
|
+
|
74
|
+
== Getting started
|
75
|
+
|
76
|
+
# Setup the environment. Assuming the CUDA Toolkit is installed in
|
77
|
+
# the default path /usr/local/cuda.
|
78
|
+
|
79
|
+
# For 64bit Linux:
|
80
|
+
export CPATH="/usr/local/cuda/include"
|
81
|
+
export LIBRARY_PATH="/usr/local/cuda/lib64"
|
82
|
+
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
|
83
|
+
export PATH="/usr/local/cuda/bin:$PATH"
|
84
|
+
|
85
|
+
gem install ffi
|
86
|
+
cd sgc-ruby-cuda
|
87
|
+
rake test
|
88
|
+
# Check out the test cases in test/ on how to use Ruby CUDA API.
|
89
|
+
|
90
|
+
gem install yard
|
91
|
+
cd sgc-ruby-cuda
|
92
|
+
rake yard
|
93
|
+
# Check out the generated documentations in html/index.html with a browser.
|
94
|
+
|
95
|
+
|
96
|
+
== License
|
97
|
+
|
98
|
+
SGC-Ruby-CUDA is released under the GNU GPLv3. See the file COPYING.
|
99
|
+
|
100
|
+
|
101
|
+
== The Author
|
102
|
+
|
103
|
+
All kinds of feedbacks and bug reports are welcomed. Here is the author's email
|
104
|
+
address:
|
105
|
+
|
106
|
+
shinyee@speedgocomputing.com
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
require 'yard'
|
6
|
+
|
7
|
+
load 'version.rb'
|
8
|
+
|
9
|
+
CUDA_PATH = "lib/cuda"
|
10
|
+
CUDA_DRIVER_PATH = "#{CUDA_PATH}/driver"
|
11
|
+
CUDA_RUNTIME_PATH = "#{CUDA_PATH}/runtime"
|
12
|
+
DOC_PATH = "doc"
|
13
|
+
HTML_OUTPUT_PATH = "html"
|
14
|
+
|
15
|
+
|
16
|
+
task :default => []
|
17
|
+
|
18
|
+
desc 'Build everything.'
|
19
|
+
task :all => [:package, :yard]
|
20
|
+
|
21
|
+
|
22
|
+
spec = Gem::Specification.new do |s|
|
23
|
+
s.platform = Gem::Platform::RUBY
|
24
|
+
s.name = 'sgc-ruby-cuda'
|
25
|
+
s.version = SGC_RUBY_CUDA_VERSION
|
26
|
+
s.summary = 'Ruby bindings for using Nvidia CUDA.'
|
27
|
+
s.description = 'SGC-Ruby-CUDA implements Ruby bindings to Nvidia CUDA SDK. It provides easy access to CUDA-enabled GPU from a Ruby program.'
|
28
|
+
|
29
|
+
s.required_ruby_version = '>= 1.9.2'
|
30
|
+
|
31
|
+
s.author = 'Chung Shin Yee'
|
32
|
+
s.email = 'shinyee@speedgocomputing.com'
|
33
|
+
s.homepage = 'https://rubyforge.org/projects/rubycuda'
|
34
|
+
s.rubyforge_project = 'rubycuda'
|
35
|
+
|
36
|
+
s.require_path = 'lib'
|
37
|
+
|
38
|
+
s.files = FileList['lib/**/*.rb', "#{DOC_PATH}/**/*.rdoc"].to_a
|
39
|
+
s.files += ['Rakefile', 'version.rb', 'README.rdoc', 'COPYING']
|
40
|
+
s.files += ['.yardopts']
|
41
|
+
s.test_files = FileList['test/{**/*.rb,vadd.cu,bad.ptx}'].to_a
|
42
|
+
|
43
|
+
s.add_dependency 'ffi', '>= 1.0.7'
|
44
|
+
s.add_dependency 'yard', '>= 0.6.7'
|
45
|
+
|
46
|
+
s.requirements << 'CUDA Toolkit 4.0'
|
47
|
+
s.requirements << 'C++ compiler'
|
48
|
+
s.requirements << 'CUDA-enabled GPU'
|
49
|
+
end
|
50
|
+
|
51
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
52
|
+
pkg.need_tar_gz = true
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
desc 'Generate SGC Ruby CUDA documentation with YARD.'
|
57
|
+
task :yard
|
58
|
+
YARD::Rake::YardocTask.new do |t|
|
59
|
+
t.files = FileList['lib/**/*.rb'].to_a
|
60
|
+
t.options += ['-o', "#{HTML_OUTPUT_PATH}"]
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
desc 'Run SGC Ruby CUDA test cases.'
|
65
|
+
task :test
|
66
|
+
Rake::TestTask.new do |t|
|
67
|
+
t.libs << 'lib'
|
68
|
+
|
69
|
+
t.test_files = FileList['test/**/test_*.rb'].to_a
|
70
|
+
t.verbose = true
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
CLEAN.include ['pkg', "#{HTML_OUTPUT_PATH}"]
|
75
|
+
CLEAN.include ['**/*.o', '**/*.so']
|
76
|
+
CLEAN.include ['test/vadd.ptx']
|
data/doc/devel.rdoc
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
= Development Plan
|
2
|
+
SGC-Ruby-CUDA development plan provides an outline of features, issues, etc.
|
3
|
+
that will be tackled in order. There is currently no strict timeline on when
|
4
|
+
these features or issues will be covered. We hope at least we are progressing
|
5
|
+
consistently and catching up the development of Nvidia CUDA SDK.
|
6
|
+
|
7
|
+
|
8
|
+
== Creating Ruby bindings for using CUDA Driver API on Linux Platform
|
9
|
+
|
10
|
+
=== On-going
|
11
|
+
* Include Ruby bindings for the use of CUDA compiler to compile a .cu file to
|
12
|
+
generate a .ptx file.
|
13
|
+
* Supporting CUDA Toolkit 4.0.
|
14
|
+
|
15
|
+
=== Todo
|
16
|
+
* Port some CUDA samples to SGC-Ruby-CUDA with benchmark.
|
17
|
+
* Develop sample programs.
|
18
|
+
|
19
|
+
|
20
|
+
== Creating Ruby bindings for using CUDA Driver API on Mac Platform
|
21
|
+
|
22
|
+
=== On-going
|
23
|
+
|
24
|
+
=== Todo
|
25
|
+
|
26
|
+
|
27
|
+
== Towards robust development of Ruby CUDA Driver API
|
28
|
+
|
29
|
+
=== On-going
|
30
|
+
|
31
|
+
=== Todo
|
32
|
+
* Update the memory abstraction or parameter passing.
|
33
|
+
* To allow one to specify a paramater as a float or double, int or long, etc.
|
34
|
+
* To provide memory buffers for more data types?
|
35
|
+
|
36
|
+
|
37
|
+
== Creating Ruby bindings for using CUDA Runtime API on Linux Platform
|
38
|
+
|
39
|
+
=== On-going
|
40
|
+
* Supporting CUDA Toolkit 4.0.
|
41
|
+
|
42
|
+
=== Todo
|
43
|
+
* Develop sample programs.
|
44
|
+
|
45
|
+
|
46
|
+
== Creating Ruby bindings for using CUDA Runtime API on Mac Platform
|
47
|
+
|
48
|
+
=== On-going
|
49
|
+
|
50
|
+
=== Todo
|
51
|
+
|
52
|
+
|
53
|
+
== Towards portable platform - supporting Linux, Mac, Windows
|
54
|
+
|
55
|
+
=== Todo
|
56
|
+
* Support portable compilations.
|
57
|
+
* Support portable paths?
|
58
|
+
* Configurable tools? compiler commands, flags, etc.
|
59
|
+
* Release SGC-Ruby-CUDA gems for multiple platforms.
|
60
|
+
|
61
|
+
|
62
|
+
== Development of generic kernel programs
|
63
|
+
|
64
|
+
=== Todo
|
65
|
+
* Identify interesting sample kernel programs bundled with CUDA Toolkit, pycuda,
|
66
|
+
ruby-opencl, etc.
|
67
|
+
* Identify open source kernels available on the web.
|
68
|
+
* Adopt or develop kernel programs.
|
69
|
+
* Develop tests and benchmark programs for the kernels.
|
70
|
+
* Optimizing the kernel performance.
|
71
|
+
|
72
|
+
|
73
|
+
== Development of benchmarking suite
|
74
|
+
|
75
|
+
=== Todo
|
76
|
+
* Identify existing benchmarking suites for GPU.
|
77
|
+
* Porting existing CUDA kernels and benchmark programs to SGC-Ruby-CUDA.
|
data/doc/features.rdoc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
== Supported CUDA 4.0 Driver API Modules
|
2
|
+
|
3
|
+
* Fully supported (excluding deprecated functions).
|
4
|
+
+ Partially supported.
|
5
|
+
- Not supported.
|
6
|
+
|
7
|
+
Feature Supported?
|
8
|
+
------------------------------------------
|
9
|
+
Version Management *
|
10
|
+
Device Management *
|
11
|
+
Context Management *
|
12
|
+
Module Management +
|
13
|
+
Memory Management +
|
14
|
+
Unified Addressing -
|
15
|
+
Peer Context Memory Access -
|
16
|
+
Execution Control +
|
17
|
+
Stream Management *
|
18
|
+
Event Management *
|
19
|
+
Texture Reference Management -
|
20
|
+
Surface Reference Management -
|
21
|
+
Graphics Interoperability -
|
22
|
+
OpenGL Interoperability -
|
23
|
+
Direct3D 9 Interoperability -
|
24
|
+
Direct3D 10 Interoperability -
|
25
|
+
Direct3D 11 Interoperability -
|
26
|
+
VDPAU Interoperability -
|
27
|
+
|
28
|
+
|
29
|
+
== Supported CUDA 4.0 Runtime API Modules
|
30
|
+
|
31
|
+
* Fully supported (excluding deprecated functions).
|
32
|
+
+ Partially supported.
|
33
|
+
- Not supported.
|
34
|
+
|
35
|
+
Feature Supported?
|
36
|
+
------------------------------------------
|
37
|
+
Version Management *
|
38
|
+
Error Handling *
|
39
|
+
Device Management +
|
40
|
+
Thread Management *
|
41
|
+
Memory Management +
|
42
|
+
Unified Addressing -
|
43
|
+
Peer Device Memory Access -
|
44
|
+
Execution Control +
|
45
|
+
Stream Management *
|
46
|
+
Event Management *
|
47
|
+
Texture Reference Management -
|
48
|
+
Surface Reference Management -
|
49
|
+
C++ API Routines -
|
50
|
+
Graphics Interoperability -
|
51
|
+
OpenGL Interoperability -
|
52
|
+
Direct3D 9 Interoperability -
|
53
|
+
Direct3D 10 Interoperability -
|
54
|
+
Direct3D 11 Interoperability -
|
55
|
+
VDPAU Interoperability -
|
@@ -0,0 +1,236 @@
|
|
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
|
+
require 'cuda/driver/ffi-cu'
|
26
|
+
require 'cuda/driver/cu'
|
27
|
+
require 'cuda/driver/error'
|
28
|
+
require 'helpers/flags'
|
29
|
+
|
30
|
+
|
31
|
+
module SGC
|
32
|
+
module CU
|
33
|
+
|
34
|
+
class CUContext
|
35
|
+
|
36
|
+
# Create a new CUDA context with _flags_ (CUContextFlags) and _device_ (CUDevice),
|
37
|
+
# then associate it with the calling thread, and return the context.
|
38
|
+
#
|
39
|
+
# @overload create(device)
|
40
|
+
# @overload create(flags, device)
|
41
|
+
# @param [Integer, CUContextFlags, Array<Integer, CUContextFlags>] flags
|
42
|
+
# The list of flags to use for the CUDA context creation.
|
43
|
+
# Setting _flags_ to 0 or ommitting _flags_ uses SCHED_AUTO.
|
44
|
+
# @param [CUDevice] device The device to create the CUDA context with.
|
45
|
+
# @return [CUContext] A CUDA context created with _flags_ and _device_.
|
46
|
+
#
|
47
|
+
# @example Create CUDA context with different flags.
|
48
|
+
# dev = CUDevice.get(0)
|
49
|
+
# CUContext.create(dev) #=> ctx
|
50
|
+
# CUContext.create(0, dev) #=> ctx
|
51
|
+
# CUContext.create(:SCHED_SPIN, dev) #=> ctx
|
52
|
+
# CUContext.create([:SCHED_SPIN, :BLOCKING_SYNC], dev) #=> ctx
|
53
|
+
def self.create(arg1, arg2 = nil)
|
54
|
+
if arg2 != nil
|
55
|
+
flags, dev = arg1, arg2
|
56
|
+
flags = CUContextFlags.value(flags)
|
57
|
+
else
|
58
|
+
flags = 0
|
59
|
+
dev = arg1
|
60
|
+
end
|
61
|
+
|
62
|
+
p = FFI::MemoryPointer.new(:CUContext)
|
63
|
+
status = API::cuCtxCreate(p, flags, dev.to_api)
|
64
|
+
Pvt::handle_error(status, "Failed to create CUDA context: flags = #{flags}.")
|
65
|
+
new(p)
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
# Destroy this CUDA context.
|
70
|
+
def destroy
|
71
|
+
status = API::cuCtxDestroy(self.to_api)
|
72
|
+
Pvt::handle_error(status, "Failed to destroy CUDA context.")
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
# @deprecated
|
78
|
+
#
|
79
|
+
# Increment the reference count on this CUDA context.
|
80
|
+
# @overload attach
|
81
|
+
# @overload attach(flags)
|
82
|
+
# @param [Integer] flags Currently _flags_ must be set to zero.
|
83
|
+
# @return [CUContext] This CUDA context.
|
84
|
+
def attach(flags = 0)
|
85
|
+
status = API::cuCtxAttach(@pcontext, flags)
|
86
|
+
Pvt::handle_error(status, "Failed to attach CUDA context: flags = #{flags}.")
|
87
|
+
self
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
# @deprecated
|
92
|
+
#
|
93
|
+
# Decrement the reference count on this CUDA context.
|
94
|
+
def detach
|
95
|
+
status = API::cuCtxDetach(self.to_api)
|
96
|
+
Pvt::handle_error(status, "Failed to detach CUDA context.")
|
97
|
+
nil
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
# @return [CUContext] The CUDA context bound to the calling CPU thread.
|
102
|
+
def self.current
|
103
|
+
p = FFI::MemoryPointer.new(:CUContext)
|
104
|
+
status = API::cuCtxGetCurrent(p)
|
105
|
+
Pvt::handle_error(status, "Failed to get the current CUDA context.")
|
106
|
+
new(p)
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# Set the current CUDA context to _context_.
|
111
|
+
# @param [CUContext] The CUDA context to set as the current CUDA context.
|
112
|
+
def self.current=(context)
|
113
|
+
status = API::cuCtxSetCurrent(context.to_api)
|
114
|
+
Pvt::handle_error(status, "Failed to set the current CUDA context.")
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
# Push this CUDA context onto the CUDA context stack, which becomes currently active CUDA context.
|
119
|
+
# @return [CUContext] This CUDA context.
|
120
|
+
def push_current
|
121
|
+
status = API::cuCtxPushCurrent(self.to_api)
|
122
|
+
Pvt::handle_error(status, "Failed to push this CUDA context.")
|
123
|
+
self
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
# @return [Integer] The API version used to create this CUDA context.
|
128
|
+
def api_version
|
129
|
+
p = FFI::MemoryPointer.new(:uint)
|
130
|
+
status = API::cuCtxGetApiVersion(self.to_api, p)
|
131
|
+
Pvt::handle_error(status, "Failed to get the API version of this CUDA context.")
|
132
|
+
p.get_uint(0)
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# @return [Integer] The API version used to create the current CUDA context.
|
137
|
+
def self.api_version
|
138
|
+
p = FFI::MemoryPointer.new(:uint)
|
139
|
+
status = API::cuCtxGetApiVersion(nil, p)
|
140
|
+
Pvt::handle_error(status, "Failed to get the API version of the current CUDA context.")
|
141
|
+
p.get_uint(0)
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
# @return [CUDevice] The device associated to the current CUDA context.
|
146
|
+
def self.device
|
147
|
+
p = FFI::MemoryPointer.new(:CUDevice)
|
148
|
+
status = API::cuCtxGetDevice(p)
|
149
|
+
Pvt::handle_error(status, "Failed to get the current CUDA context's device.")
|
150
|
+
CUDevice.send(:new, p)
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
# @param [CULimit] lim The particular limit attribute to query.
|
155
|
+
# @return [Integer] The limit _lim_ (CULimit) of the current CUDA context.
|
156
|
+
#
|
157
|
+
# @example Get the stack size limit.
|
158
|
+
# CUContext.limit(:STACK_SIZE) #=> 8192
|
159
|
+
def self.limit(lim)
|
160
|
+
p = FFI::MemoryPointer.new(:size_t)
|
161
|
+
status = API::cuCtxGetLimit(p, lim)
|
162
|
+
Pvt::handle_error(status, "Failed to get the current CUDA context limit: limit = #{lim}")
|
163
|
+
API::read_size_t(p)
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
# Set the limit _lim_ (CULimit) of the current CUDA context to _value_.
|
168
|
+
# @param [CULimit] lim The particular limit attribute to set.
|
169
|
+
# @param [Integer] value The value to set the limit to.
|
170
|
+
#
|
171
|
+
# @example Set the stack size limit.
|
172
|
+
# CUContext.limit = [:STACK_SIZE, 8192] #=> [:STACK_SIZE, 8192]
|
173
|
+
# CUContext.limit = :STACK_SIZE, 8192 #=> [:STACK_SIZE, 8192]
|
174
|
+
def self.limit=(*lim_val_pair)
|
175
|
+
lim, val = lim_val_pair.flatten
|
176
|
+
lim != nil && val != nil or raise ArgumentError, "Invalid limit and value pair given: limit = #{lim}, value = #{val}."
|
177
|
+
status = API::cuCtxSetLimit(lim, val)
|
178
|
+
Pvt::handle_error(status, "Failed to set the current CUDA context limit: limit = #{lim}, value = #{val}")
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
# @return [CUFunctionCache] The cache config of the current CUDA context.
|
183
|
+
#
|
184
|
+
# @example Get the cache config.
|
185
|
+
# CUContext.cache_config #=> :PREFER_NONE
|
186
|
+
def self.cache_config
|
187
|
+
p = FFI::MemoryPointer.new(:enum)
|
188
|
+
status = API::cuCtxGetCacheConfig(p)
|
189
|
+
Pvt::handle_error(status, "Failed to get the current CUDA context cache config.")
|
190
|
+
CUFunctionCache[API::read_enum(p)]
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
# Set the cache to _conf_ (CUFunctionCache) for the current CUDA context.
|
195
|
+
#
|
196
|
+
# @example Set the cache config to prefer shared.
|
197
|
+
# CUContext.cache_config = :PREFER_SHARED #=> :PREFER_SHARED
|
198
|
+
def self.cache_config=(conf)
|
199
|
+
status = API::cuCtxSetCacheConfig(conf)
|
200
|
+
Pvt::handle_error(status, "Failed to set the current CUDA context cache config: config = #{conf}")
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
# Pop the current CUDA context from the CUDA context stack, which becomes inactive.
|
205
|
+
def self.pop_current
|
206
|
+
p = FFI::MemoryPointer.new(:CUContext)
|
207
|
+
status = API::cuCtxPopCurrent(p)
|
208
|
+
Pvt::handle_error(status, "Failed to pop current context.")
|
209
|
+
new(p)
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
# Block until all the tasks of the current CUDA context complete.
|
214
|
+
def self.synchronize
|
215
|
+
status = API::cuCtxSynchronize
|
216
|
+
Pvt::handle_error(status, "Failed to synchronize the current context.")
|
217
|
+
nil
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
# @private
|
222
|
+
def initialize(ptr)
|
223
|
+
@pcontext = ptr
|
224
|
+
end
|
225
|
+
private_class_method(:new)
|
226
|
+
|
227
|
+
|
228
|
+
# @private
|
229
|
+
def to_api
|
230
|
+
API::read_cucontext(@pcontext)
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end # module
|
236
|
+
end # module
|