MPI_SUM的MPI_Reduce不起作用

时间:2015-12-17 01:10:23

标签: c++ mpi reduce

我试图简单地总结一下所有名为" train_hr"的变量。和" test_hr"从所有10个处理器中存储并打印处理器0上的总和。我检查以确保各个总和不是0(它们不是,它们都在1000s)。它保持报告的总和为0.我不知道为什么。我已经看了很多这方面的例子,我完全按照指示完成了。任何帮助将不胜感激。

double train_hr = 0, test_hr = 0;
double train_hr_global = 0, test_hr_global = 0;

//Master processor
if (my_rank == 0) {
    // sends a task to each processor
    int curr_task = 0;
    for(i = 0; i < num_procs; i++) {
        if (curr_task < nsamples_all) {
            MPI_Send(&curr_task, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
            curr_task++;
        }
    }
    int r;
    MPI_Status status;

    //keeps sending tasks to processors until there are no more tasks
    while (curr_task < nsamples_all) {
        MPI_Recv(&r, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);
        MPI_Send(&curr_task, 1, MPI_INT, status.MPI_SOURCE, 1, MPI_COMM_WORLD);
        curr_task++;
    }
    //tell all processors to stop receiving
    int a = -1;
    for (i = 0; i < num_procs; i++) {
        MPI_Send(&a, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
    }
}

//Helper processors
else {
    int stop = 1;
    while(stop != 0){
        int i;
        //Receives task OR stop alert from master
        MPI_Status status;
        MPI_Recv(&i, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
        if (i == -1) {
            stop = 0;
        }
        //computations
        else{
            float r;
            //unimportant computations here
            train_hr += r;
            test_hr += r;   
            //Tells master processor it is done             
            MPI_Send(&i, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
        }
    }
}
//At this point I checked the current values of train_hr and test_hr on each helper processor. They are all non-zero.

MPI_Reduce(&train_hr, &train_hr_global, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&test_hr, &test_hr_global, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

//at this point, the vales of train_hr_global and test_hr_global on the master processor (processor 0) are 0 when they should be the sum of all the processors values.
}

0 个答案:

没有答案