| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "stk_util/parallel/CommSparse.hpp" // for CommSparse |
| 37 |
#include "stk_util/parallel/Parallel.hpp" // for MPI_COMM_WORLD, MPI_Comm, ompi_communicator_t |
| 38 |
#include "stk_util/parallel/ParallelComm.hpp" // for CommBuffer |
| 39 |
#include "stk_util/stk_config.h" // for STK_HAS_MPI |
| 40 |
|
| 41 |
#if defined ( STK_HAS_MPI ) |
| 42 |
|
| 43 |
//BEGINCommSparse |
| 44 |
TEST(ParallelComm, HowToCommunicateOneValue_PackAndCommunicate) |
| 45 |
{ |
| 46 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 47 |
stk::CommSparse commSparse(comm); |
| 48 |
|
| 49 |
int myProcId = commSparse.parallel_rank(); |
| 50 |
int numProcs = commSparse.parallel_size(); |
| 51 |
|
| 52 |
double sendSomeNumber = 100-myProcId; |
| 53 |
|
| 54 |
stk::pack_and_communicate(commSparse, [&commSparse, &sendSomeNumber, &myProcId, &numProcs]() { |
| 55 |
for (int proc=0;proc<numProcs;proc++) { |
| 56 |
if ( proc != myProcId ) { |
| 57 |
stk::CommBuffer& proc_buff = commSparse.send_buffer(proc); |
| 58 |
proc_buff.pack<double>(sendSomeNumber); |
| 59 |
} |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
for (int proc=0;proc<numProcs;proc++) { |
| 64 |
if ( proc != myProcId ) { |
| 65 |
stk::CommBuffer& dataReceived = commSparse.recv_buffer(proc); |
| 66 |
double val = -1; |
| 67 |
dataReceived.unpack(val); |
| 68 |
EXPECT_EQ(100-proc, val); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
TEST(ParallelComm, HowToCommunicateOneValue_RawCommSparse) |
| 74 |
{ |
| 75 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 76 |
stk::CommSparse commSparse(comm); |
| 77 |
|
| 78 |
int myProcId = commSparse.parallel_rank(); |
| 79 |
int numProcs = commSparse.parallel_size(); |
| 80 |
|
| 81 |
double sendSomeNumber = 100-myProcId; |
| 82 |
|
| 83 |
for(int phase = 0; phase < 2; ++phase) { |
| 84 |
for (int proc=0;proc<numProcs;proc++) { |
| 85 |
if ( proc != myProcId ) { |
| 86 |
stk::CommBuffer& proc_buff = commSparse.send_buffer(proc); |
| 87 |
proc_buff.pack<double>(sendSomeNumber); |
| 88 |
} |
| 89 |
} |
| 90 |
if(phase == 0) { |
| 91 |
commSparse.allocate_buffers(); |
| 92 |
} |
| 93 |
else { |
| 94 |
commSparse.communicate(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
for (int proc=0;proc<numProcs;proc++) { |
| 99 |
if ( proc != myProcId ) { |
| 100 |
stk::CommBuffer& dataReceived = commSparse.recv_buffer(proc); |
| 101 |
double val = -1; |
| 102 |
dataReceived.unpack(val); |
| 103 |
EXPECT_EQ(100-proc, val); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
//ENDCommSparse |
| 108 |
//BEGINCommSparse2 |
| 109 |
TEST(ParallelComm, HowToCommunicateAnArbitraryNumberOfValues) |
| 110 |
{ |
| 111 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 112 |
stk::CommSparse commSparse(comm); |
| 113 |
|
| 114 |
int myProcId = commSparse.parallel_rank(); |
| 115 |
int numProcs = commSparse.parallel_size(); |
| 116 |
|
| 117 |
double sendSomeNumber = 100-myProcId; |
| 118 |
|
| 119 |
stk::pack_and_communicate(commSparse, [&commSparse, &sendSomeNumber, &myProcId, &numProcs]() { |
| 120 |
for (int proc=0;proc<numProcs;proc++) { |
| 121 |
if ( proc != myProcId ) { |
| 122 |
stk::CommBuffer& proc_buff = commSparse.send_buffer(proc); |
| 123 |
for (int i=0;i<myProcId;i++) { |
| 124 |
proc_buff.pack<double>(sendSomeNumber+i); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
}); |
| 129 |
|
| 130 |
for (int sourceProc=0; sourceProc < numProcs; sourceProc++) { |
| 131 |
if ( sourceProc != myProcId ) { |
| 132 |
stk::CommBuffer& dataReceived = commSparse.recv_buffer(sourceProc); |
| 133 |
int numItemsReceived = 0; |
| 134 |
while ( dataReceived.remaining() ) { |
| 135 |
double val = -1; |
| 136 |
dataReceived.unpack(val); |
| 137 |
EXPECT_EQ(100-sourceProc+numItemsReceived, val); |
| 138 |
numItemsReceived++; |
| 139 |
} |
| 140 |
int goldNumItemsReceived = sourceProc; |
| 141 |
EXPECT_EQ(goldNumItemsReceived, numItemsReceived); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
//ENDCommSparse2 |
| 146 |
#endif |