I have written a sample code below:
#include <stdio.h>
#include <mpi.h>
double x;
int main (int argc, char **argv) {
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank==0) x=10.1;
MPI_Barrier(MPI_COMM_WORLD);
printf("%f\n", x);
MPI_Finalize();
return 0;
}
As one may notice, this program actually defines a global variable called x and the zeroth thread tries to assign some value to it. When I have this program run on an SMP (Symmetric multiprocessing) machine with 4 cores I get the following results:
10.1
0
0
0
More interestingly, when I change my code so that each thread prints the address of variable x, i.e. &x, they all print the same thing.
My question is how it is possible that a number of threads on an SMP system share the same value for the address of a variable while they do not share the same value?
and my second question is how I should change the above code so that I get the following results?
10.1
10.1
10.1
10.1