/* * Copyright (c) 2001-2002 The Trustees of Indiana University. * All rights reserved. * Copyright (c) 1998-2001 University of Notre Dame. * All rights reserved. * Copyright (c) 1994-1998 The Ohio State University. * All rights reserved. * * This file is part of the LAM/MPI software package. For license * information, see the LICENSE file in the top level directory of the * LAM/MPI source distribution. * * $HEADER$ * * $Id: spawn.c,v 1.12 2002/10/09 20:55:48 brbarret Exp $ * * Program to test MPI_Comm_spawn with simple arguments. */ #include #include #include #include static void do_parent(char *cmd); static void do_target(MPI_Comm parent_inter); int main(int argc, char *argv[]) { MPI_Comm parent; MPI_Init(&argc, &argv); /* Check to see if we *were* spawned -- because this is a test, we can only assume the existence of this one executable. Hence, we both mpirun it and spawn it. */ parent = MPI_COMM_NULL; MPI_Comm_get_parent(&parent); if (parent != MPI_COMM_NULL) { do_target(parent); printf("Target (pid %d) finished\n", getpid()); } else { do_parent(argv[0]); printf("Parent (pid %d) finished\n", getpid()); } /* All done */ MPI_Finalize(); printf("Process %d finished\n", getpid()); return 0; } static void do_parent(char *cmd) { int errcode; int bar; MPI_Comm child_inter; /* Now try the spawn if it's found anywhere */ MPI_Comm_spawn(cmd, MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &child_inter, &errcode); bar = 17; MPI_Send(&bar, 1, MPI_INT, 0, 1, child_inter); } static void do_target(MPI_Comm parent) { int foo; /* Insert arbitrary delay to ensure that the parent is dead */ sleep(5); MPI_Recv(&foo, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, parent, MPI_STATUS_IGNORE); printf("Target got value: %d\n", foo); }