Defining global variables in mpi
Asked Answered
K

1

8

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
Kittie answered 21/5, 2013 at 14:30 Comment(3)
The variable probably have the same address and different value because it's not really threads but processes?Dehydrogenate
@Joachim: How can I find out if they are processes or threads?Kittie
With current MPI implementations each rank is a separate process.Inhumane
P
5

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.

Postilion answered 21/5, 2013 at 14:35 Comment(1)
The 0 in the above function call is the rank of the process that initiates the broadcast. As you said it is your root process that alters the value of x, that why this parameter is 0. Note that MPI_Bcast is a blocking routine. Every process within the communicator (MPI_COMM_WORLD in this case) needs to call MPI_BCast() before any function can continue.Overspread

© 2022 - 2024 — McMap. All rights reserved.