Blame test/mpi/cxx/coll/icreducex.cxx

Packit Service c5cf8c
/* -*- Mode: C++; c-basic-offset:4 ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *
Packit Service c5cf8c
 *  (C) 2003 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#include "mpi.h"
Packit Service c5cf8c
#include <iostream>
Packit Service c5cf8c
#include "mpitestconf.h"
Packit Service c5cf8c
#include "mpitestcxx.h"
Packit Service c5cf8c
Packit Service c5cf8c
static char MTEST_Descrip[] = "Simple intercomm reduce test";
Packit Service c5cf8c
Packit Service c5cf8c
int main(int argc, char *argv[])
Packit Service c5cf8c
{
Packit Service c5cf8c
    int errs = 0;
Packit Service c5cf8c
    int *sendbuf = 0, *recvbuf = 0;
Packit Service c5cf8c
    int leftGroup, i, count, rank;
Packit Service c5cf8c
    MPI::Intercomm comm;
Packit Service c5cf8c
    MPI::Datatype datatype;
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Init();
Packit Service c5cf8c
Packit Service c5cf8c
    datatype = MPI::INT;
Packit Service c5cf8c
    while (MTestGetIntercomm(comm, leftGroup, 4)) {
Packit Service c5cf8c
        if (comm == MPI::COMM_NULL)
Packit Service c5cf8c
            continue;
Packit Service c5cf8c
        for (count = 1; count < 65000; count = 2 * count) {
Packit Service c5cf8c
            sendbuf = new int[count];
Packit Service c5cf8c
            recvbuf = new int[count];
Packit Service c5cf8c
            /* Get an intercommunicator */
Packit Service c5cf8c
            for (i = 0; i < count; i++) {
Packit Service c5cf8c
                sendbuf[i] = -1;
Packit Service c5cf8c
                recvbuf[i] = -1;
Packit Service c5cf8c
            }
Packit Service c5cf8c
            if (leftGroup) {
Packit Service c5cf8c
                rank = comm.Get_rank();
Packit Service c5cf8c
                try {
Packit Service c5cf8c
                    comm.Reduce(sendbuf, recvbuf, count, datatype, MPI::SUM,
Packit Service c5cf8c
                                (rank == 0) ? MPI::ROOT : MPI::PROC_NULL);
Packit Service c5cf8c
                }
Packit Service c5cf8c
                catch(MPI::Exception e) {
Packit Service c5cf8c
                    errs++;
Packit Service c5cf8c
                    MTestPrintError(e.Get_error_code());
Packit Service c5cf8c
                }
Packit Service c5cf8c
                /* Test that no other process in this group received the
Packit Service c5cf8c
                 * broadcast, and that we got the right answers */
Packit Service c5cf8c
                if (rank == 0) {
Packit Service c5cf8c
                    int rsize;
Packit Service c5cf8c
                    rsize = comm.Get_remote_size();
Packit Service c5cf8c
                    for (i = 0; i < count; i++) {
Packit Service c5cf8c
                        if (recvbuf[i] != i * rsize) {
Packit Service c5cf8c
                            errs++;
Packit Service c5cf8c
                        }
Packit Service c5cf8c
                    }
Packit Service c5cf8c
                } else {
Packit Service c5cf8c
                    for (i = 0; i < count; i++) {
Packit Service c5cf8c
                        if (recvbuf[i] != -1) {
Packit Service c5cf8c
                            errs++;
Packit Service c5cf8c
                        }
Packit Service c5cf8c
                    }
Packit Service c5cf8c
                }
Packit Service c5cf8c
            } else {
Packit Service c5cf8c
                /* In the right group */
Packit Service c5cf8c
                for (i = 0; i < count; i++)
Packit Service c5cf8c
                    sendbuf[i] = i;
Packit Service c5cf8c
                try {
Packit Service c5cf8c
                    comm.Reduce(sendbuf, recvbuf, count, datatype, MPI::SUM, 0);
Packit Service c5cf8c
                }
Packit Service c5cf8c
                catch(MPI::Exception e) {
Packit Service c5cf8c
                    errs++;
Packit Service c5cf8c
                    MTestPrintError(e.Get_error_code());
Packit Service c5cf8c
                }
Packit Service c5cf8c
                /* Check that we have received no data */
Packit Service c5cf8c
                for (i = 0; i < count; i++) {
Packit Service c5cf8c
                    if (recvbuf[i] != -1) {
Packit Service c5cf8c
                        errs++;
Packit Service c5cf8c
                    }
Packit Service c5cf8c
                }
Packit Service c5cf8c
            }
Packit Service c5cf8c
            delete[]sendbuf;
Packit Service c5cf8c
            delete[]recvbuf;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        MTestFreeComm(comm);
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Finalize(errs);
Packit Service c5cf8c
    return 0;
Packit Service c5cf8c
}