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 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Tpetra_Details_checkGlobalError.hpp"
12 #include "Teuchos_CommHelpers.hpp"
13 #include "Teuchos_TestForException.hpp"
14 #include <iostream>
15 #include <stdexcept>
16 
17 namespace Tpetra {
18 namespace Details {
19 
20 void checkGlobalError(std::ostream& globalOutputStream,
21  const bool localSuccess,
22  const char localErrorMessage[],
23  const char globalErrorMessageHeader[],
24  const Teuchos::Comm<int>& comm) {
25  using std::endl;
26  using Teuchos::outArg;
27  using Teuchos::REDUCE_MIN;
28  using Teuchos::reduceAll;
29 
30  int lclGood = localSuccess ? 1 : 0;
31  int gblGood = 0;
32  reduceAll(comm, REDUCE_MIN, lclGood, outArg(gblGood));
33  if (gblGood != 1) {
34  const int myRank = comm.getRank();
35  if (myRank == 0) {
36  globalOutputStream << endl
37  << globalErrorMessageHeader
38  << endl;
39  }
40 
41  if (localSuccess || localErrorMessage == nullptr) {
42  Details::gathervPrint(globalOutputStream, "", comm);
43  } else {
44  std::ostringstream lclMsg;
45  lclMsg << endl;
46  constexpr int numStars = 60;
47  for (int star = 0; star < numStars; ++star) {
48  lclMsg << '*';
49  }
50  lclMsg << endl
51  << "Proc " << myRank << ": "
52  << localErrorMessage << endl;
53  Details::gathervPrint(globalOutputStream, lclMsg.str(), comm);
54  }
55 
56 #ifdef HAVE_TPETRA_MPI
57  (void)MPI_Abort(MPI_COMM_WORLD, -1);
58 #else
59  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "Tpetra reports a global error.");
60 #endif // HAVE_TPETRA_MPI
61  }
62 }
63 
64 } // namespace Details
65 } // 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.