6

I've got some code that can run with (or without) OpenMP - it depends on how the user sets up the makefile. If they want to run with OpenMP, then they just add -fopenmp to CFLAGS and CXXFLAGS.

I'm trying to determine what preprocessor macro I can use to tell when -fopenmp is in effect. The omp.h header does not look very interesting:

$ cat /usr/lib/gcc/x86_64-linux-gnu/4.8/include/omp.h | grep define
#define OMP_H 1
#define _LIBGOMP_OMP_LOCK_DEFINED 1
# define __GOMP_NOTHROW throw ()
# define __GOMP_NOTHROW __attribute__((__nothrow__))

And I can't get the preprocessor to offer up anything useful:

$ cpp -dM -fopenmp </dev/null | grep -i omp
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
#define __STDC_IEC_559_COMPLEX__ 1

$ cpp -dM -fopenmp </dev/null | grep -i version
#define __GXX_ABI_VERSION 1002
#define __VERSION__ "4.8.2"

What preprocessor define does -fopenmp provide?


This is similar to How to tell if OpenMP is working? But I'm interested in compile time, and not post-build or runtime.

Note: this project does not use Boost, does not use Autotools, and does not use Cmake. It just uses a makefile.

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

7

Your grep was overly specific, you should have looked for "openmp".

Or rather, diffing cpp -dM -fopenmp </dev/null and cpp -dM </dev/null produces a single diff:

#define _OPENMP 201107

Which should be exactly what you are looking for.

mtijanic
  • 2,872
  • 11
  • 26
  • Thank you very much. And good idea about the diff. It beats taking stabs in the dark because you can't find it in the documentation. In this case, it appears to be undocumented: 0 hits for `$ man -k "_OPENMP"`. – jww Jun 12 '15 at 12:56
  • 1
    @jww I've found some mentions of it online, including some GCC docs. I advise first checking if other compilers you may need to support (e.g. msvc) also define it before proceeding to use it. – mtijanic Jun 12 '15 at 13:00
  • 1
    @jww gcc, msvc and clang all define it, actually. You should be good to go. – mtijanic Jun 12 '15 at 13:02
  • 1
    Yeah, once you pointed out the define, I found questions like [Ignore OpenMP on Machine that doesn't have it](http://stackoverflow.com/q/1300180). Thanks again. – jww Jun 12 '15 at 13:11
  • 3
    _§2.2 Conditional Compilation_ on page 32 of the [OpenMP specification](http://www.openmp.org/mp-documents/OpenMP4.0.0.pdf) (page 40 in the PDF): "In implementations that support a preprocessor, the `_OPENMP` macro name is defined to have the decimal value _yyyymm_ where _yyyy_ and _mm_ are the year and month designations of the version of the OpenMP API that the implementation supports." – Hristo Iliev Jun 12 '15 at 17:42