主人不等待产卵的MPI_Reduce

时间:2014-10-03 14:07:52

标签: c++ mpi master slave

我有一个主文件和一个从属文件,主文件在输出结果之前没有等待MPI_Reduce。 (4)从属打印出一行并传回数字10.0,主人应该将它们相加并显示结果。

Master应输出40.0,但这不是我得到的输出。主设备在输出之前输出,让我相信它不等待MPI_Reduce有任何建议吗?

//  Master //

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>


#define MASTER 0        /* task ID of master task */
#define MAXPROCS 4  /* Max numbe of processes*/

int main(int argc, char *argv[])
{
    double  homepi, sum, pi;
    int taskid, numtasks, rc;
    MPI_Status status;
    MPI_Comm kids;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Comm_spawn("SpawnKids", MPI_ARGV_NULL, MAXPROCS, 
              MPI_INFO_NULL, 0, MPI_COMM_WORLD, &kids, MPI_ERRCODES_IGNORE);

    rc = MPI_Reduce(&homepi, &sum, 0, MPI_DOUBLE, MPI_SUM, 0, kids);
    printf("sum :%d\n", sum);

    MPI_Finalize();

    return(0);
}


// Slave //

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int rank, rc;
    double pisum, pi;

    MPI_Status status;
    MPI_Comm parent;
    MPI_Init(&argc, &argv);
    MPI_Comm_get_parent(&parent);
    MPI_Comm_rank(parent, &rank);

    pi = 10.0;
    printf("Rank: %d\n", rank);

    rc = MPI_Reduce(&pi, &pisum, 1, MPI_DOUBLE, MPI_SUM, 0, parent);

    MPI_Finalize();
    return 0;
}


// OUTPUT //

sum :-1285360816
Rank: 3
Rank: 0
Rank: 1
Rank: 2

0 个答案:

没有答案