下面是一个简单的程序,当我尝试将图像上传到 GPU 时,我的线程似乎停止了,其中 upload() 没有返回。设备信息调用一切正常,并返回适当的值。
- 是否可以在线程中上传到 GPU?
- 如果是这样,我在下面做错了什么,我怎样才能让它工作?
这是我的简单测试代码:
#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