deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
trilinos_utilities.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2022 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#include <deal.II/base/mpi.h>
15
16#ifdef DEAL_II_WITH_TRILINOS
18# ifdef DEAL_II_TRILINOS_WITH_EPETRA
19# ifdef DEAL_II_WITH_MPI
20# include <Epetra_MpiComm.h>
21# endif
22# include <Epetra_SerialComm.h>
23# endif
24# ifdef DEAL_II_WITH_MPI
25# include <Teuchos_DefaultComm.hpp>
26# endif
27# include <Teuchos_RCP.hpp>
29#endif
30
32
33namespace Utilities
34{
35#ifdef DEAL_II_TRILINOS_WITH_EPETRA
36 namespace Trilinos
37 {
38 const Epetra_Comm &
40 {
41# ifdef DEAL_II_WITH_MPI
42 static Teuchos::RCP<Epetra_MpiComm> communicator =
43 Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_WORLD), true);
44# else
45 static Teuchos::RCP<Epetra_SerialComm> communicator =
46 Teuchos::rcp(new Epetra_SerialComm(), true);
47# endif
48
49 return *communicator;
50 }
51
52
53
54 const Epetra_Comm &
56 {
57# ifdef DEAL_II_WITH_MPI
58 static Teuchos::RCP<Epetra_MpiComm> communicator =
59 Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_SELF), true);
60# else
61 static Teuchos::RCP<Epetra_SerialComm> communicator =
62 Teuchos::rcp(new Epetra_SerialComm(), true);
63# endif
64
65 return *communicator;
66 }
67
68
69
70 Epetra_Comm *
71 duplicate_communicator(const Epetra_Comm &communicator)
72 {
73# ifdef DEAL_II_WITH_MPI
74
75 // see if the communicator is in fact a
76 // parallel MPI communicator; if so,
77 // return a duplicate of it
78 const Epetra_MpiComm *mpi_comm =
79 dynamic_cast<const Epetra_MpiComm *>(&communicator);
80 if (mpi_comm != nullptr)
81 return new Epetra_MpiComm(
82 Utilities::MPI::duplicate_communicator(mpi_comm->GetMpiComm()));
83# endif
84
85 // if we don't support MPI, or if the
86 // communicator in question was in fact
87 // not an MPI communicator, return a
88 // copy of the same object again
89 Assert(dynamic_cast<const Epetra_SerialComm *>(&communicator) != nullptr,
91 return new Epetra_SerialComm(
92 dynamic_cast<const Epetra_SerialComm &>(communicator));
93 }
94
95
96
97 void
98 destroy_communicator(Epetra_Comm &communicator)
99 {
100 // save the communicator, reset the map, and delete the communicator if
101 // this whole thing was created as an MPI communicator
102# ifdef DEAL_II_WITH_MPI
103 Epetra_MpiComm *mpi_comm = dynamic_cast<Epetra_MpiComm *>(&communicator);
104 if (mpi_comm != nullptr)
105 {
106 MPI_Comm comm = mpi_comm->GetMpiComm();
107 *mpi_comm = Epetra_MpiComm(MPI_COMM_SELF);
108
110 }
111# endif
112 }
113
114
115
116 unsigned int
117 get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
118 {
119 return mpi_communicator.NumProc();
120 }
121
122
123 unsigned int
124 get_this_mpi_process(const Epetra_Comm &mpi_communicator)
125 {
126 return static_cast<unsigned int>(mpi_communicator.MyPID());
127 }
128
129
130
131 Epetra_Map
132 duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
133 {
134 if (map.LinearMap() == true)
135 {
136 // each processor stores a
137 // contiguous range of
138 // elements in the
139 // following constructor
140 // call
141 return Epetra_Map(map.NumGlobalElements(),
142 map.NumMyElements(),
143 map.IndexBase(),
144 comm);
145 }
146 else
147 {
148 // the range is not
149 // contiguous
150 return Epetra_Map(map.NumGlobalElements(),
151 map.NumMyElements(),
152 map.MyGlobalElements(),
153 0,
154 comm);
155 }
156 }
157 } // namespace Trilinos
158#endif
159
160
161#ifdef DEAL_II_TRILINOS_WITH_TPETRA
162 namespace Trilinos
163 {
164 const Teuchos::RCP<const Teuchos::Comm<int>> &
166 {
167# ifdef DEAL_II_WITH_MPI
168 static auto communicator = Teuchos::RCP<const Teuchos::Comm<int>>(
169 new Teuchos::MpiComm<int>(MPI_COMM_SELF));
170# else
171 static auto communicator =
172 Teuchos::RCP<const Teuchos::Comm<int>>(new Teuchos::Comm<int>());
173# endif
174
175 return communicator;
176 }
177
178
179
182 const Teuchos::RCP<const Teuchos::Comm<int>> &teuchos_comm)
183 {
184 MPI_Comm out;
185# ifdef DEAL_II_WITH_MPI
186 // Cast from Teuchos::Comm<int> to Teuchos::MpiComm<int>.
187 const Teuchos::MpiComm<int> *mpi_comm =
188 dynamic_cast<const Teuchos::MpiComm<int> *>(teuchos_comm.get());
189 Assert(mpi_comm != nullptr, ExcInternalError());
190 // From the Teuchos::MpiComm<int> object we can extract
191 // the MPI_Comm object via the getRawMpiComm() function.
192 out = *(mpi_comm->getRawMpiComm())();
193# else
194 out = MPI_COMM_SELF;
195# endif
196 return out;
197 }
198 } // namespace Trilinos
199#endif // DEAL_II_TRILINOS_WITH_TPETRA
200
201} // namespace Utilities
202
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Definition config.h:636
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
Definition config.h:680
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
const MPI_Comm comm
Definition mpi.cc:912
MPI_Comm duplicate_communicator(const MPI_Comm mpi_communicator)
Definition mpi.cc:154
void free_communicator(MPI_Comm mpi_communicator)
Definition mpi.cc:165
void destroy_communicator(Epetra_Comm &communicator)
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
const Epetra_Comm & comm_self()
MPI_Comm teuchos_comm_to_mpi_comm(const Teuchos::RCP< const Teuchos::Comm< int > > &teuchos_comm)
const Teuchos::RCP< const Teuchos::Comm< int > > & tpetra_comm_self()
Epetra_Comm * duplicate_communicator(const Epetra_Comm &communicator)
const Epetra_Comm & comm_world()