1

I want to write my own parallel code or at least try whether manually parallelizing some of my code is faster than having Eigen use its own internal parallel routines.

I have been following this guide and added at the top of a header file the following directive (but also tried it at the top of main):

#define EIGEN_DONT_PARALLELIZE

Yet, when I ask Eigen to print the number of threads it's been using, via Eigen::nbThreads I consistently get two. I have tried to force the issue with the initParallel() method which is designed for user-defined parallel regions but to no avail. Could it be that I need to place my pre-processor token somewhere else? I am using gcc 8.1, CLion with CMake. I have also tried to force the issue with setNbThreads(0). To eventually include OpenMP in my own code, I have followed the inclusion of OpenMP as recommended here as well as added this in my CMakeLists.txt: target_link_libraries(OpenMP::OpenMP_CXX).

Or could it be that Eigen just tells me how many cores are in principle available, which doesn't sound like what is written in the documentation.

Edit

I am not sure if this is important but CLion (editor) complains MACRO EIGEN_DONT_PARALLELIZE is never used. I looked in Eigen/Core and saw that it is used only in the form of a condition for an if statement, so I ignored this editor warning, but maybe I should not have?

I have now reproduced this behaviour with a much smaller example.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hirek
  • 435
  • 3
  • 12
  • 1
    What do you mean by "first header file"? Any code that depends on the order of inclusion of header files is suspect. –  Jan 06 '19 at 18:24
  • Good point, I just thought it mattered where I put the #define EIGEN_DONT_PARALLELIZE so to be safe I put it as high as possible but it should not matter, really. – Hirek Jan 06 '19 at 18:29
  • 1
    You should pass this as an argument to your compiler: https://godbolt.org/z/ndJuXD – chtz Jan 06 '19 at 19:01
  • hm my compiler throws me errors, is there is a section in cmakelists.txt where I can set such flags? Also, does it mean that Eigen's documentation is likely wrong/off here? – Hirek Jan 06 '19 at 19:10
  • 1
    Use [`add_definitions`](https://cmake.org/cmake/help/latest/command/add_definitions.html), or `add_compile_definitions` if your cmake version supports that. – chtz Jan 06 '19 at 22:10
  • @chtz that did the trick, in particular I added this line "add_compile_definitions(EIGEN_DONT_PARALLELIZE)" as the last line in CMakeLists.txt accomplished it. The compiler warned of double definitions which I ignored. If I remove this line from CMakeLists.txt (just leaving in the preprocessor definition in the main.cpp it goes back to using two threads. Strange why that is but at least it's working so I can use my own parallel code now. If you want to post this as an answer I shall mark it as the answer so you get credit for it. Cheers! – Hirek Jan 07 '19 at 16:01
  • Plus, maybe I am going crazy but I record slightly faster times on a single thread.. might be overhead that is involved? – Hirek Jan 07 '19 at 16:02

0 Answers0