v0.14.0
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 
14 namespace MoFEM {
15 
16 /**
17  * \brief Managing BitRefLevels
18  * \ingroup mofem_bit_ref
19  * \nosubgrouping
20  */
22 
23  inline static bool debug = false;
24  inline static Sev sev = Sev::verbose;
25 
26  MoFEMErrorCode query_interface(boost::typeindex::type_index type_index,
27  UnknownInterface **iface) const;
28 
30  bool dEbug;
31 
32  CommInterface(const MoFEM::Core &core);
33 
34  /**
35  * \brief Destructor
36  */
37  ~CommInterface() = default;
38 
39  /** \name Make elemets multishared */
40 
41  /**@{*/
42 
43  /**
44  * \brief resolve shared entities for finite elements in the problem
45  * \ingroup mofem_problems
46 
47  * @param problem_ptr problem pointer
48  * @param fe_name finite element name
49  * @param verb verbosity level
50  * @return error code
51  *
52  * This allows for tag reduction or tag exchange, f.e.
53 
54  \code
55  CHKERR m_field.resolveSharedFiniteElements(problem_ptr,"SHELL_ELEMENT");
56  Tag th;
57  CHKERR mField.get_moab().tag_get_handle("ADAPT_ORDER",th);
58  CHKERR ParallelComm* pcomm =
59  ParallelComm::get_pcomm(&mField.get_moab(),MYPCOMM_INDEX);
60  CHKERR pcomm->exchange_tags(th,prisms);
61  \endcode
62 
63  *
64  */
66  const std::string &fe_name,
67  int verb = DEFAULT_VERBOSITY);
68 
69  /**
70  * \brief resolve shared entities for finite elements in the problem
71  * \ingroup mofem_problems
72 
73  * @param name problem name
74  * @param fe_name finite element name
75  * @param verb verbosity level
76  * @return error code
77  *
78  * This allows for tag reduction or tag exchange, f.e.
79 
80  \code
81  CHKERR m_field.resolveSharedFiniteElements(problem_ptr,"SHELL_ELEMENT");
82  Tag th;
83  CHKERR mField.get_moab().tag_get_handle("ADAPT_ORDER",th);
84  ParallelComm* pcomm =
85  ParallelComm::get_pcomm(&mField.get_moab(),MYPCOMM_INDEX);
86  // CHKERR pcomm->reduce_tags(th,MPI_SUM,prisms);
87  CHKERR pcomm->exchange_tags(th,prisms);
88  \endcode
89 
90  *
91  */
92  MoFEMErrorCode resolveSharedFiniteElements(const std::string name,
93  const std::string &fe_name,
94  int verb = DEFAULT_VERBOSITY);
95 
96  /** \name Make entities multishared */
97 
98  /**
99  * @brief make entities from proc 0 shared on all proc
100  *
101  * \note collective - need tu be run on all processors in communicator
102  *
103  * @param entities
104  * @param num_entities
105  * @param my_proc default proc id to share from
106  * @param verb
107  * @return MoFEMErrorCode
108  */
110  const int num_entities,
111  const int owner_proc = 0,
112  int verb = DEFAULT_VERBOSITY);
113 
114  /**
115  * @brief make entities from proc 0 shared on all proc
116  *
117  * \note collective - need tu be run on all processors in communicator
118  *
119  * @param entities
120  * @param my_proc default proc id to share from
121  * @param verb
122  * @return MoFEMErrorCode
123  */
125  const int owner_proc = 0,
126  int verb = DEFAULT_VERBOSITY);
127 
128  /**
129  * @brief make field entities multi shared
130  *
131  * \note collective - need tu be run on all processors in communicator
132  *
133  * @param field_name
134  * @param owner_proc
135  * @param verb
136  * @return MoFEMErrorCode
137  */
139  const int owner_proc = 0,
140  int verb = DEFAULT_VERBOSITY);
141 
142  /**
143  * @brief Exchange field data
144  *
145  * Exchange field for all shared and ghosted entities. This function should be
146  * called collectively over the communicator for this ParallelComm. If the
147  * entities vector is empty, all shared entities participate in the exchange.
148  * If a proc has no owned entities this function must still be called since it
149  * is collective.
150  *
151  * \note collective - need tu be run on all processors in communicator
152  *
153  * \todo It is not working if field has entities diffrent than vertices.
154  *
155  * @param verb
156  * @param field_name @return MoFEMErrorCode
157  */
158  MoFEMErrorCode exchangeFieldData(const std::string field_name,
159  int verb = DEFAULT_VERBOSITY);
160 
161  /**@}*/
162 
163  /** \name Synchronize entities (Following functions in future will be
164  * deprecated) */
165 
166  /**@{*/
167 
168  /** synchronize entity range on processors (collective)
169  *
170  * \note collective - need tu be run on all processors in communicator
171  *
172  */
173 
174  /**
175  * @brief synchronize entity range on processors (collective)
176  *
177  * \note collective - need tu be run on all processors in communicator
178  *
179  * @param ent ents to send and received
180  * @param received_ents pointer to map with received entities
181  * @param verb
182  * @return MoFEMErrorCode
183  */
185  std::map<int, Range> *received_ents,
186  int verb = DEFAULT_VERBOSITY);
187 
188  /**
189  * @brief synchronize entity range on processors (collective)
190  *
191  * \note collective - need tu be run on all processors in communicator
192  *
193  * @param ent ents to send and received
194  * @param verb
195  * @return MoFEMErrorCode
196  */
198 
199  /** synchronize entity range on processors (collective)
200  * \ingroup mofem_field
201  *
202  * \note collective - need tu be run on all processors in communicator
203  *
204  * \param name field
205  * \param verbose level
206  *
207  */
208  MoFEMErrorCode synchroniseFieldEntities(const std::string name,
209  int verb = DEFAULT_VERBOSITY);
210 
211  /**
212  * @brief Synchronise parent entities
213  *
214  * \note collective - need tu be run on all processors in communicator
215  *
216  * Exchange parent entity tag and bitref of entity. Note thar parent handle
217  * can be different on each processor.
218  *
219  * @param ent
220  * @param verb
221  * @return MoFEMErrorCode
222  */
224  int verb = DEFAULT_VERBOSITY);
225 
226  /**@}*/
227 
228  /**@*/
229 
230  /** \name Read load and boradcoast */
231 
232  /**
233  * \brief Set partition tag to each finite element in the problem
234  * \ingroup mofem_problems_manager
235  *
236  * This will use one of the mesh partitioning programs available from PETSc
237  * See
238  * <http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatPartitioningType.html>
239  *
240  * @param ents Entities to partition
241  * @param dim Dimension of entities to partition
242  * @param adj_dim Adjacency dimension
243  * @param n_parts Number of partitions
244  * @param verb Verbosity level
245  * @return Error code
246  */
247  MoFEMErrorCode partitionMesh(const Range &ents, const int dim,
248  const int adj_dim, const int n_parts,
249  Tag *th_vertex_weights = nullptr,
250  Tag *th_edge_weights = nullptr,
251  Tag *th_part_weights = nullptr,
252  int verb = VERBOSE, const bool debug = false);
253 
254 
255  /**@}*/
256 
257  /**@{*/
258 
259  /** \name Load file */
260 
261  using LoadFileFun = std::function<std::array<Range, 4>(
262  std::array<Range, 4> &&, std::vector<const CubitMeshSets *> &&)>;
263 
264  static std::array<Range, 4>
265  defaultProcSkinFun(std::array<Range, 4> &&proc_ents_skin,
266  std::vector<const CubitMeshSets *> &&vec_ptr) {
267  return proc_ents_skin;
268  }
269 
270  /**
271  * @brief Root proc has whole mesh, other procs only part of it
272  *
273  * @param moab
274  * @param file_name
275  * @param dim
276  * @param proc_skin_fun
277  * @param options
278  * @return MoFEMErrorCode
279  */
281  moab::Interface &moab, const char *file_name, int dim,
282  LoadFileFun proc_skin_fun = defaultProcSkinFun,
283  const char *options = "PARALLEL=BCAST;PARTITION=");
284 
285  static Range getPartEntities(moab::Interface &moab, int part);
286 
287  /**@}*/
288  /**@*/
289 
290  /** \name Functions when rooot proc have all entities */
291 
292  using EntitiesPetscVector =
293  std::pair<std::pair<Range, Range>, SmartPetscObj<Vec>>;
294 
295  /**
296  * @brief Create a ghost vector for exchanging data
297  *
298  * @note Only works if loadFileRootProcAllRestDistributed function is used.
299  *
300  * @param comm
301  * @param moab
302  * @param dim dimension of partition entities
303  * @param adj_dim dimension of adjacent entities
304  * @param nb_coeffs number of coefficients
305  * @param sev
306  * @param root_rank
307  *
308  * @return std::pair<Range, SmartPetscObj<Vec>>
309  */
310  static EntitiesPetscVector
311  createEntitiesPetscVector(MPI_Comm comm, moab::Interface &moab, int dim,
312  const int nb_coeffs, Sev sev = Sev::verbose,
313  int root_rank = 0);
314 
315  using UpdateGhosts = std::function<MoFEMErrorCode(Vec vec)>;
316 
319  CHKERR VecAssemblyBegin(v);
320  CHKERR VecAssemblyEnd(v);
321  CHKERR VecGhostUpdateBegin(v, ADD_VALUES, SCATTER_REVERSE);
322  CHKERR VecGhostUpdateEnd(v, ADD_VALUES, SCATTER_REVERSE);
323  CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
324  CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
326  };
327 
328  /**
329  * @brief Exchange data between vector and data
330  *
331  * @param tag
332  * @return MoFEMErrorCode
333  */
334  static MoFEMErrorCode
336  Tag tag,
337  UpdateGhosts update_gosts = defaultUpdateGhosts);
338 
339  /**@}*/
340 
341 };
342 } // namespace MoFEM
343 
344 #endif //__COMMINTERFACE_HPP__
345 
346 /**
347  * \defgroup mofem_comm Comm intrface
348  * \brief Comm interface
349  *
350  * \ingroup mofem
351  */
MoFEM::CommInterface::UpdateGhosts
std::function< MoFEMErrorCode(Vec vec)> UpdateGhosts
Definition: CommInterface.hpp:315
DEFAULT_VERBOSITY
@ DEFAULT_VERBOSITY
Definition: definitions.h:220
MoFEM::CommInterface::LoadFileFun
std::function< std::array< Range, 4 >(std::array< Range, 4 > &&, std::vector< const CubitMeshSets * > &&)> LoadFileFun
Definition: CommInterface.hpp:262
MoFEM::CoreTmp< 0 >
Core (interface) class.
Definition: Core.hpp:82
MoFEM::CommInterface::defaultUpdateGhosts
static MoFEMErrorCode defaultUpdateGhosts(Vec v)
Definition: CommInterface.hpp:317
EntityHandle
MoFEM::CommInterface::~CommInterface
~CommInterface()=default
Destructor.
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::CommInterface::updateEntitiesPetscVector
static MoFEMErrorCode updateEntitiesPetscVector(moab::Interface &moab, EntitiesPetscVector &vec, Tag tag, UpdateGhosts update_gosts=defaultUpdateGhosts)
Exchange data between vector and data.
Definition: CommInterface.cpp:1612
MoFEM::CommInterface::partitionMesh
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.
Definition: CommInterface.cpp:875
MoFEM::CommInterface::resolveParentEntities
MoFEMErrorCode resolveParentEntities(const Range &ent, int verb=DEFAULT_VERBOSITY)
Synchronise parent entities.
Definition: CommInterface.cpp:256
MoFEM::Interface
DeprecatedCoreInterface Interface
Definition: Interface.hpp:2010
MoFEM::CommInterface::defaultProcSkinFun
static std::array< Range, 4 > defaultProcSkinFun(std::array< Range, 4 > &&proc_ents_skin, std::vector< const CubitMeshSets * > &&vec_ptr)
Definition: CommInterface.hpp:265
VERBOSE
@ VERBOSE
Definition: definitions.h:222
MoFEM::CommInterface::makeFieldEntitiesMultishared
MoFEMErrorCode makeFieldEntitiesMultishared(const std::string field_name, const int owner_proc=0, int verb=DEFAULT_VERBOSITY)
make field entities multi shared
Definition: CommInterface.cpp:810
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::CommInterface::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: CommInterface.cpp:16
MoFEM::CommInterface::resolveSharedFiniteElements
MoFEMErrorCode resolveSharedFiniteElements(const Problem *problem_ptr, const std::string &fe_name, int verb=DEFAULT_VERBOSITY)
resolve shared entities for finite elements in the problem
Definition: CommInterface.cpp:550
MoFEM::CommInterface::synchroniseFieldEntities
MoFEMErrorCode synchroniseFieldEntities(const std::string name, int verb=DEFAULT_VERBOSITY)
Definition: CommInterface.cpp:244
UnknownInterface.hpp
MoFEM interface.
MoFEM::CommInterface::EntitiesPetscVector
std::pair< std::pair< Range, Range >, SmartPetscObj< Vec > > EntitiesPetscVector
Definition: CommInterface.hpp:293
MoFEM::CommInterface::loadFileRootProcAllRestDistributed
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.
Definition: CommInterface.cpp:1221
MoFEM::LogManager::SeverityLevel
SeverityLevel
Severity levels.
Definition: LogManager.hpp:33
field_name
constexpr auto field_name
Definition: poisson_2d_homogeneous.cpp:13
MoFEM::CommInterface::synchroniseEntities
MoFEMErrorCode synchroniseEntities(Range &ent, std::map< int, Range > *received_ents, int verb=DEFAULT_VERBOSITY)
synchronize entity range on processors (collective)
Definition: CommInterface.cpp:26
MoFEM::UnknownInterface
base class for all interface classes
Definition: UnknownInterface.hpp:34
v
const double v
phase velocity of light in medium (cm/ns)
Definition: initial_diffusion.cpp:40
Range
MoFEM::CommInterface::dEbug
bool dEbug
Definition: CommInterface.hpp:30
MoFEM::CommInterface::debug
static bool debug
Definition: CommInterface.hpp:23
MoFEM::CommInterface
Managing BitRefLevels.
Definition: CommInterface.hpp:21
MoFEM::CommInterface::getPartEntities
static Range getPartEntities(moab::Interface &moab, int part)
Definition: CommInterface.cpp:1425
MoFEM::CommInterface::CommInterface
CommInterface(const MoFEM::Core &core)
Definition: CommInterface.cpp:23
EigenMatrix::Vec
const FTensor::Tensor2< T, Dim, Dim > Vec
Definition: MatrixFunction.hpp:64
MoFEM::CommInterface::makeEntitiesMultishared
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
Definition: CommInterface.cpp:654
MoFEM::CommInterface::cOre
MoFEM::Core & cOre
Definition: CommInterface.hpp:29
MoFEM::CommInterface::createEntitiesPetscVector
static EntitiesPetscVector createEntitiesPetscVector(MPI_Comm comm, moab::Interface &moab, int dim, const int nb_coeffs, Sev sev=Sev::verbose, int root_rank=0)
Create a ghost vector for exchanging data.
Definition: CommInterface.cpp:1467
MoFEM::Problem
keeps basic data about problem
Definition: ProblemsMultiIndices.hpp:54
MoFEM::SmartPetscObj< Vec >
MoFEM::CommInterface::exchangeFieldData
MoFEMErrorCode exchangeFieldData(const std::string field_name, int verb=DEFAULT_VERBOSITY)
Exchange field data.
Definition: CommInterface.cpp:825
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
MoFEM::CommInterface::sev
static Sev sev
Definition: CommInterface.hpp:24
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359