2006-10-03 06:07:58 +04:00
|
|
|
#include <stdio.h>
|
2006-10-20 22:05:16 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2006-10-03 06:07:58 +04:00
|
|
|
#include <mpi.h>
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int msg;
|
|
|
|
MPI_Comm parent, child;
|
2006-10-20 19:27:29 +04:00
|
|
|
int rank, size;
|
|
|
|
char hostname[512];
|
2006-10-20 22:05:16 +04:00
|
|
|
pid_t pid;
|
2006-10-03 06:07:58 +04:00
|
|
|
|
|
|
|
MPI_Init(NULL, NULL);
|
2007-02-02 17:34:53 +03:00
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
2006-10-03 06:07:58 +04:00
|
|
|
MPI_Comm_get_parent(&parent);
|
|
|
|
/* If we get COMM_NULL back, then we're the parent */
|
|
|
|
if (MPI_COMM_NULL == parent) {
|
2006-10-20 22:05:16 +04:00
|
|
|
pid = getpid();
|
|
|
|
printf("Parent [pid %ld] about to spawn!\n", (long)pid);
|
2007-05-22 00:13:31 +04:00
|
|
|
if (MPI_SUCCESS != MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, size, MPI_INFO_NULL,
|
|
|
|
0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE)) {
|
|
|
|
printf("Child failed to spawn\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-10-03 06:07:58 +04:00
|
|
|
printf("Parent done with spawn\n");
|
2007-02-02 17:34:53 +03:00
|
|
|
if (0 == rank) {
|
|
|
|
msg = 38;
|
|
|
|
printf("Parent sending message to child\n");
|
|
|
|
MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
|
|
|
|
}
|
2006-10-03 06:07:58 +04:00
|
|
|
MPI_Comm_disconnect(&child);
|
|
|
|
printf("Parent disconnected\n");
|
|
|
|
}
|
|
|
|
/* Otherwise, we're the child */
|
|
|
|
else {
|
2006-10-20 19:27:29 +04:00
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
|
|
gethostname(hostname, 512);
|
2006-10-20 22:05:16 +04:00
|
|
|
pid = getpid();
|
|
|
|
printf("Hello from the child %d of %d on host %s pid %ld\n", rank, size, hostname, (long)pid);
|
2007-02-02 17:34:53 +03:00
|
|
|
if (0 == rank) {
|
|
|
|
MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
|
|
|
|
printf("Child %d received msg: %d\n", rank, msg);
|
|
|
|
}
|
2006-10-03 06:07:58 +04:00
|
|
|
MPI_Comm_disconnect(&parent);
|
2006-10-20 19:27:29 +04:00
|
|
|
printf("Child %d disconnected\n", rank);
|
2006-10-03 06:07:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MPI_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|