Skip to content
Snippets Groups Projects
integrate_mp.c 1.02 KiB
Newer Older
Adam Caprez's avatar
Adam Caprez committed
/*
 * integrade.c
 * 
 * A prgram integrate the function y=x^2 from 0 to 2.
 *
 */

#include <time.h>
#include <stdio.h>
#include <omp.h> 

/* you can adjust this to get reasonable running time on your computer */
/* #define NUM_STEPS (1<<29) */
/*#define NUM_STEPS 1000000000 */

int main(int argc, char *argv[])
{
  double start, stop; /* for keeping track of running time */
  double sum;
  double x;
  long i;
  long NUM_STEPS=1000000000;

  /* time starts now */
  start = clock();

printf("thread number %d\n",omp_get_thread_num());
printf("current threads %d\n",omp_get_num_threads());
printf("max threads %d\n",omp_get_max_threads());
printf("number procs %d\n",omp_get_num_procs());

  sum = 0;

#pragma omp parallel for reduction(+: sum) 
  for (i = 0; i<NUM_STEPS; i++) {
    x = 2.0 * (double)i / (double)(NUM_STEPS); /* value of x */
    sum += x * x;
  }

  sum=sum*2./NUM_STEPS;
  /* we're done so stop the timer */
  stop = clock();

  printf("found result %f in %.3f seconds\n", sum, (stop-start)/1000000);

  return 0;
}