gpt4 book ai didi

c - 非常奇怪的 MPI 行为

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:58 25 4
gpt4 key购买 nike

我在 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;
}

最佳答案

您的邻居分配有问题。如果我们在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

然后您的代码似乎可以正常工作。

关于c - 非常奇怪的 MPI 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com