why they use this before#include<bits/stdc++.h>
mainly I've been using #include ...
Now I'm seeing these lines on a cpp program so I became curious.
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
These are indications to the compiler to change its behaviour as if you had passed the equivalent command line flags. For programming competitions, you often submit source code rather than a binary - it's then built and tested using a system you don't control (and can't set the command line on). Putting these settings in as #pragma lines lets you control settings you might not otherwise be able to do in the competition environment.
I have installed the C++ and Fortran compiler and tested the (C++) programs
(serial and parallel versions).
in Fortran when compiling a (parallelized) code without enabling the -openmp option should compile the code in (default) serial mode
but in C++ I get errors like
undefined reference to omp_get_thread_num
but in Fortran when you compile code without the -fopenmp flag it ignores any code which start with $!omp like
$!omp id = omp_get_thread_num()
Is there any option like that in C++?
You can protect your C/C++ code with the _OPENMP define. This way you can avoid introducing calls to the OpenMP runtime whenever your application is not linked against it.
For instance, you can have the following code
void foo (void)
{
#ifdef _OPENMP
printf ("I have been compiled with OpenMP support\n");
#else
printf ("I have been compiled without OpenMP support\n");
#endif
}
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.
What is the equivalent of GCC's #pragma GCC optimize("O0") or VS's #pragma optimize("", off) in Apple LLVM 5.0 compiler?
I need it to disable optimizations for just a section of code.
From a brief search it doesn't look like clang/llvm supports such a pragma at this time. If you don't want to turn off optimizations for an entire file I suggest factoring what you don't want optimized into a separate file and setting -O0 on it separately.
Actually there is now a way to do that by specifying an __attribute__ ((optnone)) to the function that wraps the code you don't want to be optimized.
For instance I'm using it to have a clear benchmark of an inline function
static void BM_notoptimizedfunction(benchmark::State& state) __attribute__ ((optnone)) {
// your code here won't be optimized by clang
}
And that's it !
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.