‘Hello OpenMP’: My first OpenMP Program.

My first test of OpenMP after setting up using Visual Studio 2015.

The processor used is an Intel Core i7 4600HQ with 4 cores and 8 threads.

Code

#include <stdio.h>
#include <omp.h>
int main()
{
 #pragma omp parallel
 {
  int ID = omp_get_thread_num();
  printf("hello(%d)", ID);
  printf("world(%d)\n", ID);
  int a;
  scanf_s("%d", &a);
 }
}

Outputs:

Test 1:

hello(0)hello(3)world(3)
hello(5)hello(7)hello(6)world(6)
hello(4)world(4)
hello(2)world(2)
world(5)
world(0)
world(7)
hello(1)world(1)

Test 2:

hello(0)world(0)
hello(6)hello(3)hello(5)world(5)
hello(7)hello(1)world(1)
hello(2)world(2)
world(3)
world(7)
world(6)
hello(4)world(4)

Test 3:

hello(0)world(0)
hello(2)hello(4)hello(5)world(5)
world(2)
hello(1)hello(7)world(4)
world(7)
hello(6)world(6)
world(1)
hello(3)world(3)

This illustrates the interleaving of thread execution.

Advertisements

#c, #openmp, #parallel-computing