C++ PROGRAMMING

OpenMP Tutorial

Date Published:
Last Modified:

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:

    #include <iostream>
    
    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:

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

    $ ./hello_world

    You should see output similar to:

    $ ./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:

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


Related Content:

Tags:

comments powered by Disqus