Recently, I need to migrate some computationally intensive code from C++ into cuda C, but these codes are based std::vector and opencv like this:
// Iterate over each track-detection pair for (unsigned int i = 0; i < tracks.size(); i++) { costMatrix.push_back(vector<double>()); Rect predBbox = tracks[i].predPosition; for (unsigned int j = 0; j < detections.size(); j++) { Rect bbox = detections[j].bbox; double overlap = computeBoundingBoxOverlap(predBbox, bbox); // Cost = 1 - overlap costMatrix[i].push_back(1.0 - overlap); } }
Because push_back()
here can only be invoked from host, and I cannot determine the size of vector in advance so malloc memory to pointer is not a good idea. I have tried thrust, but thrust::transform
also cannot append data into vector, for which I code like this:
vector<Student> host_v; // Init input data thrust::device_vector<Student> d_s(host_v.begin(), host_v.end()); thrust::device_vector<Student> d_s_1(host_v.begin(), host_v.end()); thrust::transform(d_s.begin(), d_s.end(), d_s_1.end(), [] __device__(Student s) { ... // some operation return s; }); thrust::copy(d_s_1.begin(), d_s_1.end(), host_v.begin()); // change nothing to host_v
https://stackoverflow.com/questions/65417820/how-to-realize-vector-append-with-cudaor-thrust December 23, 2020 at 09:03AM
没有评论:
发表评论