简体   繁体   English

非常奇怪的MPI行为

[英]Very strange MPI behavior

I wrote a program on MPI where it would go around each processor in a sort of ring fashion x amount of times (for example if I wanted it to go twice around the "ring" of four processors it would go to 0, 1, 2, 3, 0 ,1....3). 我在MPI上编写了一个程序,它会以一种环形方式绕过每个处理器x次(例如,如果我希望它在四个处理器的“环”周围转两次,它会转到0,1,2 ,3,0,1 ...... 3)。

Everything compiled fine but when I ran the program on my Ubuntu VM it would never output anything. 一切都很好编译,但当我在我的Ubuntu VM上运行程序时,它永远不会输出任何东西。 It wouldn't even run the first output. 它甚至不会运行第一个输出。 Can anyone explain what's going on? 谁能解释一下发生了什么?

This is my code: 这是我的代码:

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

There's a problem with your neighbour assignment. 您的邻居分配存在问题。 If we insert the following line after the next / from calculation 如果我们next / from计算后插入以下行

printf("Rank %d: from = %d, next = %d\n", rank, from, next);

we get: 我们得到:

$ 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

You want something more like 你想要更像的东西

next = (rank + 1) % size;
from = (rank - 1 + size) % size;

which gives 这使

$ 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

and after that your code seems to work. 之后你的代码似乎有效。

Whether your code is good or not, your first printf should be output. 无论您的代码是否良好,都应输出您的第一个printf。

If you have no messages printed at all, even the printf in the "if(rank==)" block, then it could be a problem with your VM. 如果根本没有打印消息,即使是“if(rank ==)”块中的printf,那么它可能是您的VM的问题。 Are you sure you have any network interface activated on that VM ? 您确定在该VM上激活了任何网络接口吗?

If the answer is yes, it might be useful to check its compatibility with MPI by checking the OpenMPI FAQ over tcp questions. 如果答案是肯定的,那么通过检查OpenMPI FAQ over tcp问题来检查它与MPI的兼容性可能是有用的 Sections 7 ( How do I tell Open MPI which TCP networks to use? ) and 13 ( Does Open MPI support virtual IP interfaces? ) seems both interesting for any possible problems with running MPI in a Virtual Machine. 第7节( 如何告诉Open MPI使用哪些TCP网络? )和13( Open MPI是否支持虚拟IP接口? )似乎对于在虚拟机中运行MPI的任何可能问题都很有趣。

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

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