Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DefaultMpiComm_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 
46 
48 #include "Teuchos_CommHelpers.hpp"
49 #include "Teuchos_DefaultComm.hpp"
51 #include "Teuchos_getConst.hpp"
52 #include "Teuchos_as.hpp"
53 
54 #ifdef HAVE_TEUCHOS_MPI
56 #endif
57 
58 #ifdef HAVE_TEUCHOS_QD
59 # include <qd/dd_real.h>
60 #endif
61 
62 namespace std {
63 
64 
65 template <typename Packet>
66 ostream & operator<< ( ostream& os, const pair<Packet, Packet>& arg)
67 {
68  os << "(" << arg.first << "," << arg.second << ")";
69  return os;
70 }
71 
72 
73 } // namespace std
74 
75 
76 namespace Teuchos {
77 
78 
79 template<typename Packet>
80 struct ScalarTraits<std::pair<Packet,Packet> >
81 {
83  typedef std::pair<typename PST::magnitudeType, typename PST::magnitudeType> magnitudeType;
84  static const bool isComplex = PST::isComplex;
85  static const bool isComparable = PST::isComparable;
86  static const bool hasMachineParameters = PST::hasMachineParameters;
87  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
88  static inline magnitudeType magnitude(std::pair<Packet,Packet> a) { return std::pair<Packet,Packet>( PST::magnitude(a.first), PST::magnitude(a.second) ); }
89  static inline std::pair<Packet,Packet> zero() { return std::pair<Packet,Packet>(PST::zero(),PST::zero()); }
90  static inline std::pair<Packet,Packet> one() { return std::pair<Packet,Packet>(PST::one(), PST::one()); }
91  static inline std::pair<Packet,Packet> conjugate(std::pair<Packet,Packet> x) { return std::pair<Packet,Packet>(PST::conjugate(x.first), PST::conjugate(x.second) ); }
92  static inline std::pair<Packet,Packet> real(std::pair<Packet,Packet> x) { return std::pair<Packet,Packet>(PST::real(x.first), PST::real(x.second) ); }
93  static inline std::pair<Packet,Packet> imag(std::pair<Packet,Packet> x) { return std::pair<Packet,Packet>(PST::imag(x.first), PST::imag(x.second) ); }
94  static inline bool isnaninf(std::pair<Packet,Packet> x) { return PST::isnaninf(x.first) || PST::isnaninf(x.second); }
95  static inline void seedrandom(unsigned int s) { PST::seedrandom(s); }
96  static inline std::pair<Packet,Packet> random() { return std::pair<Packet,Packet>( PST::random(), PST::random() ); }
97  static inline std::string name() { return "std::pair<" + Teuchos::TypeNameTraits<Packet>::name() + "," + Teuchos::TypeNameTraits<Packet>::name() + ">"; }
98  static inline std::pair<Packet,Packet> squareroot(std::pair<Packet,Packet> x) { return std::pair<Packet,Packet>(PST::squareroot(x.first), PST::squareroot(x.second)); }
99  static inline std::pair<Packet,Packet> pow(std::pair<Packet,Packet> x, std::pair<Packet,Packet> y) { return std::pair<Packet,Packet>( PST::pow(x.first,y.first), PST::pow(x.second,y.second) ); }
100 };
101 
102 template<class Packet, class ConvertToPacket>
103 class ValueTypeConversionTraits<std::pair<Packet,Packet>, ConvertToPacket> {
104 public:
105  static std::pair<Packet,Packet> convert( const ConvertToPacket t )
106  {
107  return std::pair<Packet,Packet>(t,t);
108  }
109  static std::pair<Packet,Packet> safeConvert( const ConvertToPacket t )
110  {
111  return std::pair<Packet,Packet>(t,t);
112  }
113 };
114 
115 
116 } // namespace Teuchos
117 
118 
119 namespace {
120 
121 
122 using Teuchos::as;
123 using Teuchos::RCP;
124 using Teuchos::rcp;
125 using Teuchos::Array;
126 using Teuchos::Comm;
130 using Teuchos::outArg;
131 
132 
133 bool testMpi = true;
134 
135 
136 double errorTolSlack = 1e+1;
137 
138 
139 
141 {
142 
144 
145  clp.addOutputSetupOptions(true);
146 
147  clp.setOption(
148  "test-mpi", "test-serial", &testMpi,
149  "Test MPI (if available) or force test of serial. In a serial build,"
150  " this option is ignored and a serial comm is always used." );
151 
152  clp.setOption(
153  "error-tol-slack", &errorTolSlack,
154  "Slack off of machine epsilon used to check test results" );
155 
156 }
157 
158 
159 template<class Ordinal>
160 RCP<const Comm<Ordinal> > getDefaultComm()
161 {
162  if (testMpi) {
163  return DefaultComm<Ordinal>::getComm();
164  }
165  return rcp(new Teuchos::SerialComm<Ordinal>);
166 }
167 
168 
169 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultMpiComm, basic, Ordinal )
170 {
171  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
172  out << "comm = " << Teuchos::describe(*comm);
173  TEST_EQUALITY( size(*comm), GlobalMPISession::getNProc() );
174 }
175 
176 
177 #ifdef HAVE_TEUCHOS_MPI
178 
179 
180 TEUCHOS_UNIT_TEST( DefaultMpiComm, getRawMpiComm )
181 {
182  ECHO(MPI_Comm rawMpiComm = MPI_COMM_WORLD);
183  ECHO(const RCP<const Comm<int> > comm =
184  Teuchos::createMpiComm<int>(Teuchos::opaqueWrapper(rawMpiComm)));
185  out << "comm = " << Teuchos::describe(*comm);
186  ECHO(MPI_Comm rawMpiComm2 = Teuchos::getRawMpiComm<int>(*comm));
187  TEST_EQUALITY( rawMpiComm2, rawMpiComm );
188 }
189 
190 
191 TEUCHOS_UNIT_TEST( DefaultMpiComm, getRawMpiCommWithTag )
192 {
193  ECHO(MPI_Comm rawMpiComm = MPI_COMM_WORLD);
194  ECHO(const RCP<const Comm<int> > comm =
195  Teuchos::createMpiComm<int>(Teuchos::opaqueWrapper(rawMpiComm), 123));
196  out << "comm = " << Teuchos::describe(*comm);
197  ECHO(MPI_Comm rawMpiComm2 = Teuchos::getRawMpiComm<int>(*comm));
198  TEST_EQUALITY( rawMpiComm2, rawMpiComm );
199  TEST_EQUALITY( comm->getTag(), 123);
200 }
201 
202 
203 #endif // HAVE_TEUCHOS_MPI
204 
205 
206 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, ReadySend1, Ordinal, Packet )
207 {
208 
209  using Teuchos::broadcast;
210  using Teuchos::readySend;
211  using Teuchos::wait;
212  using Teuchos::as;
213  using Teuchos::rcpFromRef;
214  using Teuchos::outArg;
215  using Teuchos::isend;
216  using Teuchos::ireceive;
217  using Teuchos::wait;
218  using Teuchos::SerialComm;
219  using Teuchos::is_null;
220  using Teuchos::arcp;
221  using Teuchos::arcpClone;
222  using Teuchos::rcp_dynamic_cast;
223  using Teuchos::ArrayRCP;
224  using Teuchos::ptr;
226  //typedef typename PT::magnitudeType PacketMag; // unused
227  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
228 
229  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
230  const Ordinal numProcs = size(*comm);
231  const Ordinal procRank = rank(*comm);
232 
233  if (
234  numProcs == 1
235  &&
236  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
237  )
238  {
239  out << "\nThis is Teuchos::SerialComm which does not support readySend!\n";
240  return; // Pass!
241  }
242 
243  PT::seedrandom(as<unsigned int>(procRank));
244  Packet origSendData = PT::random();
245  Packet origRecvData = PT::random();
246  broadcast<Ordinal, Packet>( *comm, 0, outArg(origSendData) );
247 
248  Packet sendData = origSendData;
249  Packet recvData = origRecvData;
250 
251  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
252 
253  // Post non-block receive on proc 0
254  if (procRank == 0) {
255  // Post non-blocking receive from proc n-1
256  recvRequest = ireceive<Ordinal, Packet>(
257  *comm,
258  rcp(&recvData,false),
259  numProcs-1
260  );
261  }
262  barrier(*comm);
263 
264  if (procRank == numProcs-1) {
265  // ready send from proc n-1 to proc 0
266  // send data in sendData
267  readySend<Ordinal, Packet>(
268  *comm,
269  sendData,
270  0
271  );
272  }
273  barrier(*comm);
274 
275  if (procRank == 0) {
276  // wait for request on 0
277  wait( *comm, outArg(recvRequest) );
278  }
279  barrier(*comm);
280 
281  // test that all procs have recvRequest == Teuchos::null
282  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
283 
284  // proc 0 should have recvData == sendData
285  if (procRank == 0) {
286  TEST_EQUALITY( recvData, sendData );
287  }
288  // other procs should have recvData == origRecvData (i.e., unchanged)
289  else {
290  TEST_EQUALITY( recvData, origRecvData );
291  }
292  // all procs should have sendData == origSendData
293  TEST_EQUALITY( sendData, origSendData );
294 
295  // All procs fail if any proc fails
296  int globalSuccess_int = -1;
297  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
298  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
299 }
300 
301 
302 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, ReadySend, Ordinal, Packet )
303 {
304 
305  using Teuchos::broadcast;
306  using Teuchos::readySend;
307  using Teuchos::wait;
308  using Teuchos::as;
309  using Teuchos::rcpFromRef;
310  using Teuchos::outArg;
311  using Teuchos::isend;
312  using Teuchos::ireceive;
313  using Teuchos::wait;
314  using Teuchos::SerialComm;
315  using Teuchos::is_null;
316  using Teuchos::arcp;
317  using Teuchos::arcpClone;
318  using Teuchos::rcp_dynamic_cast;
319  using Teuchos::ArrayRCP;
321  //typedef typename PT::magnitudeType PacketMag; // unused
322  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
323 
324  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
325  const Ordinal numProcs = size(*comm);
326  const Ordinal procRank = rank(*comm);
327 
328  if (
329  numProcs == 1
330  &&
331  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
332  )
333  {
334  out << "\nThis is Teuchos::SerialComm which does not support readySend!\n";
335  return; // Pass!
336  }
337 
338  const int dataLen = 3;
339 
340  const ArrayRCP<Packet> origSendData = arcp<Packet>(dataLen);
341  const ArrayRCP<Packet> origRecvData = arcp<Packet>(dataLen);
342  PT::seedrandom(as<unsigned int>(procRank));
343  for (int j = 0; j < dataLen; ++j) {
344  origSendData[j] = PT::random();
345  origRecvData[j] = PT::random();
346  }
347  broadcast<Ordinal, Packet>( *comm, 0, origSendData() );
348 
349  const ArrayRCP<Packet> sendData = arcpClone<Packet>(origSendData());
350  const ArrayRCP<Packet> recvData = arcpClone<Packet>(origRecvData());
351 
352  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
353 
354  // both proc 0 and proc n-1 will post non-block receives, into recvData
355  // then proc 0 will initiate a ready-send to proc n-1; the latter will initiate a wait
356  // a barrier
357  // then proc n-1 will initiate a ready-send to proc 0 of the data from recvData; the latter will initiate a wait
358  // a barrier
359  // now both of these procs should have matching data in sendData and recvData
360 
361  // Post non-block receive on both procs
362  if (procRank == 0) {
363  // Post non-blocking receive from proc n-1
364  recvRequest = ireceive<Ordinal, Packet>(
365  *comm,
366  recvData.persistingView(0, dataLen),
367  numProcs-1
368  );
369  }
370  else if (procRank == numProcs-1) {
371  // Post non-blocking receive from proc 0
372  recvRequest = ireceive<Ordinal, Packet>(
373  *comm,
374  recvData.persistingView(0, dataLen),
375  0
376  );
377  }
378  barrier(*comm);
379 
380  if (procRank == 0) {
381  // ready send from proc 0 to proc n-1
382  // send data in sendData
383  readySend<Ordinal, Packet>(
384  *comm,
385  sendData(),
386  numProcs-1
387  );
388  }
389  else if (procRank == numProcs-1) {
390  // wait for request on proc n-1
391  wait( *comm, outArg(recvRequest) );
392  }
393  barrier(*comm);
394 
395  if (procRank == 0) {
396  // wait for request on 0
397  wait( *comm, outArg(recvRequest) );
398  }
399  else if (procRank == numProcs-1) {
400  // ready send from proc n-1 to proc 0
401  // send data in recvData: THIS IS IMPORTANT: SEE ABOVE
402  readySend<Ordinal, Packet>(
403  *comm,
404  recvData(),
405  0
406  );
407  }
408  barrier(*comm);
409 
410  // test that all procs (even non-participating) have recvRequest == Teuchos::null
411  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
412 
413  // participating procs should have recvData == sendData
414  if (procRank == 0 || procRank == numProcs-1) {
415  TEST_COMPARE_ARRAYS( recvData, sendData );
416  }
417  // non-participating procs should have recvData == origRecvData (i.e., unchanged)
418  else {
419  TEST_COMPARE_ARRAYS( recvData, origRecvData );
420  }
421  // all procs should have sendData == origSendData
422  TEST_COMPARE_ARRAYS( sendData, origSendData );
423 
424  // All procs fail if any proc fails
425  int globalSuccess_int = -1;
426  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
427  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
428 }
429 
430 
431 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, NonblockingSendReceive, Ordinal, Packet )
432 {
433  using Teuchos::as;
434  using Teuchos::rcpFromRef;
435  using Teuchos::outArg;
436  using Teuchos::isend;
437  using Teuchos::ireceive;
438  using Teuchos::wait;
439  using Teuchos::SerialComm;
440  using Teuchos::rcp_dynamic_cast;
441  using std::endl;
443  //typedef typename PT::magnitudeType PacketMag; // unused
444  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
445 
446  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
447  const Ordinal numProcs = size(*comm);
448  const Ordinal procRank = rank(*comm);
449 
450  if (
451  numProcs == 1
452  &&
453  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
454  )
455  {
456  out << "\nThis is Teuchos::SerialComm which does not yet support isend/ireceive!\n";
457  return; // Pass!
458  }
459 
460  // Only use randomize on one proc and then broacast
461  Packet orig_input_data = PT::random();
462  broadcast( *comm, 0, &orig_input_data );
463 
464  const Packet orig_output_data = as<Packet>(-1);
465 
466  const Packet input_data = orig_input_data;
467  Packet output_data = orig_output_data;
468 
469  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
470  RCP<Teuchos::CommRequest<Ordinal> > sendRequest;
471 
472  out << "Exchanging messages" << endl;
473 
474  if (procRank == 0) {
475  // Create copy of data to make sure that persisting relationship is
476  // maintained!
477  sendRequest = isend<Ordinal, Packet>(
478  *comm, Teuchos::rcp(new Packet(input_data)), numProcs-1);
479  }
480  if (procRank == numProcs-1) {
481  // We will need to read output_data after wait(...) below
482  recvRequest = ireceive<Ordinal, Packet>(
483  *comm, rcpFromRef(output_data), 0);
484  }
485 
486  out << "Waiting for messages" << endl;
487 
488  if (procRank == 0) {
489  wait( *comm, outArg(sendRequest) );
490  }
491  if (procRank == numProcs-1) {
492  wait( *comm, outArg(recvRequest) );
493  }
494 
495  TEST_EQUALITY_CONST( sendRequest, Teuchos::null );
496  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
497 
498  out << "Testing message correctness" << endl;
499 
500  if (procRank == numProcs-1) {
501  TEST_EQUALITY( output_data, input_data );
502  }
503  else {
504  TEST_EQUALITY( output_data, orig_output_data );
505  }
506  TEST_EQUALITY( input_data, orig_input_data );
507 
508  // All procs fail if any proc fails
509  int globalSuccess_int = -1;
510  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
511  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
512 }
513 
514 
515 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, NonblockingSendReceiveSet, Ordinal, Packet )
516 {
517  using Teuchos::as;
518  using Teuchos::rcpFromRef;
519  using Teuchos::outArg;
520  using Teuchos::arcp;
521  using Teuchos::arcpClone;
522  using Teuchos::ArrayRCP;
523  using Teuchos::isend;
524  using Teuchos::ireceive;
525  using Teuchos::wait;
526  using Teuchos::broadcast;
527  using Teuchos::SerialComm;
528  using Teuchos::rcp_dynamic_cast;
529  using std::cerr;
530  using std::endl;
532  //typedef typename PT::magnitudeType PacketMag; // unused
533  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
534 
535  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
536  const Ordinal numProcs = size(*comm);
537  const Ordinal procRank = rank(*comm);
538 
539  if (
540  numProcs == 1
541  &&
542  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
543  )
544  {
545  out << "\nThis is Teuchos::SerialComm which does not yet support isend/ireceive!\n";
546  return; // Pass!
547  }
548 
549  const int numSendRecv = 4;
550  const int sendLen = 3;
551 
552  cerr << "Creating data" << endl;
553 
554  const ArrayRCP<Packet> origInputData = arcp<Packet>(numSendRecv*sendLen);
555  const ArrayRCP<Packet> origOutputData = arcp<Packet>(numSendRecv*sendLen);
556  {
557  int offset = 0;
558  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
559  const ArrayRCP<Packet> origInputData_i =
560  origInputData.persistingView(offset, sendLen);
561  const ArrayRCP<Packet> origOutputData_i =
562  origOutputData.persistingView(offset, sendLen);
563  for (int j = 0; j < sendLen; ++j) {
564  origInputData_i[j] = PT::random();
565  origOutputData_i[j] = PT::random();
566  }
567  }
568  }
569  cerr << "Broadcasting data" << endl;
570  broadcast<Ordinal, Packet>( *comm, 0, origInputData() );
571 
572  const ArrayRCP<Packet> inputData = arcpClone<Packet>(origInputData());
573  const ArrayRCP<Packet> outputData = arcpClone<Packet>(origOutputData());
574 
575  Array<RCP<Teuchos::CommRequest<Ordinal> > > recvRequests;
576  Array<RCP<Teuchos::CommRequest<Ordinal> > > sendRequests;
577 
578  cerr << "Exchanging data" << endl;
579 
580  // Send from proc 0 to proc numProcs-1
581  if (procRank == 0) {
582  // Create copy of data to make sure that persisting relationship is
583  // maintained!
584  int offset = 0;
585  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
586  sendRequests.push_back(
587  isend<Ordinal, Packet>(
588  *comm,
589  arcpClone<Packet>(inputData(offset, sendLen)),
590  numProcs-1
591  )
592  );
593  }
594  }
595 
596  // Receive from proc 0 on proc numProcs-1
597  if (procRank == numProcs-1) {
598  // We will need to read output_data after wait(...) below
599  int offset = 0;
600  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
601  recvRequests.push_back(
602  ireceive<Ordinal, Packet>(
603  *comm, outputData.persistingView(offset, sendLen), 0
604  )
605  );
606  }
607  }
608 
609  cerr << "Waiting on messages" << endl;
610 
611  if (procRank == 0) {
612  waitAll( *comm, sendRequests() );
613  }
614  if (procRank == numProcs-1) {
615  waitAll( *comm, recvRequests() );
616  }
617 
618  cerr << "Testing received data" << endl;
619 
620  if (!sendRequests.empty()) {
621  for (int i = 0; i < numSendRecv; ++i) {
622  TEST_EQUALITY_CONST( sendRequests[i], Teuchos::null );
623  }
624  }
625 
626  if (!recvRequests.empty()) {
627  for (int i = 0; i < numSendRecv; ++i) {
628  TEST_EQUALITY_CONST( recvRequests[i], Teuchos::null );
629  }
630  }
631  // ToDo: Write a test macro for this in one shot!
632 
633  if (procRank == numProcs-1) {
634  TEST_COMPARE_ARRAYS( outputData, inputData );
635  }
636  else {
637  TEST_COMPARE_ARRAYS( outputData, origOutputData );
638  }
639  TEST_COMPARE_ARRAYS( inputData, origInputData );
640 
641  // All procs fail if any proc fails
642  int globalSuccess_int = -1;
643  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
644  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
645 
646 }
647 
648 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, duplicate, Ordinal)
649 {
650  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
651  int initialRank = comm->getRank();
652  int initialSize = comm->getSize();
653 
654  RCP< const Comm<Ordinal> > newComm = comm->duplicate();
655  TEST_EQUALITY(newComm->getSize(), initialSize);
656  TEST_EQUALITY(newComm->getRank(), initialRank);
657 
658  // TODO Make sure the communication space is distinct.
659 }
660 
661 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, split, Ordinal) {
662  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
663  int initialRank = comm->getRank();
664  int initialSize = comm->getSize();
665 
666  // Partition this communicator into two: one with the odd ranks and one with
667  // the even ones. Pass a common key for everyone to maintain the same
668  // ordering as in the initial communicator.
669  RCP< const Comm<Ordinal> > newComm = comm->split(initialRank % 2, 0);
670 
671  // Check the size of the new communicator and my rank within it.
672  int halfSize = initialSize / 2;
673  int newSize = newComm->getSize();
674  int newRank = newComm->getRank();
675  if (initialSize % 2 == 0) {
676  TEST_EQUALITY(newSize, halfSize);
677  }
678  else {
679  TEST_EQUALITY(newSize, initialRank % 2 == 0 ? halfSize + 1 : halfSize);
680  }
681  TEST_EQUALITY(newRank, initialRank / 2);
682 
683  // Negative color values get a null communicator.
684  RCP< const Comm<Ordinal> > shouldBeNull = comm->split(-1, 0);
685  TEST_ASSERT(shouldBeNull.is_null());
686 }
687 
688 
689 namespace {
690 
691 
692 template<typename ValueType>
693 class MonotoneSequence
694 {
695  ValueType currentValue_;
696 public:
697  typedef ValueType value_type;
698 
699  MonotoneSequence(const value_type& initialValue) : currentValue_(initialValue)
700  {}
701 
702  value_type operator()()
703  {
704  return currentValue_++;
705  }
706 };
707 
708 
709 } // namepsace
710 
711 
712 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, createSubcommunicator, Ordinal) {
713  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
714  int initialRank = comm->getRank();
715  int initialSize = comm->getSize();
716 
717  // Create a new communicator that reverses all of the ranks.
718  std::vector< int > ranks(initialSize);
719  std::generate(ranks.begin(), ranks.end(), MonotoneSequence<int>(0));
720  std::reverse(ranks.begin(), ranks.end());
721  RCP< const Comm<Ordinal> > newComm = comm->createSubcommunicator(ranks);
722  TEST_EQUALITY(newComm->getSize(), initialSize);
723  int expectedNewRank = initialSize - initialRank - 1;
724  TEST_EQUALITY(newComm->getRank(), expectedNewRank);
725 
726  // Processes that aren't in the group get a null communicator.
727  std::vector<int> rank0Only(1, 0);
728  RCP< const Comm<Ordinal> > rank0Comm = comm->createSubcommunicator(rank0Only);
729  // Original rank 0 should be valid, all others should be null.
730  if (initialRank == 0) {
731  TEST_ASSERT(rank0Comm.is_valid_ptr());
732  } else {
733  TEST_ASSERT(rank0Comm.is_null());
734  }
735 }
736 
737 
738 #ifdef HAVE_TEUCHOS_MPI
739 
740 
741 TEUCHOS_UNIT_TEST(DefaultMpiComm, TagConsistency )
742 {
743  using Teuchos::tuple; using Teuchos::inoutArg;
744 
745  const Teuchos::RCP<const Teuchos::Comm<int> > defaultComm =
747  const int comm_size = defaultComm->getSize();
748  const int comm_rank = defaultComm->getRank();
749 
750  // Must have at least two processes to run this test!
751  if (comm_size < 2) {
752  return;
753  }
754 
755  // Create a subcomm that contains just two processes
756  const Teuchos::RCP<const Teuchos::Comm<int> > masterComm =
757  defaultComm->createSubcommunicator(tuple<int>(0, 1)());
758 
759  if (comm_rank <= 1) {
760 
761  const int masterComm_size = masterComm->getSize();
762  (void) masterComm_size; // Forestall "unused variable" warning.
763  const int masterComm_rank = masterComm->getRank();
764 
765  // Split the main communicator into 2 overlapping groups
767  masterComm->createSubcommunicator(tuple<int>(0, 1)());
769  masterComm->createSubcommunicator(tuple<int>(0)());
770 
771  // Create another communicator.
773  masterComm->createSubcommunicator(tuple<int>(0, 1)());
774 
775  // Get my mpi tag for comm 3.
776  int my_tag = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(
777  comm_3 )->getTag();
778 
779  // Collect the tags for comm 3.
780  int tag1 = 0;
781  if (masterComm_rank == 0) { tag1 = my_tag; }
782  masterComm->barrier();
783  Teuchos::broadcast( *masterComm, 0, inoutArg(tag1) );
784 
785  int tag2 = 0;
786  if (masterComm_rank == 1) { tag2 = my_tag; }
787  masterComm->barrier();
788  Teuchos::broadcast( *masterComm, 1, inoutArg(tag2) );
789 
790  // This currently fails.
791  TEST_EQUALITY( tag1, tag2 );
792 
793  }
794 
795 }
796 
797 
798 #endif // HAVE_TEUCHOS_MPI
799 
800 
801 //
802 // Instantiations
803 //
804 
805 
806 #ifdef HAVE_TEUCHOS_COMPLEX
807 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(TEST_GROUP, TEST_NAME, ORDINAL)\
808  typedef std::complex<float> ComplexFloat; \
809  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT(TEST_GROUP, TEST_NAME, ORDINAL, ComplexFloat)
810 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(TEST_GROUP, TEST_NAME, ORDINAL)\
811  typedef std::complex<double> ComplexDouble; \
812  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT(TEST_GROUP, TEST_NAME, ORDINAL, ComplexDouble)
813 #else
814 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(TEST_GROUP, TEST_NAME, ORDINAL)
815 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(TEST_GROUP, TEST_NAME, ORDINAL)
816 #endif
817 
818 
819 #define UNIT_TEST_GROUP_ORDINAL_PACKET( ORDINAL, PACKET ) \
820  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceive, ORDINAL, PACKET ) \
821  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceiveSet, ORDINAL, PACKET ) \
822  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend1, ORDINAL, PACKET ) \
823  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend, ORDINAL, PACKET )
824 
825 #ifdef HAVE_TEUCHOS_QD
826 # define UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL) \
827  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, dd_real) \
828  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, qd_real)
829 #else
830 # define UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL)
831 #endif
832 
833 #define UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS( ORDINAL, PAIROFPACKETS ) \
834  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceive, ORDINAL, PAIROFPACKETS ) \
835  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceiveSet, ORDINAL, PAIROFPACKETS ) \
836  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend1, ORDINAL, PAIROFPACKETS ) \
837  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend, ORDINAL, PAIROFPACKETS )
838 
839 #define UNIT_TEST_GROUP_ORDINAL_SUBCOMMUNICATORS( ORDINAL ) \
840  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, duplicate, ORDINAL ) \
841  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, split, ORDINAL ) \
842  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, createSubcommunicator, ORDINAL )
843 
844 
845 typedef std::pair<short, short> PairOfShorts;
846 typedef std::pair<int,int> PairOfInts;
847 typedef std::pair<float,float> PairOfFloats;
848 typedef std::pair<double,double> PairOfDoubles;
849 
850 
851 // Uncomment this for really fast development cycles but make sure to comment
852 // it back again before checking in so that we can test all the types.
853 // #define FAST_DEVELOPMENT_UNIT_TEST_BUILD
854 
855 
856 #ifdef FAST_DEVELOPMENT_UNIT_TEST_BUILD
857 
858 # define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
859  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
860  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfDoubles) \
861 
863 
864 #else // FAST_DEVELOPMENT_UNIT_TEST_BUILD
865 
866 # define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
867  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, basic, ORDINAL ) \
868  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, short) \
869  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, int) \
870  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, float) \
871  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
872  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
873  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend1, ORDINAL) \
874  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend, ORDINAL) \
875  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
876  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend1, ORDINAL) \
877  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend, ORDINAL) \
878  UNIT_TEST_GROUP_ORDINAL_SUBCOMMUNICATORS(ORDINAL)
879 
880 # define UNIT_TEST_GROUP_ORDINAL_WITH_PAIRS_AND_QD( ORDINAL ) \
881  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, basic, ORDINAL ) \
882  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, short) \
883  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, int) \
884  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, float) \
885  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
886  UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL) \
887  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfShorts) \
888  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfInts) \
889  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfFloats) \
890  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfDoubles) \
891  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
892  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend1, ORDINAL) \
893  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend, ORDINAL) \
894  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
895  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend1, ORDINAL) \
896  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend, ORDINAL)
897 
898  typedef short int ShortInt;
899  UNIT_TEST_GROUP_ORDINAL(ShortInt)
901  typedef long int LongInt;
902  UNIT_TEST_GROUP_ORDINAL(LongInt) // can't do QD with LongInt, one of the tests complains
903 
904  typedef long long int LongLongInt;
905  UNIT_TEST_GROUP_ORDINAL(LongLongInt)
906 
907 #endif // FAST_DEVELOPMENT_UNIT_TEST_BUILD
908 
909 
910 } // namespace
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
static std::pair< Packet, Packet > real(std::pair< Packet, Packet > x)
#define TEST_ASSERT(v1)
Assert the given statement is true.
void addOutputSetupOptions(const bool &addOutputSetupOptions)
Set if options will be automatically added to setup Teuchos::VerboseObjectBase::getDefaultOStream().
#define UNIT_TEST_GROUP_ORDINAL_WITH_PAIRS_AND_QD(ORDINAL)
Scalar defaultSmallNumber()
void broadcast(const Comm< Ordinal > &comm, const int rootRank, const Ordinal count, Packet buffer[])
Broadcast array of objects that use value semantics.
#define ECHO(statement)
Echo the given statement before it is executed.
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
static bool isnaninf(std::pair< Packet, Packet > x)
static CommandLineProcessor & getCLP()
Return the CLP to add options to.
static magnitudeType magnitude(std::pair< Packet, Packet > a)
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
static const bool isComparable
Determines if scalar type supports relational operators such as &lt;, &gt;, &lt;=, &gt;=.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
int rank(const Comm< Ordinal > &comm)
Get the process rank.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL(TEST_GROUP, TEST_NAME, TYPE1, TYPE2)
Macro for defining a templated unit test with two template parameters.
Concrete serial communicator subclass.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
static std::pair< Packet, Packet > squareroot(std::pair< Packet, Packet > x)
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Initialize, finalize, and query the global MPI session.
static const bool hasMachineParameters
Determines if scalar type have machine-specific parameters (i.e. eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax() are supported).
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
static std::pair< Packet, Packet > imag(std::pair< Packet, Packet > x)
void reduceAll(const Comm< Ordinal > &comm, const ValueTypeReductionOp< Ordinal, Packet > &reductOp, const Ordinal count, const Packet sendBuffer[], Packet globalReducts[])
Wrapper for MPI_Allreduce that takes a custom reduction operator.
static std::pair< Packet, Packet > pow(std::pair< Packet, Packet > x, std::pair< Packet, Packet > y)
This structure defines some basic traits for a scalar field type.
std::vector< std::string > split(const std::string &str, const std::string &delimiters, const size_t start)
Split the given string using the given set of delimiters.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
void waitAll(const Comm< Ordinal > &comm, const ArrayView< RCP< CommRequest< Ordinal > > > &requests)
Wait for an array of Teuchos::CommRequest objects.
#define UNIT_TEST_GROUP_ORDINAL(ORDINAL)
Unit testing support.
Implementation of Teuchos wrappers for MPI.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
void barrier(const Comm< Ordinal > &comm)
Barrier.
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
Return a default global communicator appropriate for the build.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
Default traits class for all conversions between value types.
Definition: Teuchos_as.hpp:179
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
RCP< CommRequest< Ordinal > > ireceive(const ArrayRCP< Packet > &recvBuffer, const int sourceRank, const int tag, const Comm< Ordinal > &comm)
Variant of ireceive that takes a tag argument (and restores the correct order of arguments).
Abstract interface for distributed-memory communication.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
std::pair< typename PST::magnitudeType, typename PST::magnitudeType > magnitudeType
RCP< Teuchos::CommRequest< int > > isend(const ArrayRCP< const double > &sendBuffer, const int destRank, const int tag, const Comm< int > &comm)
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
RCP< CommStatus< Ordinal > > wait(const Comm< Ordinal > &comm, const Ptr< RCP< CommRequest< Ordinal > > > &request)
Wait on a single communication request, and return its status.
Smart reference counting pointer class for automatic garbage collection.
static std::pair< Packet, Packet > conjugate(std::pair< Packet, Packet > x)
static const bool isComplex
Determines if scalar type is std::complex.
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Definition of Teuchos::as, for conversions between types.
void readySend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of readySend() that accepts a message tag.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
TEUCHOS_STATIC_SETUP()
Reference-counted smart pointer for managing arrays.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...