Blog Archive

Wednesday, July 13, 2011

MPI quick tutorial

http://en.wikipedia.org/wiki/OpenMP


1) Code:        Refer to attachment at the end of this article
2) Compile:   gcc -fopenmp hello.c
3) Run:          ./a.out
4) Results:    (It depends on your own case, mine is 2-core machine)
Hello World from thread 1
Hello World from thread 0
There are 2 threads


Attachment:
#############################

Hello World

This is a basic program, that exercises the parallel, private and barrier directives, as well as the functions omp_get_thread_num and omp_get_num_threads (not to be confused).

This program can be compiled using gcc-4.4 with the flag -fopenmp
//FILENAME: hello.c
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
 
int main (int argc, char *argv[]) {
  int th_id, nthreads;
  #pragma omp parallel private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
    }
  }
  return EXIT_SUCCESS;
}

No comments:

Post a Comment