0

I want to use MPI_Reduce function find largest value and its PID(rank) at the same time, but the result shows it is not true, I don't know how to fix it, result:

PID:1, loc_num:2 
PID:2, loc_num:3 
PID:3, loc_num:4 
global data: 1 
coresponding PID: 0

my program:

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

int main(int argc, char *argv[])
{
    //init MPI
    int PID, P;
    MPI_Init(&argc, &argv);      
    MPI_Comm_size(MPI_COMM_WORLD, &P);
    MPI_Comm_rank(MPI_COMM_WORLD, &PID);
    struct{
        int value;
        int PID;
    } in, out;
    int value = 1;
    in.value = value;
    in.PID = PID;

   for(int i = 1; i <= P; i++){
        if (PID == i){                
            value = value + i;
            printf("PID:%d, loc_num:%d \n",PID, value);
        }

    } 
    MPI_Reduce(&in, &out, 1, MPI_2INT, MPI_MAXLOC, 0, MPI_COMM_WORLD);
    int max_PID = out.PID;
    int max_num = out.value;

    if (PID == 0){

        printf("global data: %d \n", max_num);
        printf("coresponding PID: %d \n",max_PID);
    }
    MPI_Finalize();
    return 0;       
}

I just follow the structure of in.value= value and in.PID=PID and then,every PID calculate value=value+PID so the answer is when PID=1, loc=2;when PID=2, loc=3 ...next compare all of them by max, and sent them to PID=0

5
  • on every ranks, in.value is 1, so what do you expect? Commented Mar 6, 2020 at 0:27
  • in.value is 1 right, and then, in.value=in.value+PID and out.value=max(in.value), and I want to get corresponding PID Commented Mar 6, 2020 at 5:56
  • nope, you never in.value = in.value = PID ! Commented Mar 6, 2020 at 6:03
  • what did you mean? in.value=in.value+PID; it can work Commented Mar 6, 2020 at 8:06
  • my point is you do not make such assignment. Commented Mar 6, 2020 at 9:01

1 Answer 1

2

There is no error in the MPI_Reduce of your example. As @Gilles pointed out, the issue is you are not assigning the newly calculated value to in.value.

If you put the assignment statement after calculation as below, then everything work as expected.

   for(int i = 1; i <= P; i++){
        if (PID == i){
            value = value + i;
            printf("PID:%d, loc_num:%d \n",PID, value);
        }

    }
    in.value = value;
    in.PID = PID;
    MPI_Reduce(&in, &out, 1, MPI_2INT, MPI_MAXLOC, 0, MPI_COMM_WORLD);

In your example below, you are not assigning the calculated values to the in struct object.

    in.value = value; // value is set as 1
    in.PID = PID;
    for(int i = 1; i <= P; i++){
    if (PID == i){
        value = value + i; // calculating the value but not assigning to in.value
        printf("PID:%d, loc_num:%d \n",PID, value);
       }
     }
    // uses the old value for in.value (i.e 1) for reduction
    MPI_Reduce(&in, &out, 1, MPI_2INT, MPI_MAXLOC, 0, MPI_COMM_WORLD);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.