并行处理 - 从设备是否生成数据集?

时间:2011-05-24 04:56:13

标签: c++ mpi

在我的并行编程书中,我遇到了这个代码,它说奴隶生成了数据集,但是,我认为master实际上生成了数据集。

这一行特别是我相信master生成数据集的原因。

for (i=0; i < ARRAY_SIZE; i++) 
        numbers[i] = i; 

有人可以确认主设备或从设备是否生成数据集吗?

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

#define TRIALS 20 
#define ARRAY_SIZE 1000000 

int main(int argc, char *argv[]) 
{ 
    int myid, numprocs; 
    double startwtime, endwtime; 
    int namelen; 
    int* numbers = new int[ARRAY_SIZE]; 
    int i, j, sum, part_sum; 
    int s, s0, startIndex, endIndex; 
    double totalTime; 

    char processor_name[MPI_MAX_PROCESSOR_NAME]; 

    MPI_Init(&argc,&argv); 
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
    MPI_Get_processor_name(processor_name,&namelen); 

    fprintf(stderr,"Process %d on %s\n", myid, processor_name); 
    fflush(stderr); 

    for (i=0; i < ARRAY_SIZE; i++) 
        numbers[i] = i; 

    if (myid == 0) 
    { 
        s = (int) floor(ARRAY_SIZE/numprocs); 
        s0 = s + ARRAY_SIZE%numprocs; 
        //printf("s=%d , s0= %d\n", s, s0); 
    } 

    MPI_Bcast(&s, 1, MPI_INT, 0, MPI_COMM_WORLD); 
    MPI_Bcast(&s0, 1, MPI_INT, 0, MPI_COMM_WORLD); 

    startIndex = s0 + (myid - 1)*s; 
    endIndex = startIndex + s; 

    totalTime = 0; 

    for (j = 1; j <= TRIALS; j++) 
    { 
        if (myid == 0) 
        { 
            startwtime = MPI_Wtime(); 
        } 

        sum = 0; 
        part_sum = 0; 

        if (myid == 0) // master 
        { 
            // compute sum of master's numbers 
            for (i = 0; i < s0; i++) 
            { 
                part_sum += numbers[i]; 
            } 
        } 
        else 
        { 
            for (i = startIndex; i < endIndex; i++) 
            { 
                part_sum += numbers[i]; 
            } 
        } 
        MPI_Reduce(&part_sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); 
        if (myid == 0) 
        { 
            double runTime; 
            endwtime = MPI_Wtime(); 
            runTime = endwtime - startwtime; 

            printf("Trial %d : Execution time (sec) = %f\n", j, runTime); 
            printf("Sum = %d \n", sum); 
            totalTime += runTime; 
        } 
    } // end for 
    if (myid == 0) 
        printf("Average time for %d trials = %f", TRIALS, totalTime/TRIALS); 

    MPI_Finalize(); 
} 

1 个答案:

答案 0 :(得分:2)

主服务器都会生成整个数组。您必须记住,您的程序在所有节点上运行,并且有问题的代码部分不区分主/从。所以你的书的措辞没有错,但可以澄清。 :)