Blame test/mpi/spawn/spawn1.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2003 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#ifdef HAVE_STRINGS_H
Packit 0848f5
#include <strings.h>
Packit 0848f5
#endif
Packit 0848f5
#ifdef HAVE_STRING_H
Packit 0848f5
#include <string.h>
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "A simple test of Comm_spawn";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0, err;
Packit 0848f5
    int rank, size, rsize, i;
Packit 0848f5
    int np = 2;
Packit 0848f5
    int errcodes[2];
Packit 0848f5
    MPI_Comm parentcomm, intercomm;
Packit 0848f5
    MPI_Status status;
Packit 0848f5
    int can_spawn;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    errs += MTestSpawnPossible(&can_spawn);
Packit 0848f5
    if (can_spawn) {
Packit 0848f5
        MPI_Comm_get_parent(&parentcomm);
Packit 0848f5
Packit 0848f5
        if (parentcomm == MPI_COMM_NULL) {
Packit 0848f5
            /* Create 2 more processes */
Packit 0848f5
            MPI_Comm_spawn((char *) "./spawn1", MPI_ARGV_NULL, np,
Packit 0848f5
                           MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm, errcodes);
Packit 0848f5
        }
Packit 0848f5
        else
Packit 0848f5
            intercomm = parentcomm;
Packit 0848f5
Packit 0848f5
        /* We now have a valid intercomm */
Packit 0848f5
Packit 0848f5
        MPI_Comm_remote_size(intercomm, &rsize);
Packit 0848f5
        MPI_Comm_size(intercomm, &size);
Packit 0848f5
        MPI_Comm_rank(intercomm, &rank;;
Packit 0848f5
Packit 0848f5
        if (parentcomm == MPI_COMM_NULL) {
Packit 0848f5
            /* Master */
Packit 0848f5
            if (rsize != np) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Did not create %d processes (got %d)\n", np, rsize);
Packit 0848f5
            }
Packit 0848f5
            if (rank == 0) {
Packit 0848f5
                for (i = 0; i < rsize; i++) {
Packit 0848f5
                    MPI_Send(&i, 1, MPI_INT, i, 0, intercomm);
Packit 0848f5
                }
Packit 0848f5
                /* We could use intercomm reduce to get the errors from the
Packit 0848f5
                 * children, but we'll use a simpler loop to make sure that
Packit 0848f5
                 * we get valid data */
Packit 0848f5
                for (i = 0; i < rsize; i++) {
Packit 0848f5
                    MPI_Recv(&err, 1, MPI_INT, i, 1, intercomm, MPI_STATUS_IGNORE);
Packit 0848f5
                    errs += err;
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            /* Child */
Packit 0848f5
            char cname[MPI_MAX_OBJECT_NAME];
Packit 0848f5
            int rlen;
Packit 0848f5
Packit 0848f5
            if (size != np) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("(Child) Did not create %d processes (got %d)\n", np, size);
Packit 0848f5
            }
Packit 0848f5
            /* Check the name of the parent */
Packit 0848f5
            cname[0] = 0;
Packit 0848f5
            MPI_Comm_get_name(intercomm, cname, &rlen);
Packit 0848f5
            /* MPI-2 section 8.4 requires that the parent have this
Packit 0848f5
             * default name */
Packit 0848f5
            if (strcmp(cname, "MPI_COMM_PARENT") != 0) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Name of parent is not correct\n");
Packit 0848f5
                if (rlen > 0 && cname[0]) {
Packit 0848f5
                    printf(" Got %s but expected MPI_COMM_PARENT\n", cname);
Packit 0848f5
                }
Packit 0848f5
                else {
Packit 0848f5
                    printf(" Expected MPI_COMM_PARENT but no name set\n");
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
            MPI_Recv(&i, 1, MPI_INT, 0, 0, intercomm, &status);
Packit 0848f5
            if (i != rank) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Unexpected rank on child %d (%d)\n", rank, i);
Packit 0848f5
            }
Packit 0848f5
            /* Send the errs back to the master process */
Packit 0848f5
            MPI_Ssend(&errs, 1, MPI_INT, 0, 1, intercomm);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* It isn't necessary to free the intercomm, but it should not hurt */
Packit 0848f5
        /* Using Comm_disconnect instead of free should provide a stronger
Packit 0848f5
         * test, as a high-quality MPI implementation will be able to
Packit 0848f5
         * recover some resources that it should hold on to in the case
Packit 0848f5
         * of MPI_Comm_free */
Packit 0848f5
        /*     MPI_Comm_free(&intercomm); */
Packit 0848f5
        MPI_Comm_disconnect(&intercomm);
Packit 0848f5
Packit 0848f5
        /* Note that the MTest_Finalize get errs only over COMM_WORLD */
Packit 0848f5
        /* Note also that both the parent and child will generate "No Errors"
Packit 0848f5
         * if both call MTest_Finalize */
Packit 0848f5
        if (parentcomm == MPI_COMM_NULL) {
Packit 0848f5
            MTest_Finalize(errs);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MTest_Finalize(errs);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}