Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_checkGlobalError.cpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
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 // ************************************************************************
39 // @HEADER
40 */
41 
42 #include "Tpetra_Details_checkGlobalError.hpp"
44 #include "Teuchos_CommHelpers.hpp"
45 #include "Teuchos_TestForException.hpp"
46 #include <iostream>
47 #include <stdexcept>
48 
49 namespace Tpetra {
50 namespace Details {
51 
52 void
53 checkGlobalError(std::ostream& globalOutputStream,
54  const bool localSuccess,
55  const char localErrorMessage[],
56  const char globalErrorMessageHeader[],
57  const Teuchos::Comm<int>& comm)
58 {
59  using Teuchos::outArg;
60  using Teuchos::REDUCE_MIN;
61  using Teuchos::reduceAll;
62  using std::endl;
63 
64  int lclGood = localSuccess ? 1 : 0;
65  int gblGood = 0;
66  reduceAll(comm, REDUCE_MIN, lclGood, outArg(gblGood));
67  if (gblGood != 1) {
68  const int myRank = comm.getRank();
69  if (myRank == 0) {
70  globalOutputStream << endl << globalErrorMessageHeader
71  << endl;
72  }
73 
74  if (localSuccess || localErrorMessage == nullptr) {
75  Details::gathervPrint(globalOutputStream, "", comm);
76  }
77  else {
78  std::ostringstream lclMsg;
79  lclMsg << endl;
80  constexpr int numStars = 60;
81  for (int star = 0; star < numStars; ++star) {
82  lclMsg << '*';
83  }
84  lclMsg << endl << "Proc " << myRank << ": "
85  << localErrorMessage << endl;
86  Details::gathervPrint(globalOutputStream, lclMsg.str(), comm);
87  }
88 
89 #ifdef HAVE_TPETRA_MPI
90  (void) MPI_Abort(MPI_COMM_WORLD, -1);
91 #else
92  TEUCHOS_TEST_FOR_EXCEPTION
93  (true, std::runtime_error, "Tpetra reports a global error.");
94 #endif // HAVE_TPETRA_MPI
95  }
96 }
97 
98 } // namespace Details
99 } // namespace Tpetra
Declaration of a function that prints strings from each process.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator, in rank order.