Please explain the following parallel code template

First, I’d like to say this is a bad place to ask question because you’ve obviously not done any research in to the matter yourself (especially as this template is pretty self explanatory).

I’ll explain it briefly for you however:

#include <omp.h>

Allows access to the openmp library so you can use all of its functions.

main () {

Please tell me you understand this line? It’s very bad practice to not return an int here however, you should really have an int main.

int var1, var2, var3;

Define 3 integers.
Where serial code is written, you can just read normal code here all executing on one thread/processor.

#pragma omp parallel private(var1, var2) shared(var3)

This line is perhaps the most important. It basically says the code in the next set of { } can be executed in parallel. private(var1, var2) means that each thread will get its own copy of these variables (I.e. thread 1’s var1 is not the same as thread 2’s var1) and shared(var3) means that var3 is the same on all threads (and if it changes on thread 1, it also changes on thread 2)

The code executes in parallel until that } is reached at which point the code returns to normal operating mode if you like on one thread.

You should really read some basic OMP tutorials which you can find anywhere on the internet with a very simple google.

I hope this gets you started though.

Leave a Comment