1

I'm practicing this code, but my problem is I cannot make sure that each x created for individual ranks is in fact related to the correct rank.

#include <iostream>
#include "mpi.h"
using namespace std;
const int N = 3;
void print(int x[N]) {
    for (int i = 0; i <N ;i++) {
        cout << x[i] << "\t";
    }
}

int main()
{
    MPI_Init(NULL, NULL);
    int rank;
    int size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    cout << " My rank is : "<<rank << endl; 

    int x[N];
    for (int i = 0; i < N;i++) {
        x[i] = 2*rank + i;
    }
    print(x);
    MPI_Finalize();
}

The result is:

 My rank is : 0
 My rank is : 2
 My rank is : 1
 My rank is : 3
6       7       8
0       1       2
2       3       4
4       5       6

Looking into the results, it is clear that x has been created for ranks correctly, but I expected this result. and I do not know why I am not getting this one.

   My rank is : 0
   0       1       2
   My rank is : 2
   4       5       6
   My rank is : 1
   2       3       4
   My rank is : 3
   6       7       8
MA19
  • 510
  • 3
  • 15

2 Answers2

1

cout buffers output until either a newline or an end of output is encountered (at program exit).

In your case there's only one newline here:

   cout << " My rank is : "<<rank << endl; 

So the rest of the output is written to the console when the program ends, which happens after ranks are synchronized as part of MPI_Finalize(). So that acts as a barrier, synchronizing the output.

Try adding cout << endl; at the end of print():

void print(int x[N]) {
    for (int i = 0; i <N ;i++) {
        cout << x[i] << "\t";
    }
    cout << endl; // <------ here
}

Then you should see your expected interleaved output.

 My rank is : 0
0       1       2
 My rank is : 1
2       3       4
 My rank is : 2
4       5       6
 My rank is : 3
6       7       8

Or even

 My rank is : 3
6       7       8
 My rank is :  My rank is : 10
0       1       2

2       3       4
 My rank is : 2
4       5       6

- the output from different ranks isn't really synchronized.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • I'm not getting the first version of the output you wrote. I am getting the second one and something hello from ranks between numbers – MA19 Mar 07 '21 at 21:23
  • 1
    As I said, the output from different ranks isn't synchronized. Each rank is a separate process, and if they all write to stdout at the same time like you're doing, a messy result is to be expected. See [Ordering Output in MPI](https://stackoverflow.com/questions/5305061/ordering-output-in-mpi) for possible solutions. – rustyx Mar 07 '21 at 21:32
0

Using MPI_Gather and printing data in root solves the problem!

MA19
  • 510
  • 3
  • 15