Parallelizing a for loop using openmp amp; replacing push_back(使用 openmp amp; 并行化 for 循环替换 push_back)
问题描述
我想并行化以下代码段,但我是 openmp 和创建并行代码的新手.
I'd like to parallelize the following piece of code but am new to openmp and creating parallel code.
std::vector<DMatch> good_matches;
for (int i = 0; i < descriptors_A.rows; i++) {
if (matches_RM[i].distance < 3 * min_dist) {
good_matches.push_back(matches_RM[i]);
}
}
我试过了
std::vector<DMatch> good_matches;
#pragma omp parallel for
for (int i = 0; i < descriptors_A.rows; i++) {
if (matches_RM[i].distance < 3 * min_dist) {
good_matches[i] = matches_RM[i];
}
}
和
std::vector<DMatch> good_matches;
cv::DMatch temp;
#pragma omp parallel for
for (int i = 0; i < descriptors_A.rows; i++) {
if (matches_RM[i].distance < 3 * min_dist) {
temp = matches_RM[i];
good_matches[i] = temp;
// AND ALSO good_matches.push_back(temp);
}
我也试过
#omp parallel critical
good_matches.push_back(matches_RM[i]);
此条款有效,但不会加快任何速度.这个 for 循环可能无法加速,但如果可以的话就太好了.我也想加快速度
This clause works but does not speed anything up. It may be the case that this for loop cannot be sped up but it'd be great if it can be. I'd also like to speed this up as well
std::vector<Point2f> obj, scene;
for (int i = 0; i < good_matches.size(); i++) {
obj.push_back(keypoints_A[good_matches[i].queryIdx].pt);
scene.push_back(keypoints_B[good_matches[i].trainIdx].pt);
}
如果这个问题得到解答,我们深表歉意,非常感谢任何可以提供帮助的人.
Apologies if this question as been answered and thank you very much to anyone who can help.
推荐答案
我在这里展示了如何做到这一点 c-openmp-parallel-for-loop-alternatives-to-stdvector
I showed how to do this here c-openmp-parallel-for-loop-alternatives-to-stdvector
制作 std::vector 的私有版本,并在临界区填充共享 std::vector,如下所示:
Make private versions of the std::vector and fill the shared std::vector in a critical section like this:
std::vector<DMatch> good_matches;
#pragma omp parallel
{
std::vector<DMatch> good_matches_private;
#pragma omp for nowait
for (int i = 0; i < descriptors_A.rows; i++) {
if (matches_RM[i].distance < 3 * min_dist) {
good_matches_private.push_back(matches_RM[i]);
}
}
#pragma omp critical
good_matches.insert(good_matches.end(), good_matches_private.begin(), good_matches_private.end());
}
这篇关于使用 openmp & 并行化 for 循环替换 push_back的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 openmp & 并行化 for 循环替换 push_back
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01