c++ - Defining global variables in mpi -


i have written 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 1 may notice, program defines global variable called x , zeroth thread tries assign value it. when have program run on smp (symmetric multiprocessing) machine 4 cores following results:

10.1 0 0 0 

more interestingly, when change code each thread prints address of variable x, i.e. &x, print same thing.

my question how possible number of threads on smp system share same value address of variable while not share same value?

and second question how should change above code following results?

10.1 10.1 10.1 10.1 

you use broadcast:

mpi_bcast(&x,1,mpi_double,0,mpi_comm_world); 

this sent value of x on process 0 other processes.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -