Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_DefaultMpiComm.cpp
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#include <Teuchos_DefaultMpiComm_def.hpp>
12
13// Only enable the contents of this file if building with MPI.
14#ifdef HAVE_TEUCHOS_MPI
15
16namespace Teuchos {
17
18 std::string
19 mpiErrorCodeToString (const int errCode)
20 {
21 if (errCode == MPI_SUCCESS) {
22 return "MPI_SUCCESS";
23 }
24 else {
25 char rawErrString[MPI_MAX_ERROR_STRING];
26 int len = 0;
27 int err = MPI_Error_string (errCode, rawErrString, &len);
28 if (err != MPI_SUCCESS) {
29 // Assume that the string wasn't written. This means it might
30 // not be null terminated, so make it a valid empty string by
31 // writing the null termination character to it.
32 if (MPI_MAX_ERROR_STRING > 0) {
33 rawErrString[0] = '\0';
34 }
35 }
36 return std::string (rawErrString);
37 }
38 }
39
40 namespace details {
41 void safeCommFree (MPI_Comm* comm) {
42 // FIXME (mfh 08 Dec 2014) Use the MPI_Finalize hook trick to
43 // call MPI_Comm_free at MPI_Finalize automatically, if it
44 // hasn't already been called on the object. Store the MPI_Comm
45 // (by allocated pointer) as the value of the (key,value) pair
46 // (used in the hook), and be sure to free the pair if the free
47 // function is called before MPI_Finalize.
48 int finalized = 0;
49 const int err = MPI_Finalized (&finalized);
50 // Just to be safe, don't do anything if calling MPI_Finalized
51 // didn't succeed. It's better to leak memory than to crash.
52 if (err == MPI_SUCCESS && ! finalized) {
53 // Don't throw an exception if MPI_Comm_free reports an error,
54 // since we're likely to be in a destructor and destructors
55 // shouldn't throw exceptions.
56 (void) MPI_Comm_free (comm);
57 }
58 }
59
60 int setCommErrhandler (MPI_Comm comm, MPI_Errhandler handler) {
61#if MPI_VERSION >= 2
62 return MPI_Comm_set_errhandler (comm, handler);
63#else // MPI 1
64 return MPI_Errhandler_set (comm, handler);
65#endif // MPI_VERSION >= 2
66 }
67 } // namespace details
68
69} // namespace Teuchos
70
71#define TEUCHOS_MPI_COMM_INSTANT(ORDINAL) \
72 template class Teuchos::MpiCommStatus<ORDINAL>; \
73 \
74 template Teuchos::RCP<Teuchos::MpiCommStatus<ORDINAL>> \
75 Teuchos::mpiCommStatus(MPI_Status rawMpiStatus); \
76 \
77 template class Teuchos::MpiCommRequestBase<ORDINAL>; \
78 \
79 template class Teuchos::MpiCommRequest<ORDINAL>; \
80 \
81 template Teuchos::RCP<Teuchos::MpiCommRequest<ORDINAL>> \
82 Teuchos::mpiCommRequest(MPI_Request, \
83 const Teuchos::ArrayView<char>::size_type); \
84 template class Teuchos::MpiComm<ORDINAL>; \
85 \
86 template Teuchos::RCP<Teuchos::MpiComm<ORDINAL>> Teuchos::createMpiComm( \
87 const RCP<const OpaqueWrapper<MPI_Comm>> &); \
88 \
89 template Teuchos::RCP<Teuchos::MpiComm<ORDINAL>> Teuchos::createMpiComm( \
90 const RCP<const OpaqueWrapper<MPI_Comm>> &, const int); \
91 \
92 template MPI_Comm Teuchos::getRawMpiComm(const Comm<ORDINAL> &comm);
93
94TEUCHOS_MPI_COMM_INSTANT(short)
95TEUCHOS_MPI_COMM_INSTANT(int)
96TEUCHOS_MPI_COMM_INSTANT(long)
97TEUCHOS_MPI_COMM_INSTANT(long long)
98
99#endif // HAVE_TEUCHOS_MPI
Implementation of Teuchos wrappers for MPI.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos implementation details.