openmptest

#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);
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值