#include<stdio.h>
#include<time.h>
//串行程序
//Seriel code
static long num_steps = 1000000;
double step;
void main()
{
clock_t begin, end;
int i;
double x, pi, sum = 0.0;
double cost;
step = 1.0 / (double)num_steps;
begin = clock();
for (i = 1; i <= num_steps; i++)
{
x = (i - 0.5) * step;
sum = sum + 4.0 / (1.0 + x * x);
}
pi = step * sum;
end = clock();
cost = (double)(end - begin);
printf("%lf \n", pi);
printf("The seriel costed time is :%lf", cost);
}
//使用并行化域并行化程序
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>
static long num_steps = 100000;
double step;
#define NUM_THREADS 2
void main()
{
clock_t begin, end;
double cost;
int i;
double x, pi, sum[NUM_THREADS];
step = 1.0 / (double)num_steps;
omp_set_num_threads(NUM_THREADS);
begin = clock();
#pragma omp parallel
{
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]= sum[id] + 4.0 / (1.0 + x * x);
}
}
for(i=0,pi=0.0;i<NUM_THREADS;i++)
pi = pi+step * sum[i];
end = clock();
cost = (double)(end - begin);
printf("%lf \n", pi);
printf("The costed time is :%lf", cost);
}
//使用共享任务结构并行化的程序
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>
static long num_steps = 1000000;
double step;
#define NUM_THREADS 2
void main()
{
clock_t begin, end;
double cost;
int i;
double pi, sum[NUM_THREADS];
step = 1.0 / (double)num_steps;
omp_set_num_threads(NUM_THREADS);
begin = clock();
#pragma omp parallel
{
double x;
int id;
id = omp_get_thread_num();
sum[id] = 0.0;
#pragma omp for
for (i = id; i < num_steps; i++)
{
x = (i + 0.5) * step;
sum[id] = sum[id] + 4.0 / (1.0 + x * x);
}
}
for (i = 0, pi = 0.0; i < NUM_THREADS; i++)
pi = pi + step * sum[i];
end = clock();
cost = (double)(end - begin);
printf("%lf \n", pi);
printf("The parallel costed time is :%lf", cost);
}
//使用private字句和critical部分并行化的程序
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>
static long num_steps = 1000000;
double step;
#define NUM_THREADS 2
void main()
{
clock_t begin, end;
double cost;
double x;
int i;
double pi=0, sum=0.0;
step = 1.0 / (double)num_steps;
int id;
omp_set_num_threads(NUM_THREADS);
begin = clock();
#pragma omp parallel private(x,sum)
{
id = omp_get_thread_num();
for (i = id,sum=0.0; i < num_steps; i=i+NUM_THREADS)
{
x = (i + 0.5) * step;
sum = sum + 4.0 / (1.0 + x * x);
}
#pragma omp critical
pi =pi+sum*step;
}
end = clock();
cost = (double)(end - begin);
printf("%lf \n", pi);
printf("The parallel costed time is :%lf", cost);
}
//使用并行规约得出的程序
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>
static long num_steps = 1000000;
double step;
#define NUM_THREADS 4
void main()
{
clock_t begin, end;
double cost;
double x;
int i;
double pi = 0, sum = 0.0;
step = 1.0 / (double)num_steps;
int id;
omp_set_num_threads(NUM_THREADS);
begin = clock();
id = omp_get_thread_num();
#pragma omp parallel for reduction(+:sum) private(x)
for (i = 1; i <= num_steps; i++)
{
x = (i -0.5) * step;
sum = sum + 4.0 / (1.0 + x * x);
}
pi =sum * step;
end = clock();
cost = (double)(end - begin);
printf("%lf \n", pi);
printf("The parallel costed time is :%lf", cost);
}