gpt4 book ai didi

c++ - GpuMat::upload 在线程中调用时停止?

转载 作者:行者123 更新时间:2023-11-28 07:36:10 24 4
gpt4 key购买 nike

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

  1. 是否可以在线程中上传到 GPU?
  2. 如果是这样,我在下面做错了什么,我该如何让它发挥作用?

这是我的简单测试代码:

#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

最佳答案

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

关于c++ - GpuMat::upload 在线程中调用时停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16739530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com