Lesson 13

Date: 4/19/2017
High Performance Computing (part I: OpenMP)
Linux System Administration

Parallel regions

  • Simple "Hello World" program. Every thread executes all code enclosed in the parallel section. OpenMP library routines are used to obtain thread identifiers and total number of threads.
    C / C++ - Parallel Region Example
    #include <omp.h>
    #include <stdio.h>
    main ()  {
    int nthreads, tid;
    /* Fork a team of threads with each thread having a private tid variable */
    #pragma omp parallel private(tid)
      /* Obtain and print thread id */
      tid = omp_get_thread_num();
      printf("Hello World from thread = %d\n", tid);
      /* Only master thread does this */
      if (tid == 0) 
        nthreads = omp_get_num_threads();
        printf("Number of threads = %d\n", nthreads);
      }  /* All threads join master thread and terminate */
    Copy the content of the code above into file hello.c, then compile and run it as follows:
    gcc -fopenmp -o hello.x hello.c
    export OMP_NUM_THREADS=4

  • Take me to the Course Website