I have been testing how exactly mpi works with the following code
#include <iostream>
#include <mpi.h>
using namespace std;
int main(int argc, char *argv[]){
r = 3.0;
int id;
int p;
int a[100];
for(int i=0;i<100;++i){a[i]=i+5; }
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &p);
cout<<id<<" "<<r<<" "<<a[id]<<endl;
MPI_Finalize();
cout<< "Hello world " <<endl;
return 0;
}
I am using 30 cores to run the code. but the output is somehow surprising on 2 aspects,
- Here this question says that if you initialize a variable before MPI_Init(), the other process but 0 will be blind about the initialized value, but in my output file all the process of different ids output the r=3 and the correct value for
a[i]
, so does this mean that I can initialize a variable or an array before calling MPI_Init() and all processes will share the same value for that variable? - The output has a lot of lines of "Hello world " which apparently indicates that each process is printing "Hello world " even after calling MPI_Finalize(), why is that?
By the way, I am using mpicc for compiling the code.