Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining global variables in mpi

Tags:

c++

c

mpi

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
like image 743
Mehrdad Avatar asked May 21 '13 14:05

Mehrdad


1 Answers

You could use broadcast:

MPI_Bcast(&x,1,MPI_DOUBLE,0,MPI_COMM_WORLD);

This will sent the value of x on process 0 to all other processes.

like image 67
tune2fs Avatar answered Oct 12 '22 16:10

tune2fs