Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlobalMPISession_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Array.hpp"
12 
13 #ifdef HAVE_MPI
14 # include "mpi.h"
15 #endif
16 
18 
19 #ifdef HAVE_TEUCHOSCORE_KOKKOS
20 # include <string>
21 // NOTE (mfh 18 Apr 2016) KokkosCore requires C++11, so we may include
22 // type_traits here. Please do not include it unconditionally,
23 // because TeuchosCore does NOT require C++11.
24 # include <type_traits>
25 #endif // HAVE_TEUCHOSCORE_KOKKOS
26 
27 //
28 // Unit tests for GlobalMPISession
29 //
30 // NOTE: Becuase this class is used to implement the parallel reduction
31 // feature of the unit test harness, we can't use that feature here and we
32 // have to do the global reductions across processes ourselves.
33 //
34 
35 
36 namespace Teuchos {
37 
38 
39 void globalReduceSuccess(bool &success, FancyOStream &out)
40 {
41 #ifdef HAVE_MPI
42  int globalSumSuccessInt = -1;
43  int localSuccessInt = (success ? 0 : 1);
44  MPI_Allreduce(&localSuccessInt, &globalSumSuccessInt, 1,
45  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
46  TEST_EQUALITY_CONST(globalSumSuccessInt, 0);
47 #endif
48 }
49 
50 
52 #ifdef HAVE_MPI
54  int numProcs = -1;
55  ECHO(::MPI_Comm_size(MPI_COMM_WORLD, &numProcs));
57  int procRank = -1;
58  ECHO(::MPI_Comm_rank(MPI_COMM_WORLD, &procRank));
60 #else // HAVE_MPI
64 #endif // HAVE_MPI
66  globalReduceSuccess(success, out);
67 }
68 
69 
71  out << "*** Just make sure the basic barrier does not hang or something.\n";
73  globalReduceSuccess(success, out);
74 }
75 
76 
78  ECHO(const int globalSum = GlobalMPISession::sum(GlobalMPISession::getRank()+1));
79  ECHO(const int n = GlobalMPISession::getNProc());
80  TEST_EQUALITY(globalSum, (n*(n+1))/2);
81  globalReduceSuccess(success, out);
82 }
83 
84 
86 {
87  const int numProcs = GlobalMPISession::getNProc();
88  const int procRank = GlobalMPISession::getRank();
89  {
90  Array<int> allInts;
91  ECHO(allInts.resize(numProcs-1));
92  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
93  ECHO(allInts.resize(numProcs+1));
94  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
95  }
96  {
97  Array<int> allInts_expected(numProcs);
98  for (int k = 0; k < numProcs; ++k) {
99  allInts_expected[k] = k+1;
100  }
101  Array<int> allInts(numProcs);
102  ECHO(GlobalMPISession::allGather(procRank+1, allInts()));
103  TEST_EQUALITY(allInts, allInts_expected);
104  }
105 }
106 
107 
108 #ifdef HAVE_TEUCHOSCORE_KOKKOS
109 // Check whether GlobalMPISession's constructor (called in the unit
110 // test harness, not in this file) correctly saved a copy of the
111 // command-line arguments. Command-line arguments _should_ be
112 // propagated to all MPI processes.
113 //
114 //
115 TEUCHOS_UNIT_TEST( GlobalMPISession, getArgv )
116 {
117  auto argvCopy = GlobalMPISession::getArgv ();
118  const auto numArgs = argvCopy.size ();
119  static_assert (std::is_integral<std::decay<decltype (numArgs)>::type>::value,
120  "The return type of getArgv has a size() method, "
121  "but it does not return an integer.");
122  if (numArgs > 0) {
123  // This tests that argvCopy has an operator[](int) const.
124  const auto arg0 = argvCopy[0];
125  static_assert (std::is_convertible<std::decay<decltype (arg0)>::type,
126  std::string>::value,
127  "The return type of getArgv must have an operator[](int) "
128  "that returns a type convertible to std::string.");
129  }
130 }
131 #endif // HAVE_TEUCHOSCORE_KOKKOS
132 
133 
134 } // namespace Teuchos
135 
136 
137 
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
#define TEST_ASSERT(v1)
Assert the given statement is true.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
Initialize, finalize, and query the global MPI session.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
void globalReduceSuccess(bool &success, FancyOStream &out)
static bool mpiIsFinalized()
Return whether MPI was already finalized.
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
void resize(size_type new_size, const value_type &x=value_type())
static int sum(int localVal)
Sum a set of integers across processes.
Templated array class derived from the STL std::vector.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
static void barrier()
Call MPI_Barrier() on MPI_COMM_WORLD.
static bool mpiIsInitialized()
Return whether MPI was initialized.
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.