问题描述
- mpi的sendrecv函数实现雅可比迭代出错,小弟初学,跪求指教。
-
//一维泊松方程 u(x)=x^3,cpu为四核
#include "stdio.h"
#include "math.h"
#define MPICH_SKIP_MPICXX //没有这句就会出现overriding virtual function differs from 'MPI::的编译错误
#include "mpi.h"//将函数库包含进来
#include
#pragma comment (lib, "mpi.lib")//没有这句就会出现unresolved external symbol _MPI_Finalize的连接错误。
double f(double x)
{double y;
y=6*x;
return y;
}
int main( int argc, char argv[])
{
double l=1;//长度
double w=0,e=1;//左右边界条件
int num=2;//进程数
int n=5;//网格数
double dd=l/n/num;//网格大小
double u[7],u0[7];
double eps=0;
int i,j;
int myid, numprocs, namelen;//定义进程号、进程数、进程名
double startwtime, endwtime;//定义起止时间,为了计算效率
char processor_name[MPI_MAX_PROCESSOR_NAME];//用进程名最长的作为长度
MPI_Init(&argc,&argv);//初始化语句,每个程序都一模一样
MPI_Status status;
startwtime = MPI_Wtime();//得到开始时间
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);//得到进程数
MPI_Comm_rank(MPI_COMM_WORLD,&myid);//得到进程号
MPI_Get_processor_name(processor_name,&namelen);//得到进程名,其实进程名不是必须的
for(j=0;j
u[j]=0;
u[0]=0;
u[n+1]=1;}//初始化
te:
eps=0;
for(j=1;j
u0[j]=u[j];}
//for(j=0;j
//u0[j]=pow(((j-0.5)*dd+myid*n*dd),3);
//u0[j]=u[j];}
int up=myid-1;
if (up
int down=myid+1;
if (down>1){ down=MPI_PROC_NULL;}
//if(myid
//MPI_Recv(&u[n+1],1,MPI_DOUBLE,myid+1,1000,MPI_COMM_WORLD,&status);
//MPI_Send(&u[n],1,MPI_DOUBLE,myid+1,1000,MPI_COMM_WORLD);}
//if (myid>0) {
//MPI_Send(&u[1],1,MPI_DOUBLE,myid-1,1000,MPI_COMM_WORLD);
//MPI_Recv(&u[0],1,MPI_DOUBLE,myid-1,1000,MPI_COMM_WORLD,&status);}
//if(myid>0&&myid<1){
MPI_Sendrecv(&u[1],1,MPI_DOUBLE,down,1000,&u[n+1],1,MPI_DOUBLE,myid,1000,MPI_COMM_WORLD,&status);
MPI_Sendrecv(&u[n],1,MPI_DOUBLE,up,1000,&u[0],1,MPI_DOUBLE,myid,1000,MPI_COMM_WORLD,&status);
for(i=1;i<=n;i++)
{
double t=(i-0.5)*dd+myid*n*dd;
if(i==1){
u[i]=(u[i+1]+2*u[0]-f(t)*dd*dd)/3.0;
continue;}
if(i==(n)){
u[i]=(2*u[n+1]+u[i-1]-f(t)*dd*dd)/3.0;
continue;}
u[i]=(u[i-1]+u[i+1]-f(t)*dd*dd)/2;
}
//if(myid<num-1){u[myid][n+1]=/*u[myid+1][0];}/(u[myid+1][1]+u[myid][n]-f((myid+1)*n*dd)*dd*dd)/2;
//u[myid+1][0]=u[myid][n+1];}
//if(myid>0){u[myid][0]=/*u[myid-1][n+1];}*/(u[myid][1]+u[myid-1][n+1]-f(myid*n*dd)*dd*dd)/2;
//u[myid-1][n+1]=u[myid][0];}
for(j=1;j
eps=eps+fabs(u0[j]-u[j]);}
if(eps>0.1){goto te;}
endwtime = MPI_Wtime();//得到终止时间
printf("wall clock time = %fn", endwtime-startwtime);//得到墙上时间
MPI_Finalize();//关闭MPI
for(j=1;j<n+1;j++){
printf("u[%d]=%fn",j,u[j]);}
return 0;
}
解决方案
初学者吗?你这又是进程又是动态库的,如果你不是练手玩的可以帮你调一下,不过我可没有mpi.lib的库。看起来也没那么复杂
时间: 2024-11-01 20:17:39