mpi Process creation and management Spawn

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Master process spawns two worker processes and scatters sendbuf to workers.

master.c

#include "mpi.h"

int main(int argc, char *argv[])
{ 
   int n_spawns = 2;
   MPI_Comm intercomm;

   MPI_Init(&argc, &argv);

   MPI_Comm_spawn("worker_program", MPI_ARGV_NULL, n_spawns, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE); 

   int sendbuf[2] = {3, 5};
   int recvbuf; // redundant for master.

   MPI_Scatter(sendbuf, 1, MPI_INT, &recvbuf, 1, MPI_INT, MPI_ROOT, intercomm);

   MPI_Finalize();
   return 0;
}

worker.c

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

int main(int argc, char *argv[])
{  
   MPI_Init(&argc, &argv);

   MPI_Comm intercomm; 
   MPI_Comm_get_parent(&intercomm);

   int sendbuf[2]; // redundant for worker.
   int recvbuf;

   MPI_Scatter(sendbuf, 1, MPI_INT, &recvbuf, 1, MPI_INT, 0, intercomm);
   printf("recvbuf = %d\n", recvbuf);

   MPI_Finalize();
   return 0;
}

Execution

mpicc master.c -o master_program
mpicc worker.c -o worker_program
mpirun -n 1 master_program


Got any mpi Question?