Lesson 13

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


Parallel loops

Exercise
  • The loop iterations can be computed concurrently by threads.
    C / C++ - for Directive Example
    #include <stdio.h>
    #include <omp.h>
    
    #define CHUNKSIZE 100
    #define N     1000
    
    main ()  
    {
    
    int i, chunk, tid;
    float a[N], b[N], c[N];
    
    /* Some initializations */
    for (i=0; i < N; i++)
      a[i] = b[i] = i * 1.0;
    chunk = CHUNKSIZE;
    
    #pragma omp parallel shared(a,b,c,chunk) private(i)
      {
    
      #pragma omp for schedule(dynamic,chunk) nowait
      for (i=0; i < N; i++)
       {
         c[i] = a[i] + b[i];
    
          /* Obtain and print thread id and array index number */
         tid = omp_get_thread_num();
         printf("thread = %d, i = %d\n", tid, i);
       }
    
      }  /* end of parallel section */
    
    }
    
    Copy the content of the code above into file for.c, then compile and run it as follows:
    gcc -fopenmp -o for.x for.c
    export OMP_NUM_THREADS=4
    ./for.x
    
    Run ./for.x several times and observe how the array elements are distributed across the threads.




  • Take me to the Course Website