我是MPI编程的初学者。我正在尝试执行一个代码,其中使用Anew [i] [j] = Aold [i] [j] + Aold [i] [j + 1]从旧矩阵形成新矩阵,这将发生在10倍。我写了以下代码:
#include"mpi.h"
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void calculate();
int taskid,numtasks,numworkers,rowmean,offset,t,
BEGIN=1,
msgtype,
start,
RTAG=3,
LTAG=2,
NX=20,
NY=20,
MASTER=0,
NONE=0,
DONE=4,
TIMESTEP=10,
source,
ext,
end,
left,
rows,
right,
dest;
MPI_Status status;
float u[2][NX][NY];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
numworkers=numtasks-1;
rowmean=NX/numworkers;
ext=NX%numworkers;
if (taskid==MASTER)
{
offset=0;
for (int i=1;i<=numworkers;i++)
{
if (i==1)
{
left=NONE;
}
else
{
left=i-1;
}
if (i==numworkers)
{
right=NONE;
}
else
{
right=i+1;
}
dest=i;
rows=(i<=ext) ? rowmean+1:rowmean;
MPI_Send(&u[0][offset][0],rows*NY,MPI_FLOAT,dest,BEGIN,MPI_COMM_WORLD);
MPI_Send(&offset,1,MPI_INT,dest,BEGIN,MPI_COMM_WORLD);
MPI_Send(&rows,1,MPI_INT,dest,BEGIN,MPI_COMM_WORLD);
MPI_Send(&right,1,MPI_INT,dest,BEGIN,MPI_COMM_WORLD);
MPI_Send(&left,1,MPI_INT,dest,BEGIN,MPI_COMM_WORLD);
offset=offset+rows;
}
for(int i=1;i<=numworkers;i++)
{
source=i;
msgtype=DONE;
MPI_Recv(&u[1][offset][0],rows*NY,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&offset,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&rows,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
}
MPI_Finalize();
/*end of master code*/
}
/ ************************************工作代码******** ************************************************** **** /
if (taskid!=MASTER)
{
/* initialization*/
for (int i=0;i<2;i++)
{
for (int j=0;j<NX;j++)
{
for(int k=0;k<NY;k++)
{
u[i][j][k]=i+j+k;
//cout<<u[i][j][k]<<" ";
}
//cout<<"\n";
}
//cout<<"\n\n";
}
source=MASTER;
msgtype=BEGIN;
MPI_Recv(&u[0][offset][0],rows*NY,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&offset,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&rows,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&left,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
MPI_Recv(&right,1,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
int iz=0;
for (int t=1;t<=TIMESTEP;t++)
{
start=offset;
if (offset==0)
{
start=1;
}
end=offset+rows-1;
if (end==NY-1)
{
end=NY-2;
}
if (left!=NONE)
{
MPI_Send(&u[iz][offset][0],NY,MPI_FLOAT,left,RTAG,MPI_COMM_WORLD);
source=left;
msgtype=LTAG;
MPI_Recv(&u[iz][offset-1][0],NY,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
}
if (right!=NONE)
{
MPI_Send(&u[iz][offset+rows-1][0],NY,MPI_FLOAT,right,LTAG,MPI_COMM_WORLD);
source=right;
msgtype=RTAG;
MPI_Recv(&u[iz][offset+rows][0],NY,MPI_FLOAT,source,msgtype,MPI_COMM_WORLD,&status);
}
iz=1-iz;
calculate(start,end,NY,&u[iz][offset][0],&u[1-iz][offset][0]);
}
MPI_Send(&u[iz][offset][0],rows*NY,MPI_FLOAT,MASTER,DONE,MPI_COMM_WORLD);
MPI_Send(&offset, 1, MPI_INT, MASTER, DONE, MPI_COMM_WORLD);
MPI_Send(&rows, 1, MPI_INT, MASTER, DONE, MPI_COMM_WORLD);
MPI_Finalize;
}
return 0;
}
/* calculate function*/
void calculate(int start,int end,int NY,float *u1, float *u2)
{
for(int i=start;i<=end;i++)
{
for (int j=0;j<NY;j++)
{
*(u2+i*NY+j)=*(u1+i*NY+j)+*(u1+(i+1)*NY+j);
}
}
}
使用
编译代码mpicc mpi_matrix.c
但不使用
运行mpirun -n 4 mpi_matrix
给出错误
mpirun was unable to find the specified executable file, and therefore
did not launch the job. This error was first reported for process
rank 0; it may have occurred for other processes as well.
任何人都可以帮忙吗? 谢谢
编辑1:在尝试了@mko之后说道。该程序编译并运行但有错误。以下是错误
[shekhar-HP-Pavilion-Notebook:3296] *** An error occurred in MPI_Recv
[shekhar-HP-Pavilion-Notebook:3296] *** reported by process [3817603073,3]
[shekhar-HP-Pavilion-Notebook:3296] *** on communicator MPI_COMM_WORLD
[shekhar-HP-Pavilion-Notebook:3296] *** MPI_ERR_TRUNCATE: message truncated
[shekhar-HP-Pavilion-Notebook:3296] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[shekhar-HP-Pavilion-Notebook:3296] *** and potentially your MPI job)
答案 0 :(得分:1)
如果使用mpicc进行编译,则需要指定 -o 以提供目标文件名。
试试这个
mpicc -o mpi_matrix mpi_matrix.c
mpirun -np 4 ./mpi_matrix
或者像这样开始
mpirun -np 4 ./a.out
默认情况下,如果您不提供-o,mpicc将创建 a.out 可执行文件。