|
Packit |
0848f5 |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
|
|
Packit |
0848f5 |
/*
|
|
Packit |
0848f5 |
* (C) 2001 by Argonne National Laboratory.
|
|
Packit |
0848f5 |
* See COPYRIGHT in top-level directory.
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
#include <stdio.h>
|
|
Packit |
0848f5 |
#include "mpi.h"
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
int main( int argc, char *argv[] )
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
char str[10];
|
|
Packit |
0848f5 |
int err=0, errcodes[256], rank, rank1, nprocs;
|
|
Packit |
0848f5 |
MPI_Comm intercomm1, intercomm2, intracomm;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Init(&argc, &argv);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
|
|
Packit |
0848f5 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank1);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (nprocs != 2) {
|
|
Packit |
0848f5 |
printf("Run this program with 2 processes\n");
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD,1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank1 == 0) {
|
|
Packit |
0848f5 |
printf("Parents spawning 2 children...\n");
|
|
Packit |
0848f5 |
fflush(stdout);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
err = MPI_Comm_spawn("spawn_merge_child1", MPI_ARGV_NULL, 2,
|
|
Packit |
0848f5 |
MPI_INFO_NULL, 1, MPI_COMM_WORLD,
|
|
Packit |
0848f5 |
&intercomm1, errcodes);
|
|
Packit |
0848f5 |
if (err) printf("Error in MPI_Comm_spawn\n");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank1 == 0) {
|
|
Packit |
0848f5 |
printf("Parents and children merging to form new intracommunicator...\n");
|
|
Packit |
0848f5 |
fflush(stdout);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Intercomm_merge(intercomm1, 0, &intracomm);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank1 == 0) {
|
|
Packit |
0848f5 |
printf("Merged parents spawning 2 more children and communicating with them...\n");
|
|
Packit |
0848f5 |
fflush(stdout);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
err = MPI_Comm_spawn("spawn_merge_child2", MPI_ARGV_NULL, 2,
|
|
Packit |
0848f5 |
MPI_INFO_NULL, 2, intracomm,
|
|
Packit |
0848f5 |
&intercomm2, errcodes);
|
|
Packit |
0848f5 |
if (err) printf("Error in MPI_Comm_spawn\n");
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_rank(intercomm2, &rank;;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank == 2) {
|
|
Packit |
0848f5 |
err = MPI_Recv(str, 3, MPI_CHAR, 1, 0, intercomm2, MPI_STATUS_IGNORE);
|
|
Packit |
0848f5 |
printf("Parent received from child: %s\n", str);
|
|
Packit |
0848f5 |
fflush(stdout);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
err = MPI_Send("bye", 4, MPI_CHAR, 1, 0, intercomm2);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Finalize();
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
return 0;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|