以下代码假设向每个节点发送一系列消息,并报告每次通信所需的时间。目前它可以正常退出进程,但如果我运行超过 2 个进程,它会挂在最后一个交换上。
我在以前的版本中放了语句来检查它挂在哪里,我 90% 确定它是 MPI_FINALIZE 语句,但我不太明白为什么。有任何想法吗?
#include <stdio.h>
#include "/usr/include/mpich2/mpi.h"
#define ping 101
#define pong 101
float buffer[100000];
int main (int argc, char *argv[]){
int error, rank, size; //mpi holders
int i, j, k; //loops
extern float buffer[100000]; //message buffer
int length; //loop again
double start, final, time;
extern float buffer[100000];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(rank!=0){
MPI_Status status;
for(i=1;i<size;i++){
for(length=1;length<=30000;length+=1000){
for(j=0;j<100;j++){
MPI_Recv(buffer, length, MPI_FLOAT, 0, ping, MPI_COMM_WORLD, &status);
MPI_Send(buffer, length, MPI_FLOAT, 0, pong, MPI_COMM_WORLD);
}
}
}
}
if(rank==0){
MPI_Status status;
for(i=1;i<size;i++){
for(length=1;length<=30000;length+=1000){
start = MPI_Wtime();
for(j=0;j<100;j++){
MPI_Send(buffer, length, MPI_FLOAT, i, ping, MPI_COMM_WORLD);
MPI_Recv(buffer, length, MPI_FLOAT, MPI_ANY_SOURCE, pong, MPI_COMM_WORLD, &status);
}
final = MPI_Wtime();
time = final-start;
printf("%s\t%d\t%f\n", "Node", i, time);
}
}
}
MPI_Finalize();
return 0;
}