Part of the content is transferred from
Https://chenrudan.github.io/blog/2015/07/22/cudastream.html
Http://stackoverflow.com/questions/10415204/how-to-create-a-cuda-context
Early on, it was discovered that the first function that was run on Cuda would take a long time to explain because Cuda initialization.
So what's the main problem with Cuda Initializaiton?
One of them is to create a cuda context.即调用这些函数的时候,需要已经有context 存在了。
Cuda context is important as a container that manages the life cycle of all objects, and most cuda function calls require context. These objects are as follows:
All allocated memory modules, similar to the dynamic-link library, ends with. Cubin and. Ptx "To use cuda streams in Jcuda, managing execution Unit concurrency Cuda Eventstexture and surface reference the local memory (device memory) used inside the kernel to debug, parse, and synchronize the fixed buffers used for paging replication of internal resources
即调用这些函数的时候,需要已经有context存在了。那么context 如何创建呢?
There are two ways of implicitly invoking and explicitly calling (that is, a bit incorrect, but this means)
implicit invocation
The Library of the Cuda Runtime software layer is implicitly called.
Starting with 4.0, the Cuda runtime creates a context for all threads, that is, one device corresponds to a context, and all threads are available.
Cuda runtime does not provide the API to create the CUDA context directly, but instead creates the context by delaying initialization (deferred initialization), which is the lazy initialization. In particular, when invoking each of the Cudart library functions, it checks whether there is currently a context, and if it needs a context, it is created automatically. That is, creating a context when you need to create these objects. You can explicitly control the initialization, which is called Cudafree (0), forcing initialization. Cuda runtime incorporates the concept of context and device, that is, operating on a GPU can be viewed as a context. Thus the CUDA runtime provides a function similar to cudadevicesynchronize () rather than the cuctxsynchronize () corresponding to the driver API. The application can access the current context stack through the driver API. Context-related operations are implemented as driver APIs in the form of cuctxxxxx ().
Explicitly called
Cuda driver API, driver layer library, explicit invocation
Cuda driver API Creates a context for a thread, a device that corresponds to multiple context, each context corresponds to multiple threads, and the context between threads can be transferred.
In the driver API, each CPU thread must create a context, or transfer a context from another CPU thread. If there is no context, it will be an error. How to return to cause error? That is, if you do not create a context, call the driver API directly to create those objects, you will get an error. Because the objects above have functions that can be created in both the runtime and the driver API. So, pay attention!!!
Each CPU thread has a current context stack, and a new context is built into the stack. For each thread there can only be one out of the stack into the current context that can be used, and this free context can be transferred to another CPU thread, implemented by the function cuctxpushcurrent/cuctxpopcurrent.
When the context is destroyed, the resources allocated within the context are also destroyed, and the other context of the allocated resources in one of the contents cannot be used.
Attention:
1, the implicit invocation of the context is primary context; the context in which the call is displayed is standard context
2, each Cuda initialization comparison fee time, one of the work may be using runtime to make an implicit call to the context. Therefore, if you want to avoid this part, one way is to use Cudasetdevice () to create the context in advance
The canonical-to-Force runtime API, context establishment is cudaFree(0) -to-call. If you had multiple devices, call with the ID of the cudaSetDevice() device you want to establish a context on, then to cudaFree(0) estab Lish the context.
Edit:note that as of CUDA 5.0, it appears the heuristics of the context establishment is slightly different and cudaSetDevice() I Tself establishes context on the device was it called on. The explicit call is cudaFree(0) no longer necessary (although it won ' t hurt anything).
Using the runtime API: cudaDeviceSynchronize , cudaDeviceGetLimit , or anything that actually accesses the context should work. I ' m quite certain you ' re not using the driver API, as it doesn ' t does that sort of lazy initialization, but for others ' bene Fit the driver call would is cuCtxCreate .
The meanings of the above English are summarized as follows:
In the case of runtime, the call implicitly invokes the function that created the context, such as cudasetdevice,cudadevicesynchronize.
If it is the drive API, you must use Cuctxcreate.
Why do I have to tangle with this problem: I use multithreading to call Cuda, but one problem is that each thread needs to create a context, this will add a lot of time, if only a runtime created a context can save a lot of time.
But the question is, if multiple threads are using a context, will there be any pitfalls?
Understanding of Cuda Context