Source

articles_code / parallel_article / openmp / omp_for_eval.c

/*Listing 2: omp_for_eval.c*/
/* Work Sharing construct
https://computing.llnl.gov/tutorials/openMP/#DO

Distributes an array of elements across threads, where each
element is passed as a parameter to a function to be evaluated

*/

#include <omp.h>
#include <stdio.h>
#define N 100000
#define CHUNKSIZE 100
/* dummy function*/
float myfun(float a)
{
  return a*a;
}

int main (int argc, char **argv)  
{

  int i, chunk,tid;
  float a[N], b[N];

  for (i=0; i < N; i++)
    a[i] = i * 1.0;


  /* Get the number of processors */
  printf("Number of processors available:: %d\n",omp_get_num_procs());

  /* Set the chunk size*/
  chunk = CHUNKSIZE;

  /* Set the number of threads to the number of processors*/
  omp_set_num_threads(omp_get_num_procs());
  
#pragma omp parallel shared(a,b,chunk) private(i)
  {
    
#pragma omp for schedule(dynamic,chunk)
    for (i=0; i< N; i++)
      {
	b[i] = myfun(a[i]);
      }
  }  /* end of parallel section */

  printf("For evaluation completed, the result has been stored in array B\n");

  return 0;  
}