让这个迭代器循环在 CUDA 上工作时,我遇到了一个问题。 有人可以帮忙吗?
std::vector<cv::DMatch> matches;
std::vector<cv::KeyPoint> key_pts1, key_pts2;
std::vector<cv::Point2f> points1, points2;
for (std::vector<cv::DMatch>::const_iterator itr = matches.begin(); itr!= matches.end(); ++it)
{
float x = key_pts1[itr->queryIdx].pt.x;
float y = key_pts1[itr->queryIdx].pt.y;
points1.push_back(cv::Point2f(x,y));
x = key_pts2[itr->trainIdx].pt.x;
y = key_pts2[itr->trainIdx].pt.y;
points2.push_back(cv::Point2f(x,y));
}
上述转换为 CUDA - 并行处理,我认为这对我来说似乎相当困难。
void dmatchLoopHomography(float *itr, float *match_being, float *match_end, float *keypoint_1, float *keypoint_2, float *pts1, float *pts2)
{
float x, y;
// allocate memory in GPU memory
unsigned char *mtch_begin, *mtch_end, *keypt_1, *keypt_2, points1, *points2;
cudaHostGetDevicePointer(&mtch_begin, match_being, 0);
cudaHostGetDevicePointer(&mtch_end, match_end, 0);
cudaHostGetDevicePointer(&keypt_1, keypoint_1, 0);
cudaHostGetDevicePointer(&keypt_2, keypoint_2, 0);
cudaHostGetDevicePointer(&points1, pts1, 0);
cudaHostGetDevicePointer(&points2, pts2, 0);
//dim3 blocks(16, 16);
dim3 threads(itr, itr);
//kernal
dmatchLoopHomography_ker<<<itr,itr>>>(mtch_begin, mtch_end, keypt_1, keypt_2, points1. points2)
cudaThreadSynchronize();
}
和
__global__ void dmatchLoopHomography_ker(float *itr, float *match_being, float *match_end, float *keypoint_1, float *keypoint_2, float *pts1, float *pts2)
{
//how do I go about it ??
}
最佳答案
首先,我注意到您的程序包括移动一个 vector<KeyPoint>
进入vector<Point2f>
结构。 OpenCV 有一个非常好的单行代码可以为您完成这项工作:
using namespace cv;
KeyPoint::convert(key_pts1, points1); //vector<KeyPoint> to vector<Point2f>
现在,让我们谈谈 GPU 方面的事情。结果是 cudaHostGetDevicePointer()
不分配内存。你会想要 cudaMalloc()
用于分配内存。例如:
//compile with nvcc, not gcc
float* device_matches;
int match_length = matches.end() - matches.begin();
cudaMalloc(&device_matches, match_length*sizeof(float));
//do the same as above for key_pts1, key_pts2, points1, and points2
现在,device_matches
只是一个普通的 C 数组,而不是 STL vector 。所以,你没有迭代器。相反,您必须只使用普通的数组索引。如果你真的想要 GPU 上的迭代器,请查看 Thrust library . Thrust 确实很方便,但缺点是 Thrust 仅提供一组特定的预烘焙函数。
更大的问题是您是否想在 GPU 上执行程序的这个特定部分。我建议将 GPU 用于真正计算密集型的东西(例如,实际的特征匹配),但在数据格式之间移动数据(如在您的示例代码中)比特征匹配便宜很多数量级。
另外,请记住,您通常需要在 GPU 上构建与在 CPU 上不同的数据结构。这种重组不一定在计算上很昂贵,但你需要留出一些时间在白板上解决它,撕掉你的头发等。最后,如果你认真对待 GPU 的东西,它可能值得工作通过一些简单的 GPU 编程示例(我喜欢 Dr. Dobbs Supercomputing for the Masses tutorials ),参加 GPU/并行类(class),或与一些 GPU 黑客 friend 交谈。
关于c++ - opencv for-loop with CUDA - 并行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266804/