在线程中调用时,GpuMat :: upload会停止吗?

时间:2013-05-24 16:22:35

标签: c++ opencv cuda pthreads

下面是一个简单的程序,当我尝试将图像上传到GPU时,我的线程似乎停止了,而upload()没有返回。设备信息调用所有工作正常,并返回适当的值。

  1. 是否可以在线程中上传到GPU?
  2. 如果是这样,我在下面做错了什么,我怎么能让它运作起来?
  3. 这是我的简单测试代码:

    #include <cstdlib>
    #include <iostream>
    #include <pthread.h>
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/core/core.hpp"
    #include "opencv2/gpu/gpu.hpp" // For GPU processing
    
    using namespace std;
    
    
    void *PrintHello(void *threadid)
    {
        cv::gpu::GpuMat gInputImage, gOutputImage;
        cv::Mat image, image2, image3;
    
        long tid;
        tid = (long)threadid;
    
        cout << "Hello World! Thread ID, " << tid << endl;
        cout << "loading image in main memory" << endl;
        image = cv::imread("tdf_1972_poster.jpg");
    
        cout << "deviceID: " << cv::gpu::getDevice() << endl;
        cv::gpu::DeviceInfo mydeviceinfo = cv::gpu::DeviceInfo();
        cout << "name: " << mydeviceinfo.name() << endl;
    
        cv::gpu::setDevice(0); // I don't think this will help at all.
    
        cout << "cols before cpu resize: " << image.cols << endl;
        cv::resize(image, image2, cv::Size(), .5, 0.5, cv::INTER_NEAREST);
        cout << "cols after cpu resize: " << image2.cols << endl;
    
        cout << "cols before gpu resize: " << image.cols << endl;
        gInputImage.upload(image); // thread stalls here.
        cv::gpu::resize(gInputImage, gOutputImage, cv::Size(), .5, 0.5, cv::INTER_NEAREST);
        gOutputImage.download(image3);
        cout << "cols after gpu resize: " << image3.cols << endl;
    
        pthread_exit(NULL);
    }
    
    int main ()
    {
        pthread_t threads[1];
        int rc;
        int i;
    
        cout << "main() : creating thread, "<< endl;
        rc = pthread_create(&threads[0], NULL, PrintHello, 0);
        if (rc){
          cout << "Error:unable to create thread," << rc << endl;
          exit(1);
        }
        pthread_exit(NULL);
    }
    

    这是我得到的输出:

    main() : creating thread, 
    Hello World! Thread ID, 0
    loading image in main memory
    deviceID: 0
    name: GeForce GTX 560 Ti
    cols before cpu resize: 374
    cols after cpu resize: 187
    cols before gpu resize: 374
    

1 个答案:

答案 0 :(得分:1)

问题是我没有耐心。我忘了第一次GPU调用由于即时编译而返回需要多长时间。