开发者

MPI help with finding the max of n numbers

开发者 https://www.devze.com 2023-03-08 13:42 出处:网络
I am having trouble using MPI_Send and MPI_Recv to calculate the max of ARRAY_SIZE numbers. My logic is to generate ARRAY_SIZE numbers in the master, and split the numbers between the master and slave

I am having trouble using MPI_Send and MPI_Recv to calculate the max of ARRAY_SIZE numbers. My logic is to generate ARRAY_SIZE numbers in the master, and split the numbers between the master and slaves.

I am getting these errors:

[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). 

here is my code

#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 + A开发者_运维问答RRAY_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(); 
}


Can't tell if that's the only problem, but you're only initializing slaveSize and masterSize in the master thread. In the slaves, startIndex and endIndex are undefined, since you're calculating them from those sizes which are uninitialized. (Unless those stack variables are somehow shared, but then you've got a plain old race condition there.)

You Send and Recv don't really look balanced either. The first MPI_Send will get called (numprocs-1)² times but there's only one MPI_Recv to match it?


What Mat said; there are too many things wrong with this code...

Why does the array have the full size on all processors? What on earth are you using MPI_Send/Recv for? Have a big array on root, small ones elsewhere, distribute the big array using MPI_Scatter, calculate the max locally, and do an MPI_Reduce(...MPI_MAX...)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号