Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_DefaultMpiComm.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 
12 // Only enable the contents of this file if building with MPI.
13 #ifdef HAVE_TEUCHOS_MPI
14 
15 namespace Teuchos {
16 
17  std::string
18  mpiErrorCodeToString (const int errCode)
19  {
20  if (errCode == MPI_SUCCESS) {
21  return "MPI_SUCCESS";
22  }
23  else {
24  char rawErrString[MPI_MAX_ERROR_STRING];
25  int len = 0;
26  int err = MPI_Error_string (errCode, rawErrString, &len);
27  if (err != MPI_SUCCESS) {
28  // Assume that the string wasn't written. This means it might
29  // not be null terminated, so make it a valid empty string by
30  // writing the null termination character to it.
31  if (MPI_MAX_ERROR_STRING > 0) {
32  rawErrString[0] = '\0';
33  }
34  }
35  return std::string (rawErrString);
36  }
37  }
38 
39  namespace details {
40  void safeCommFree (MPI_Comm* comm) {
41  // FIXME (mfh 08 Dec 2014) Use the MPI_Finalize hook trick to
42  // call MPI_Comm_free at MPI_Finalize automatically, if it
43  // hasn't already been called on the object. Store the MPI_Comm
44  // (by allocated pointer) as the value of the (key,value) pair
45  // (used in the hook), and be sure to free the pair if the free
46  // function is called before MPI_Finalize.
47  int finalized = 0;
48  const int err = MPI_Finalized (&finalized);
49  // Just to be safe, don't do anything if calling MPI_Finalized
50  // didn't succeed. It's better to leak memory than to crash.
51  if (err == MPI_SUCCESS && ! finalized) {
52  // Don't throw an exception if MPI_Comm_free reports an error,
53  // since we're likely to be in a destructor and destructors
54  // shouldn't throw exceptions.
55  (void) MPI_Comm_free (comm);
56  }
57  }
58 
59  int setCommErrhandler (MPI_Comm comm, MPI_Errhandler handler) {
60 #if MPI_VERSION >= 2
61  return MPI_Comm_set_errhandler (comm, handler);
62 #else // MPI 1
63  return MPI_Errhandler_set (comm, handler);
64 #endif // MPI_VERSION >= 2
65  }
66  } // namespace details
67 
68 } // namespace Teuchos
69 
70 #endif // HAVE_TEUCHOS_MPI
Implementation of Teuchos wrappers for MPI.