我在MPI上编写了一个程序,它会以一种环形方式绕过每个处理器x次(例如,如果我希望它在四个处理器的“环”周围转两次,它会变为0, 1,2,3,0,1 ...... 3)。
一切都编译得很好但是当我在我的Ubuntu VM上运行程序时,它永远不会输出任何东西。它甚至不会运行第一个输出。谁能解释一下发生了什么?
这是我的代码:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv){
int rank, size, tag, next, from, num;
tag = 201;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
next = (rank + 1)/ size;
from = (rank - 1)/size;
if (rank == 0){
printf("How many times around the ring? :: ");
scanf ("%d", &num);
MPI_Send(&num, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
}
do{
MPI_Recv(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %d from process %d\n", rank, num, status.MPI_SOURCE);
if (rank == 0){
num--;
printf("Process 0 has decremented the number\n");
}
printf("Process %d sending %d to process %d\n", rank, num ,next);
MPI_Send(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
}while (num > 0);
printf("Process %d has exited", rank);
if (rank == 0){
MPI_Recv(&num, 1, MPI_INT, size - 1, tag, MPI_COMM_WORLD, &status);
printf("Process 0 has received the last round, exiting");
}
MPI_Finalize();
return 0;
}
答案 0 :(得分:3)
您的邻居分配存在问题。如果我们在next
/ from
计算
printf("Rank %d: from = %d, next = %d\n", rank, from, next);
我们得到:
$ mpirun -np 4 ./ring
Rank 0: from = 0, next = 0
Rank 1: from = 0, next = 0
Rank 2: from = 0, next = 0
Rank 3: from = 0, next = 1
你想要更像
的东西next = (rank + 1) % size;
from = (rank - 1 + size) % size;
给出了
$ mpirun -np 4 ./ring
Rank 0: from = 3, next = 1
Rank 1: from = 0, next = 2
Rank 2: from = 1, next = 3
Rank 3: from = 2, next = 0
之后,您的代码似乎有效。
答案 1 :(得分:0)
无论您的代码是否良好,都应输出您的第一个printf。
如果您根本没有打印任何消息,即使是&#34; if(rank ==)&#34;阻止,那么它可能是您的VM的问题。您确定在该VM上激活了任何网络接口吗?
如果答案是肯定的,通过检查the OpenMPI FAQ over tcp questions.第7节(如何告诉Open MPI使用哪些TCP网络?)来检查其与MPI的兼容性可能很有用。 13( Open MPI是否支持虚拟IP接口?)对于在虚拟机中运行MPI的任何可能问题似乎都很有趣。