如何在C ++程序中使用openmp [英] how to use openmp in a c++ program

查看:144
本文介绍了如何在C ++程序中使用openmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenMP来并行化c ++程序中的某些功能.

I would like to use OpenMP to parallelize some of my functions in a c++ program.

我在具有4核的intel i5上使用ubuntu 12.04. 但是,按照某些步骤操作后,我看不到性能有任何改善.我可以看到只使用了一个CPU内核. (ubuntu中的系统监视器)

I am using ubuntu 12.04, on intel i5 with 4 cores. But after following certain steps, I do not see any improve in performance. I could see that only one CPU core is being used. (system monitor in ubuntu )

我做了什么.

添加了#include <omp.h>

在for循环之前添加了这两行

added these two lines before a for loop

omp_set_num_threads(4);

#pragma omp parallel for

在CMakeLists.txt中,

in CMakeLists.txt,

我添加了target_link_libraries (executable -fopenmp -lgomp ${PCL_LIBRARIES} )

I added target_link_libraries (executable -fopenmp -lgomp ${PCL_LIBRARIES} )

您能帮我获得并行性吗?

Can you please help me in getting the parallelism ?

谢谢!

推荐答案

如@Mikael Persson所述,我从目标链接库中删除了-fopenmp并添加了 find_package(OpenMP) if (OPENMP_FOUND) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") endif()

As mentioned by @Mikael Persson , I removed -fopenmp from the target link libraries and added find_package(OpenMP) if (OPENMP_FOUND) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") endif()

此功能正常.

出现错误r *** double free or corruption (!prev): 0x00007ff424006b20 ***是因为在for循环中,我试图使用push_back()函数加载堆栈,并且索引取决于for循环索引.我认为这就是问题的原因.

The error r *** double free or corruption (!prev): 0x00007ff424006b20 *** was arising because in the for loop, i was trying to use push_back() function to load a stack and the index was dependant on the for loop index. I think that was the cause of the problem.

我学到了另一件事,即可以嵌套用于循环,并且OMP如果变量是独立的并且可以产生有效结果,则可以正常工作

I learnt one more thing that one can have nested for loops and OMP can work without errors if their variables are independent and produce valid results

这篇关于如何在C ++程序中使用openmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