Class: SGC::CU::CUContext

Inherits:
Object
  • Object
show all
Defined in:
lib/cuda/driver/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_versionInteger

Returns The API version used to create the current CUDA context.

Returns:

  • (Integer)

    The API version used to create the current CUDA context.



137
138
139
140
141
142
# File 'lib/cuda/driver/context.rb', line 137

def self.api_version
    p = FFI::MemoryPointer.new(:uint)
    status = API::cuCtxGetApiVersion(nil, p)
    Pvt::handle_error(status, "Failed to get the API version of the current CUDA context.")
    p.get_uint(0)
end

.cache_configCUFunctionCache

Returns The cache config of the current CUDA context.

Examples:

Get the cache config.

CUContext.cache_config    #=> :PREFER_NONE

Returns:



186
187
188
189
190
191
# File 'lib/cuda/driver/context.rb', line 186

def self.cache_config
    p = FFI::MemoryPointer.new(:enum)
    status = API::cuCtxGetCacheConfig(p)
    Pvt::handle_error(status, "Failed to get the current CUDA context cache config.")
    CUFunctionCache[API::read_enum(p)]
end

.cache_config=(conf) ⇒ Object

Set the cache to conf (CUFunctionCache) for the current CUDA context.

Examples:

Set the cache config to prefer shared.

CUContext.cache_config = :PREFER_SHARED    #=> :PREFER_SHARED


198
199
200
201
# File 'lib/cuda/driver/context.rb', line 198

def self.cache_config=(conf)
    status = API::cuCtxSetCacheConfig(conf)
    Pvt::handle_error(status, "Failed to set the current CUDA context cache config: config = #{conf}")
end

.create(device) ⇒ CUContext .create(flags, device) ⇒ CUContext

Create a new CUDA context with flags (CUContextFlags) and device (CUDevice), then associate it with the calling thread, and return the context.

Examples:

Create CUDA context with different flags.

dev = CUDevice.get(0)
CUContext.create(dev)                 #=> ctx
CUContext.create(0, dev)              #=> ctx
CUContext.create(:SCHED_SPIN, dev)    #=> ctx
CUContext.create([:SCHED_SPIN, :BLOCKING_SYNC], dev)    #=> ctx

Parameters:

  • flags (Integer, CUContextFlags, Array<Integer, CUContextFlags>)

    The list of flags to use for the CUDA context creation. Setting flags to 0 or ommitting flags uses SCHED_AUTO.

  • device (CUDevice)

    The device to create the CUDA context with.

Returns:

  • (CUContext)

    A CUDA context created with flags and device.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cuda/driver/context.rb', line 53

def self.create(arg1, arg2 = nil)
    if arg2 != nil
        flags, dev = arg1, arg2
        flags = CUContextFlags.value(flags)
    else
        flags = 0
        dev = arg1
    end

    p = FFI::MemoryPointer.new(:CUContext)
    status = API::cuCtxCreate(p, flags, dev.to_api)
    Pvt::handle_error(status, "Failed to create CUDA context: flags = #{flags}.")
    new(p)
end

.currentCUContext

Returns The CUDA context bound to the calling CPU thread.

Returns:

  • (CUContext)

    The CUDA context bound to the calling CPU thread.



102
103
104
105
106
107
# File 'lib/cuda/driver/context.rb', line 102

def self.current
    p = FFI::MemoryPointer.new(:CUContext)
    status = API::cuCtxGetCurrent(p)
    Pvt::handle_error(status, "Failed to get the current CUDA context.")
    new(p)
end

.current=(context) ⇒ Object

Set the current CUDA context to context.

Parameters:

  • The (CUContext)

    CUDA context to set as the current CUDA context.



112
113
114
115
# File 'lib/cuda/driver/context.rb', line 112

def self.current=(context)
    status = API::cuCtxSetCurrent(context.to_api)
    Pvt::handle_error(status, "Failed to set the current CUDA context.")
end

.deviceCUDevice

Returns The device associated to the current CUDA context.

Returns:

  • (CUDevice)

    The device associated to the current CUDA context.



146
147
148
149
150
151
# File 'lib/cuda/driver/context.rb', line 146

def self.device
    p = FFI::MemoryPointer.new(:CUDevice)
    status = API::cuCtxGetDevice(p)
    Pvt::handle_error(status, "Failed to get the current CUDA context's device.")
    CUDevice.send(:new, p)
end

.disable_peer_access(peer_context) ⇒ Class

Disable the current context from accessing the memory of the peer context.

Parameters:

  • peer_context (CUContext)

    The peer context.

Returns:

  • (Class)

    This class.

Since:

  • CUDA 4.0



239
240
241
242
243
# File 'lib/cuda/driver/context.rb', line 239

def self.disable_peer_access(peer_context)
    status = API::cuCtxDisablePeerAccess(peer_context.to_api)
    Pvt::handle_error(status, "Failed to disable peer access.")
    self
end

.enable_peer_access(peer_context, flags = 0) ⇒ Class

