简体   繁体   中英

how to use openmp in a c++ program

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

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 )

What I did..

added #include <omp.h>

added these two lines before a for loop

omp_set_num_threads(4);

#pragma omp parallel for

in CMakeLists.txt,

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

Can you please help me in getting the parallelism ?

Thank you!

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()

This is working.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM