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