Class DriverAPINativeMethods.ContextManagement
Combines all API calls for context management
Inheritance
Inherited Members
Namespace: ManagedCuda
Assembly: ManagedCuda.dll
Syntax
public static class ContextManagement
Methods
cuCtxAttach(ref CUcontext, CUCtxAttachFlags)
Increments the usage count of the context and passes back a context handle in pctx
that must be passed to cuCtxDetach(CUcontext)
when the application is done with the context. cuCtxAttach(ref CUcontext, CUCtxAttachFlags) fails if there is no context current to the
thread. Currently, the flags
parameter must be None.
Declaration
[Obsolete("Don't use this CUDA API call with CUDA version >= 4.0.")]
public static CUResult cuCtxAttach(ref CUcontext pctx, CUCtxAttachFlags flags)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned context handle of the current context |
CUCtxAttachFlags | flags | Context attach flags (must be None) |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxCreate_v2(ref CUcontext, CUCtxFlags, CUdevice)
Creates a new CUDA context and associates it with the calling thread. The flags
parameter is described in CUCtxFlags. The
context is created with a usage count of 1 and the caller of cuCtxCreate_v2(ref CUcontext, CUCtxFlags, CUdevice) must call cuCtxDestroy(CUcontext) or cuCtxDetach(CUcontext)
when done using the context. If a context is already current to the thread, it is supplanted by the newly created context
and may be restored by a subsequent call to cuCtxPopCurrent(ref CUcontext).
Declaration
public static CUResult cuCtxCreate_v2(ref CUcontext pctx, CUCtxFlags flags, CUdevice dev)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned context handle of the new context |
CUCtxFlags | flags | Context creation flags. See CUCtxFlags |
CUdevice | dev | Device to create context on |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue, ErrorInvalidDevice, ErrorOutOfMemory, ErrorUnknown.
|
cuCtxDestroy(CUcontext)
Destroys the CUDA context specified by ctx
. If the context usage count is not equal to 1, or the context is current
to any CPU thread other than the current one, this function fails. Floating contexts (detached from a CPU thread via
cuCtxPopCurrent(ref CUcontext)) may be destroyed by this function.
Declaration
[Obsolete("Don't use this CUDA API call with CUDA version >= 4.0.")]
public static CUResult cuCtxDestroy(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Context to destroy |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxDestroy_v2(CUcontext)
Destroys the CUDA context specified by ctx
. The context ctx
will be destroyed regardless of how many threads it is current to.
It is the responsibility of the calling function to ensure that no API call is issued to ctx
while cuCtxDestroy_v2() is executing.
If ctx
is current to the calling thread then ctx
will also be
popped from the current thread's context stack (as though cuCtxPopCurrent()
were called). If ctx
is current to other threads, then ctx
will
remain current to those threads, and attempting to access ctx
from
those threads will result in the error ErrorContextIsDestroyed.
Declaration
public static CUResult cuCtxDestroy_v2(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Context to destroy |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue, ErrorContextIsDestroyed.
|
cuCtxDetach(CUcontext)
Decrements the usage count of the context ctx
, and destroys the context if the usage count goes to 0. The context
must be a handle that was passed back by cuCtxCreate_v2(ref CUcontext, CUCtxFlags, CUdevice) or cuCtxAttach(ref CUcontext, CUCtxAttachFlags), and must be current to the calling thread.
Declaration
[Obsolete("Don't use this CUDA API call with CUDA version >= 4.0.")]
public static CUResult cuCtxDetach(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Context to destroy |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuCtxGetApiVersion(CUcontext, ref UInt32)
Returns the API version used to create ctx
in version
. If ctx
is NULL, returns the API version used to create the currently bound
context.
This wil return the API version used to create a context (for example, 3010 or 3020), which library developers can use to direct callers to a specific API version. Note that this API version may not be the same as returned by cuDriverGetVersion(ref Int32).
Declaration
public static CUResult cuCtxGetApiVersion(CUcontext ctx, ref uint version)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | |
System.UInt32 | version |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorUnknown.
|
cuCtxGetCacheConfig(ref CUFuncCache)
On devices where the L1 cache and shared memory use the same hardware
resources, this function returns through pconfig
the preferred cache configuration
for the current context. This is only a preference. The driver will use
the requested configuration if possible, but it is free to choose a different
configuration if required to execute functions.
This will return a pconfig
of PreferNone on devices
where the size of the L1 cache and shared memory are fixed.
Declaration
public static CUResult cuCtxGetCacheConfig(ref CUFuncCache pconfig)
Parameters
Type | Name | Description |
---|---|---|
CUFuncCache | pconfig |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxGetCurrent(ref CUcontext)
Returns in ctx
the CUDA context bound to the calling CPU thread.
If no context is bound to the calling CPU thread then ctx
is
set to NULL and Success is returned.
Declaration
public static CUResult cuCtxGetCurrent(ref CUcontext pctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned context handle |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuCtxGetDevice(ref CUdevice)
Returns in device
the ordinal of the current context’s device.
Declaration
public static CUResult cuCtxGetDevice(ref CUdevice device)
Parameters
Type | Name | Description |
---|---|---|
CUdevice | device | Returned device ID for the current context |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxGetFlags(ref CUCtxFlags)
Returns the flags for the current context
Returns in \p *flags the flags of the current context. See ::cuCtxCreate for flag values.
Declaration
public static CUResult cuCtxGetFlags(ref CUCtxFlags flags)
Parameters
Type | Name | Description |
---|---|---|
CUCtxFlags | flags | Pointer to store flags of current context |
Returns
Type | Description |
---|---|
CUResult |
cuCtxGetSharedMemConfig(ref CUsharedconfig)
Returns the current shared memory configuration for the current context.
This function will return in \p pConfig the current size of shared memory banks in the current context. On devices with configurable shared memory banks, cuCtxSetSharedMemConfig(CUsharedconfig) can be used to change this setting, so that all subsequent kernel launches will by default use the new bank size. When cuCtxGetSharedMemConfig(ref CUsharedconfig) is called on devices without configurable shared memory, it will return the fixed bank size of the hardware.
The returned bank configurations can be either: - FourByteBankSize: set shared memory bank width to be natively four bytes. - EightByteBankSize: set shared memory bank width to be natively eight bytes.
Declaration
public static CUResult cuCtxGetSharedMemConfig(ref CUsharedconfig pConfig)
Parameters
Type | Name | Description |
---|---|---|
CUsharedconfig | pConfig | returned shared memory configuration |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized, ErrorInvalidContext, ErrorInvalidValue. |
cuCtxGetStreamPriorityRange(ref Int32, ref Int32)
Returns numerical values that correspond to the least and greatest stream priorities.
Returns in leastPriority
and greatestPriority
the numerical values that correspond
to the least and greatest stream priorities respectively. Stream priorities
follow a convention where lower numbers imply greater priorities. The range of
meaningful stream priorities is given by [greatestPriority
, leastPriority
].
If the user attempts to create a stream with a priority value that is
outside the meaningful range as specified by this API, the priority is
automatically clamped down or up to either leastPriority
or greatestPriority
respectively. See ::cuStreamCreateWithPriority for details on creating a
priority stream.
A NULL may be passed in for leastPriority
or greatestPriority
if the value
is not desired.
This function will return '0' in both leastPriority
and greatestPriority
if
the current context's device does not support stream priorities
(see ::cuDeviceGetAttribute).
Declaration
public static CUResult cuCtxGetStreamPriorityRange(ref int leastPriority, ref int greatestPriority)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | leastPriority | Pointer to an int in which the numerical value for least stream priority is returned |
System.Int32 | greatestPriority | Pointer to an int in which the numerical value for greatest stream priority is returned |
Returns
Type | Description |
---|---|
CUResult |
cuCtxPopCurrent(ref CUcontext)
Pops the current CUDA context from the CPU thread. The CUDA context must have a usage count of 1. CUDA contexts have a usage count of 1 upon creation; the usage count may be incremented with cuCtxAttach(ref CUcontext, CUCtxAttachFlags) and decremented with cuCtxDetach(CUcontext).
If successful, cuCtxPopCurrent(ref CUcontext) passes back the old context handle in pctx
. That context may then be made current
to a different CPU thread by calling cuCtxPushCurrent(CUcontext).
Floating contexts may be destroyed by calling cuCtxDestroy(CUcontext).
If a context was current to the CPU thread before cuCtxCreate_v2(ref CUcontext, CUCtxFlags, CUdevice) or cuCtxPushCurrent(CUcontext) was called, this function makes that context current to the CPU thread again.
Declaration
[Obsolete("Don't use this CUDA API call with CUDA version >= 4.0.")]
public static CUResult cuCtxPopCurrent(ref CUcontext pctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned new context handle |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuCtxPopCurrent_v2(ref CUcontext)
Pops the current CUDA context from the CPU thread. The CUDA context must have a usage count of 1. CUDA contexts have a usage count of 1 upon creation; the usage count may be incremented with cuCtxAttach(ref CUcontext, CUCtxAttachFlags) and decremented with cuCtxDetach(CUcontext).
If successful, cuCtxPopCurrent(ref CUcontext) passes back the old context handle in pctx
. That context may then be made current
to a different CPU thread by calling cuCtxPushCurrent(CUcontext).
Floating contexts may be destroyed by calling cuCtxDestroy(CUcontext).
If a context was current to the CPU thread before cuCtxCreate_v2(ref CUcontext, CUCtxFlags, CUdevice) or cuCtxPushCurrent(CUcontext) was called, this function makes that context current to the CPU thread again.
Declaration
public static CUResult cuCtxPopCurrent_v2(ref CUcontext pctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned new context handle |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuCtxPushCurrent(CUcontext)
Pushes the given context ctx
onto the CPU thread’s stack of current contexts. The specified context becomes the
CPU thread’s current context, so all CUDA functions that operate on the current context are affected.
The previous current context may be made current again by calling cuCtxDestroy(CUcontext) or cuCtxPopCurrent(ref CUcontext).
The context must be "floating," i.e. not attached to any thread. Contexts are made to float by calling cuCtxPopCurrent(ref CUcontext).
Declaration
[Obsolete("Don't use this CUDA API call with CUDA version >= 4.0.")]
public static CUResult cuCtxPushCurrent(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Floating context to attach |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxPushCurrent_v2(CUcontext)
Pushes the given context ctx
onto the CPU thread’s stack of current contexts. The specified context becomes the
CPU thread’s current context, so all CUDA functions that operate on the current context are affected.
The previous current context may be made current again by calling cuCtxDestroy(CUcontext) or cuCtxPopCurrent(ref CUcontext).
The context must be "floating," i.e. not attached to any thread. Contexts are made to float by calling cuCtxPopCurrent(ref CUcontext).
Declaration
public static CUResult cuCtxPushCurrent_v2(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Floating context to attach |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxSetCacheConfig(CUFuncCache)
On devices where the L1 cache and shared memory use the same hardware
resources, this sets through config
the preferred cache configuration for
the current context. This is only a preference. The driver will use
the requested configuration if possible, but it is free to choose a different
configuration if required to execute the function. Any function preference
set via cuFuncSetCacheConfig(CUfunction, CUFuncCache) will be preferred over this context-wide
setting. Setting the context-wide cache configuration to
PreferNone will cause subsequent kernel launches to prefer
to not change the cache configuration unless required to launch the kernel.
This setting does nothing on devices where the size of the L1 cache and shared memory are fixed.
Launching a kernel with a different preference than the most recent preference setting may insert a device-side synchronization point.
Declaration
public static CUResult cuCtxSetCacheConfig(CUFuncCache config)
Parameters
Type | Name | Description |
---|---|---|
CUFuncCache | config |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext, ErrorInvalidValue.
|
cuCtxSetCurrent(CUcontext)
Binds the specified CUDA context to the calling CPU thread.
If ctx
is NULL then the CUDA context previously bound to the
calling CPU thread is unbound and Success is returned.
If there exists a CUDA context stack on the calling CPU thread, this
will replace the top of that stack with ctx
.
If ctx
is NULL then this will be equivalent to popping the top
of the calling CPU thread's CUDA context stack (or a no-op if the
calling CPU thread's CUDA context stack is empty).
Declaration
public static CUResult cuCtxSetCurrent(CUcontext ctx)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | ctx | Context to bind to the calling CPU thread |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuCtxSetSharedMemConfig(CUsharedconfig)
Sets the shared memory configuration for the current context.
On devices with configurable shared memory banks, this function will set the context's shared memory bank size which is used for subsequent kernel launches.
Changed the shared memory configuration between launches may insert a device side synchronization point between those launches.
Changing the shared memory bank size will not increase shared memory usage or affect occupancy of kernels, but may have major effects on performance. Larger bank sizes will allow for greater potential bandwidth to shared memory, but will change what kinds of accesses to shared memory will result in bank conflicts.
This function will do nothing on devices with fixed shared memory bank size.
The supported bank configurations are: - DefaultBankSize: set bank width to the default initial setting (currently, four bytes). - FourByteBankSize: set shared memory bank width to be natively four bytes. - EightByteBankSize: set shared memory bank width to be natively eight bytes.
Declaration
public static CUResult cuCtxSetSharedMemConfig(CUsharedconfig config)
Parameters
Type | Name | Description |
---|---|---|
CUsharedconfig | config | requested shared memory configuration |
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized, ErrorInvalidContext, ErrorInvalidValue. |
cuCtxSynchronize()
Blocks until the device has completed all preceding requested tasks. cuCtxSynchronize() returns an error if one of the preceding tasks failed. If the context was created with the BlockingSync flag, the CPU thread will block until the GPU context has finished its work.
Declaration
public static CUResult cuCtxSynchronize()
Returns
Type | Description |
---|---|
CUResult | CUDA Error Codes: Success, ErrorDeinitialized, ErrorNotInitialized,
ErrorInvalidContext.
|
cuDevicePrimaryCtxGetState(CUdevice, ref CUCtxFlags, ref Int32)
Get the state of the primary context
Returns in \p *flags the flags for the primary context of \p dev, and in \p *active whether it is active. See ::cuDevicePrimaryCtxSetFlags for flag values.
Declaration
public static CUResult cuDevicePrimaryCtxGetState(CUdevice dev, ref CUCtxFlags flags, ref int active)
Parameters
Type | Name | Description |
---|---|---|
CUdevice | dev | Device to get primary context flags for |
CUCtxFlags | flags | Pointer to store flags |
System.Int32 | active | Pointer to store context state; 0 = inactive, 1 = active |
Returns
Type | Description |
---|---|
CUResult |
cuDevicePrimaryCtxRelease(CUdevice)
Release the primary context on the GPU
Releases the primary context interop on the device by decreasing the usage count by 1. If the usage drops to 0 the primary context of device \p dev will be destroyed regardless of how many threads it is current to.
Please note that unlike ::cuCtxDestroy() this method does not pop the context from stack in any circumstances.
Declaration
public static CUResult cuDevicePrimaryCtxRelease(CUdevice dev)
Parameters
Type | Name | Description |
---|---|---|
CUdevice | dev | Device which primary context is released |
Returns
Type | Description |
---|---|
CUResult |
cuDevicePrimaryCtxReset(CUdevice)
Destroy all allocations and reset all state on the primary context
Explicitly destroys and cleans up all resources associated with the current device in the current process.
Note that it is responsibility of the calling function to ensure that no other module in the process is using the device any more. For that reason it is recommended to use ::cuDevicePrimaryCtxRelease() in most cases. However it is safe for other modules to call ::cuDevicePrimaryCtxRelease() even after resetting the device.
Declaration
public static CUResult cuDevicePrimaryCtxReset(CUdevice dev)
Parameters
Type | Name | Description |
---|---|---|
CUdevice | dev | Device for which primary context is destroyed |
Returns
Type | Description |
---|---|
CUResult |
cuDevicePrimaryCtxRetain(ref CUcontext, CUdevice)
Retain the primary context on the GPU.
Retains the primary context on the device, creating it if necessary, increasing its usage count. The caller must call ::cuDevicePrimaryCtxRelease() when done using the context. Unlike ::cuCtxCreate() the newly created context is not pushed onto the stack.
Context creation will fail with ::CUDA_ERROR_UNKNOWN if the compute mode of the device is ::CU_COMPUTEMODE_PROHIBITED. Similarly, context creation will also fail with ::CUDA_ERROR_UNKNOWN if the compute mode for the device is set to ::CU_COMPUTEMODE_EXCLUSIVE and there is already an active, non-primary, context on the device. The function ::cuDeviceGetAttribute() can be used with ::CU_DEVICE_ATTRIBUTE_COMPUTE_MODE to determine the compute mode of the device. The nvidia-smi tool can be used to set the compute mode for devices. Documentation for nvidia-smi can be obtained by passing a -h option to it.
Please note that the primary context always supports pinned allocations. Other flags can be specified by ::cuDevicePrimaryCtxSetFlags().
Declaration
public static CUResult cuDevicePrimaryCtxRetain(ref CUcontext pctx, CUdevice dev)
Parameters
Type | Name | Description |
---|---|---|
CUcontext | pctx | Returned context handle of the new context |
CUdevice | dev | Device for which primary context is requested |
Returns
Type | Description |
---|---|
CUResult |
cuDevicePrimaryCtxSetFlags(CUdevice, CUCtxFlags)
Set flags for the primary context
Sets the flags for the primary context on the device overwriting perviously set ones. If the primary context is already created ::CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE is returned.
The three LSBs of the \p flags parameter can be used to control how the OS thread, which owns the CUDA context at the time of an API call, interacts with the OS scheduler when waiting for results from the GPU. Only one of the scheduling flags can be set when creating a context.
Declaration
public static CUResult cuDevicePrimaryCtxSetFlags(CUdevice dev, CUCtxFlags flags)
Parameters
Type | Name | Description |
---|---|---|
CUdevice | dev | Device for which the primary context flags are set |
CUCtxFlags | flags | New flags for the device |
Returns
Type | Description |
---|---|
CUResult |