By Joel Yliluoma, September 2007; last update in June 2016 for OpenMP 4.5
In this document, we concentrate on the C++ language in particular, and use GCC to compile the examples.
To harness that power, it is becoming important for programmers to be knowledgeable in parallel programming — making a program execute multiple things simultaneously.
This document attempts to give a quick introduction to OpenMP, a simple C/C++/Fortran compiler extension that allows to add parallelism into existing source code without significantly having to entirely rewrite it.
Note: If your GCC complains that "-fopenmp" is valid for D but not for C++ when you try to use it, or does not recognize the option at all, your GCC version is too old. If your linker complains about missing GOMP functions, you forgot to specify "-fopenmp" in the linking.
More information: http://openmp.org/wp/openmp-compilers/
Here are two simple example programs demonstrating OpenMP.
You can compile them like this:
g++ tmp.cpp -fopenmp
#include <cmath>
int main()
{
const int size = 256;
double sinTable[size];
#pragma omp parallel for
for(int n=0; n<size; ++n)
sinTable[n] = std::sin(2 * M_PI * n / size);
// the table is now initialized
}
#include <cmath>
int main()
{
const int size = 256;
double sinTable[size];
#pragma omp simd
for(int n=0; n<size; ++n)
sinTable[n] = std::sin(2 * M_PI * n / size);
// the table is now initialized
}
#include <cmath>
int main()
{
const int size = 256;
double sinTable[size];
#pragma omp target teams distribute parallel for map(from:sinTable[0:256])
for(int n=0; n<size; ++n)
sinTable[n] = std::sin(2 * M_PI * n / size);
// the table is now initialized
}
#include <complex>
#include <cstdio>
typedef std::complex<double> complex;
int MandelbrotCalculate(complex c, int maxiter)
{
// iterates z = z + c until |z| >= 2 or maxiter is reached,
// returns the number of iterations.
complex z = c;
int n=0;
for(; n<maxiter; ++n)
{
if( std::abs(z) >= 2.0) break;
z = z*z + c;
}
return n;
}
int main()
{
const int width = 78, height = 44, num_pixels = width*height;
const complex center(-.7, 0), span(2.7, -(4/3.0)*2.7*height/width);
const complex begin = center-span/2.0;//, end = center+span/2.0;
const int maxiter = 100000;
#pragma omp parallel for ordered schedule(dynamic)
for(int pix=0; pix<num_pixels; ++pix)
{
const int x = pix%width, y = pix/width;
complex c = begin + complex(x * span.real() / (width +1.0),
y * span.imag() / (height+1.0));
int n = MandelbrotCalculate(c, maxiter);
if(n