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 #endif // HAVE_TEUCHOS_MPI
192 
193 
194 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, ReadySend1, Ordinal, Packet )
195 {
196 
197  using Teuchos::broadcast;
198  using Teuchos::readySend;
199  using Teuchos::wait;
200  using Teuchos::as;
201  using Teuchos::rcpFromRef;
202  using Teuchos::outArg;
203  using Teuchos::isend;
204  using Teuchos::ireceive;
205  using Teuchos::wait;
206  using Teuchos::SerialComm;
207  using Teuchos::is_null;
208  using Teuchos::arcp;
209  using Teuchos::arcpClone;
210  using Teuchos::rcp_dynamic_cast;
211  using Teuchos::ArrayRCP;
212  using Teuchos::ptr;
214  //typedef typename PT::magnitudeType PacketMag; // unused
215  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
216 
217  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
218  const Ordinal numProcs = size(*comm);
219  const Ordinal procRank = rank(*comm);
220 
221  if (
222  numProcs == 1
223  &&
224  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
225  )
226  {
227  out << "\nThis is Teuchos::SerialComm which does not support readySend!\n";
228  return; // Pass!
229  }
230 
231  PT::seedrandom(as<unsigned int>(procRank));
232  Packet origSendData = PT::random();
233  Packet origRecvData = PT::random();
234  broadcast<Ordinal, Packet>( *comm, 0, outArg(origSendData) );
235 
236  Packet sendData = origSendData;
237  Packet recvData = origRecvData;
238 
239  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
240 
241  // Post non-block receive on proc 0
242  if (procRank == 0) {
243  // Post non-blocking receive from proc n-1
244  recvRequest = ireceive<Ordinal, Packet>(
245  *comm,
246  rcp(&recvData,false),
247  numProcs-1
248  );
249  }
250  barrier(*comm);
251 
252  if (procRank == numProcs-1) {
253  // ready send from proc n-1 to proc 0
254  // send data in sendData
255  readySend<Ordinal, Packet>(
256  *comm,
257  sendData,
258  0
259  );
260  }
261  barrier(*comm);
262 
263  if (procRank == 0) {
264  // wait for request on 0
265  wait( *comm, outArg(recvRequest) );
266  }
267  barrier(*comm);
268 
269  // test that all procs have recvRequest == Teuchos::null
270  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
271 
272  // proc 0 should have recvData == sendData
273  if (procRank == 0) {
274  TEST_EQUALITY( recvData, sendData );
275  }
276  // other procs should have recvData == origRecvData (i.e., unchanged)
277  else {
278  TEST_EQUALITY( recvData, origRecvData );
279  }
280  // all procs should have sendData == origSendData
281  TEST_EQUALITY( sendData, origSendData );
282 
283  // All procs fail if any proc fails
284  int globalSuccess_int = -1;
285  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
286  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
287 }
288 
289 
290 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, ReadySend, Ordinal, Packet )
291 {
292 
293  using Teuchos::broadcast;
294  using Teuchos::readySend;
295  using Teuchos::wait;
296  using Teuchos::as;
297  using Teuchos::rcpFromRef;
298  using Teuchos::outArg;
299  using Teuchos::isend;
300  using Teuchos::ireceive;
301  using Teuchos::wait;
302  using Teuchos::SerialComm;
303  using Teuchos::is_null;
304  using Teuchos::arcp;
305  using Teuchos::arcpClone;
306  using Teuchos::rcp_dynamic_cast;
307  using Teuchos::ArrayRCP;
309  //typedef typename PT::magnitudeType PacketMag; // unused
310  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
311 
312  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
313  const Ordinal numProcs = size(*comm);
314  const Ordinal procRank = rank(*comm);
315 
316  if (
317  numProcs == 1
318  &&
319  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
320  )
321  {
322  out << "\nThis is Teuchos::SerialComm which does not support readySend!\n";
323  return; // Pass!
324  }
325 
326  const int dataLen = 3;
327 
328  const ArrayRCP<Packet> origSendData = arcp<Packet>(dataLen);
329  const ArrayRCP<Packet> origRecvData = arcp<Packet>(dataLen);
330  PT::seedrandom(as<unsigned int>(procRank));
331  for (int j = 0; j < dataLen; ++j) {
332  origSendData[j] = PT::random();
333  origRecvData[j] = PT::random();
334  }
335  broadcast<Ordinal, Packet>( *comm, 0, origSendData() );
336 
337  const ArrayRCP<Packet> sendData = arcpClone<Packet>(origSendData());
338  const ArrayRCP<Packet> recvData = arcpClone<Packet>(origRecvData());
339 
340  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
341 
342  // both proc 0 and proc n-1 will post non-block receives, into recvData
343  // then proc 0 will initiate a ready-send to proc n-1; the latter will initiate a wait
344  // a barrier
345  // then proc n-1 will initiate a ready-send to proc 0 of the data from recvData; the latter will initiate a wait
346  // a barrier
347  // now both of these procs should have matching data in sendData and recvData
348 
349  // Post non-block receive on both procs
350  if (procRank == 0) {
351  // Post non-blocking receive from proc n-1
352  recvRequest = ireceive<Ordinal, Packet>(
353  *comm,
354  recvData.persistingView(0, dataLen),
355  numProcs-1
356  );
357  }
358  else if (procRank == numProcs-1) {
359  // Post non-blocking receive from proc 0
360  recvRequest = ireceive<Ordinal, Packet>(
361  *comm,
362  recvData.persistingView(0, dataLen),
363  0
364  );
365  }
366  barrier(*comm);
367 
368  if (procRank == 0) {
369  // ready send from proc 0 to proc n-1
370  // send data in sendData
371  readySend<Ordinal, Packet>(
372  *comm,
373  sendData(),
374  numProcs-1
375  );
376  }
377  else if (procRank == numProcs-1) {
378  // wait for request on proc n-1
379  wait( *comm, outArg(recvRequest) );
380  }
381  barrier(*comm);
382 
383  if (procRank == 0) {
384  // wait for request on 0
385  wait( *comm, outArg(recvRequest) );
386  }
387  else if (procRank == numProcs-1) {
388  // ready send from proc n-1 to proc 0
389  // send data in recvData: THIS IS IMPORTANT: SEE ABOVE
390  readySend<Ordinal, Packet>(
391  *comm,
392  recvData(),
393  0
394  );
395  }
396  barrier(*comm);
397 
398  // test that all procs (even non-participating) have recvRequest == Teuchos::null
399  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
400 
401  // participating procs should have recvData == sendData
402  if (procRank == 0 || procRank == numProcs-1) {
403  TEST_COMPARE_ARRAYS( recvData, sendData );
404  }
405  // non-participating procs should have recvData == origRecvData (i.e., unchanged)
406  else {
407  TEST_COMPARE_ARRAYS( recvData, origRecvData );
408  }
409  // all procs should have sendData == origSendData
410  TEST_COMPARE_ARRAYS( sendData, origSendData );
411 
412  // All procs fail if any proc fails
413  int globalSuccess_int = -1;
414  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
415  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
416 }
417 
418 
419 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, NonblockingSendReceive, Ordinal, Packet )
420 {
421  using Teuchos::as;
422  using Teuchos::rcpFromRef;
423  using Teuchos::outArg;
424  using Teuchos::isend;
425  using Teuchos::ireceive;
426  using Teuchos::wait;
427  using Teuchos::SerialComm;
428  using Teuchos::rcp_dynamic_cast;
429  using std::endl;
431  //typedef typename PT::magnitudeType PacketMag; // unused
432  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
433 
434  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
435  const Ordinal numProcs = size(*comm);
436  const Ordinal procRank = rank(*comm);
437 
438  if (
439  numProcs == 1
440  &&
441  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
442  )
443  {
444  out << "\nThis is Teuchos::SerialComm which does not yet support isend/ireceive!\n";
445  return; // Pass!
446  }
447 
448  // Only use randomize on one proc and then broacast
449  Packet orig_input_data = PT::random();
450  broadcast( *comm, 0, &orig_input_data );
451 
452  const Packet orig_output_data = as<Packet>(-1);
453 
454  const Packet input_data = orig_input_data;
455  Packet output_data = orig_output_data;
456 
457  RCP<Teuchos::CommRequest<Ordinal> > recvRequest;
458  RCP<Teuchos::CommRequest<Ordinal> > sendRequest;
459 
460  out << "Exchanging messages" << endl;
461 
462  if (procRank == 0) {
463  // Create copy of data to make sure that persisting relationship is
464  // maintained!
465  sendRequest = isend<Ordinal, Packet>(
466  *comm, Teuchos::rcp(new Packet(input_data)), numProcs-1);
467  }
468  if (procRank == numProcs-1) {
469  // We will need to read output_data after wait(...) below
470  recvRequest = ireceive<Ordinal, Packet>(
471  *comm, rcpFromRef(output_data), 0);
472  }
473 
474  out << "Waiting for messages" << endl;
475 
476  if (procRank == 0) {
477  wait( *comm, outArg(sendRequest) );
478  }
479  if (procRank == numProcs-1) {
480  wait( *comm, outArg(recvRequest) );
481  }
482 
483  TEST_EQUALITY_CONST( sendRequest, Teuchos::null );
484  TEST_EQUALITY_CONST( recvRequest, Teuchos::null );
485 
486  out << "Testing message correctness" << endl;
487 
488  if (procRank == numProcs-1) {
489  TEST_EQUALITY( output_data, input_data );
490  }
491  else {
492  TEST_EQUALITY( output_data, orig_output_data );
493  }
494  TEST_EQUALITY( input_data, orig_input_data );
495 
496  // All procs fail if any proc fails
497  int globalSuccess_int = -1;
498  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
499  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
500 }
501 
502 
503 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( DefaultMpiComm, NonblockingSendReceiveSet, Ordinal, Packet )
504 {
505  using Teuchos::as;
506  using Teuchos::rcpFromRef;
507  using Teuchos::outArg;
508  using Teuchos::arcp;
509  using Teuchos::arcpClone;
510  using Teuchos::ArrayRCP;
511  using Teuchos::isend;
512  using Teuchos::ireceive;
513  using Teuchos::wait;
514  using Teuchos::broadcast;
515  using Teuchos::SerialComm;
516  using Teuchos::rcp_dynamic_cast;
517  using std::cerr;
518  using std::endl;
520  //typedef typename PT::magnitudeType PacketMag; // unused
521  //typedef Teuchos::ScalarTraits<PacketMag> PMT; // unused
522 
523  RCP<const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
524  const Ordinal numProcs = size(*comm);
525  const Ordinal procRank = rank(*comm);
526 
527  if (
528  numProcs == 1
529  &&
530  !is_null(rcp_dynamic_cast<const SerialComm<Ordinal> >(comm))
531  )
532  {
533  out << "\nThis is Teuchos::SerialComm which does not yet support isend/ireceive!\n";
534  return; // Pass!
535  }
536 
537  const int numSendRecv = 4;
538  const int sendLen = 3;
539 
540  cerr << "Creating data" << endl;
541 
542  const ArrayRCP<Packet> origInputData = arcp<Packet>(numSendRecv*sendLen);
543  const ArrayRCP<Packet> origOutputData = arcp<Packet>(numSendRecv*sendLen);
544  {
545  int offset = 0;
546  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
547  const ArrayRCP<Packet> origInputData_i =
548  origInputData.persistingView(offset, sendLen);
549  const ArrayRCP<Packet> origOutputData_i =
550  origOutputData.persistingView(offset, sendLen);
551  for (int j = 0; j < sendLen; ++j) {
552  origInputData_i[j] = PT::random();
553  origOutputData_i[j] = PT::random();
554  }
555  }
556  }
557  cerr << "Broadcasting data" << endl;
558  broadcast<Ordinal, Packet>( *comm, 0, origInputData() );
559 
560  const ArrayRCP<Packet> inputData = arcpClone<Packet>(origInputData());
561  const ArrayRCP<Packet> outputData = arcpClone<Packet>(origOutputData());
562 
563  Array<RCP<Teuchos::CommRequest<Ordinal> > > recvRequests;
564  Array<RCP<Teuchos::CommRequest<Ordinal> > > sendRequests;
565 
566  cerr << "Exchanging data" << endl;
567 
568  // Send from proc 0 to proc numProcs-1
569  if (procRank == 0) {
570  // Create copy of data to make sure that persisting relationship is
571  // maintained!
572  int offset = 0;
573  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
574  sendRequests.push_back(
575  isend<Ordinal, Packet>(
576  *comm,
577  arcpClone<Packet>(inputData(offset, sendLen)),
578  numProcs-1
579  )
580  );
581  }
582  }
583 
584  // Receive from proc 0 on proc numProcs-1
585  if (procRank == numProcs-1) {
586  // We will need to read output_data after wait(...) below
587  int offset = 0;
588  for (int i = 0; i < numSendRecv; ++i, offset += sendLen) {
589  recvRequests.push_back(
590  ireceive<Ordinal, Packet>(
591  *comm, outputData.persistingView(offset, sendLen), 0
592  )
593  );
594  }
595  }
596 
597  cerr << "Waiting on messages" << endl;
598 
599  if (procRank == 0) {
600  waitAll( *comm, sendRequests() );
601  }
602  if (procRank == numProcs-1) {
603  waitAll( *comm, recvRequests() );
604  }
605 
606  cerr << "Testing received data" << endl;
607 
608  if (!sendRequests.empty()) {
609  for (int i = 0; i < numSendRecv; ++i) {
610  TEST_EQUALITY_CONST( sendRequests[i], Teuchos::null );
611  }
612  }
613 
614  if (!recvRequests.empty()) {
615  for (int i = 0; i < numSendRecv; ++i) {
616  TEST_EQUALITY_CONST( recvRequests[i], Teuchos::null );
617  }
618  }
619  // ToDo: Write a test macro for this in one shot!
620 
621  if (procRank == numProcs-1) {
622  TEST_COMPARE_ARRAYS( outputData, inputData );
623  }
624  else {
625  TEST_COMPARE_ARRAYS( outputData, origOutputData );
626  }
627  TEST_COMPARE_ARRAYS( inputData, origInputData );
628 
629  // All procs fail if any proc fails
630  int globalSuccess_int = -1;
631  reduceAll( *comm, Teuchos::REDUCE_SUM, success ? 0 : 1, outArg(globalSuccess_int) );
632  TEST_EQUALITY_CONST( globalSuccess_int, 0 );
633 
634 }
635 
636 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, duplicate, Ordinal)
637 {
638  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
639  int initialRank = comm->getRank();
640  int initialSize = comm->getSize();
641 
642  RCP< const Comm<Ordinal> > newComm = comm->duplicate();
643  TEST_EQUALITY(newComm->getSize(), initialSize);
644  TEST_EQUALITY(newComm->getRank(), initialRank);
645 
646  // TODO Make sure the communication space is distinct.
647 }
648 
649 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, split, Ordinal) {
650  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
651  int initialRank = comm->getRank();
652  int initialSize = comm->getSize();
653 
654  // Partition this communicator into two: one with the odd ranks and one with
655  // the even ones. Pass a common key for everyone to maintain the same
656  // ordering as in the initial communicator.
657  RCP< const Comm<Ordinal> > newComm = comm->split(initialRank % 2, 0);
658 
659  // Check the size of the new communicator and my rank within it.
660  int halfSize = initialSize / 2;
661  int newSize = newComm->getSize();
662  int newRank = newComm->getRank();
663  if (initialSize % 2 == 0) {
664  TEST_EQUALITY(newSize, halfSize);
665  }
666  else {
667  TEST_EQUALITY(newSize, initialRank % 2 == 0 ? halfSize + 1 : halfSize);
668  }
669  TEST_EQUALITY(newRank, initialRank / 2);
670 
671  // Negative color values get a null communicator.
672  RCP< const Comm<Ordinal> > shouldBeNull = comm->split(-1, 0);
673  TEST_ASSERT(shouldBeNull.is_null());
674 }
675 
676 
677 namespace {
678 
679 
680 template<typename ValueType>
681 class MonotoneSequence
682 {
683  ValueType currentValue_;
684 public:
685  typedef ValueType value_type;
686 
687  MonotoneSequence(const value_type& initialValue) : currentValue_(initialValue)
688  {}
689 
690  value_type operator()()
691  {
692  return currentValue_++;
693  }
694 };
695 
696 
697 } // namepsace
698 
699 
700 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(DefaultMpiComm, createSubcommunicator, Ordinal) {
701  RCP< const Comm<Ordinal> > comm = getDefaultComm<Ordinal>();
702  int initialRank = comm->getRank();
703  int initialSize = comm->getSize();
704 
705  // Create a new communicator that reverses all of the ranks.
706  std::vector< int > ranks(initialSize);
707  std::generate(ranks.begin(), ranks.end(), MonotoneSequence<int>(0));
708  std::reverse(ranks.begin(), ranks.end());
709  RCP< const Comm<Ordinal> > newComm = comm->createSubcommunicator(ranks);
710  TEST_EQUALITY(newComm->getSize(), initialSize);
711  int expectedNewRank = initialSize - initialRank - 1;
712  TEST_EQUALITY(newComm->getRank(), expectedNewRank);
713 
714  // Processes that aren't in the group get a null communicator.
715  std::vector<int> rank0Only(1, 0);
716  RCP< const Comm<Ordinal> > rank0Comm = comm->createSubcommunicator(rank0Only);
717  // Original rank 0 should be valid, all others should be null.
718  if (initialRank == 0) {
719  TEST_ASSERT(rank0Comm.is_valid_ptr());
720  } else {
721  TEST_ASSERT(rank0Comm.is_null());
722  }
723 }
724 
725 
726 #ifdef HAVE_TEUCHOS_MPI
727 
728 
729 TEUCHOS_UNIT_TEST(DefaultMpiComm, TagConsistency )
730 {
731  using Teuchos::tuple; using Teuchos::inoutArg;
732 
733  const Teuchos::RCP<const Teuchos::Comm<int> > defaultComm =
735  const int comm_size = defaultComm->getSize();
736  const int comm_rank = defaultComm->getRank();
737 
738  // Must have at least two processes to run this test!
739  if (comm_size < 2) {
740  return;
741  }
742 
743  // Create a subcomm that contains just two processes
744  const Teuchos::RCP<const Teuchos::Comm<int> > masterComm =
745  defaultComm->createSubcommunicator(tuple<int>(0, 1)());
746 
747  if (comm_rank <= 1) {
748 
749  const int masterComm_size = masterComm->getSize();
750  (void) masterComm_size; // Forestall "unused variable" warning.
751  const int masterComm_rank = masterComm->getRank();
752 
753  // Split the main communicator into 2 overlapping groups
755  masterComm->createSubcommunicator(tuple<int>(0, 1)());
757  masterComm->createSubcommunicator(tuple<int>(0)());
758 
759  // Create another communicator.
761  masterComm->createSubcommunicator(tuple<int>(0, 1)());
762 
763  // Get my mpi tag for comm 3.
764  int my_tag = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(
765  comm_3 )->getTag();
766 
767  // Collect the tags for comm 3.
768  int tag1 = 0;
769  if (masterComm_rank == 0) { tag1 = my_tag; }
770  masterComm->barrier();
771  Teuchos::broadcast( *masterComm, 0, inoutArg(tag1) );
772 
773  int tag2 = 0;
774  if (masterComm_rank == 1) { tag2 = my_tag; }
775  masterComm->barrier();
776  Teuchos::broadcast( *masterComm, 1, inoutArg(tag2) );
777 
778  // This currently fails.
779  TEST_EQUALITY( tag1, tag2 );
780 
781  }
782 
783 }
784 
785 
786 #endif // HAVE_TEUCHOS_MPI
787 
788 
789 //
790 // Instantiations
791 //
792 
793 
794 #ifdef HAVE_TEUCHOS_COMPLEX
795 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(TEST_GROUP, TEST_NAME, ORDINAL)\
796  typedef std::complex<float> ComplexFloat; \
797  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT(TEST_GROUP, TEST_NAME, ORDINAL, ComplexFloat)
798 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(TEST_GROUP, TEST_NAME, ORDINAL)\
799  typedef std::complex<double> ComplexDouble; \
800  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT(TEST_GROUP, TEST_NAME, ORDINAL, ComplexDouble)
801 #else
802 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(TEST_GROUP, TEST_NAME, ORDINAL)
803 # define UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(TEST_GROUP, TEST_NAME, ORDINAL)
804 #endif
805 
806 
807 #define UNIT_TEST_GROUP_ORDINAL_PACKET( ORDINAL, PACKET ) \
808  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceive, ORDINAL, PACKET ) \
809  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceiveSet, ORDINAL, PACKET ) \
810  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend1, ORDINAL, PACKET ) \
811  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend, ORDINAL, PACKET )
812 
813 #ifdef HAVE_TEUCHOS_QD
814 # define UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL) \
815  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, dd_real) \
816  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, qd_real)
817 #else
818 # define UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL)
819 #endif
820 
821 #define UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS( ORDINAL, PAIROFPACKETS ) \
822  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceive, ORDINAL, PAIROFPACKETS ) \
823  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, NonblockingSendReceiveSet, ORDINAL, PAIROFPACKETS ) \
824  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend1, ORDINAL, PAIROFPACKETS ) \
825  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( DefaultMpiComm, ReadySend, ORDINAL, PAIROFPACKETS )
826 
827 #define UNIT_TEST_GROUP_ORDINAL_SUBCOMMUNICATORS( ORDINAL ) \
828  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, duplicate, ORDINAL ) \
829  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, split, ORDINAL ) \
830  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, createSubcommunicator, ORDINAL )
831 
832 
833 typedef std::pair<short, short> PairOfShorts;
834 typedef std::pair<int,int> PairOfInts;
835 typedef std::pair<float,float> PairOfFloats;
836 typedef std::pair<double,double> PairOfDoubles;
837 
838 
839 // Uncomment this for really fast development cycles but make sure to comment
840 // it back again before checking in so that we can test all the types.
841 // #define FAST_DEVELOPMENT_UNIT_TEST_BUILD
842 
843 
844 #ifdef FAST_DEVELOPMENT_UNIT_TEST_BUILD
845 
846 # define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
847  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
848  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfDoubles) \
849 
851 
852 #else // FAST_DEVELOPMENT_UNIT_TEST_BUILD
853 
854 # define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
855  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, basic, ORDINAL ) \
856  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, short) \
857  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, int) \
858  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, float) \
859  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
860  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
861  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend1, ORDINAL) \
862  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend, ORDINAL) \
863  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
864  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend1, ORDINAL) \
865  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend, ORDINAL) \
866  UNIT_TEST_GROUP_ORDINAL_SUBCOMMUNICATORS(ORDINAL)
867 
868 # define UNIT_TEST_GROUP_ORDINAL_WITH_PAIRS_AND_QD( ORDINAL ) \
869  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( DefaultMpiComm, basic, ORDINAL ) \
870  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, short) \
871  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, int) \
872  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, float) \
873  UNIT_TEST_GROUP_ORDINAL_PACKET(ORDINAL, double) \
874  UNIT_TEST_GROUP_ORDINAL_QD(ORDINAL) \
875  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfShorts) \
876  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfInts) \
877  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfFloats) \
878  UNIT_TEST_GROUP_ORDINAL_PAIROFPACKETS(ORDINAL, PairOfDoubles) \
879  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
880  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend1, ORDINAL) \
881  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_FLOAT(DefaultMpiComm, ReadySend, ORDINAL) \
882  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, NonblockingSendReceive, ORDINAL) \
883  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend1, ORDINAL) \
884  UNIT_TEST_TEMPLATE_2_INSTANT_COMPLEX_DOUBLE(DefaultMpiComm, ReadySend, ORDINAL)
885 
886  typedef short int ShortInt;
887  UNIT_TEST_GROUP_ORDINAL(ShortInt)
889  typedef long int LongInt;
890  UNIT_TEST_GROUP_ORDINAL(LongInt) // can't do QD with LongInt, one of the tests complains
891 
892  typedef long long int LongLongInt;
893  UNIT_TEST_GROUP_ORDINAL(LongLongInt)
894 
895 #endif // FAST_DEVELOPMENT_UNIT_TEST_BUILD
896 
897 
898 } // 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...