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 
52 //
53 // Unit tests for GlobalMPISession
54 //
55 // NOTE: Becuase this class is used to implement the parallel reduction
56 // feature of the unit test harness, we can't use that feature here and we
57 // have to do the global reductions across processes ourselves.
58 //
59 
60 
61 namespace Teuchos {
62 
63 
64 void globalReduceSuccess(bool &success, FancyOStream &out)
65 {
66 #ifdef HAVE_MPI
67  int globalSumSuccessInt = -1;
68  int localSuccessInt = (success ? 0 : 1);
69  MPI_Allreduce(&localSuccessInt, &globalSumSuccessInt, 1,
70  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
71  TEST_EQUALITY_CONST(globalSumSuccessInt, 0);
72 #endif
73 }
74 
75 
77 #ifdef HAVE_MPI
79  int numProcs = -1;
80  ECHO(::MPI_Comm_size(MPI_COMM_WORLD, &numProcs));
82  int procRank = -1;
83  ECHO(::MPI_Comm_rank(MPI_COMM_WORLD, &procRank));
85 #else // HAVE_MPI
89 #endif // HAVE_MPI
91  globalReduceSuccess(success, out);
92 }
93 
94 
96  out << "*** Just make sure the basic barrier does not hang or something.\n";
98  globalReduceSuccess(success, out);
99 }
100 
101 
103  ECHO(const int globalSum = GlobalMPISession::sum(GlobalMPISession::getRank()+1));
104  ECHO(const int n = GlobalMPISession::getNProc());
105  TEST_EQUALITY(globalSum, (n*(n+1))/2);
106  globalReduceSuccess(success, out);
107 }
108 
109 
111 {
112  const int numProcs = GlobalMPISession::getNProc();
113  const int procRank = GlobalMPISession::getRank();
114  {
115  Array<int> allInts;
116  ECHO(allInts.resize(numProcs-1));
117  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
118  ECHO(allInts.resize(numProcs+1));
119  TEST_THROW(GlobalMPISession::allGather(procRank+1, allInts()), std::out_of_range);
120  }
121  {
122  Array<int> allInts_expected(numProcs);
123  for (int k = 0; k < numProcs; ++k) {
124  allInts_expected[k] = k+1;
125  }
126  Array<int> allInts(numProcs);
127  ECHO(GlobalMPISession::allGather(procRank+1, allInts()));
128  TEST_EQUALITY(allInts, allInts_expected);
129  }
130 }
131 
132 
133 } // namespace Teuchos
134 
135 
136 
static int getRank()
Returns the process rank relative to MPI_COMM_WORLD
#define TEST_ASSERT(v1)
Assert the given statement is true.
static int getNProc()
Returns the number of processors relative to 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()
Perform a barrier so that all processes line up.
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.