| 1 | /* |
| 2 | * Copyright (C) by Argonne National Laboratory |
| 3 | * See COPYRIGHT in top-level directory |
| 4 | */ |
| 5 | |
| 6 | #include "mpi.h" |
| 7 | #include "mpitestconf.h" |
| 8 | #ifdef HAVE_IOSTREAM |
| 9 | // Not all C++ compilers have iostream instead of iostream.h |
| 10 | #include <iostream> |
| 11 | #ifdef HAVE_NAMESPACE_STD |
| 12 | // Those that do often need the std namespace; otherwise, a bare "cout" |
| 13 | // is likely to fail to compile |
| 14 | using namespace std; |
| 15 | #endif |
| 16 | #else |
| 17 | #include <iostream.h> |
| 18 | #endif |
| 19 | #include "mpitestcxx.h" |
| 20 | |
| 21 | static char MTEST_Descrip[] = "Simple intercomm alltoall test"; |
| 22 | |
| 23 | int main(int argc, char *argv[]) |
| 24 | { |
| 25 | int errs = 0; |
| 26 | int *sendbuf = 0, *recvbuf = 0; |
| 27 | int leftGroup, i, j, idx, count, rank, rsize; |
| 28 | MPI::Intercomm comm; |
| 29 | MPI::Datatype datatype; |
| 30 | |
| 31 | MTest_Init(); |
| 32 | |
| 33 | datatype = MPI::INT; |
| 34 | while (MTestGetIntercomm(comm, leftGroup, 4)) { |
| 35 | if (comm == MPI::COMM_NULL) |
| 36 | continue; |
| 37 | for (count = 1; count < 66000; count = 2 * count) { |
| 38 | /* Get an intercommunicator */ |
| 39 | rsize = comm.Get_remote_size(); |
| 40 | rank = comm.Get_rank(); |
| 41 | sendbuf = new int[rsize * count]; |
| 42 | recvbuf = new int[rsize * count]; |
| 43 | for (i = 0; i < rsize * count; i++) |
| 44 | recvbuf[i] = -1; |
| 45 | if (leftGroup) { |
| 46 | idx = 0; |
| 47 | for (j = 0; j < rsize; j++) { |
| 48 | for (i = 0; i < count; i++) { |
| 49 | sendbuf[idx++] = i + rank; |
| 50 | } |
| 51 | } |
| 52 | try { |
| 53 | comm.Alltoall(sendbuf, count, datatype, NULL, 0, datatype); |
| 54 | } |
| 55 | catch(MPI::Exception e) { |
| 56 | errs++; |
| 57 | MTestPrintError(e.Get_error_code()); |
| 58 | } |
| 59 | } else { |
| 60 | int rank, size; |
| 61 | |
| 62 | rank = comm.Get_rank(); |
| 63 | size = comm.Get_size(); |
| 64 | |
| 65 | /* In the right group */ |
| 66 | try { |
| 67 | comm.Alltoall(NULL, 0, datatype, recvbuf, count, datatype); |
| 68 | } |
| 69 | catch(MPI::Exception e) { |
| 70 | errs++; |
| 71 | MTestPrintError(e.Get_error_code()); |
| 72 | } |
| 73 | /* Check that we have received the correct data */ |
| 74 | idx = 0; |
| 75 | for (j = 0; j < rsize; j++) { |
| 76 | for (i = 0; i < count; i++) { |
| 77 | if (recvbuf[idx++] != i + j) { |
| 78 | errs++; |
| 79 | if (errs < 10) |
| 80 | cout << "buf[" << i << "] = " << |
| 81 | recvbuf[i] << " on " << rank << "\n"; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | delete[]recvbuf; |
| 87 | delete[]sendbuf; |
| 88 | } |
| 89 | MTestFreeComm(comm); |
| 90 | } |
| 91 | |
| 92 | MTest_Finalize(errs); |
| 93 | return 0; |
| 94 | } |