v0.15.5
Loading...
Searching...
No Matches
CommInterface.hpp
Go to the documentation of this file.
1/** \file CommInterface.hpp
2 * \brief Interface for communication functions
3 * \ingroup mofem_comm
4 *
5 * Functions used to communicate, share entities, share data, etc.
6 *
7 */
8
9#ifndef __COMMINTERFACE_HPP__
10#define __COMMINTERFACE_HPP__
11
12#include "UnknownInterface.hpp"
13
14namespace MoFEM {
15
17
18 static auto &getMap() {
19 static std::map<moab::Core *, boost::weak_ptr<ParallelComm>> pcomm_map;
20 return pcomm_map;
21 }
22
23 static auto getOrCreate(moab::Core *core_mesh_ptr) {
24 auto *mesh_ptr = core_mesh_ptr;
25 auto &pcomm_map = getMap();
26
27 if (auto it = pcomm_map.find(mesh_ptr); it != pcomm_map.end()) {
28 if (auto pcomm_ptr = it->second.lock())
29 return pcomm_ptr;
30 }
31
32 if (auto *raw_pcomm = ParallelComm::get_pcomm(mesh_ptr, MYPCOMM_INDEX)) {
33 auto pcomm_ptr = boost::shared_ptr<ParallelComm>(
34 raw_pcomm, [mesh_ptr](ParallelComm *pcomm) {});
35 pcomm_map[mesh_ptr] = pcomm_ptr;
36 return pcomm_ptr;
37 }
38
39 auto pcomm_ptr = boost::shared_ptr<ParallelComm>(
40 new ParallelComm(mesh_ptr, PETSC_COMM_WORLD),
41 [mesh_ptr](ParallelComm *pcomm) {
42 delete pcomm;
43 return;
44 });
45 return pcomm_ptr;
46 }
47
48 /** @deprecated Do not use this function, use getOrCreate(moab::Core*) instead */
49 static auto getOrCreate(const boost::shared_ptr<moab::Core> &core_mesh_ptr) {
50 return MeshPCommRegistry::getOrCreate(core_mesh_ptr.get());
51 }
52};
53
54/**
55 * \brief Managing BitRefLevels
56 * \ingroup mofem_bit_ref
57 * \nosubgrouping
58 */
60
61 inline static bool debug = false;
62 inline static Sev sev = Sev::verbose;
63
64 MoFEMErrorCode query_interface(boost::typeindex::type_index type_index,
65 UnknownInterface **iface) const;
66
68 bool dEbug;
69
70 CommInterface(const MoFEM::Core &core);
71
72 /**
73 * \brief Destructor
74 */
75 ~CommInterface() = default;
76
77 /** \name Make elements multishared */
78
79 /**@{*/
80
81 /**
82 * \brief resolve shared entities for finite elements in the problem
83 * \ingroup mofem_problems
84
85 * @param problem_ptr problem pointer
86 * @param fe_name finite element name
87 * @param verb verbosity level
88 * @return error code
89 *
90 * This allows for tag reduction or tag exchange, f.e.
91
92 \code
93 CHKERR m_field.resolveSharedFiniteElements(problem_ptr,"SHELL_ELEMENT");
94 Tag th;
95 CHKERR mField.get_moab().tag_get_handle("ADAPT_ORDER",th);
96 CHKERR ParallelComm* pcomm =
97 ParallelComm::get_pcomm(&mField.get_moab(),MYPCOMM_INDEX);
98 CHKERR pcomm->exchange_tags(th,prisms);
99 \endcode
100
101 *
102 */
104 const std::string &fe_name,
105 int verb = DEFAULT_VERBOSITY);
106
107 /**
108 * \brief resolve shared entities for finite elements in the problem
109 * \ingroup mofem_problems
110
111 * @param name problem name
112 * @param fe_name finite element name
113 * @param verb verbosity level
114 * @return error code
115 *
116 * This allows for tag reduction or tag exchange, f.e.
117
118 \code
119 CHKERR m_field.resolveSharedFiniteElements(problem_ptr,"SHELL_ELEMENT");
120 Tag th;
121 CHKERR mField.get_moab().tag_get_handle("ADAPT_ORDER",th);
122 ParallelComm* pcomm =
123 ParallelComm::get_pcomm(&mField.get_moab(),MYPCOMM_INDEX);
124 // CHKERR pcomm->reduce_tags(th,MPI_SUM,prisms);
125 CHKERR pcomm->exchange_tags(th,prisms);
126 \endcode
127
128 *
129 */
130 MoFEMErrorCode resolveSharedFiniteElements(const std::string name,
131 const std::string &fe_name,
132 int verb = DEFAULT_VERBOSITY);
133
134 /** \name Make entities multishared */
135
136 /**
137 * @brief make entities from proc 0 shared on all proc
138 *
139 * \note collective - need to be run on all processors in communicator
140 *
141 * @param entities
142 * @param num_entities
143 * @param my_proc default proc id to share from
144 * @param verb
145 * @return MoFEMErrorCode
146 */
148 const int num_entities,
149 const int owner_proc = 0,
150 int verb = DEFAULT_VERBOSITY);
151
152 /**
153 * @brief make entities from proc 0 shared on all proc
154 *
155 * \note collective - need to be run on all processors in communicator
156 *
157 * @param entities
158 * @param my_proc default proc id to share from
159 * @param verb
160 * @return MoFEMErrorCode
161 */
163 const int owner_proc = 0,
164 int verb = DEFAULT_VERBOSITY);
165
166 /**
167 * @brief make field entities multi shared
168 *
169 * \note collective - need to be run on all processors in communicator
170 *
171 * @param field_name
172 * @param owner_proc
173 * @param verb
174 * @return MoFEMErrorCode
175 */
177 const int owner_proc = 0,
178 int verb = DEFAULT_VERBOSITY);
179
180 /**
181 * @brief Exchange field data
182 *
183 * Exchange field for all shared and ghosted entities. This function should be
184 * called collectively over the communicator for this ParallelComm. If the
185 * entities vector is empty, all shared entities participate in the exchange.
186 * If a proc has no owned entities this function must still be called since it
187 * is collective.
188 *
189 * \note collective - need to be run on all processors in communicator
190 *
191 * \todo It is not working if field has entities different than vertices.
192 *
193 * @param verb
194 * @param field_name @return MoFEMErrorCode
195 */
197 int verb = DEFAULT_VERBOSITY);
198
199 /**@}*/
200
201 /** \name Synchronize entities (Following functions in future will be
202 * deprecated) */
203
204 /**@{*/
205
206 /** synchronize entity range on processors (collective)
207 *
208 * \note collective - need to be run on all processors in communicator
209 *
210 */
211
212 /**
213 * @brief synchronize entity range on processors (collective)
214 *
215 * \note collective - need to be run on all processors in communicator
216 *
217 * @param ent ents to send and received
218 * @param received_ents pointer to map with received entities
219 * @param verb
220 * @return MoFEMErrorCode
221 */
223 std::map<int, Range> *received_ents,
224 int verb = DEFAULT_VERBOSITY);
225
226 /**
227 * @brief synchronize entity range on processors (collective)
228 *
229 * \note collective - need to be run on all processors in communicator
230 *
231 * @param ent ents to send and received
232 * @param verb
233 * @return MoFEMErrorCode
234 */
236
237 /** synchronize entity range on processors (collective)
238 * \ingroup mofem_field
239 *
240 * \note collective - need to be run on all processors in communicator
241 *
242 * \param name field
243 * \param verbose level
244 *
245 */
246 MoFEMErrorCode synchroniseFieldEntities(const std::string name,
247 int verb = DEFAULT_VERBOSITY);
248
249 /**
250 * @brief Synchronise parent entities
251 *
252 * \note collective - need to be run on all processors in communicator
253 *
254 * Exchange parent entity tag and bitref of entity. Note thar parent handle
255 * can be different on each processor.
256 *
257 * @param ent
258 * @param verb
259 * @return MoFEMErrorCode
260 */
262 int verb = DEFAULT_VERBOSITY);
263
264 /**@}*/
265
266 /**@*/
267
268 /** \name Read, load and broadcast */
269
270 /**
271 * \brief Set partition tag to each finite element in the problem
272 * \ingroup mofem_problems_manager
273 *
274 * This will use one of the mesh partitioning programs available from PETSc
275 * See
276 * <http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatPartitioningType.html>
277 *
278 * @param ents Entities to partition
279 * @param dim Dimension of entities to partition
280 * @param adj_dim Adjacency dimension
281 * @param n_parts Number of partitions
282 * @param verb Verbosity level
283 * @return Error code
284 */
285 MoFEMErrorCode partitionMesh(const Range &ents, const int dim,
286 const int adj_dim, const int n_parts,
287 Tag *th_vertex_weights = nullptr,
288 Tag *th_edge_weights = nullptr,
289 Tag *th_part_weights = nullptr,
290 int verb = VERBOSE, const bool debug = false);
291
292
293 /**@}*/
294
295 /**@{*/
296
297 /** \name Load file */
298
299 using LoadFileFun = std::function<std::array<Range, 4>(
300 std::array<Range, 4> &&, std::vector<const CubitMeshSets *> &&)>;
301
302 static std::array<Range, 4>
303 defaultProcSkinFun(std::array<Range, 4> &&proc_ents_skin,
304 std::vector<const CubitMeshSets *> &&vec_ptr) {
305 return proc_ents_skin;
306 }
307
308 /**
309 * @brief Root proc has whole mesh, other procs only part of it
310 *
311 * @param moab
312 * @param file_name
313 * @param dim
314 * @param proc_skin_fun
315 * @param options
316 * @return MoFEMErrorCode
317 */
319 moab::Interface &moab, const char *file_name, int dim,
320 LoadFileFun proc_skin_fun = defaultProcSkinFun,
321 const char *options = "PARALLEL=BCAST;PARTITION=");
322
323 static Range getPartEntities(moab::Interface &moab, int part);
324
325 /**@}*/
326 /**@*/
327
328 /** \name Functions when root proc has all entities */
329
331 std::pair<std::pair<Range, Range>, SmartPetscObj<Vec>>;
332
333 /**
334 * @brief Create a ghost vector for exchanging data
335 *
336 * @note Only works if loadFileRootProcAllRestDistributed function is used.
337 *
338 * @param comm
339 * @param moab
340 * @param get_entities_fun function to get entities to partition
341 * @param dim dimension of partition entities
342 * @param adj_dim dimension of adjacent entities
343 * @param nb_coeffs number of coefficients
344 * @param sev
345 * @param root_rank
346 * @param get_vertices whether to get vertices along with entities
347 *
348 * Example:
349 * \code
350
351 // create vector for face exchange
352 auto face_exchange = CommInterface::createEntitiesPetscVector(
353 mField.get_comm(), mField.get_moab(), 2, 3, Sev::inform);
354 // update data in vector from tag.
355 CHKERR CommInterface::updateEntitiesPetscVector(
356 mField.get_moab(), face_exchange, tag_value_to_exchange);
357
358 * \endcode
359 *
360 * @return std::pair<Range, SmartPetscObj<Vec>>
361 */
363 createEntitiesPetscVector(MPI_Comm comm, moab::Interface &moab,
364 std::function<Range(Range)> get_entities_fun,
365 const int nb_coeffs, Sev sev = Sev::verbose,
366 int root_rank = 0, bool get_vertices = true);
367
368 /**
369 * @brief Create a ghost vector for exchanging data
370 *
371 * @note Only works if loadFileRootProcAllRestDistributed function is used.
372 *
373 * @param comm
374 * @param moab
375 * @param dim dimension of partition entities
376 * @param adj_dim dimension of adjacent entities
377 * @param nb_coeffs number of coefficients
378 * @param sev
379 * @param root_rank
380 *
381 * @return std::pair<Range, SmartPetscObj<Vec>>
382 */
384 createEntitiesPetscVector(MPI_Comm comm, moab::Interface &moab, int dim,
385 const int nb_coeffs, Sev sev = Sev::verbose,
386 int root_rank = 0);
387
388 using UpdateGhosts = std::function<MoFEMErrorCode(Vec vec)>;
389
392 CHKERR VecAssemblyBegin(v);
393 CHKERR VecAssemblyEnd(v);
394 CHKERR VecGhostUpdateBegin(v, ADD_VALUES, SCATTER_REVERSE);
395 CHKERR VecGhostUpdateEnd(v, ADD_VALUES, SCATTER_REVERSE);
396 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
397 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
399 };
400
401 static MoFEMErrorCode setVectorFromTag(moab::Interface &moab,
402 EntitiesPetscVector &vec, Tag tag);
403 static MoFEMErrorCode setTagFromVector(moab::Interface &moab,
404 EntitiesPetscVector &vec, Tag tag);
405
406 /**
407 * @brief Exchange data between vector and data
408 *
409 * @param tag
410 * @return MoFEMErrorCode
411 */
412 static MoFEMErrorCode
413 updateEntitiesPetscVector(moab::Interface &moab, EntitiesPetscVector &vec,
414 Tag tag,
415 UpdateGhosts update_gosts = defaultUpdateGhosts);
416
417 /**@}*/
418
419};
420} // namespace MoFEM
421
422#endif //__COMMINTERFACE_HPP__
423
424/**
425 * \defgroup mofem_comm Comm interface
426 * \brief Communication interface
427 *
428 * \ingroup mofem
429 */
MoFEM interface.
@ DEFAULT_VERBOSITY
@ VERBOSE
#define MYPCOMM_INDEX
default communicator number PCOMM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
MoFEMErrorCode synchroniseFieldEntities(const std::string name, int verb=DEFAULT_VERBOSITY)
SeverityLevel
Severity levels.
MoFEMErrorCode partitionMesh(const Range &ents, const int dim, const int adj_dim, const int n_parts, Tag *th_vertex_weights=nullptr, Tag *th_edge_weights=nullptr, Tag *th_part_weights=nullptr, int verb=VERBOSE, const bool debug=false)
Set partition tag to each finite element in the problem.
MoFEMErrorCode resolveSharedFiniteElements(const Problem *problem_ptr, const std::string &fe_name, int verb=DEFAULT_VERBOSITY)
resolve shared entities for finite elements in the problem
const double v
phase velocity of light in medium (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
constexpr auto field_name
Managing BitRefLevels.
static MoFEMErrorCode updateEntitiesPetscVector(moab::Interface &moab, EntitiesPetscVector &vec, Tag tag, UpdateGhosts update_gosts=defaultUpdateGhosts)
Exchange data between vector and data.
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
std::function< MoFEMErrorCode(Vec vec)> UpdateGhosts
static Range getPartEntities(moab::Interface &moab, int part)
~CommInterface()=default
Destructor.
static MoFEMErrorCode defaultUpdateGhosts(Vec v)
MoFEMErrorCode makeEntitiesMultishared(const EntityHandle *entities, const int num_entities, const int owner_proc=0, int verb=DEFAULT_VERBOSITY)
make entities from proc 0 shared on all proc
static std::array< Range, 4 > defaultProcSkinFun(std::array< Range, 4 > &&proc_ents_skin, std::vector< const CubitMeshSets * > &&vec_ptr)
static MoFEMErrorCode loadFileRootProcAllRestDistributed(moab::Interface &moab, const char *file_name, int dim, LoadFileFun proc_skin_fun=defaultProcSkinFun, const char *options="PARALLEL=BCAST;PARTITION=")
Root proc has whole mesh, other procs only part of it.
MoFEMErrorCode synchroniseEntities(Range &ent, std::map< int, Range > *received_ents, int verb=DEFAULT_VERBOSITY)
synchronize entity range on processors (collective)
std::function< std::array< Range, 4 >(std::array< Range, 4 > &&, std::vector< const CubitMeshSets * > &&)> LoadFileFun
static MoFEMErrorCode setVectorFromTag(moab::Interface &moab, EntitiesPetscVector &vec, Tag tag)
static MoFEMErrorCode setTagFromVector(moab::Interface &moab, EntitiesPetscVector &vec, Tag tag)
MoFEMErrorCode makeFieldEntitiesMultishared(const std::string field_name, const int owner_proc=0, int verb=DEFAULT_VERBOSITY)
make field entities multi shared
std::pair< std::pair< Range, Range >, SmartPetscObj< Vec > > EntitiesPetscVector
MoFEMErrorCode resolveParentEntities(const Range &ent, int verb=DEFAULT_VERBOSITY)
Synchronise parent entities.
MoFEMErrorCode exchangeFieldData(const std::string field_name, int verb=DEFAULT_VERBOSITY)
Exchange field data.
static EntitiesPetscVector createEntitiesPetscVector(MPI_Comm comm, moab::Interface &moab, std::function< Range(Range)> get_entities_fun, const int nb_coeffs, Sev sev=Sev::verbose, int root_rank=0, bool get_vertices=true)
Create a ghost vector for exchanging data.
Core (interface) class.
Definition Core.hpp:82
static auto getOrCreate(const boost::shared_ptr< moab::Core > &core_mesh_ptr)
static auto getOrCreate(moab::Core *core_mesh_ptr)
keeps basic data about problem
intrusive_ptr for managing petsc objects
base class for all interface classes