⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sin(x)integral.c

📁 计算sin(x)的并行程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>#include "mpi.h"double startwtime=0.0,endwtime1=0.0,endwtime2=0.0;int main(int argc,char *argv[]){	int n=0;              /*n:要分的精度,即有多少块*/	float *ab;             /*存放上限和下限*/	float temp,*result,sum1=0,w;  /*temp:每个处理器计算出的结果临时存放的地方。result:每个处理器
									处理的结果数组.sum1最终得到的结果.w:每一块有多长*/	int MyID, SumID;	int i;

	float f(float x);
	float Integral(float w,int n,int SumID,int MyID,float ab[2]);
	int ErrMsg(char *msg);
	int GetDataSize();
	float simp(float a,float b,int n);
	MPI_Status status;                   	MPI_Init(&argc,&argv);                /*MPI 初始化*/	MPI_Comm_rank(MPI_COMM_WORLD,&MyID);  /*每个处理器确定各自ID*/    MPI_Comm_size(MPI_COMM_WORLD,&SumID); /*每个处理器确定总处理器个数*/	if(MyID==0)                           /*主处理器*/	{
		
		n=GetDataSize();       /*读入精度,即块数*/
	}
	MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);                                              /*主处理器广播要分的块数*/	
	if(MyID==0)
	{                     		ab=(float *)malloc(2*sizeof(float)); /*主处理器分配积分的上下限*/		if(ab==0) ErrMsg("Malloc memory error!");

		result=(float *)malloc(SumID*sizeof(float)); /*主处理器分配各进程计算的结果*/
		if(result==0) ErrMsg("Malloc memory error!");
		
	}
	if(MyID==0)
	{
		fprintf(stderr,"Please Input a:");fflush(stderr);
		scanf("%f",&ab[0]);
		
		fprintf(stderr,"Please Input b:");fflush(stderr);
		scanf("%f",&ab[1]);
		if(ab[0]!=0)
			w=(ab[1]-ab[0]+1)/SumID;
		else
			w=(ab[1]-ab[0])/SumID;
	}	/*向各个处理器播送每个小块的宽度*/	MPI_Bcast(&w, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
	temp=Integral(w,n,SumID,MyID,ab);/*各个处理器分别计算分配到的块的积分值*/	/*从各个处理器收集已计算好的积分,将结果存到result数组中*/	if(MyID==0)
	{
		*(result)=temp;

		for(i=1;i<SumID;i++)			MPI_Recv(result+i,1,MPI_FLOAT,i,0,MPI_COMM_WORLD,&status);
		endwtime2=MPI_Wtime();	}	else		MPI_Send(&temp,1,MPI_FLOAT,0,0,MPI_COMM_WORLD);	/*将result中收集到的每个处理器计算的积分值相加,得到最终的结果*/	if(MyID==0)
	{
		for(i=0;i<SumID;i++)
		{
			sum1=sum1+result[i];		}
		fprintf(stderr,"The result is %f\n",sum1);fflush(stderr);
		if(SumID==1)
		{
			fprintf(stderr,"The serial time is %f\n",endwtime1-startwtime);
			fflush(stderr);
		}
		else
		{
			fprintf(stderr,"The parallel time is (without commnication time)%f\n",endwtime1-startwtime);
			fprintf(stderr,"The parallel time is(add commnication time) %f\n",endwtime2-startwtime);
			fflush(stderr);
		}	}		MPI_Finalize();           return 0;}float Integral(float w,int n,int SumID,int MyID,float ab[2]){    float sum=0,start,end;
	start=w*(float)MyID;      /*所属部分数据起始位置*/	end=start+w; /*所属部分数据结束位置*/
    
	if(MyID==0)
	{
		start=start+ab[0];
	}
	if(MyID==0)
		startwtime=MPI_Wtime();
	
	sum=simp(start,end,n/SumID);
	
	if(MyID==0)
		endwtime1=MPI_Wtime();
	return sum;
	}int GetDataSize(){	int n;	while(1)
	{		fprintf(stderr,"Input the N :");
		fflush(stderr);		scanf("%d",&n);		if((n>0) && (n<=65535))			break;		ErrMsg("Wrong Data Size, must between [1..65535]");	}	return n;}int ErrMsg(char *msg){	printf("Error: %s \n",msg);    return 1;}
float f(float x)
{
	return (float)sin(x);
}
float simp(float a,float b,int n)
{
	int i;
	float h,s;
	h=(b-a)/(2*n);
	s=(float)(0.5*(f(a)-f(b)));
	for(i=1;i<=n;i++)
		s+=2*f(a+(2*i-1)*h)+f(a+2*i*h);
	return ((b-a)*s/(3*n));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -