Amaey 帮我解决了这个问题。
我试图了解MPI_Comm_spawn函数来生成进程,因为我正在将项目从PVM迁移到MPI。我在这里找到了一个很好的示例程序。所以我决定稍微改一下,让父进程向两个子进程发送消息,然后让子进程输出消息。问题是等级为 0 的子进程没有正确接收到消息,它只是接收到其中的一部分,而等级为 1 的子进程接收到消息并正常输出。有人可以解释为什么会发生这种情况,我做错了什么或如何解决这个问题。非常感谢那些可以提供帮助的人!
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define NUM_SPAWNS 2
// Based on the example from: http://mpi.deino.net/mpi_functions/MPI_Comm_spawn.html
int main( int argc, char *argv[] )
{
int my_rank;
int size;
int np = NUM_SPAWNS;
int errcodes[NUM_SPAWNS];
MPI_Comm parentcomm, intercomm;
char greeting[100];
char greeting2[100];
char greeting3[100];
MPI_Init( &argc, &argv );
MPI_Status stat;
MPI_Comm_get_parent( &parentcomm );
if (parentcomm == MPI_COMM_NULL)
{
/* Create 2 more processes - this example must be called spawn_example.exe for this to work. */
MPI_Comm_spawn( "spawn_example", MPI_ARGV_NULL, np, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm, errcodes );
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Called this Jreeting because process 0 in the new MPI_COMM_WORLD was only receiving a part of this string.
sprintf(greeting2, "Jreeting from master1 %d of %d\n", my_rank, size);
sprintf(greeting3, "Greeting from master2 %d of %d\n", my_rank, size);
for(int i = 0; i<np;i++)
{
if(i == 0)
{
MPI_Send(greeting2, strlen(greeting)+1, MPI_BYTE, i,1,intercomm);
}
if(i == 1)
{
MPI_Send(greeting3, strlen(greeting)+1, MPI_BYTE, i,1,intercomm);
}
MPI_Recv(greeting, sizeof(greeting), MPI_BYTE, i, 1, intercomm, &stat);
fputs (greeting, stdout);
}
}
else
{
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(my_rank == 0)
{
MPI_Recv(greeting2, sizeof(greeting2), MPI_BYTE, 0, 1, parentcomm, &stat);
std::cout << greeting2 << "\n";
}
if(my_rank == 1)
{
MPI_Recv(greeting3, sizeof(greeting3), MPI_BYTE, 0, 1, parentcomm, &stat);
std::cout << greeting3 << "\n";
}
sprintf(greeting, "Hello world: processor %d of %d\n", my_rank, size);
MPI_Send(greeting, strlen(greeting)+1, MPI_BYTE, 0,1,parentcomm);
}
fflush(stdout);
MPI_Finalize();
return 0;
}
当我编译时,我有警告......:
hrognkelsi:MPI_TUTORIAL gumundureinarsson$ mpic++ spawn_example.cc -o spawn_example
spawn_example.cc: In function ‘int main(int, char**)’:
spawn_example.cc:24: warning: deprecated conversion from string constant to ‘char*’
当我运行时:
hrognkelsi:MPI_TUTORIAL gumundureinarsson$ mpirun spawn_example
Jre
Hello world: processor 0 of 2
Greeting from master2 0 of 1
Hello world: processor 1 of 2
如您所见,子进程仅输出Jre而不是Jreeting from master1 0 of 1应该的。这是怎么回事?为什么它适用于其他子进程?