C++ PROGRAMMING

# OpenMP Tutorial

Article by:

## 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

1. Create a new empty working directory and cd into it.

2. Create a new file called main.cpp with the following contents:

 1 2 3 4 5 6 7 8  #include int main() { #pragma omp parallel num_threads(4) { std::cout << "Hello!\n"; } } 
3. Run the following command to compile/link your code into an executable:

 1  $g++ main.cpp -o hello_world -fopenmp  4. Run the following command to run your program:  1  $ ./hello_world 

You should see output similar to:

 1 2 3 4 5  \$ ./hello_world Hello! Hello! Hello! Hello! 

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:

 1  std::cout << "Hello, " << "world" << std::endl; 

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).

## Authors

### Geoffrey Hunter

Dude making stuff.