Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
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
13namespace Tpetra {
14namespace Details {
15
33bool
34congruent (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
70std::unique_ptr<std::string>
71createPrefix(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
79std::unique_ptr<std::string>
80createPrefix(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
88std::unique_ptr<std::string>
89createPrefix(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
Stand-alone utility functions and macros.
Nonmember function that computes a residual Computes R = B - A * X.
std::unique_ptr< std::string > createPrefix(const int myRank, const char prefix[])
Create string prefix for each line of verbose output.
bool congruent(const Teuchos::Comm< int > &comm1, const Teuchos::Comm< int > &comm2)
Whether the two communicators are congruent.
Namespace Tpetra contains the class and methods constituting the Tpetra library.