简体   繁体   English

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

[英]mpi program in c compiles but doesnt run

I am a beginner in MPI programming. 我是MPI编程的初学者。 I am trying to execute a code in which a new matrix is formed from an old one using Anew[i][j]=Aold[i][j]+Aold[i][j+1] and this takes place for 10 times. 我正在尝试执行一个代码,其中使用Anew [i] [j] = Aold [i] [j] + Aold [i] [j + 1]由一个旧矩阵形成一个新矩阵,并且发生10次倍。 I write the following code : 我写下面的代码:

#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 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);
    }
}

} }

The code compiles fine using 该代码可以很好地编译

mpicc mpi_matrix.c

but doesn't run using 但不能使用

mpirun -n 4 mpi_matrix

gives the error 给出错误

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.

Can Anyone help? 有人可以帮忙吗? Thank you 谢谢

EDIT 1: after trying what @mko said. 编辑1:尝试@mko说了什么之后。 The program compiles and runs but with an error. 该程序编译并运行,但出现错误。 Following is the error 以下是错误

[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)

If you compile with mpicc you need to specify -o to provide target file name. 如果使用mpicc进行编译,则需要指定-o以提供目标文件名。

Try this 尝试这个

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

alternatively start it like this 或者像这样开始

mpirun -np 4 ./a.out

By default, if you don't provide -o, mpicc will create a.out executable. 默认情况下,如果不提供-o,则mpicc将创建a.out可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM