Module 10: Open Multi-Processing
  Lecture 19: What is Parallelization?
 


Example 1

A Multi-threaded “Hello World” Program

Example Code
Sample Output
#include “omp.h”
void main(){
#pragma omp parallel
{
int id = omp get thread num();
printf(“hello(%d)”,ID);
}
}
hello(0) hello(3) hello(1)
hello(2)
hello(1) hello(2) hello(0)
hello(3)

“IF” Clause

If an “if” clause is present it must evaluate to .TRUE. (Fortran) or non-zero (C/C++) in order to create a team of threads. Otherwise, the region is executed serially by master thread.

Example 2

A Multi-threaded “Hello World” Program With Clauses

#include “omp.h”
void main( ){
int x = 10;
#pragma omp parallel if(x > 10) num threads(4)
{
int id = omp get thread num();
printf(“hello(%d)”,ID);
}
}

  • Num threads clause to request certain no of threads
  • Omp get thread num() runtime function to return thread ID