一、基本原理
所以,圆周率可由上式积分结果乘4求得!
二、原始代码
#include <stdio.h>
#include <omp.h>
double step;
void main()
{
for(int NUM_THREADS=1; NUM_THREADS<=20; NUM_THREADS ++)
{
printf("NUM_THREADS is: %dn", NUM_THREADS);
long num_steps = 1000000000;
double x, pi, sum[NUM_THREADS];
step = 1.0/(double)num_steps;
omp_set_num_threads(NUM_THREADS);
double t_start, t_end;
t_start = omp_get_wtime();
#pragma omp parallel
{
double x;
int i;
int id; id = omp_get_thread_num();
for (i = id, sum[id] = 0.0; i<num_steps; i = i + NUM_THREADS)
{
x = (i - 0.5)*step;
sum[id] += 4.0/(1.0 + x*x);
}
}
int j;
for(j=0,pi=0.0; j<NUM_THREADS; j++) pi += sum[j] * step;
t_end = omp_get