问题
I'm trying to implement the following scenario using mpi_comm_spawn & scatter :
1- Master spawns 2 processes with a job.
2- He scatters an array to those spawned processes.
3- The spawned processes receive the scattered array sort it then send it back.
4- The master receives the sorted parts of the array.
I'd like to know how to do the step 2, so far i've tried with send and receives, they work perfectly but i want to do it with the scatter function.
Edit : Here's what i'd like to do in the master code , i'm missing the part in the slave's where i receive the scattered array
/*Master Here*/
MPI_Comm_spawn(slave, MPI_ARGV_NULL, 2, MPI_INFO_NULL,0, MPI_COMM_WORLD, &inter_comm, array_of_errcodes);
printf("MASTER Sending a message to slaves \n");
MPI_Send(message, 50, MPI_CHAR,0 , tag, inter_comm);
MPI_Scatter(array, 10, MPI_INT, &array_r, 10, MPI_INT, MPI_ROOT, inter_comm);
Thanks.
回答1:
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;
}
Command line
mpicc master.c -o master_program
mpicc worker.c -o worker_program
mpirun -n 1 master_program
来源:https://stackoverflow.com/questions/42729665/receiving-data-in-slaves-mpi-spawn-c