Hi friends!
have a silly little concurrent programming question
Code:
#include <stdio.h>
#include <omp.h>
static long num_steps = 100000000;
double step;
double excecutionTime;
#define PAD 8
int main()
{
double pi = 0.0;
double sum[4][PAD];
sum[0][0] = 0.0;
sum[1][0] = 0.0;
sum[2][0] = 0.0;
sum[3][0] = 0.0;
step = 1.0/(double) num_steps;
double timeStart = omp_get_wtime();
#pragma omp parallel num_threads(4)
{
int i;
double x;
int ID = omp_get_thread_num();
for (i = ID; i<num_steps;i=i+4)
{
x = (i+0.5)*step;
sum[ID][0] = sum[ID][0] + 4.0/(1.0+x*x);
}
}
double totalsum = sum[0][0]+sum[1][0]+sum[2][0]+sum[3][0];
pi = step * totalsum;
double timeFinish = omp_get_wtime();
excecutionTime = timeFinish-timeStart;
printf("pi = %f \n",pi);
printf("executiontime = %fs \n",excecutionTime);
}
this is a quick little parallel implementation of the openmp "helloworld" and estimating pi by summing the integral of 4/(1+x^2) between 0 and 1
i've ran a serial implementation 10 times as well as this one, how come the serial implementation's execution time stayed at 1.2s all 10 times where as the parallel implementation went between 0.4s-0.9s(yeah, its faster, but the time it takes jumps all over the place). is there anyway to make the execution time more "stable"? did i do something wrong/or is there any important concepts that i'm missing?
i don't mind if u just give me a wikipedia page to read =)