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
34 congruent (const Teuchos::Comm<int>& comm1,
35  const Teuchos::Comm<int>& comm2)
36 {
37 #ifdef HAVE_MPI
38  using Teuchos::Comm;
39  using Teuchos::RCP;
40  using Teuchos::rcpFromRef;
41  using Teuchos::MpiComm;
42  using Teuchos::rcp_dynamic_cast;
43 
44  RCP<const MpiComm<int> > mpiComm1 =
45  rcp_dynamic_cast<const MpiComm<int> > (rcpFromRef (comm1));
46  RCP<const MpiComm<int> > mpiComm2 =
47  rcp_dynamic_cast<const MpiComm<int> > (rcpFromRef (comm2));
48 
49  if (mpiComm1.is_null ()) { // comm1 is not an MpiComm
50  return comm1.getSize () == comm2.getSize (); // hope for the best
51  } else { // comm1 is an MpiComm
52  if (mpiComm2.is_null ()) { // comm2 is not an MpiComm
53  return comm1.getSize () == comm2.getSize (); // hope for the best
54  } else { // both comm1 and comm2 are MpiComm
55  MPI_Comm rawMpiComm1 = * (mpiComm1->getRawMpiComm ());
56  MPI_Comm rawMpiComm2 = * (mpiComm2->getRawMpiComm ());
57 
58  int result = MPI_UNEQUAL;
59  const int err = MPI_Comm_compare (rawMpiComm1, rawMpiComm2, &result);
60  TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
61  "congruent: MPI_Comm_compare failed");
62  return result == MPI_IDENT || result == MPI_CONGRUENT;
63  }
64  }
65 #else // NOT HAVE_MPI
66  return comm1.getSize () == comm2.getSize (); // hope for the best
67 #endif // HAVE_MPI
68 }
69 
70 std::unique_ptr<std::string>
71 createPrefix(const int myRank,
72  const char prefix[])
73 {
74  std::ostringstream os;
75  os << "Proc " << myRank << ": " << prefix << ": ";
76  return std::unique_ptr<std::string>(new std::string(os.str()));
77 }
78 
79 std::unique_ptr<std::string>
80 createPrefix(const Teuchos::Comm<int>* comm,
81  const char functionName[])
82 {
83  const int myRank = comm == nullptr ? -1 : comm->getRank();
84  const std::string prefix = std::string("Tpetra::") + functionName;
85  return createPrefix(myRank, prefix.c_str());
86 }
87 
88 std::unique_ptr<std::string>
89 createPrefix(const Teuchos::Comm<int>* comm,
90  const char className[],
91  const char methodName[])
92 {
93  const int myRank = comm == nullptr ? -1 : comm->getRank();
94  const std::string prefix = std::string("Tpetra::") +
95  className + std::string("::") + methodName;
96  return createPrefix(myRank, prefix.c_str());
97 }
98 
99 } // namespace Details
100 } // namespace Tpetra
bool congruent(const Teuchos::Comm< int > &comm1, const Teuchos::Comm< int > &comm2)
Whether the two communicators are congruent.
Definition: Tpetra_Util.cpp:34
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:71