Hello world

C - Serial

download

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <stdio.h>

int main()
{
  int nthread = 1;
  int ithread = 1;
  printf("Hello World from %d of %d!\n", ithread, nthread);

  return 0;
}

C - OpenMP

download

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include <omp.h>

int main()
{
//  omp_set_num_threads(6);
#pragma omp parallel
  {
    int nthread = omp_get_num_threads();
    int ithread = omp_get_thread_num();
    printf("Hello World from %d of %d!\n", ithread, nthread);
  }
  
  return 0;
}

Fortran 90 - OpenMP

download

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
program main
  use OMP_LIB
  implicit none
  
  !$omp parallel
  write(*,*) 'Hello World!'
  !$omp end parallel

  stop
end program main

Threads control

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ export OMP_NUM_THREADS=2
$ ./hello_omp_c.x
Hello World!
Hello World!
$
$ export OMP_NUM_THREADS=4
$ ./hello_omp_c.x
Hello World!
Hello World!
Hello World!
Hello World!