LifeV
MapEpetra.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief MapEpetra
30 
31  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
32  @author Simone Deparis <simone.deparis@epfl.ch>
33  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
34  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
35 
36  @date 26-10-2006
37 
38  This class manages the distribution of elements of matrices or vectors on a parallel machine
39  */
40 
41 #ifndef _EPETRAMAP_
42 #define _EPETRAMAP_
43 
44 
45 #include <Epetra_Map.h>
46 #include <Epetra_Export.h>
47 #include <Epetra_Import.h>
48 #include <Epetra_Comm.h>
49 
50 #ifdef HAVE_HDF5
51 #include <EpetraExt_HDF5.h>
52 #endif
53 
54 #include <lifev/core/LifeV.hpp>
55 #include <lifev/core/array/EnumMapEpetra.hpp>
56 #include <lifev/core/array/MapEpetraData.hpp>
57 #include <lifev/core/array/MapVector.hpp>
58 
59 namespace LifeV
60 {
61 
62 //! MapEpetra - Wrapper for Epetra_Map
63 /*!
64  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
65  @author Simone Deparis <simone.deparis@epfl.ch>
66  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
67 
68  The MapEpetra class provides a general interface for the Epetra_Map class of Trilinos.
69 
70  Visit http://trilinos.sandia.gov for more informations about Epetra_Map.
71  */
72 class MapEpetra
73 {
74 public:
75 
76  //! @name Public Types
77  //@{
78 
81 
83 
84  /* Double shared_ptr are used here to ensure that all the similar MapEpetra
85  point to the same exporter/importer. If double shared_ptr were not used, a
86  map initialized without importer/exporter (i.e. with a shared_ptr
87  pointing to 0) would not "gain" the importer/exporter created by another
88  map.*/
91 
92 
95 
96  //@}
97 
98  //! @name Constructors & Destructor
99  //@{
100 
101  //! Empty Constructor
102  MapEpetra();
103 
104  //! Constructor
105  /*!
106  To define a linear map, set MyGlobalElements = 0
107  @param numGlobalElements Number of global elements
108  @param numMyElements Number of local elements
109  @param myGlobalElements Array of Id of the local element
110  @param commPtr Pointer to the communicator
111  */
112  MapEpetra ( Int numGlobalElements,
113  Int numMyElements,
114  Int* myGlobalElements,
115  const commPtr_Type& commPtr );
116 
117  //! Constructor
118  /*!
119  To define a linear map, set MyGlobalElements = 0
120  @param mapData Structure containing Ids for the local Unique and Repeated map
121  @param commPtr Pointer to the communicator
122  */
123  MapEpetra ( mapData_Type const& mapData, commPtr_Type const& commPtr );
124 
125  //! Constructor
126  /*
127  Build a nearly equally distributed map.
128 
129  The map is equally distributed if NumGlobalElements % Rank = 0
130  @param NumGlobalElements Total number of elements inside the map
131  @param CommPtr A pointer to the Epetra communicator
132  */
133  MapEpetra ( const Int numGlobalElements,
134  const Int notUsed,
135  const commPtr_Type& commPtr );
136 
137  //! Constructor
138  /*!
139  @param size Size of the map
140  @param commPtr Pointer to the communicator
141  */
142  MapEpetra ( const Int size,
143  const commPtr_Type& commPtr );
144 
145  //! Copy constructor
146  /*!
147  @param epetraMap An MapEpetra object
148  */
149  MapEpetra ( const MapEpetra& epetraMap );
150 
151  //! Constructor
152  /*!
153  Builds a submap of map _epetraMap with a given positive offset and
154  the maximum id to consider
155 
156  e.g:
157  <ol>
158  <li> offset = 2,
159  <li> maxid = 6
160  <li> epetraMap = [ 0 2 5 7 8 10 1 ]
161  <li> this = [ 0 3 5 6 ]
162  </ol>
163 
164  @param blockMap Epetra_BlockMap
165  @param offset Offset to be used to build the map
166  @param maxId Maximum Id
167  */
168  MapEpetra ( const Epetra_BlockMap& blockMap,
169  const Int offset,
170  const Int maxId);
171 
172 private:
173  //! Constructor from raw Epetra_Map
174  /*!
175  This constructor should be used only inside this class,
176  therefore it is private
177  @param map: underlying Epetra_Map
178  */
179  MapEpetra ( const map_Type map );
180 
181 public:
182  //! Destructor
183  ~MapEpetra() {}
184 
185  //@}
186 
187 
188  //! @name Operators
189  //@{
190 
191  //! Assignment operator
192  /*!
193  The assignment operator will copy the pointers of the maps, exporter and importer
194  @param epetraMap MapEpetra to be assigned to the current matrix
195  */
196  MapEpetra& operator = ( const MapEpetra& epetraMap );
197 
198  //! Addition operator
199  /*!
200  The addition operator combines two map together
201  @param epetraMap MapEpetra to be combined with the current map
202  */
203  MapEpetra& operator += ( const MapEpetra& epetraMap );
204 
205  //! Addition operator
206  /*!
207  The addition operator create a map of size "size" and add it to the current map
208  to create a new map
209  @param size Size of the map to be added to the current map
210  */
211  MapEpetra& operator+= (Int const size);
212  //@}
213 
214  //! @name Methods
215  //@{
216 
217  //! This method creates a pointer to a MapEpetra that has points only on processor root
218  /*!
219  @param root processor on which to export all the points
220  */
221  std::shared_ptr<MapEpetra> createRootMap ( Int const root ) const;
222 
223  //! This method return true if both the unique map and the repeated map are identical
224  bool mapsAreSimilar ( MapEpetra const& epetraMap ) const;
225 
226 #ifdef HAVE_HDF5
227  //! Save the matrix into a HDF5 (.h5) file
228  /*!
229  @param fileName Name of the file where the map will be saved, without extension (.h5)
230  @param mapName Name of the map in the HDF5 file
231  @param truncate True if the file has to be truncated; False if the file already exist and should not be truncated
232  */
233  void exportToHDF5 ( std::string const& fileName, std::string const& mapName = "map", bool const truncate = true );
234 
235  //! Read a matrix from a HDF5 (.h5) file
236  /*!
237  @param fileName Name of the file where the map will be saved, without extension (.h5)
238  @param matrixName Name of the map in the HDF5 file
239  */
240  void importFromHDF5 ( std::string const& fileName, std::string const& mapName = "map" );
241 #endif
242 
243  //! Show informations about the map
244  void showMe ( std::ostream& output = std::cout ) const;
245 
246  //! Getter for the global number of entries
247  UInt mapSize() const
248  {
249  return map (Unique)->NumGlobalElements();
250  }
251 
252  //! check if a global id is owned by the current partition
253  bool isOwned ( const UInt globalId ) const
254  {
255  return ( M_uniqueMapEpetra->LID ( static_cast<int> (globalId) ) > -1 );
256  }
257 
258  //@}
259 
260  //! @name Get Methods
261  //@{
262 
263  //! Return the communicator
264  comm_Type const& comm() const
265  {
266  return *M_commPtr;
267  }
268 
269  //! Return a shared pointer on the communicator
270  commPtr_Type const& commPtr() const
271  {
272  return M_commPtr;
273  }
275  {
276  return M_commPtr;
277  }
278 
279  //! Return a shared pointer on the internal Epetra_Map
280  mapPtr_Type const& map ( MapEpetraType mapType ) const;
281 
282  //! Getter for the Epetra_Export
283  Epetra_Export const& exporter();
284 
285  //! Getter for the Epetra_Import
286  Epetra_Import const& importer();
287  //@}
288 
289  //! @name Set Methods
290  //@{
291 
292  //! Set the communicator
293  void setComm ( commPtr_Type const& commPtr );
294 
295  //! set the internal Epetra_Maps
296  void setMap ( mapPtr_Type map, MapEpetraType mapType );
297 
298  //@}
299 
300 private:
301 
302  //! @name Private Methods
303  //@{
304 
305  //! Create a map
306  /*!
307  Note: createMap does not call createImportExport
308  @param numGlobalElements Number of global elements of the map
309  @param numMyElements number of local element
310  @param myGlobalElements Array of Id of the global elements of the map
311  @param comm Communicator
312  */
313  void createMap ( Int numGlobalElements,
314  Int numMyElements,
315  Int* myGlobalElements,
316  const comm_Type& comm );
317 
318  //! Getter for the repeated map
320  {
321  return M_repeatedMapEpetra;
322  }
323 
324  //! Getter for the unique map
325  mapPtr_Type const& getUniqueMap() const
326  {
327  return M_uniqueMapEpetra;
328  }
329 
330  //! Reset the internal unique map and recompute it using the repeated map
331  void uniqueMap();
332 
333  //! Reset and rebuild the importer and exporter for the map
334  void createImportExport();
335 
336  //! Sort the element given using a bubble sort algorithm
337  /*!
338  @param elements Epetra_IntSerialDenseVector vector to be sorted
339  */
340  void bubbleSort ( Epetra_IntSerialDenseVector& elements );
341 
342  //@}
343 
349 };
350 
351 // ===================================================
352 // External Operators
353 // ===================================================
354 
355 //! Addition operator
356 /*!
357  The addition operator combines two map together to create a new map
358  @param epetraMap MapEpetra to be combined with the current map
359  */
360 MapEpetra operator+ (const MapEpetra& map1, const MapEpetra& map2);
361 
362 //! Addition operator
363 /*!
364  The addition operator create a map of size "size" and add it to the current map
365  to create a new map
366  @param size Size of the map to be added to the current map
367  */
368 MapEpetra operator+ (const MapEpetra& map, Int size);
369 
370 //! Juxtaposition operator
371 /*!
372  This operator is used when block structures are used. Indeed, it creates
373  from two different maps a MapVector that can be used to initialize
374  block structures, such as matrices and vectors (see \ref BlockAlgebraPage "this page" for examples).
375  */
376 MapVector<MapEpetra> operator| (const MapEpetra& map1, const MapEpetra& map2);
377 
379 
380 } // end namespace LifeV
381 
382 #endif
Epetra_Comm comm_Type
Definition: MapEpetra.hpp:93
bool mapsAreSimilar(MapEpetra const &epetraMap) const
This method return true if both the unique map and the repeated map are identical.
Definition: MapEpetra.cpp:278
Epetra_Map map_Type
Definition: MapEpetra.hpp:79
~MapEpetra()
Destructor.
Definition: MapEpetra.hpp:183
MapEpetra(const MapEpetra &epetraMap)
Copy constructor.
Definition: MapEpetra.cpp:429
MapEpetra(const map_Type map)
Constructor from raw Epetra_Map.
Definition: MapEpetra.cpp:147
mapPtr_Type M_repeatedMapEpetra
Definition: MapEpetra.hpp:344
MapEpetra & operator+=(const MapEpetra &epetraMap)
Addition operator.
Definition: MapEpetra.cpp:198
MapEpetra(mapData_Type const &mapData, commPtr_Type const &commPtr)
Constructor.
Definition: MapEpetra.cpp:74
MapEpetra()
Empty Constructor.
Definition: MapEpetra.cpp:53
MapVector< MapEpetra > operator|(const MapEpetra &map1, const MapEpetra &map2)
Juxtaposition operator.
Definition: MapEpetra.cpp:515
mapPtr_Type const & getUniqueMap() const
Getter for the unique map.
Definition: MapEpetra.hpp:325
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
MapEpetra & operator+=(Int const size)
Addition operator.
Definition: MapEpetra.cpp:258
comm_Type const & comm() const
Return the communicator.
Definition: MapEpetra.hpp:264
commPtr_Type M_commPtr
Definition: MapEpetra.hpp:348
void bubbleSort(Epetra_IntSerialDenseVector &elements)
Sort the element given using a bubble sort algorithm.
Definition: MapEpetra.cpp:481
void updateInverseJacobian(const UInt &iQuadPt)
void createMap(Int numGlobalElements, Int numMyElements, Int *myGlobalElements, const comm_Type &comm)
Create a map.
Definition: MapEpetra.cpp:434
MapEpetra(Int numGlobalElements, Int numMyElements, Int *myGlobalElements, const commPtr_Type &commPtr)
Constructor.
Definition: MapEpetra.cpp:60
void setMap(mapPtr_Type map, MapEpetraType mapType)
set the internal Epetra_Maps
Definition: MapEpetra.cpp:414
UInt mapSize() const
Getter for the global number of entries.
Definition: MapEpetra.hpp:247
MapEpetra(const Int numGlobalElements, const Int notUsed, const commPtr_Type &commPtr)
Constructor.
Definition: MapEpetra.cpp:93
MapEpetra(const Epetra_BlockMap &blockMap, const Int offset, const Int maxId)
Constructor.
Definition: MapEpetra.cpp:154
MapEpetra(const Int size, const commPtr_Type &commPtr)
Constructor.
Definition: MapEpetra.cpp:113
mapPtr_Type M_uniqueMapEpetra
Definition: MapEpetra.hpp:345
void createImportExport()
Reset and rebuild the importer and exporter for the map.
Definition: MapEpetra.cpp:462
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
exporterPtr_Type M_exporter
Definition: MapEpetra.hpp:346
importerPtr_Type M_importer
Definition: MapEpetra.hpp:347
MapVector< MapEpetra > MapEpetraVector
Definition: MapEpetra.hpp:378
MapEpetra operator+(const MapEpetra &map1, const MapEpetra &map2)
Addition operator.
Definition: MapEpetra.cpp:499
std::shared_ptr< map_Type > mapPtr_Type
Definition: MapEpetra.hpp:80
MapEpetraData.
commPtr_Type & commPtr()
Definition: MapEpetra.hpp:274
std::shared_ptr< MapEpetra > createRootMap(Int const root) const
This method creates a pointer to a MapEpetra that has points only on processor root.
Definition: MapEpetra.cpp:272
void uniqueMap()
Reset the internal unique map and recompute it using the repeated map.
Definition: MapEpetra.cpp:454
This class is used to store maps that will be used for block defined problems.
Definition: MapVector.hpp:61
MapEpetraData mapData_Type
Definition: MapEpetra.hpp:82
void showMe(std::ostream &output=std::cout) const
Show informations about the map.
Definition: MapEpetra.cpp:361
MapEpetra & operator=(const MapEpetra &epetraMap)
Assignment operator.
Definition: MapEpetra.cpp:184
MapEpetra operator+(const MapEpetra &map, Int size)
Addition operator.
Definition: MapEpetra.cpp:507
void setComm(commPtr_Type const &commPtr)
Set the communicator.
Definition: MapEpetra.cpp:407
bool isOwned(const UInt globalId) const
check if a global id is owned by the current partition
Definition: MapEpetra.hpp:253
std::shared_ptr< comm_Type > commPtr_Type
Definition: MapEpetra.hpp:94
mapPtr_Type const & map(MapEpetraType mapType) const
Return a shared pointer on the internal Epetra_Map.
Definition: MapEpetra.cpp:373
commPtr_Type const & commPtr() const
Return a shared pointer on the communicator.
Definition: MapEpetra.hpp:270
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
mapPtr_Type const & getRepeatedMap() const
Getter for the repeated map.
Definition: MapEpetra.hpp:319