Blame test/mpi/spawn/multiple_ports.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
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 <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include <string.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
#define IF_VERBOSE(a) if (verbose) { printf a ; fflush(stdout); }
Packit 0848f5
Packit 0848f5
/* This test checks to make sure that two MPI_Comm_connects to two different MPI
Packit 0848f5
 * ports match their corresponding MPI_Comm_accepts.  The root process opens two
Packit 0848f5
 * MPI ports and sends the first port to process 1 and the second to process 2.
Packit 0848f5
 * Then the root process accepts a connection from the second port followed by
Packit 0848f5
 * the first port.  Processes 1 and 2 both connect back to the root but process
Packit 0848f5
 * 2 first sleeps for three seconds to give process 1 time to attempt to connect
Packit 0848f5
 * to the root.  The root should wait until process 2 connects before accepting
Packit 0848f5
 * the connection from process 1.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int num_errors = 0, total_num_errors = 0;
Packit 0848f5
    int rank, size;
Packit 0848f5
    char port1[MPI_MAX_PORT_NAME];
Packit 0848f5
    char port2[MPI_MAX_PORT_NAME];
Packit 0848f5
    MPI_Status status;
Packit 0848f5
    MPI_Comm comm1, comm2;
Packit 0848f5
    int verbose = 0;
Packit 0848f5
    int data = 0;
Packit 0848f5
Packit 0848f5
    MTEST_VG_MEM_INIT(port1, MPI_MAX_PORT_NAME * sizeof(char));
Packit 0848f5
    MTEST_VG_MEM_INIT(port2, MPI_MAX_PORT_NAME * sizeof(char));
Packit 0848f5
Packit 0848f5
    if (getenv("MPITEST_VERBOSE")) {
Packit 0848f5
        verbose = 1;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
Packit 0848f5
    if (size < 3) {
Packit 0848f5
        printf("Three processes needed to run this test.\n");
Packit 0848f5
        MPI_Finalize();
Packit 0848f5
        return 0;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
        IF_VERBOSE(("0: opening ports.\n"));
Packit 0848f5
        MPI_Open_port(MPI_INFO_NULL, port1);
Packit 0848f5
        MPI_Open_port(MPI_INFO_NULL, port2);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: opened port1: <%s>\n", port1));
Packit 0848f5
        IF_VERBOSE(("0: opened port2: <%s>\n", port2));
Packit 0848f5
        IF_VERBOSE(("0: sending ports.\n"));
Packit 0848f5
        MPI_Send(port1, MPI_MAX_PORT_NAME, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Send(port2, MPI_MAX_PORT_NAME, MPI_CHAR, 2, 0, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: accepting port2.\n"));
Packit 0848f5
        MPI_Comm_accept(port2, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm2;;
Packit 0848f5
        IF_VERBOSE(("0: accepting port1.\n"));
Packit 0848f5
        MPI_Comm_accept(port1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm1;;
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: closing ports.\n"));
Packit 0848f5
        MPI_Close_port(port1);
Packit 0848f5
        MPI_Close_port(port2);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: sending 1 to process 1.\n"));
Packit 0848f5
        data = 1;
Packit 0848f5
        MPI_Send(&data, 1, MPI_INT, 0, 0, comm1);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: sending 2 to process 2.\n"));
Packit 0848f5
        data = 2;
Packit 0848f5
        MPI_Send(&data, 1, MPI_INT, 0, 0, comm2);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("0: disconnecting.\n"));
Packit 0848f5
        MPI_Comm_disconnect(&comm1;;
Packit 0848f5
        MPI_Comm_disconnect(&comm2;;
Packit 0848f5
    }
Packit 0848f5
    else if (rank == 1) {
Packit 0848f5
        IF_VERBOSE(("1: receiving port.\n"));
Packit 0848f5
        MPI_Recv(port1, MPI_MAX_PORT_NAME, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("1: received port1: <%s>\n", port1));
Packit 0848f5
        IF_VERBOSE(("1: connecting.\n"));
Packit 0848f5
        MPI_Comm_connect(port1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm1;;
Packit 0848f5
Packit 0848f5
        MPI_Recv(&data, 1, MPI_INT, 0, 0, comm1, &status);
Packit 0848f5
        if (data != 1) {
Packit 0848f5
            printf("Received %d from root when expecting 1\n", data);
Packit 0848f5
            fflush(stdout);
Packit 0848f5
            num_errors++;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("1: disconnecting.\n"));
Packit 0848f5
        MPI_Comm_disconnect(&comm1;;
Packit 0848f5
    }
Packit 0848f5
    else if (rank == 2) {
Packit 0848f5
        IF_VERBOSE(("2: receiving port.\n"));
Packit 0848f5
        MPI_Recv(port2, MPI_MAX_PORT_NAME, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("2: received port2: <%s>\n", port2));
Packit 0848f5
        /* make sure process 1 has time to do the connect before this process
Packit 0848f5
         * attempts to connect */
Packit 0848f5
        MTestSleep(3);
Packit 0848f5
        IF_VERBOSE(("2: connecting.\n"));
Packit 0848f5
        MPI_Comm_connect(port2, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm2;;
Packit 0848f5
Packit 0848f5
        MPI_Recv(&data, 1, MPI_INT, 0, 0, comm2, &status);
Packit 0848f5
        if (data != 2) {
Packit 0848f5
            printf("Received %d from root when expecting 2\n", data);
Packit 0848f5
            fflush(stdout);
Packit 0848f5
            num_errors++;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        IF_VERBOSE(("2: disconnecting.\n"));
Packit 0848f5
        MPI_Comm_disconnect(&comm2;;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    MPI_Reduce(&num_errors, &total_num_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
        if (total_num_errors) {
Packit 0848f5
            printf(" Found %d errors\n", total_num_errors);
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            printf(" No Errors\n");
Packit 0848f5
        }
Packit 0848f5
        fflush(stdout);
Packit 0848f5
    }
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return total_num_errors;
Packit 0848f5
}