gpt4 book ai didi

c - c 中的 mpi 程序可以编译但无法运行

转载 作者:行者123 更新时间:2023-11-30 14:55:47 31 4
gpt4 key购买 nike

我是 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*/
}

/**************************************** worker 代码******** ****************************************************** ****/

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)

最佳答案

如果使用 mpicc 编译,则需要指定 -o 来提供目标文件名。

试试这个

mpicc -o mpi_matrix mpi_matrix.c
mpirun -np 4 ./mpi_matrix

或者像这样启动

mpirun -np 4 ./a.out

默认情况下,如果您不提供 -o,mpicc 将创建 a.out 可执行文件。

关于c - c 中的 mpi 程序可以编译但无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513387/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com