Ignore OpenMP on machine that does not have it - c++

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 <omp.h>, OpenMP directives (like #pragma omp parallel ...) and/or library functions (like tid = omp_get_thread_num();) ?

OpenMP compilation adds the preprocessor definition "_OPENMP", so you can do:
#if defined(_OPENMP)
#pragma omp ...
#endif
For some examples, see http://bisqwit.iki.fi/story/howto/openmp/#Discussion and the code which follows.

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.

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__

How could I make my program know if a machine has no OpenMP and ignore those #include <omp.h>, OpenMP directives (like #pragma omp parallel ...) and/or library functions (like tid = omp_get_thread_num();) ?
Here's a late answer, but we just got a bug report due to use of #pragma omp simd on Microsoft compilers.
According to OpenMP Specification, section 2.2:
Conditional Compilation
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 onth designations of the version of the OpenMP API that the
implementation supports.
It appears modern Microsoft compilers only support OpenMP from sometime between 2000 and 2005. I can only say "sometime between" because OpenMP 2.0 was released in 2000, and OpenMP 2.5 was released in 2005. But Microsoft advertises a version from 2002.
Here are some _OPENMP numbers...
Visual Studio 2012 - OpenMP 200203
Visual Studio 2017 - OpenMP 200203
IBM XLC 13.01 - OpenMP 201107
Clang 7.0 - OpenMP 201107
GCC 4.8 - OpenMP 201107
GCC 8.2 - OpenMP 201511
So if you want to use, say #pragma omp simd to guard a loop, and #pragma omp simd is available in OpenMP 4.0, then:
#if _OPENMP >= 201307
#pragma omp simd
for (size_t i = 0; i < 16; ++i)
data[i] += x[i];
#else
for (size_t i = 0; i < 16; ++i)
data[i] += x[i];
#endif
which will run on several machines that may have or not have OpenMP installed.
And to be clear, you probably need to build your program on each of those machines. The x86_64 ABI does not guarantee OpenMP is available on x86, x32 or x86_64 machines. And I have not read you can build on one machine, and then run on another machine.

There is another approach that I like, borrowed from Bisqwit:
#if defined(_OPENMP)
#include <omp.h>
extern const bool parallelism_enabled = true;
#else
extern const bool parallelism_enabled = false;
#endif
Then, start your OpenMP parallel for loops like this:
#pragma omp parallel for if(parallelism_enabled)
Note: there are valid reasons for not using pragma, which is non-standard, hence why Google and others do not support it.

Related

remove thread::hardware_concurrency by preprocessor if not declared

I have C++ code that uses
int nthreads = thread::hardware_concurrency();
When I try to build my application on some computers, I get the error message
error: 'thread' has not been declared
I do not ask how to solve the error. I'm more interested in if there is a special "guard" that I can use to simply remove the lines of code by the preprocessor like for example in OpenMP I can use:
#ifdef _OPENMP
...
#endif
std::thread::hardware_concurrency is provided by C++11. If compilers on those "some computers" support it you may have to toggle C++11 support (e.g. with mingw/gcc -std=c++11).
To disable hardware_concurrency usage when unavailable, the best solution might be to detect C++11 support.
This answer may work out for you, even if you use Visual Studio:
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
int nthreads = std::thread::hardware_concurrency();
#else
int nthreads = 4; // whatever fallback value
#endif
Do note that it detects the VS 2015 compiler version as it was the first one to advertize full C++11 support. You can tweak the _MSC_VER check to an older version if hardware_concurrency happens to be available on older VS compilers.

Results of OpenMP target directives on PGI

I'm using PGI to compile the following program which uses OpenMP's target directives to offload work to a GPU:
#include <iostream>
#include <cmath>
int main(){
const int SIZE = 400000;
double *m;
m = new double[SIZE];
#pragma omp target teams distribute parallel for
for(int i=0;i<SIZE;i++)
m[i] = std::sin((double)i);
for(int i=0;i<SIZE;i++)
std::cout<<m[i]<<"\n";
}
My compilation string is as follows:
pgc++ -omp -ta=tesla,pinned,cc60 -Minfo=accel -fast test2.cpp
Compilation succeeds, but it lacks the series of outputs that I get with OpenACC that tell me what the compiler actually did with the directive, like so:
main:
8, Accelerator kernel generated
Generating Tesla code
11, #pragma acc loop gang, vector(128) /* blockIdx.x threadIdx.x */
8, Generating implicit copyout(m[:400000])
How can I get similar information for OpenMP? -Minfo by itself didn't seem to yield anything useful.
"-Minfo" (which is the same as "-Minfo=all"), or "-Minfo=mp" will give you compiler feedback messages for OpenMP compilation.
Though, PGI only supports OpenMP 4.5 directives with our LLVM back-end compilers. These are available by default on IBM Power based systems or as a part of our LLVM beta compilers on x86. The x86 beta compilers can be found at http://www.pgroup.com/support/download_llvm.php but do require a Professional Edition license.
Also, our current OpenMP 4.5 only targets multicore CPU. We're working on GPU target offload as well but this support wont be available for awhile.

How do I create a wrapper for a pragma and have it compile properly?

I've been trying to build a wrapper around MPI and OpenMP to have unified format to code in instead of having to keep switching between MPI_xxx, omp_xxx and #pragma omp xxx.
I've been having issues creating a wrapper around the various #pragma omp directives, so far the best I've gotten is to have it as:
#define _mmc_(x) _Pragma("omp ## #x")
(mmc is the tentative name for my library)
So if I wanted to have
#pragma omp parallel for
The corresponding wrapper should be
_mmc_(parallel for)
However when it compiles the compiler seems to evaluate it differently, giving me the compilation warning
test.cpp:22:0: warning: ignoring #pragma omp [-Wunknown-pragmas]
_mmc_(parallel for)
I am compiling with mpic++ for MPICH 3.0.4 around gcc 4.8.4 in Ubuntu 14.04, with the flags
-fopenmp -lm -std=c++11 -Wall
Is there something that I can do or add to the code to make this work, or is this just something that cannot be done with the current tools?
This should work, as seen here:
#define PRAGMA(x) _Pragma(#x)
#define _mmc_(x) PRAGMA(omp x)
_Pragma is a bit strict on taking a string literal, so we make that literal from the entire pragma argument string rather than piecing it together inside _Pragma. Preprocessor operators will not work inside a string literal, as you've tried in your post.

Why OpenMP program runs only in one thread

I just tried OpenMP with a simple c program
test() {
for(int i=0;i<100000000;i++);
}
main() {
printf("Num of CPU: %d\n", omp_get_num_procs());
#pragma omp parallel for num_threads(4)
for(int i=0;i<100;i++) test();
}
Compiled with g++ -fopenmp. It can correctly print out that I have 4 CPUs, but all test functions are running at thread 0.
I tried to modify the OMP_NUM_THREADS. But it has no effect also.
I had everything the same as the online examples but why wouldn't I get it to work?
Your problem is here:
#pragma omp parallel for num_thread(4) <---
The correct clause is num_threads(4), not num_thread(4). Incorrect openmp pragmas are ignored and so you ended up with a sequential program. :)
I'm surprised you didn't get a compiler warning, because I did.
I had this problem in visual studio and finally I understood that I had forgotten to enable Open MP support in visual studio. It didn't give me any error but executed the program just for one thread
first choose project _> properties -> c/c++ -> language -> open mp support -> choose yes
and then you will find above conformance mode (make it no )
use the function omp_set_num_threads(4) before calling the omp parallel section.
also, how do you determine the number of threads ??
embed your printfs in a critical section just to make sure everything is getting printed.
I encountered the very same situation on my ubuntu desktop when I extends numpy module with C code. openmp only ran with one thread. I happened to remove libopenblas-base and install libatlas-base-dev.(to deal with numpy installation problem) Then multi-threading openmp came back:)
I have tested it on a ubuntu server with 64 cores and it works just as my desktop!
I think this is because libopenblas conflicts with libraries like atlas.

How to disable OpenMP directives in a nice way?

I have C++ code with OpenMP pragmas inside. I want to test this code both for multithread mode (with OpenMP) and in single thread mode (no OpenMP).
For now, to switch between modes I need to comment #pragma omp (or at least parallel).
What is the cleanest, or default, way to enable / disable OpenMP?
If you do not compile with -fopenmp option, you won't get the parallel code. You can do it with an appropiate define and makefile that generates all codes.
The OpenMP documentation says (only an example):
#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_thread_num() 0
#endif
See http://www.openmp.org/mp-documents/spec30.pdf (conditional compilation).
Look into the compiler manual for the switch that disables OpenMP. For GCC, OpenMP is disabled by default and enabled with the -fopenmp option.
Another option would be to run the code with the OMP_NUM_THREADS environment variable set to 1, though that is not exactly the same as compiling without OpenMP in the first place.
The way such things are usually handled (the general case) is with #defines and #ifdef:
In your header file:
#ifndef SINGLETHREADED
#pragma omp
#endif
When you compile, add -DSINGLETHREADED to disable OpenMP:
cc -DSINGLETHREADED <other flags go here> code.c