OpenMP Tutorial
Prerequisites
- You are familiar with the command-line and you have access to a Linux/Mac OS, or you know how to tweak these commands where needed to run on Windows (you shouldn’t need to change much)
- You have a modern version of GCC installed.
The Hello, World Example
-
Create a new empty working directory and
cd
into it. -
Create a new file called
main.cpp
with the following contents: -
Run the following command to compile/link your code into an executable:
-
Run the following command to run your program:
You should see output similar to:
The #pragma omp parallel
macro told the compile to parallelize the code within the braces. A team of 4 theads are created, as specified by num_threads(4)
. These threads ran concurrently (and potentially in parallel if you have multiple cores) and each printed out "Hello!"
.
You shouldn’t see any mangling of the "Hello!\n"
as a single stream operation (<<
) is guaranteed to be atomic. However, we would run into a mess if we tried to do something like:
as there would be no guarantee that "world"
will be printed directly after "hello"
…another thread may grab control of std::cout
and print something in-between (the same applies to the std::endl
, which normally prints a \n
).
-fopenmp
told GCC to compile using OpenMP (otherwise the #pragma ...
macro would of been ignored).