45 #ifdef HAVE_TEUCHOS_MPI
47 #endif // HAVE_TEUCHOS_MPI
51 template<
class PacketType>
56 #ifdef HAVE_TEUCHOS_MPI
57 using Teuchos::MpiComm;
58 #endif // HAVE_TEUCHOS_MPI
62 typedef PacketType packet_type;
67 out <<
"Testing Teuchos::scatter<int, "
68 << TypeNameTraits<packet_type>::name ()
69 <<
"> with root = " << root << endl;
73 int gblSuccess = lclSuccess;
75 #ifdef HAVE_TEUCHOS_MPI
76 using Teuchos::MpiComm;
77 int errCode = MPI_SUCCESS;
79 const MpiComm<int>* mpiComm =
dynamic_cast<const MpiComm<int>*
> (&comm);
81 (mpiComm == NULL, std::logic_error,
"Building with MPI, but default "
82 "communicator is not a Teuchos::MpiComm!");
83 MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
84 #endif // HAVE_TEUCHOS_MPI
88 const int myRank = comm.
getRank ();
89 const int numProcs = comm.
getSize ();
90 const int sendCount = 10;
91 const int recvCount = 10;
93 out <<
"Initializing receive buffer (on all processes)" << endl;
97 for (
int i = 0; i < recvCount; ++i) {
101 out <<
"Filling send buffer (on root process only)" << endl;
107 if (myRank == root) {
108 sendBuf.
resize (sendCount * numProcs);
113 for (
int p = 0; p < numProcs; ++p) {
114 for (
int k = 0; k < sendCount; ++k) {
115 const packet_type val = static_cast<packet_type> (p+1) *
116 (ONE + static_cast<packet_type> (k));
117 sendBuf[p*sendCount + k] = val;
120 }
catch (std::exception& e) {
121 std::cerr <<
"Root process " << root <<
" threw an exception: "
122 << e.what () << endl;
127 #ifdef HAVE_TEUCHOS_MPI
131 gblSuccess = lclSuccess;
132 errCode = MPI_Bcast (&gblSuccess, 1, MPI_INT, root, rawMpiComm);
134 (errCode != MPI_SUCCESS, std::logic_error,
"MPI_Bcast failed!");
136 (gblSuccess != 1, std::logic_error,
"Filling the send buffer failed on "
137 "the root (" << root <<
") process! This probably indicates a bug in "
139 #endif // HAVE_TEUCHOS_MPI
142 out <<
"About to invoke scatter" << endl;
143 scatter<int, packet_type> (sendBuf.
getRawPtr (), sendCount,
146 out <<
"Done with scatter" << endl;
149 for (
int k = 0; k < recvCount; ++k) {
150 const packet_type expectedVal = static_cast<packet_type> (myRank+1) *
151 (ONE + static_cast<packet_type> (k));
154 lclSuccess = success ? 1 : 0;
155 gblSuccess = lclSuccess;
165 #ifdef HAVE_TEUCHOS_MPI
166 errCode = MPI_Allreduce (&lclSuccess, &gblSuccess, 1,
167 MPI_INT, MPI_MIN, rawMpiComm);
169 (errCode != MPI_SUCCESS, std::logic_error,
"MPI_Allreduce failed!");
170 #else // HAVE_TEUCHOS_MPI
172 (comm.
getSize () != 1, std::logic_error,
"Not building with MPI, but "
173 "communicator has size = " << comm.
getSize () <<
" != 1. We don't know "
174 "how to test this case.");
176 (comm.
getRank () != root, std::logic_error,
"Not building with MPI, but "
177 "the one process of rank " << comm.
getRank () <<
" is not the root "
178 "process of rank " << root <<
". We don't know how to test this case.");
179 #endif // HAVE_TEUCHOS_MPI
181 return gblSuccess == 1;
190 typedef PacketType packet_type;
193 out <<
"Testing Teuchos::scatter" << endl;
197 const int numProcs = comm->getSize ();
201 for (
int root = 0; root < numProcs; ++root) {
202 out <<
"Test for root = " << root << endl;
203 const bool curSuccess = testScatter<packet_type> (success, out, root, *comm);
205 success = success && curSuccess;
209 out <<
"Done with test!" << endl;