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