Text-only Table of Contents (frame/ no frame)
(16) Parallel Regions Example Improved Previous Top Next

Parallel Regions Example Improved

/* C/C++ Example */

/* NAME: PI SPMD final version without false sharing

This program will numerically compute the integral of


from 0 to 1. The value of this integral is pi -- which
is great since it gives us an easy way to check the answer.

History: Written by Tim Mattson, 11/99.

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

#define MAX_THREADS 4

static long num_steps = 100000000;
double step;
int main ()
int i,j;
double pi, full_sum = 0.0;
double start_time, run_time;
double sum[MAX_THREADS];

step = 1.0/(double) num_steps;

for(j=1;j<=MAX_THREADS ;j++){
full_sum = 0.0;
start_time = omp_get_wtime();
#pragma omp parallel private(i)
int id = omp_get_thread_num();
int numthreads = omp_get_num_threads();
double x;

double partial_sum = 0;

#pragma omp single
printf(" num_threads = %d",numthreads);

for (i=id;i< num_steps; i+=numthreads){
x = (i+0.5)*step;
partial_sum += + 4.0/(1.0+x*x);
#pragma omp critical
full_sum += partial_sum;

pi = step * full_sum;
run_time = omp_get_wtime() - start_time;
printf("\n pi is %f in %f seconds %d threds \n ",pi,run_time,j);

Previous Top Next

parallel_regions2.src  last modified Mar 23, 2009 Introduction Table of Contents
(frame/no frame)
(single file)
© Dartmouth College