# Running dnn models inside different threads on different gpu device

**URL:** <https://forum.opencv.org/t/running-dnn-models-inside-different-threads-on-different-gpu-device/4370>\
**Category:** C++\
**Tags:** cuda, dnn\
**Created:** [July 17, 2021, 7:31am UTC](https://forum.opencv.org/t/running-dnn-models-inside-different-threads-on-different-gpu-device/4370 "2021-07-17T07:31:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xvling](https://avatars.discourse-cdn.com/v4/letter/x/50afbb/32.png) [@xvling](https://forum.opencv.org/u/xvling)\
**Post date:** [July 17, 2021, 7:31am UTC](https://forum.opencv.org/t/running-dnn-models-inside-different-threads-on-different-gpu-device/4370/1 "2021-07-17T07:31:28Z")

</div>

I do this inside each dnn algorithm class constructor,

_**cv::cuda::setDevice(cuda\_id);**_  
_ **this-\>cuda\_id = cuda\_id;** _  
_**net = cv::dnn::readNetFromCaffe(model\_deploy, model\_bin);**_  
_**this-\>net.setPreferableBackend(cv::dnn::DNN\_BACKEND\_CUDA);**_  
_**this-\>net.setPreferableTarget(cv::dnn::DNN\_TARGET\_CUDA);**_

and init them inside every thread’s run function. My PC has 4x3080 gpus. when the batch num is big (like \>4 ) it crashed. However, It is working with batch =16 with single gpu, and running 4 independent single gpu process on different device is also working.

what else should to be done to running in one process with multiple threads?
