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 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // ************************************************************************
38 // @HEADER
39 
40 #include "Tpetra_Util.hpp"
41 #include "Teuchos_Comm.hpp"
42 
43 namespace Tpetra {
44 namespace Details {
45 
63 bool
64 congruent (const Teuchos::Comm<int>& comm1,
65  const Teuchos::Comm<int>& comm2)
66 {
67 #ifdef HAVE_MPI
68  using Teuchos::Comm;
69  using Teuchos::RCP;
70  using Teuchos::rcpFromRef;
71  using Teuchos::MpiComm;
72  using Teuchos::rcp_dynamic_cast;
73 
74  RCP<const MpiComm<int> > mpiComm1 =
75  rcp_dynamic_cast<const MpiComm<int> > (rcpFromRef (comm1));
76  RCP<const MpiComm<int> > mpiComm2 =
77  rcp_dynamic_cast<const MpiComm<int> > (rcpFromRef (comm2));
78 
79  if (mpiComm1.is_null ()) { // comm1 is not an MpiComm
80  return comm1.getSize () == comm2.getSize (); // hope for the best
81  } else { // comm1 is an MpiComm
82  if (mpiComm2.is_null ()) { // comm2 is not an MpiComm
83  return comm1.getSize () == comm2.getSize (); // hope for the best
84  } else { // both comm1 and comm2 are MpiComm
85  MPI_Comm rawMpiComm1 = * (mpiComm1->getRawMpiComm ());
86  MPI_Comm rawMpiComm2 = * (mpiComm2->getRawMpiComm ());
87 
88  int result = MPI_UNEQUAL;
89  const int err = MPI_Comm_compare (rawMpiComm1, rawMpiComm2, &result);
90  TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
91  "congruent: MPI_Comm_compare failed");
92  return result == MPI_IDENT || result == MPI_CONGRUENT;
93  }
94  }
95 #else // NOT HAVE_MPI
96  return comm1.getSize () == comm2.getSize (); // hope for the best
97 #endif // HAVE_MPI
98 }
99 
100 std::unique_ptr<std::string>
101 createPrefix(const int myRank,
102  const char prefix[])
103 {
104  std::ostringstream os;
105  os << "Proc " << myRank << ": " << prefix << ": ";
106  return std::unique_ptr<std::string>(new std::string(os.str()));
107 }
108 
109 std::unique_ptr<std::string>
110 createPrefix(const Teuchos::Comm<int>* comm,
111  const char functionName[])
112 {
113  const int myRank = comm == nullptr ? -1 : comm->getRank();
114  const std::string prefix = std::string("Tpetra::") + functionName;
115  return createPrefix(myRank, prefix.c_str());
116 }
117 
118 std::unique_ptr<std::string>
119 createPrefix(const Teuchos::Comm<int>* comm,
120  const char className[],
121  const char methodName[])
122 {
123  const int myRank = comm == nullptr ? -1 : comm->getRank();
124  const std::string prefix = std::string("Tpetra::") +
125  className + std::string("::") + methodName;
126  return createPrefix(myRank, prefix.c_str());
127 }
128 
129 } // namespace Details
130 } // namespace Tpetra
bool congruent(const Teuchos::Comm< int > &comm1, const Teuchos::Comm< int > &comm2)
Whether the two communicators are congruent.
Definition: Tpetra_Util.cpp:64
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.