Search code examples
cperformanceparallel-processingmpihpc

Updating an array using 2 different process using MPI


I am trying to update an array int arr[2] using 2 processes, where the first index will be filled by process 1 and the second index will be filled by process 2. I have written the following code for the same

#include<stdio.h>
#include"mpi.h"

int main( int argc, char *argv[])
{
    int arr[2];
      
    
    MPI_Init(&argc, &argv);
    
    int myrank, size; //size will take care of number of processes 
    
    
    
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank) ;
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    
    if(myrank==0)
    arr[0]=1;
    
    if(myrank==1)
    arr[1]=2;
    
     MPI_Finalize();
    
    printf("%d %d\n",arr[0],arr[1]);
    
    
    return 0;
        
        
}

As expected the output is

0 2
1 0

Or sometimes (when process 1 reaches the print statement first)

1 0
0 2

My question is, I have declared the array outside MPI_Init and I am printing the array after MPI_Finalize(), then why both the processes are printing? And is there any way to store the output

1 2

in the array without actually sending the data from one process to another?


Solution

  • My question is, I have declared the array outside MPI_Init and I am printing the array after MPI_Finalize(), then why both the processes are printing?

    From MPT_INIT one can read:

    The MPI standard does not say what a program can do before an MPI_INIT or after an MPI_FINALIZE. In the MPICH implementation, you should do as little as possible. In particular, avoid anything that changes the external state of the program, such as opening files, reading standard input or writing to standard output.

    and from MPI_FINALIZE:

    All processes must call this routine before exiting. The number of processes running after this routine is called is undefined; it is best not to perform much more than a return rc after calling MPI_Finalize.

    and :

    The MPI standard requires that MPI_Finalize be called only by the same thread that initialized MPI with either MPI_Init or MPI_Init_thread.

    So in your specific case all processes are allocation the array and print it.

    And is there any way to store the output 1 2 in the array without actually sending the data from one process to another?

    There is no processing sending the message between each other. What is happening (very informally) is that MPI starts a complete copy of your program per process, including also the allocation of the array int arr[2];.

    If you want to have only one process allocation it, let us say the process with rank = 0 then do:

    ...
    if(myrank == 0){
     
       int arr[2];
    }
    

    I personally prefer to do the following, declare a pointer:

    int *arr = NULL;
    

    and the allocate it only on the process that needs it:

    if(myrank == 0){
      arr = malloc(sizeof(int) * 2);
      ...
    }