Showing posts with label OpenMP Addition. Show all posts
Showing posts with label OpenMP Addition. Show all posts

Monday, June 4, 2012

Array addition using OpenMP

OpenMP is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming.

To compile this in linux environment
$gcc -fopenmp omp_add.c -o omp_add

Then you need to define number of threads:
$export OMP_NUM_THREADS=10

To run program:
$./omp_add

Here is omp_add.c
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[]) {
  
    int i, tid, nthreads, n = 10, N = 100000000;
    double *A, *B, tResult, fResult;
    
    time_t start, stop;
    clock_t ticks; long count;

      A = (double *) malloc(N*sizeof(double));
      B = (double *) malloc(N*sizeof(double));

      for (i=0; i<N; i++) {
          A[i] = (double)(i+1);
        B[i] = (double)(i+1);
      }

    time(&start);

    /*
    //this block use single process
    for (i=0; i<N; i++)
    {
            fResult = fResult + A[i] + B[i];
    }
    
    */
    
    //begin of parallel section
    
    #pragma omp parallel private(tid, i,tResult) shared(n,A,B,fResult)
    {
        tid = omp_get_thread_num();
        if (tid == 0) 
        {
            nthreads = omp_get_num_threads();
            printf("Number of threads = %d\n", nthreads);
        }

    #pragma omp for schedule (static, n)
        for (i=0; i < N; i++) {
            tResult = tResult + A[i] + B[i];
        }

    #pragma omp for nowait
        for (i=0; i < n; i++) 
        {
            printf("Thread %d does iteration %d\n", tid, i);
        }
        
    #pragma omp critical 
        fResult = fResult + tResult; 
    }
    //end of parallel section
    
    time(&stop);

      printf("%f\n",fResult);
      
       printf("Finished in about %.0f seconds. \n", difftime(stop, start));
  
     exit(0);
}

Special thanks for Dr. M.C. Jayawardena - BSc (Col), PhD(Uppsala), MIEEE, AMCS(SL) (Lecturer)

For more examples