3

In my c++ project, there are several #pragma omp parallel for private(i) statements. When I try to track down bugs in my code using valgrind, the OpenMP adornments result in "possibly lost" memory leak messages. I would like to totally disable all of the aforementioned #pragma statements so that I can isolate the problem.

However, I use omp_get_wtime() in my code, and I do not wish to disable these function calls. So I don't want to totally disable all OpenMP functionality in my project.

How can I simply turn off all the #pragma omp parallel for private(i) statements?

I use Eclipse CDT to automatically manage makefiles, and so I normally compile in release mode by: make all -C release. Ideally, I would like a solution to my problem that permits me to compile using a statement such as make all -C release -TURN_OFF_PARALLEL which would result in all the aforementioned #pragma statements being turned off.

synaptik
  • 8,971
  • 16
  • 71
  • 98

3 Answers3

2

The simplest solution is to:

  1. disable OpenMP
  2. link the OpenMP stub library functions

In case your OpenMP implementation doesn't provide stub functions, you can create your own copying from Appendix B of the standard.

Massimiliano
  • 7,842
  • 2
  • 47
  • 62
  • Can you elaborate on what you mean by "link the OpenMP stub library functions"? – synaptik Jun 13 '13 at 19:45
  • The idea is to link a _serial library_ with the API defined in `omp.h`. Some compilers provide a flag to do that, so the only thing you need is just to change a flag at compile time. This, for instance, is a [reference for Intel](http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011Update/compiler_c/copts/common_options/option_openmp_stubs.htm) – Massimiliano Jun 13 '13 at 19:55
  • What is your opinion on using the `boost::timer` [class](http://www.boost.org/doc/libs/1_53_0/libs/timer/doc/cpu_timers.html) instead of the OpenMP `omp_get_wtime()` function? Because I'm not using any other OpenMP functions. – synaptik Jun 13 '13 at 20:28
  • I don't see any issue with that – Massimiliano Jun 14 '13 at 04:24
  • @Massimiliano, How do you link only the stub function in GCC? In Visual Studio it's not a problem as omp_get_wtime works without setting OpenMP support. In ICC you can use -openmp-stubs. I don't see an option for GCC. –  Jun 14 '13 at 08:27
  • Appendix B says "double omp_get_wtime(void) { /* This function does not provide a working * wallclock timer. Replace it with a version * customized for the target machine. */ return 0.0; }". That's not a good solution if you need the function. –  Jun 14 '13 at 08:30
  • @raxman I don't think GCC provides a stub implementation. As for the timing functions, obviously they depend on the machine. What the standard prescribes is the API to which you have to conform. – Massimiliano Jun 14 '13 at 08:42
2

Following some dwelling around an interesting question about a non-working OpenMP code, it turns out that it is perfectly possible to get the equivalent of a stub OpenMP lib with GCC by only replacing the -fopenmp with -lgomp. I doubt it was a intended feature, but it works out of the box nonetheless.

Community
  • 1
  • 1
Gilles
  • 9,269
  • 4
  • 34
  • 53
1

For GCC I don't see an option to use only the stubs. Appendix B of the OpenMP standard says

    double omp_get_wtime(void)
    {
    /* This function does not provide a working
    * wallclock timer. Replace it with a version
    * customized for the target machine.
    */
    return 0.0;
    }

That's useless if you actually want the time. With GCC, either you have to write your own time function or you search for "#pragma omp" and replace it with "//#pragma omp"

Rather than changing the whole code base you could implement your own time function for GCC only. Computing time in linux :granularity and precision

Community
  • 1
  • 1
  • See it this way: implementing the stub `omp_get_wtime()` you modify an external substitute library in one single place. Your advice is to modify potentially all the codebase of a project. – Massimiliano Jun 14 '13 at 08:47
  • Yes, I understand, but writing your own accurate cross platform timing function can be a pain, that's the whole point in using omp_get_wtime(). –  Jun 14 '13 at 08:54