Repository: opencv
Issue: cv::DNN - Cuda 10.2 - MultiThread Error
Labels: category: gpu/cuda (contrib),incomplete,category: dnn,needs investigation

##### System information (version)
<!-- Example
- OpenCV => 4.4.0 with Cuda 10.2 and cuDNN 7.6.5
- Operating System / Platform => Windows 10 - 64 Bit
- Compiler => Visual Studio 2019
-->

- OpenCV => 4.4.0 with Cuda 10.2 and cuDNN 7.6.5 on Quadro RTX 4000
- Operating System / Platform => Windows 10 - 64 Bit
- Compiler => Visual Studio 2019 C++

##### Detailed description

Hello,
we have problem with moduleDNN and multithreading. I use Yolov4 original weights and config [https://github.com/AlexeyAB/darknet](url). Each thread has its own instance of cv :: Net, seems to work fine but after a few iterations an execution appears. The DNN module is'nt thread safe?

Here is a minimum example, 


##### Steps to reproduce

```
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <numeric>
#include <thread>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/cudaoptflow.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/dnn.hpp>
using namespace std;
using namespace cv;
using namespace dnn;
using namespace cv::cuda;
```

```
class thread_dnn 
{
public:
    thread_dnn(uint iden, string modelConfiguration, string modelWeights, string fileclasse, string fileimage) 
    {
        id = iden;
        // Load the network
        net = readNetFromDarknet(modelConfiguration, modelWeights);
        net.setPreferableBackend(DNN_BACKEND_CUDA);

        net.setPreferableTarget(DNN_TARGET_CUDA);
        ifstream ifs(fileclasse.c_str());
        string line;
        while (getline(ifs, line)) classes.push_back(line);

        if (names.empty())
        {
            //Get the indices of the output layers, i.e. the layers with unconnected outputs
            vector<int> outLayers = net.getUnconnectedOutLayers();

            //get the names of all the layers in the network
            vector<String> layersNames = net.getLayerNames();

            // Get the names of the output layers in names
            names.resize(outLayers.size());
            for (size_t i = 0; i < outLayers.size(); ++i)
                names[i] = layersNames[outLayers[i] - 1];
        }

        image = imread(fileimage);
    }

    void operator()(int iter)
    {
        int inpWidth = 512;  // Width of network's input image
        int inpHeight = 512; // Height of network's input image

        for (int i = 0; i < iter; i++)
        {
            cv::Mat blob;
            // Create a 4D blob from a frame.  
            blobFromImage(image, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0), false, false);
            //Sets the input to the network
            net.setInput(blob);
            // Runs the forward pass to get output of the output layers
            vector<Mat> outs;
            net.forward(outs, names);
        }
    }

    uint id;
    cv::Mat image;
    Net net;
    vector<string> classes;
    vector<String> names;
}
```

```
int main()
{
    int nbIter = 1000;
    string PathModel("D:\\Data\\Darknet_Data\\Model_yolov4\\yolov4-Original\\yolov4-Original.");

    // Create and execute the thread
    thread_dnn dnn0(0, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\00.png");
    std::thread thread0(dnn0, nbIter);

    thread_dnn dnn1(1, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\01.png");
    std::thread thread1(dnn1, nbIter);

    thread_dnn dnn2(2, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\02.png");
    std::thread thread2(dnn2, nbIter);

    thread_dnn dnn3(3, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\03.png");
    std::thread thread3(dnn3, nbIter);

    thread_dnn dnn4(4, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\04.png");
    std::thread thread4(dnn4, nbIter);

    thread_dnn dnn5(5, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\05.png");
    std::thread thread5(dnn5, nbIter);

    thread_dnn dnn6(6, PathModel + "cfg", PathModel + "weights", PathModel + "names", "C:\\Users\\Alex\\Desktop\\Img\\06.png");
    std::thread thread6(dnn6, nbIter);

    // Wait for the thread to finish, this is a blocking operation
    thread0.join();
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    thread5.join();
    thread6.join();

    return 0;
}
```
##### Error
Systematic, Error after N iteration, Execption Thrown : 
cudaErrorLaunchFailure 
D:\SDK\OpenCV\4.4.0\opencv-4.4.0\modules\dnn\src\cuda4dnn\csl\pointer.hpp  
cv::dnn::cuda4dnn::csl::memcpy ligne 366

##### Issue submission checklist

 - [ ] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [X] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [X] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [X] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->