inducer / inducer/pycuda

Is there a way to share context among threads, if not why?

Open
#306 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.1k
Forks
298
Avg merge
4m
Merged PRs (30d)
1

Description

Basically I want to achieve concurrent work with multithreading and my current inference code is pycuda + tensorrt.
**why I want to do so**
I'm trying to optimize the inference throughput for a model with dynamic input. the size difference between samples can be quite significant. So I want to avoid padding but still do **something similar to batching**, I want to run several samples concurrently with the same engine. The inference time will still be bottlenecked by the biggest sample in the batch but a lot of flops are saved, also it prevents possible performance drop from padding too much.

**my current understanding of the problem**
From what I understood If works are in different cuda contexts there is no real parallel working, instead it is just better scheduling. Also one process can only have one cuda context but threads can share contexts. It may not be true for pycuda so I need to . But I didn't find anything talking about how to share one context among threads yet.

I found the official example here for using multithreading with pycuda [link](https://github.com/inducer/pycuda/blob/main/examples/from-wiki/multiple_threads.py)

Device.make_context()
There's not much difference between multithreading and multiprocess then. If each thread owns it's own context then there is no real concurrent work.

**My question:**
I just wonder if my understanding on context is right. And I wonder if there is a way to share context between different threads. I feel it should be possible, if it is not possible with pycuda, can anyone briefly explain why?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.