Osman Khalid
Osman Khalid

Reputation: 798

Open MPI's MPI_reduce not combining array sums

I am very new to Open MPI. I have made a small program that computes the sum of an array, by splitting array into pieces equal to the number of processes. The problem in my program is that each process is computing right sum of its share of the array, but the individually computed sums are not summed by MPI_reduce function. I tried my best to solve and also consulted the Open MPI manual, but there is still something that I might be missing. I would be grateful for any kind of guidance. Below is the program I made:

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

int main(int argc, char *argv[])
{
    int n, rank, nrofProcs, i;
    int sum, ans;
               //  0,1,2, 3,4,5, 6,7,8, 9
    int myarr[] = {1,5,9, 2,8,3, 7,4,6, 10};

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nrofProcs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    n = 10;
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

    sum = 0.0;

    int remaining = n % nrofProcs;
    int lower =rank*(n/nrofProcs);
    int upper = (lower+(n/nrofProcs))-1;

    for (i = lower; i <= upper; i++)
    {
        sum = sum + myarr[i];
    }

    if(rank==nrofProcs-1)
    {
        while(i<=remaining)
        {
        sum = sum + myarr[i];
        i++;
        }
    }

        /* (PROBLEM IS HERE, IT IS NOT COMBINING "sums") */

    MPI_Reduce(&sum, &ans, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

//  if (rank == 0)
        printf( "rank: %d, Sum ans: %d\n", rank, sum);

    /* shut down MPI */
    MPI_Finalize();

    return 0;
}


Output: 

rank: 2, Sum ans: 17
rank: 1, Sum ans: 13
rank: 0, Sum ans: 15

(Output should be rank: 0, Sum ans: 55)

Upvotes: 1

Views: 1471

Answers (1)

Osman Khalid
Osman Khalid

Reputation: 798

Sorry, I made some mistakes, that I corrected after running parallel debugging on my program. Here I am sharing code to split an array of length N on M processes, where N and M can have any value:

/*
An MPI program split an array of length N on M processes, where N and M can have any value    
*/

    #include <math.h> 
    #include "mpi.h" 
    #include <iostream>
    #include <vector>

    using namespace std;

    int main(int argc, char *argv[])
    {
        int n, rank, nrofProcs, i;
        int sum, ans;
                   //  0,1,2, 3,4,5, 6,7,8, 9, 10
        int myarr[] = {1,5,9, 2,8,3, 7,4,6,11,10};
        vector<int> myvec (myarr, myarr + sizeof(myarr) / sizeof(int) );
        n = myvec.size(); // number of items in array

        MPI_Init(&argc, &argv);

        MPI_Comm_size(MPI_COMM_WORLD, &nrofProcs);

        MPI_Comm_rank(MPI_COMM_WORLD, &rank);

        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

        sum = 0.0;

        int remaining = n % nrofProcs;
        int lower =rank*(n/nrofProcs);
        int upper = (lower+(n/nrofProcs))-1;

        for (i = lower; i <= upper; i++)
        {
            sum = sum + myvec[i];
        }

        if(rank==nrofProcs-1)
        {
            int ctr=0;

            while(ctr<remaining)
            {
            sum = sum + myvec[i];
            ctr++;
            i++;
            }
        }
            /* combine everyone's calculations */
        MPI_Reduce(&sum, &ans, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);


        if (rank == 0)
            cout << "rank: " <<rank << " Sum ans: " << ans<< endl;

        /* shut down MPI */
        MPI_Finalize();

        return 0;
    }

Upvotes: 0

Related Questions