Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPI and global variables

Tags:

mpi

I have to implement an MPI program. There are some global variables (4 arrays of float numbers and other 6 single float variables) which are first inizialized by the main process reading data from a file. Then I call MPI_Init and, while process of rank 0 waits for results, the other processes (rank 1,2,3,4) work on the arrays etc... The problem is that those array seem not to be initialized anymore, all is set to 0. I tried to move global variable inside the main function but the result is the same. When MPI_Init() is called all processes are created by fork right? So everyone has a memory copy of the father so why do they see not initizialized arrays?

like image 990
SagittariusA Avatar asked Dec 11 '13 18:12

SagittariusA


People also ask

Do MPI processes share global variables?

global variables are private to each MPI rank.

Are global variables on the heap?

Global data structures or global variables are not consumed by stack or heap. They basically allocated in a fixed memory block, which remains unchanged.

Does CPP have global variable?

In the program, the variable “global” is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.

Can a parameter be a global variable?

Passing the PowerShell global variable as a parameter to the function. You can also pass a global variable as a parameter to a function and set its value inside the function. But of course, you'll have to pass the global variable as a REF object.


1 Answers

I fear you have misunderstood.

It is probably best to think of each MPI process as an independent program, albeit one with the same source code as every other process in the computation. Operations that process 0 carries out on variables in its address space have no impact on the contents of the address spaces of other processes.

I'm not sure that the MPI standard even requires process 0 to have values for variables which were declared and initialised prior to the call to mpi_init, that is before process 0 really exists.

Whether it does or not you will have to write code to get the values into the variables in the address space of the other processes. One way to do this would be to have process 0 send the values to the other processes, either one by one or using a broadcast. Another way would be for all processes to read the values from the input files; if you choose this option watch out for contention over i/o resources.

In passing, I don't think it is common for MPI implementations to create processes by forking at the call to mpi_init, forking is more commonly used for creating threads. I think that most MPI implementations actually create the processes when you make a call to mpiexec, the call to mpi_init is the formality which announces that your program is starting its parallel computations.

like image 110
High Performance Mark Avatar answered Oct 30 '22 16:10

High Performance Mark