Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: [OMPI users] experiencing some problems in MPI_reduce usage
From: Osman Khalid (osmankhalid2005_at_[hidden])
Date: 2013-11-05 00:02:19


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];

        sum = sum + myarr[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 */

    return 0;


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

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