pycuda with python mutliple host thread
I want use mutliple thread with python on host. I tried following methods:
- use autoinit in main thread, and try trt inference in python ThreadPoolExecutor, but get "no activity context" error when use cuda API:
cuda.mem_alloc - then I try create ThreadPoolExecutor with initializer like this:
def cuda_ctx_init():
_device = cuda.Device(0)
ctx = _device.make_context()
after done this, I can successfully allocate gpu memory with cuda.mem_alloc in the thread,but get another error when do trt inference:
Parameter check failed at: ../rtSafe/safeContext.cpp::terminateCommonContext::165, condition: cudaEventDestroy(context.start) failure.
Parameter check failed at: ../rtSafe/safeContext.cpp::terminateCommonContext::170, condition: cudaEventDestroy(context.stop) failure.
../rtSafe/safeRuntime.cpp (32) - Cuda Error in free: 77 (an illegal memory access was encountered)
terminate called after throwing an instance of 'nvinfer1::CudaError'
I found a solution with cpp:DLA and GPU cores at the same time and a python solution : How to use TensorRT by the multi-threading package of python
I noticed that unlike cpp version, the Python version requires additional operations :
ctx.push()
ctx.pop()
In my test,this operations spend too much time each interface.And I want ask is there any other way to use pycuda with python mutlipe thread?
Thanks!
Try the recently-added retain_primary_context to get PyCUDA to use the same context as runtime-API-based libraries.
Try the recently-added
retain_primary_contextto get PyCUDA to use the same context as runtime-API-based libraries.
Thanks for your reply~
I had try retain_primary_context, but i still got error:
pycuda._driver.LogicError: explicit_context_dependent failed: invalid device context - no currently active context?
Code like this:
from concurrent.futures import ThreadPoolExecutor, wait
import pycuda.driver as cuda
# import pycuda
import pycuda.autoinit
primary_cuda_device = pycuda.autoinit.device
def loop_func():
# pre-process code
# ....
ctx = primary_cuda_device.retain_primary_context()
# if `cuda.Context.attach()` here
# pycuda can alloc memory success
# but meet another error: `[TensorRT] ERROR: ../rtSafe/safeRuntime.cpp (32) - Cuda Error in free: 77 (an illegal memory access was encountered)`
# cuda.Context.attach()
size = 10000
# failed here
stream = cuda.Stream()
device_mem = cuda.mem_alloc(size)
# trt inference
if __name__ == "__main__":
pool = ThreadPoolExecutor(5)
ts = []
for __ in range(5):
t = pool.submit(loop_func)
ts.append(t)
wait(ts)
Two comments:
- Do not use
autoinit. - You need to
pushthe primary context onto the context stack to activate it.
I known.But I have one more question.I need to push primary context onto the context stack in every sub thread?
I find a description in cuda api:cuCtxPushCurrent:
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.
so, if I push context in every thread, will other threads be affected?
Only one thread at a time can use the gpu/cuda ?
Thanks!
Context stacks are thread-local, see the CUDA docs.
Thanks~
Two comments:
- Do not use
autoinit.- You need to
pushthe primary context onto the context stack to activate it.
@inducer regarding to multiple cuda application scenario(e.g. develop a pycuda + tensorRT application, put it in docker, and then launch mutiple dockers on host), the CUDA dev guide mentions "If multiple CUDA application processes access the same GPU concurrently, this almost always implies multiple contexts," how should I use the primary context via retain_primary_context? thanks!
My understanding is that each process will see a separate "primvary context" for its GPU access.
Thanks~
Have you solved the problem? can you show me the code? Thanks !!!
Thanks~
Have you solved the problem? can you show me the code? Thanks !!!
import pycuda.driver as cuda
cuda.init()
device = cuda.Device(0)
ctx = device.make_context()
...
ctx.push()
---
Execute Your Code
---
ctx.pop()