【问题标题】:MPI Ring Leader Election returns segmentation faultMPI 环领导选举返回分段错误
【发布时间】:2017-11-08 17:47:37
【问题描述】:

这就是我想要达到的目标。

Blue is the message.
Yellow is when the specific node changes the leader known to it.
Green is the final election of each node.

代码对我来说似乎是正确的,但无论我尝试什么,它总是卡在 while 循环中。对于运行时的少数节点,它会在一段时间后返回分段错误。

election_status=0;
firstmsg[0]=world_rank;     // self rank
firstmsg[1]=0;              // counter for hops
chief=world_rank;           // each node declares himself as leader
counter=0;                  // message counter for each node

// each node sends the first message to the next one
MPI_Send(&firstmsg, 2, MPI_INT, (world_rank+1)%world_size, 1, MPI_COMM_WORLD);
printf("Sent ID with counter to the right node [%d -> %d]\n",world_rank, (world_rank+1)%world_size);

while (election_status==0){
    // EDIT: Split MPI_Recv for rank 0 and rest
    if (world_rank==0){
        MPI_Recv(&incoming, 2, MPI_INT, world_size-1, 1, MPI_COMM_WORLD, &status);
    }
    else {
        MPI_Recv(&incoming, 2, MPI_INT, (world_rank-1)%world_size, 1, MPI_COMM_WORLD, &status);
    }
    counter=counter+1;
    if (incoming[0]<chief){
        chief=incoming[0];
    }
    incoming[1]=incoming[1]+1;

    // if message is my own and hopped same times as counter
    if (incoming[0]==world_rank && incoming[1]==counter) {
        printf("Node %d declares node %d a leader.\n", world_rank, chief);  
        election_status=1;
    }
    //sends the incremented message to the next node
    MPI_Send(&incoming, 2, MPI_INT, (world_rank+1)%world_size, 1, MPI_COMM_WORLD);  
}

MPI_Finalize();

【问题讨论】:

  • 如果你不能跳出while循环,就意味着你永远不会进入第二个if()。所以你需要明白为什么。
  • 对我来说似乎很好奇,计数器和传入[1] 正在不停地递增。为什么在理论上应该对每个节点执行的命令进行延续?是不是应该在节点收到消息后,对每个节点执行counter=counter+1命令?
  • 嘿,你的(world_rank-1)MPI_Recv 看起来不太好。用rank=0 考虑root。
  • 谢谢!这就是问题所在,我将接收分成 2 个 if 并且它正在工作!

标签: c mpi distributed-computing mpich leader


【解决方案1】:

为了确定所有等级的多个等级中的某个最小数量,请使用MPI_Allreduce

  • MPI_Send 正在阻塞。它可以永远阻塞,直到发布匹配的接收。您的程序在第一次调用 MPI_Send 时出现死锁 - 任何后续的一次调用都应该是巧合完成的。为避免这种情况,请专门使用MPI_Sendrecv
  • (world_rank-1)%world_size 将为world_rank == 0 生成-1。使用-1 作为排名号无效。可能巧合的是MPI_ANY_SOURCE

【讨论】:

  • 我将接收分成 2 个 if。它工作得很好......谢谢!
  • 你的程序还是不正确!不要依赖MPI_Send 有时表现出的非阻塞行为。将MPI_Sendrecv 用于可移植的一致性程序。
猜你喜欢
  • 2016-03-22
  • 2021-11-08
  • 2014-05-07
  • 2017-06-25
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 2012-05-11
相关资源
最近更新 更多