使用MPI_Comm_spawn生成进程的MPI中的奇怪输出

时间:2012-06-05 14:38:11

标签: c++ mpi hpc

Amaey帮助我解决了这个问题。

我试图了解 MPI_Comm_spawn 函数来生成进程,因为我正在努力将项目从 PVM 迁移到 MPI 。我找到了一个很好的示例程序here。所以我决定把它改成一点 使父进程向两个子进程发送消息,然后让子进程输出消息。问题是,具有等级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来自master1 0 of 1 。这是怎么回事?为什么它适用于其他子进程?

1 个答案:

答案 0 :(得分:2)

看看这一行: MPI_Send(greeting2, strlen(greeting)+1, MPI_BYTE, i,1,intercomm);

所以除非我忽略了某些东西不是'strlen(问候)'而只是0.你肯定会在发送缓冲区中放入比1个元素更多的东西。我想你想把'strlen(greeting2)'放在那里。

我认为正在发生的是父进程发送一个截断的字符串,并从进程0获取一个回复,填充'greeting'。因此,在第二个MPI_Send'sizeof(问候)'非零,因此您可以通过发送整个消息。