pycuda icon indicating copy to clipboard operation
pycuda copied to clipboard

pycuda with python mutliple host thread

Open trobr opened this issue 6 years ago • 10 comments

I want use mutliple thread with python on host. I tried following methods:

  1. 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
  2. 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!

trobr avatar Sep 13 '20 15:09 trobr

Try the recently-added retain_primary_context to get PyCUDA to use the same context as runtime-API-based libraries.

inducer avatar Sep 13 '20 17:09 inducer

Try the recently-added retain_primary_context to 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)

trobr avatar Sep 14 '20 04:09 trobr

Two comments:

  • Do not use autoinit.
  • You need to push the primary context onto the context stack to activate it.

inducer avatar Sep 14 '20 17:09 inducer

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!

trobr avatar Sep 15 '20 02:09 trobr

Context stacks are thread-local, see the CUDA docs.

inducer avatar Sep 15 '20 03:09 inducer

Thanks~

trobr avatar Sep 15 '20 05:09 trobr

Two comments:

  • Do not use autoinit.
  • You need to push the 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!

Jutcq avatar Apr 13 '21 01:04 Jutcq

My understanding is that each process will see a separate "primvary context" for its GPU access.

inducer avatar Apr 13 '21 01:04 inducer

Thanks~

Have you solved the problem? can you show me the code? Thanks !!!

hao6699 avatar Jun 10 '21 08:06 hao6699

Thanks~

Have you solved the problem? can you show me the code? Thanks !!!

Source:

import pycuda.driver as cuda

cuda.init()
device = cuda.Device(0)
ctx = device.make_context()

...

ctx.push()
---
Execute Your Code
---
ctx.pop()

gcunhase avatar Sep 24 '21 09:09 gcunhase