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

📄 mpi.cpp

📁 一个用c写的机遇MPI的并行程序
💻 CPP
字号:
#include<iostream.h>
#include<mpi.h>
#include<stdio.h>
#include<math.h>
double x2(double);
double x2(double x)
{
	return (x*x);
}
int main(int argc,char *argv[])
{
	while(true)
	{
     int done=0,myid,size,i;
	 long n;
     double value,sum,p,h,x;
     double startwtime=0.0,endwtime;
     int namelen;
     char processor_name[MPI_MAX_PROCESSOR_NAME];
     MPI_Init(&argc,&argv);
      MPI_Comm_size(MPI_COMM_WORLD,&size);
      MPI_Comm_rank(MPI_COMM_WORLD,&myid);
      MPI_Get_processor_name(processor_name,&namelen);
      fprintf(stdout,"Process %d of %d on %s\n",myid,size,processor_name);
      n=0;
      if(myid==0){
	   printf("please give N:" );
	   cin>>n;
	   startwtime=MPI_Wtime();
  }
	  MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
	  h=1.0/(double)n;
	  sum=0.0;
	  for(i=myid+1;i<=n;i+=size){
	    x=h*((double)i-0.5);
		sum+=x2(x);
	  }
	  value=h*sum;
	  MPI_Reduce(&value,&p,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
	  if(myid==0)
	  {   
		  printf("the value is about %.16f\n,error is %.16f\n",p,fabs(p-double(1)/3));
		  endwtime=MPI_Wtime();
		  printf("wall clock time=%f\n",endwtime-startwtime);
		  fflush(stdout);
	  }
	  MPI_Finalize();
	  
	}
	  return 1;
     
}

⌨️ 快捷键说明

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