Reputation: 580
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
Upvotes: 1
Views: 233
Reputation: 85341
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.
Upvotes: 1