Reputation: 161
I have a simulation that I want to write in MPI and I have started to read about it online. In my simulation I have a function to which I pass an argument as a pointer to the array of particles because I need to modify it during simulation. Now, I do not fully understand how MPI works so I wrote this little program.
#include <stdio.h>
#include "mpi.h"
void function(int* a)
{
int size, rank;
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank!=0)
{
printf("%d ", *a);
printf("process: %d", rank);
}
else
{
(*a)++;
printf("%d ", *a);
printf("process: %d", rank);
}
printf("\n");
MPI_Finalize();
}
int main (void) {
int a,i;
i=0;
a=5;
function(&a);
printf("%d %d\n",a,(i++));
printf("%d\n", i);
return 0;
}
when I run it with: mpirun -np 2 ./program
and I got the output like below:
6 process: 0
5 process: 1
5 0
6 0
1
1
Does this means that each process has its own copy of variables a
and i
, and how is MPI still running after I have called MPI_Finalize()
inside function()
.
Upvotes: 1
Views: 3579
Reputation: 8303
This seems to be a fairly common misunderstanding among MPI beginners.
MPI_Init()
and MPI_Finalize()
do not mark the start and end of parallel processing. They mark the start and end of where other MPI calls are permitted. MPI runs multiple separate instances of your entire program, each with its own separate memory space. So yes, each process has its own copy of a
and i
.
Upvotes: 6