MPI帮助找到n个数字的最大值

时间:2011-05-27 05:51:23

标签: c++ mpi

我无法使用MPI_Send和MPI_Recv来计算ARRAY_SIZE数字的最大值。 我的逻辑是在主设备中生成ARRAY_SIZE数字,并在主设备和从设备之间拆分数字。

我收到了这些错误:

[compute-0-8:19284] *** Process received signal ***
[compute-0-8:19284] Signal: Segmentation fault (11)
[compute-0-8:19284] Signal code: Address not mapped (1)
[compute-0-8:19284] Failing at address: 0x2995ee4b50
[compute-0-8:19284] [ 0] /lib64/tls/libpthread.so.0 [0x3a2b50c320]
[compute-0-8:19284] [ 1] ./project/project.out(main+0x27c) [0x408b9e]
[compute-0-8:19284] [ 2] /lib64/tls/libc.so.6(__libc_start_main+0xdb) [0x3a2ac1c4bb]
[compute-0-8:19284] [ 3] ./project/project.out(__gxx_personality_v0+0x8a) [0x40877a]
[compute-0-8:19284] *** End of error message ***
mpirun noticed that job rank 0 with PID 19283 on node compute-0-8.local exited on signal 15 (Terminated). 

这是我的代码

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

#define TRIALS 20 
// n = 10000, 100000, 1000000
#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, max, part_max; 
    int slaveSize, masterSize, 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); 
    MPI_Status status;

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

    //check if master   
    if (myid == 0) 
    { 
        //master is generating data set of size n
        for (i=0; i < ARRAY_SIZE; i++) 
            numbers[i] = i;

        slaveSize = (int) floor(ARRAY_SIZE / numprocs); 
        masterSize = slaveSize + ARRAY_SIZE % numprocs; 
        //printf("slaveSize = %d , masterSize = %d\n", slaveSize, masterSize); 
    } 

    startIndex = masterSize + (myid - 1) * slaveSize; 
    endIndex = startIndex + slaveSize; 

    if (myid != 0)
    {
        for (int i = 1; i < numprocs; i++)
            MPI_Send(&numbers,1,MPI_INT,i,10,MPI_COMM_WORLD);
    }
    else
    {
        MPI_Recv(&numbers,1,MPI_INT,0,10,MPI_COMM_WORLD,&status);
    }

    totalTime = 0; 

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

        max = 0; 
        part_max = numbers[0]; 


        //CALCULATE THE MAX

        if (myid == 0) // master 
        { 
            // compute max of master's numbers 
            for (i = 1; i < masterSize; i++) 
            { 
                if (part_max < numbers[i])
                    part_max = numbers[i]; 
            } 
        } 
        else 
        { 
            for (i = startIndex; i < endIndex; i++) 
            { 
                if (part_max < numbers[i])
                    part_max = numbers[i]; 
            } 
        } 

        if (myid == 0)
        {
            for (int i = 1; i < numprocs; i++)
                MPI_Recv(&part_max,1,MPI_INT,i,11,MPI_COMM_WORLD,&status);
        }
        else
        {
            MPI_Send(&max,1,MPI_INT,0,11,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("Max = %d \n", max); 
            totalTime += runTime; 
        } 
    } // end for 
    if (myid == 0) 
        printf("Average time for %d trials = %f", TRIALS, totalTime/TRIALS); 

    MPI_Finalize(); 
}

2 个答案:

答案 0 :(得分:2)

无法判断这是否是唯一的问题,但您只是在主线程中初始化slaveSizemasterSize。在奴隶中,startIndexendIndex未定义,因为您是从那些未初始化的大小计算出来的。 (除非那些堆栈变量以某种方式共享,但那时你已经有了一个普通的旧竞争条件。)

SendRecv看起来并不平衡。第一个MPI_Send会被调用(numprocs-1)²次,但只有一个MPI_Recv可以匹配吗?

答案 1 :(得分:2)

Mat说什么;这段代码有太多问题......

为什么阵列在所有处理器上都有完整的大小?究竟是什么 你使用MPI_Send / Recv吗?在root上有一个大数组,在其他地方有小数组, 使用MPI_Scatter分配大数组,在本地计算最大值,并执行MPI_Reduce(... MPI_MAX ...)