I am stuck with a problem to have this loop of iterators work on CUDA.
can anyone help here ?
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));
}
This above conversion to CUDA – parallel processing, as I have thought seems quite difficult to me.
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();
}
and
__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 ??
}
First, I noticed that your program consists of moving a
vector<KeyPoint>into avector<Point2f>structure. OpenCV has a really nice one-liner to do this for you:Now, let’s talk GPU stuff. It turns out that
cudaHostGetDevicePointer()doesn’t allocate memory. You’ll wantcudaMalloc()for allocating memory. For example:Now,
device_matchesis just a plain C array, not an STL vector. So, you don’t have iterators. Instead, you have to just use ordinary array indices. If you really want iterators on the GPU, look at the Thrust library. Thrust is really handy, but the downside is that Thrust only provides a specific set of pre-baked functions.The bigger question is whether you want to do this particular part of your program on the GPU. I’d recommend using the GPU for really compute-intensive stuff (for example, the actual feature matching), but moving data between data formats (as in your example code) is many orders of magnitude cheaper than feature matching.
Also, bear in mind that you often have to structure your data differently on the GPU than you would on the CPU. This restructuring isn’t necessarily computationally expensive, but you’ll want to set aside some time to work it out on the whiteboard, tear your hair out, etc. Finally, if you’re serious about GPU stuff, it might be worth working through a few simple GPU programming examples (I’ve enjoyed the Dr. Dobbs Supercomputing for the Masses tutorials), taking a GPU/parallel class, or talking to some GPU-hacker friends.