0

node00 可以成功发送信息到 rank 为 1 的 node01,但是在第二次发送到 node01 时会被阻塞。为什么会这样?非常感谢。我认为代码中没有死锁。

给定 MPI 编程,有 5 个节点

.....
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
MPI_Status status;

int buff;

if(rank ==0)
{  
    buff=123;
    for(int i=1;i<size;i++){
    MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD);  //succeed
    MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD);  //blocked
   }
}
else
{
   MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);//succeed
   MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);//blocked
}
............
4

3 回答 3

0
#include<stdio.h>
#include<math.h>
#include<mpi.h>
#define tag 777
int rank;
int size;
int main(int argc,char *argv[])
{


  MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//MPI_Get_processor_name(processor_name, &namelen);
MPI_Status status;

int buff;
int i;
if(rank ==0)
{  printf("hai");
    buff=123;
    for(i=1;i<size;i++){
    MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD);  //succeed
    MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD);  //blocked
   }
}
else
{
printf("hello");
   MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);//succeed
   MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);//blocked
}

//MPI_Finalize(); //finalize MPI operations
return 0;
}
于 2014-06-11T09:28:41.660 回答
0

为什么不使用 MPI_Isend 和 MPI_Irecv。这些是非阻塞调用。

于 2013-04-23T07:44:12.610 回答
0

您应该使用 mpi_finialize,如下所示:

int main(int argc, char *argv[])
{

    int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    int buff, tag = 99;
    MPI_Status status;
    if (rank == 0)
    {
        buff = 123;
        for (int i = 1; i < size; i++)
        {
            MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //succeed
            MPI_Send(&buff, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //blocked
        }
    }
    else
    {
        MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); //succeed
        printf("%d\n", buff);
        MPI_Recv(&buff, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); //blocked
        printf("%d\n", buff);
    }

    MPI_Finalize();
    return 0;
}
于 2019-08-01T14:16:44.413 回答