Blame test/mpi/comm/probe-intercomm.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 <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include <string.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "Test MPI_Probe() for an intercomm";
Packit 0848f5
*/
Packit 0848f5
#define MAX_DATA_LEN 100
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0, recvlen, isLeft;
Packit 0848f5
    MPI_Status status;
Packit 0848f5
    int rank, size;
Packit 0848f5
    MPI_Comm intercomm;
Packit 0848f5
    char buf[MAX_DATA_LEN];
Packit 0848f5
    const char *test_str = "test";
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit 0848f5
Packit 0848f5
    if (size < 2) {
Packit 0848f5
        fprintf(stderr, "This test requires at least two processes.");
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    while (MTestGetIntercomm(&intercomm, &isLeft, 2)) {
Packit 0848f5
        if (intercomm == MPI_COMM_NULL)
Packit 0848f5
            continue;
Packit 0848f5
Packit 0848f5
        MPI_Comm_rank(intercomm, &rank;;
Packit 0848f5
Packit 0848f5
        /* 0 ranks on each side communicate, everyone else does nothing */
Packit 0848f5
        if (rank == 0) {
Packit 0848f5
            if (isLeft) {
Packit 0848f5
                recvlen = -1;
Packit 0848f5
                MPI_Probe(0, 0, intercomm, &status);
Packit 0848f5
                MPI_Get_count(&status, MPI_CHAR, &recvlen);
Packit 0848f5
                if (recvlen != (strlen(test_str) + 1)) {
Packit 0848f5
                    printf(" Error: recvlen (%d) != strlen(\"%s\")+1 (%d)\n", recvlen, test_str,
Packit 0848f5
                           (int) strlen(test_str) + 1);
Packit 0848f5
                    ++errs;
Packit 0848f5
                }
Packit 0848f5
                buf[0] = '\0';
Packit 0848f5
                MPI_Recv(buf, recvlen, MPI_CHAR, 0, 0, intercomm, &status);
Packit 0848f5
                if (strcmp(test_str, buf)) {
Packit 0848f5
                    printf(" Error: strcmp(test_str,buf)!=0\n");
Packit 0848f5
                    ++errs;
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
            else {
Packit 0848f5
                strncpy(buf, test_str, 5);
Packit 0848f5
                MPI_Send(buf, strlen(buf) + 1, MPI_CHAR, 0, 0, intercomm);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MTestFreeComm(&intercomm);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}