Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Util.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_Util.hpp"
11 #include "Teuchos_Comm.hpp"
12 
13 namespace Tpetra {
14 namespace Details {
15 
33 bool congruent(const Teuchos::Comm<int>& comm1,
34  const Teuchos::Comm<int>& comm2) {
35 #ifdef HAVE_MPI
36  using Teuchos::Comm;
37  using Teuchos::MpiComm;
38  using Teuchos::RCP;
39  using Teuchos::rcp_dynamic_cast;
40  using Teuchos::rcpFromRef;
41 
42  RCP<const MpiComm<int> > mpiComm1 =
43  rcp_dynamic_cast<const MpiComm<int> >(rcpFromRef(comm1));
44  RCP<const MpiComm<int> > mpiComm2 =
45  rcp_dynamic_cast<const MpiComm<int> >(rcpFromRef(comm2));
46 
47  if (mpiComm1.is_null()) { // comm1 is not an MpiComm
48  return comm1.getSize() == comm2.getSize(); // hope for the best
49  } else { // comm1 is an MpiComm
50  if (mpiComm2.is_null()) { // comm2 is not an MpiComm
51  return comm1.getSize() == comm2.getSize(); // hope for the best
52  } else { // both comm1 and comm2 are MpiComm
53  MPI_Comm rawMpiComm1 = *(mpiComm1->getRawMpiComm());
54  MPI_Comm rawMpiComm2 = *(mpiComm2->getRawMpiComm());
55 
56  int result = MPI_UNEQUAL;
57  const int err = MPI_Comm_compare(rawMpiComm1, rawMpiComm2, &result);
58  TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
59  "congruent: MPI_Comm_compare failed");
60  return result == MPI_IDENT || result == MPI_CONGRUENT;
61  }
62  }
63 #else // NOT HAVE_MPI
64  return comm1.getSize() == comm2.getSize(); // hope for the best
65 #endif // HAVE_MPI
66 }
67 
68 std::unique_ptr<std::string>
69 createPrefix(const int myRank,
70  const char prefix[]) {
71  std::ostringstream os;
72  os << "Proc " << myRank << ": " << prefix << ": ";
73  return std::unique_ptr<std::string>(new std::string(os.str()));
74 }
75 
76 std::unique_ptr<std::string>
77 createPrefix(const Teuchos::Comm<int>* comm,
78  const char functionName[]) {
79  const int myRank = comm == nullptr ? -1 : comm->getRank();
80  const std::string prefix = std::string("Tpetra::") + functionName;
81  return createPrefix(myRank, prefix.c_str());
82 }
83 
84 std::unique_ptr<std::string>
85 createPrefix(const Teuchos::Comm<int>* comm,
86  const char className[],
87  const char methodName[]) {
88  const int myRank = comm == nullptr ? -1 : comm->getRank();
89  const std::string prefix = std::string("Tpetra::") +
90  className + std::string("::") + methodName;
91  return createPrefix(myRank, prefix.c_str());
92 }
93 
94 } // namespace Details
95 } // namespace Tpetra
bool congruent(const Teuchos::Comm< int > &comm1, const Teuchos::Comm< int > &comm2)
Whether the two communicators are congruent.
Definition: Tpetra_Util.cpp:33
Stand-alone utility functions and macros.
std::unique_ptr< std::string > createPrefix(const int myRank, const char prefix[])
Create string prefix for each line of verbose output.
Definition: Tpetra_Util.cpp:69