Enable the current context to access the memory of the peer context.

Parameters:

  • peer_context (CUContext)

    The peer context’s memory to be accessed.

  • flags (Integer) (defaults to: 0)

    Currently flags must be set to zero.

Returns:

  • (Class)

    This class.

Since:

  • CUDA 4.0



227
228
229
230
231
# File 'lib/cuda/driver/context.rb', line 227

def self.enable_peer_access(peer_context, flags = 0)
    status = API::cuCtxEnablePeerAccess(peer_context.to_api, flags)
    Pvt::handle_error(status, "Failed to enable peer access: flags = #{flags}.")
    self
end

.limit(lim) ⇒ Integer

Returns The limit lim (CULimit) of the current CUDA context.

Examples:

Get the stack size limit.

CUContext.limit(:STACK_SIZE)    #=> 8192

Parameters:

  • lim (CULimit)

    The particular limit attribute to query.

Returns:

  • (Integer)

    The limit lim (CULimit) of the current CUDA context.



159
160
161
162
163
164
# File 'lib/cuda/driver/context.rb', line 159

def self.limit(lim)
    p = FFI::MemoryPointer.new(:size_t)
    status = API::cuCtxGetLimit(p, lim)
    Pvt::handle_error(status, "Failed to get the current CUDA context limit: limit = #{lim}")
    API::read_size_t(p)
end

.limit=(*lim_val_pair) ⇒ Object

Set the limit lim (CULimit) of the current CUDA context to value.

Examples:

Set the stack size limit.

CUContext.limit = [:STACK_SIZE, 8192]    #=> [:STACK_SIZE, 8192]
CUContext.limit = :STACK_SIZE, 8192      #=> [:STACK_SIZE, 8192]

Parameters:

  • lim (CULimit)

    The particular limit attribute to set.

  • value (Integer)

    The value to set the limit to.



174
175
176
177
178
179
# File 'lib/cuda/driver/context.rb', line 174

def self.limit=(*lim_val_pair)
    lim, val = lim_val_pair.flatten
    lim != nil && val != nil or raise ArgumentError, "Invalid limit and value pair given: limit = #{lim}, value = #{val}."
    status = API::cuCtxSetLimit(lim, val)
    Pvt::handle_error(status, "Failed to set the current CUDA context limit: limit = #{lim}, value = #{val}")
end

.pop_currentObject

Pop the current CUDA context from the CUDA context stack, which becomes inactive.



205
206
207
208
209
210
# File 'lib/cuda/driver/context.rb', line 205

def self.pop_current
    p = FFI::MemoryPointer.new(:CUContext)
    status = API::cuCtxPopCurrent(p)
    Pvt::handle_error(status, "Failed to pop current context.")
    new(p)
end

.synchronizeObject

Block until all the tasks of the current CUDA context complete.



214
215
216
217
218
# File 'lib/cuda/driver/context.rb', line 214

def self.synchronize
    status = API::cuCtxSynchronize
    Pvt::handle_error(status, "Failed to synchronize the current context.")
    nil
end

Instance Method Details

#api_versionInteger

Returns The API version used to create this CUDA context.

Returns:

  • (Integer)

    The API version used to create this CUDA context.



128
129
130
131
132
133
# File 'lib/cuda/driver/context.rb', line 128

def api_version
    p = FFI::MemoryPointer.new(:uint)
    status = API::cuCtxGetApiVersion(self.to_api, p)
    Pvt::handle_error(status, "Failed to get the API version of this CUDA context.")
    p.get_uint(0)
end

#attachCUContext #attach(flags) ⇒ CUContext

Deprecated.

Increment the reference count on this CUDA context.

Parameters:

  • flags (Integer) (defaults to: 0)

    Currently flags must be set to zero.

Returns:



84
85
86
87
88
# File 'lib/cuda/driver/context.rb', line 84

def attach(flags = 0)
    status = API::cuCtxAttach(@pcontext, flags)
    Pvt::handle_error(status, "Failed to attach CUDA context: flags = #{flags}.")
    self
end

#destroyObject

Destroy this CUDA context.



70
71
72
73
74
# File 'lib/cuda/driver/context.rb', line 70

def destroy
    status = API::cuCtxDestroy(self.to_api)
    Pvt::handle_error(status, "Failed to destroy CUDA context.")
    nil
end

#detachObject

Deprecated.

Decrement the reference count on this CUDA context.



94
95
96
97
98
# File 'lib/cuda/driver/context.rb', line 94

def detach
    status = API::cuCtxDetach(self.to_api)
    Pvt::handle_error(status, "Failed to detach CUDA context.")
    nil
end

#push_currentCUContext

Push this CUDA context onto the CUDA context stack, which becomes currently active CUDA context.

Returns:



120
121
122
123
124
# File 'lib/cuda/driver/context.rb', line 120

def push_current
    status = API::cuCtxPushCurrent(self.to_api)
    Pvt::handle_error(status, "Failed to push this CUDA context.")
    self
end