EpetraExt  Development
EpetraExt_MMHelpers.h
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
42 #ifndef EPETRAEXT_MMHELPERS_H
43 #define EPETRAEXT_MMHELPERS_H
44 
45 #include "EpetraExt_ConfigDefs.h"
46 #include "Epetra_DistObject.h"
47 #include "Epetra_Map.h"
48 #include "Teuchos_RCP.hpp"
49 
50 #include <vector>
51 #include <set>
52 #include <map>
53 
54 
55 class Epetra_CrsMatrix;
56 class Epetra_Import;
57 class Epetra_Distributor;
58 
59 namespace EpetraExt {
60 class LightweightCrsMatrix;
61 
62 // Turn on to enable the special MMM timers
63 //#define ENABLE_MMM_TIMINGS
64 
65 //#define HAVE_EPETRAEXT_DEBUG // for extra sanity checks
66 
67 // ==============================================================
68 //struct that holds views of the contents of a CrsMatrix. These
69 //contents may be a mixture of local and remote rows of the
70 //actual matrix.
72 public:
74 
75  virtual ~CrsMatrixStruct();
76 
77  void deleteContents();
78 
79  int numRows;
80 
81  // The following class members get used in the transpose modes of the MMM
82  // but not in the A*B mode.
84  int** indices;
85  double** values;
86  bool* remote;
87  int numRemote;
88  const Epetra_BlockMap* importColMap;
89 
90  // Maps and matrices (all modes)
91  const Epetra_Map* origRowMap;
92  const Epetra_Map* rowMap;
93  const Epetra_Map* colMap;
94  const Epetra_Map* domainMap;
96  const Epetra_CrsMatrix *origMatrix;
97 
98  // The following class members are only used for A*B mode
99  std::vector<int> targetMapToOrigRow;
100  std::vector<int> targetMapToImportRow;
101 };
102 
104 
105 // ==============================================================
106 class CrsWrapper {
107  public:
108  virtual ~CrsWrapper(){}
109 
110  virtual const Epetra_Map& RowMap() const = 0;
111 
112  virtual bool Filled() = 0;
113 
114  virtual int InsertGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices) = 0;
115 
116  virtual int SumIntoGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices) = 0;
117 };
118 
119 // ==============================================================
121  public:
122  CrsWrapper_Epetra_CrsMatrix(Epetra_CrsMatrix& epetracrsmatrix);
123  virtual ~CrsWrapper_Epetra_CrsMatrix();
124 
125  const Epetra_Map& RowMap() const;
126 
127  bool Filled();
128 
129  int InsertGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices);
130  int SumIntoGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices);
131 
132  private:
133  Epetra_CrsMatrix& ecrsmat_;
134 };
135 
136 // ==============================================================
138  public:
139  CrsWrapper_GraphBuilder(const Epetra_Map& emap);
140  virtual ~CrsWrapper_GraphBuilder();
141 
142  const Epetra_Map& RowMap() const {return rowmap_; }
143 
144  bool Filled();
145 
146  int InsertGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices);
147  int SumIntoGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices);
148 
149  std::map<int,std::set<int>*>& get_graph();
150 
151  int get_max_row_length() { return max_row_length_; }
152 
153  private:
154  std::map<int,std::set<int>*> graph_;
155  const Epetra_Map& rowmap_;
156  int max_row_length_;
157 };
158 
159 // ==============================================================
161  Epetra_CrsMatrix& C);
162 
163 void pack_outgoing_rows(const Epetra_CrsMatrix& mtx,
164  const std::vector<int>& proc_col_ranges,
165  std::vector<int>& send_rows,
166  std::vector<int>& rows_per_send_proc);
167 
168 std::pair<int,int> get_col_range(const Epetra_Map& emap);
169 
170 std::pair<int,int> get_col_range(const Epetra_CrsMatrix& mtx);
171 
172 // ==============================================================
173 class LightweightMapData : Epetra_Data {
174  friend class LightweightMap;
175  public:
179  std::vector<int> MyGlobalElements_;
180  Epetra_HashTable<int> * LIDHash_;
181 
182  // For "copy" constructor only...
183  Epetra_Map * CopyMap_;
184 
185  };
186 
188  public:
189  LightweightMap();
190  LightweightMap(int NumGlobalElements,int NumMyElements, const int * MyGlobalElements, int IndexBase, bool GenerateHash=true);
191  LightweightMap(const Epetra_Map & Map);
192  LightweightMap(const LightweightMap & Map);
193  ~LightweightMap();
194 
195  LightweightMap & operator=(const LightweightMap & map);
196  int LID(int GID) const;
197  int GID(int LID) const;
198  int NumMyElements() const;
199  int* MyGlobalElements() const;
200  int IndexBase() const {return Data_->IndexBase_;}
201 
202  int MinLID() const;
203  int MaxLID() const;
204 
205  private:
206  void CleanupData();
207  LightweightMapData *Data_;
208  //Epetra_BlockMapData* Data_;
209 };
210 
211 
212 // ==============================================================
214  public:
215  RemoteOnlyImport(const Epetra_Import & Importer, LightweightMap & RemoteOnlyTargetMap);
216  ~RemoteOnlyImport();
217 
218  int NumSameIDs() {return 0;}
219 
220  int NumPermuteIDs() {return 0;}
221 
222  int NumRemoteIDs() {return NumRemoteIDs_;}
223 
224  int NumExportIDs() {return NumExportIDs_;}
225 
226  int* ExportLIDs() {return ExportLIDs_;}
227 
228  int* ExportPIDs() {return ExportPIDs_;}
229 
230  int* RemoteLIDs() {return RemoteLIDs_;}
231 
232  int* PermuteToLIDs() {return 0;}
233 
234  int* PermuteFromLIDs() {return 0;}
235 
236  int NumSend() {return NumSend_;}
237 
238  Epetra_Distributor & Distributor() {return *Distor_;}
239 
240  const Epetra_BlockMap & SourceMap() const {return *SourceMap_;}
241  const LightweightMap & TargetMap() const {return *TargetMap_;}
242 
243  private:
244  int NumSend_;
245  int NumRemoteIDs_;
246  int NumExportIDs_;
247  int * ExportLIDs_;
248  int * ExportPIDs_;
249  int * RemoteLIDs_;
250  Epetra_Distributor* Distor_;
251  const Epetra_BlockMap* SourceMap_;
252  const LightweightMap *TargetMap_;
253 };
254 
255 // ==============================================================
257  public:
258  LightweightCrsMatrix(const Epetra_CrsMatrix & A, RemoteOnlyImport & RowImporter);
259  LightweightCrsMatrix(const Epetra_CrsMatrix & A, Epetra_Import & RowImporter);
261 
262  // Standard crs data structures
263  std::vector<int> rowptr_;
264  std::vector<int> colind_;
265  std::vector<double> vals_;
266 
267  // Colind in LL-GID space (if needed)
268  std::vector<long long> colind_LL_;
269 
270  // Epetra Maps
271  bool use_lw;
273  Epetra_BlockMap *RowMapEP_;
275  Epetra_Map DomainMap_;
276 
277 
278  // List of owning PIDs (from the DomainMap) as ordered by entries in the column map.
279  std::vector<int> ColMapOwningPIDs_;
280 
281  // For building the final importer for C
282  std::vector<int> ExportLIDs_;
283  std::vector<int> ExportPIDs_;
284 
285  private:
286 
287  template <typename ImportType>
288  void Construct(const Epetra_CrsMatrix & A, ImportType & RowImporter);
289 
290  // Templated versions of MakeColMapAndReindex (to prevent code duplication)
291  template <class GO>
292  int MakeColMapAndReindex(std::vector<int> owningPIDs,std::vector<GO> Gcolind);
293 
294  template <int>
295  int MakeColMapAndReindex(std::vector<int> owningPIDs,std::vector<int> Gcolind);
296 
297  template <long long>
298  int MakeColMapAndReindex(std::vector<int> owningPIDs,std::vector<long long> Gcolind);
299 
300 
301  template<typename ImportType>
302  int PackAndPrepareReverseComm(const Epetra_CrsMatrix & SourceMatrix, ImportType & RowImporter,
303  std::vector<int> &ReverseSendSizes, std::vector<int> &ReverseSendBuffer);
304 
305  template<typename ImportType>
306  int MakeExportLists(const Epetra_CrsMatrix & SourceMatrix, ImportType & RowImporter,
307  std::vector<int> &ReverseRecvSizes, const int *ReverseRecvBuffer,
308  std::vector<int> & ExportPIDs, std::vector<int> & ExportLIDs);
309 
310 };
311 
312 }//namespace EpetraExt
313 
314 #endif
315 
LightweightCrsMatrix * importMatrix
std::vector< int > targetMapToOrigRow
void pack_outgoing_rows(const Epetra_CrsMatrix &mtx, const std::vector< int > &proc_col_ranges, std::vector< int > &send_rows, std::vector< int > &rows_per_send_proc)
const Epetra_CrsMatrix * origMatrix
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
std::vector< int > targetMapToImportRow
std::pair< int, int > get_col_range(const Epetra_Map &emap)
Epetra_HashTable< int > * LIDHash_
const Epetra_BlockMap & SourceMap() const
const LightweightMap & TargetMap() const
const Epetra_Map & RowMap() const
Epetra_Distributor & Distributor()
const Epetra_BlockMap * importColMap
int dumpCrsMatrixStruct(const CrsMatrixStruct &M)
void insert_matrix_locations(CrsWrapper_GraphBuilder &graphbuilder, Epetra_CrsMatrix &C)
std::vector< long long > colind_LL_