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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 #include "Teuchos_Array.hpp"
44 
45 #ifdef HAVE_MPI
46 # include "mpi.h"
47 #endif
48 
50 
51 #ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
52 # include <string>
53 // NOTE (mfh 18 Apr 2016) KokkosCore requires C++11, so we may include
54 // type_traits here. Please do not include it unconditionally,
55 // because TeuchosCore does NOT require C++11.
56 # include <type_traits>
57 #endif // HAVE_TEUCHOSCORE_KOKKOSCORE
58 
59 //
60 // Unit tests for GlobalMPISession
61 //
62 // NOTE: Becuase this class is used to implement the parallel reduction
63 // feature of the unit test harness, we can't use that feature here and we
64 // have to do the global reductions across processes ourselves.
65 //
66 
67 
68 namespace Teuchos {
69 
70 
71 void globalReduceSuccess(bool &success, FancyOStream &out)
72 {
73 #ifdef HAVE_MPI
74  int globalSumSuccessInt = -1;
75  int localSuccessInt = (success ? 0 : 1);
76  MPI_Allreduce(&localSuccessInt, &globalSumSuccessInt, 1,
77  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
78  TEST_EQUALITY_CONST(globalSumSuccessInt, 0);
79 #endif
80 }
81 
82 
84 #ifdef HAVE_MPI
86  int numProcs = -1;
87  ECHO(::MPI_Comm_size(MPI_COMM_WORLD, &numProcs));
89  int procRank = -1;
90  ECHO(::MPI_Comm_rank(MPI_COMM_WORLD, &procRank));
92 #else // HAVE_MPI
96 #endif // HAVE_MPI
98  globalReduceSuccess(success, out);
99 }
100 
101 
103  out << "*** Just make sure the basic barrier does not hang or something.\n";
105  globalReduceSuccess(success, out);
106 }
107 
108 
110  ECHO(const int globalSum = GlobalMPISession::sum(GlobalMPISession::getRank()+1));
111  ECHO(const int n = GlobalMPISession::getNProc());
112  TEST_EQUALITY(globalSum, (n*(n+1))/2);
113  globalReduceSuccess(success, out);
114 }
115 
116 
118 {
119  const int numProcs = GlobalMPISession::getNProc();
120  const int procRank = GlobalMPISession::getRank();
121  {
122  Array<int> allInts;
123  ECHO(allInts.resize(numProcs-1));
124  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
125  ECHO(allInts.resize(numProcs+1));
126  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
127  }
128  {
129  Array<int> allInts_expected(numProcs);
130  for (int k = 0; k < numProcs; ++k) {
131  allInts_expected[k] = k+1;
132  }
133  Array<int> allInts(numProcs);
134  ECHO(GlobalMPISession::allGather(procRank+1, allInts()));
135  TEST_EQUALITY(allInts, allInts_expected);
136  }
137 }
138 
139 
140 #ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
141 // Check whether GlobalMPISession's constructor (called in the unit
142 // test harness, not in this file) correctly saved a copy of the
143 // command-line arguments. Command-line arguments _should_ be
144 // propagated to all MPI processes.
145 //
146 //
147 TEUCHOS_UNIT_TEST( GlobalMPISession, getArgv )
148 {
149  auto argvCopy = GlobalMPISession::getArgv ();
150  const auto numArgs = argvCopy.size ();
151  static_assert (std::is_integral<std::decay<decltype (numArgs)>::type>::value,
152  "The return type of getArgv has a size() method, "
153  "but it does not return an integer.");
154  if (numArgs > 0) {
155  // This tests that argvCopy has an operator[](int) const.
156  const auto arg0 = argvCopy[0];
157  static_assert (std::is_convertible<std::decay<decltype (arg0)>::type,
158  std::string>::value,
159  "The return type of getArgv must have an operator[](int) "
160  "that returns a type convertible to std::string.");
161  }
162 }
163 #endif // HAVE_TEUCHOSCORE_KOKKOSCORE
164 
165 
166 } // namespace Teuchos
167 
168 
169 
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.