views:

290

answers:

3

Hi, I have a C++ program using OpenMP, which will run on several machines that may have or not have OpenMP installed.

How could I make my program know if a machine has no OpenMP and ignore those "#include ", OpenMP directives (like "#pragma omp parallel ...") and/or library functions (like "tid = omp_get_thread_num();") ?

Thanks and regards!

+3  A: 

OpenMP is a compiler runtime thing and not a platform thing.

ie. If you compile your app using Visual Studio 2005 or higher, then you always have OpenMP available as the runtime supports it. (and if the end-user doesn't have the Visual Studio C runtime installed, then your app won't work at all).

So, you don't need to worry, if you can use it, it will always be there just like functions such as strcmp. To make sure they have the CRT, then you can install the visual studio redistributable.

edit:

ok, but GCC 4.1 will not be able to compile your openMP app, so the issue is not the target machine, but the target compiler. As all compilers have pre-defined macros giving their version, wrap your OpenMP calls with #ifdef blocks. for example, GCC uses 3 macros to identify the compiler version, __GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__

gbjbaanb
My problem is that I want to run the program without multi-threading on those machines that don't have it. GCC below version 4.2.x doesn't support OpenMP. So I want to make my Makefile be able to tell this and ask g++ to ignore the OpenMP part in my program instead of failing the compilation. Any idea?
Tim
A: 

Ditch OpenMP and go with Thread Building Blocks from Intel (which does the same thing but without compiler hacks). To eliminate the dependency on a .DLL you can get the source code and link it into your application as a static library.

TheVinn
+5  A: 

Compilers are supposed to ignore #pragma directives they don't understand; that's the whole point of the syntax. And the functions defined in openmp.h have simple well-defined meanings on a non-parallel system -- in particular, the header file will check for whether the compiler defines ENABLE_OPENMP and, if it's not enabled, provide the right fallbacks.

So, all you need is a copy of openmp.h to link to. Here's one: http://cms.mcc.uiuc.edu/qmcdev/docs/html/OpenMP_8h-source.html .

The relevant portion of the code, though, is just this:

#if defined(ENABLE_OPENMP)
#include <omp.h>
#else
typedef int omp_int_t;
inline omp_int_t omp_get_thread_num() { return 0;}
inline omp_int_t omp_get_max_threads() { return 1;}
#endif

At worst, you can just take those three lines and put them in a dummy openmp.h file, and use that. The rest will just work.

Brooks Moses