EpetraExt  Development
EpetraExt_BlockUtility.cpp
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 #include "EpetraExt_BlockUtility.h"
43 #include "Epetra_Map.h"
44 #include "Epetra_Comm.h"
45 
46 namespace EpetraExt {
47 
48 using std::vector;
49 
50 //==============================================================================
52  const Epetra_BlockMap & BaseMap,
53  const int * RowIndices,
54  int NumBlockRows,
55  const Epetra_Comm & GlobalComm,
56  int Offset)
57 {
58  int BaseIndex = BaseMap.IndexBase();
59  if (Offset == 0)
60  Offset = BlockUtility::CalculateOffset(BaseMap);
61 
62  //Get Base Global IDs
63  int Size = BaseMap.NumMyElements();
64  int TotalSize = NumBlockRows * Size;
65  vector<int> GIDs(Size);
66  BaseMap.MyGlobalElements( &GIDs[0] );
67 
68  vector<int> GlobalGIDs( TotalSize );
69  for( int i = 0; i < NumBlockRows; ++i )
70  {
71  for( int j = 0; j < Size; ++j )
72  GlobalGIDs[i*Size+j] = GIDs[j] + RowIndices[i] * Offset;
73  }
74 
75  int GlobalSize;
76  GlobalComm.SumAll( &TotalSize, &GlobalSize, 1 );
77 
78  Epetra_Map *GlobalMap =
79  new Epetra_Map( GlobalSize, TotalSize, &GlobalGIDs[0], BaseIndex,
80  GlobalComm );
81 
82  return GlobalMap;
83 }
84 
85 //==============================================================================
87  const Epetra_BlockMap & BaseMap,
88  const std::vector<int>& RowIndices,
89  const Epetra_Comm & GlobalComm,
90  int Offset )
91 {
92  return GenerateBlockMap(BaseMap, &RowIndices[0], RowIndices.size(),
93  GlobalComm, Offset);
94 }
95 
96 //==============================================================================
98  const Epetra_BlockMap & BaseMap,
99  const Epetra_BlockMap& BlockMap,
100  const Epetra_Comm & GlobalComm,
101  int Offset)
102 {
103  return GenerateBlockMap(BaseMap,
104  BlockMap.MyGlobalElements(),
105  BlockMap.NumMyElements(),
106  GlobalComm,
107  Offset);
108 }
109 
110 //==============================================================================
111 Epetra_CrsGraph * BlockUtility::GenerateBlockGraph(
112  const Epetra_CrsGraph & BaseGraph,
113  const vector< vector<int> > & RowStencil,
114  const vector<int> & RowIndices,
115  const Epetra_Comm & GlobalComm )
116 {
117 
118  const Epetra_BlockMap & BaseMap = BaseGraph.RowMap();
119  int BaseIndex = BaseMap.IndexBase();
120  int Offset = BlockUtility::CalculateOffset(BaseMap);
121 
122  //Get Base Global IDs
123  int NumBlockRows = RowIndices.size();
124  int Size = BaseMap.NumMyElements();
125  int TotalSize = NumBlockRows * Size;
126  vector<int> GIDs(Size);
127  BaseMap.MyGlobalElements( &GIDs[0] );
128 
129  vector<int> GlobalGIDs( TotalSize );
130  for( int i = 0; i < NumBlockRows; ++i )
131  {
132  for( int j = 0; j < Size; ++j )
133  GlobalGIDs[i*Size+j] = GIDs[j] + RowIndices[i] * Offset;
134  }
135 
136  int GlobalSize;
137  GlobalComm.SumAll( &TotalSize, &GlobalSize, 1 );
138 
139  Epetra_Map GlobalMap( GlobalSize, TotalSize, &GlobalGIDs[0], BaseIndex, GlobalComm );
140 
141  int MaxIndices = BaseGraph.MaxNumIndices();
142  vector<int> Indices(MaxIndices);
143  int NumIndices;
144 
145  Epetra_CrsGraph * GlobalGraph = new Epetra_CrsGraph( Copy,
146  dynamic_cast<Epetra_BlockMap&>(GlobalMap),
147  0 );
148 
149  for( int i = 0; i < NumBlockRows; ++i )
150  {
151  int StencilSize = RowStencil[i].size();
152  for( int j = 0; j < Size; ++j )
153  {
154  int BaseRow = BaseMap.GID(j);
155  int GlobalRow = GlobalMap.GID(j+i*Size);
156 
157  BaseGraph.ExtractGlobalRowCopy( BaseRow, MaxIndices, NumIndices, &Indices[0] );
158  for( int k = 0; k < StencilSize; ++k )
159  {
160  int ColOffset = (RowIndices[i]+RowStencil[i][k]) * Offset;
161  if( k > 0 ) ColOffset -= (RowIndices[i]+RowStencil[i][k-1]) * Offset;
162 
163  for( int l = 0; l < NumIndices; ++l )
164  Indices[l] += ColOffset;
165 
166  GlobalGraph->InsertGlobalIndices( GlobalRow, NumIndices, &Indices[0] );
167  }
168  }
169  }
170 
171  GlobalGraph->FillComplete();
172 
173  return GlobalGraph;
174 }
175 
176 //==============================================================================
177 Epetra_CrsGraph * BlockUtility::GenerateBlockGraph(
178  const Epetra_RowMatrix & BaseMatrix,
179  const vector< vector<int> > & RowStencil,
180  const vector<int> & RowIndices,
181  const Epetra_Comm & GlobalComm )
182 {
183 
184  const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
185  const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
186  int BaseIndex = BaseMap.IndexBase();
187  int Offset = BlockUtility::CalculateOffset(BaseMap);
188 
189  //Get Base Global IDs
190  int NumBlockRows = RowIndices.size();
191  int Size = BaseMap.NumMyElements();
192  int TotalSize = NumBlockRows * Size;
193  vector<int> GIDs(Size);
194  BaseMap.MyGlobalElements( &GIDs[0] );
195 
196  vector<int> GlobalGIDs( TotalSize );
197  for( int i = 0; i < NumBlockRows; ++i )
198  {
199  for( int j = 0; j < Size; ++j )
200  GlobalGIDs[i*Size+j] = GIDs[j] + RowIndices[i] * Offset;
201  }
202 
203  int GlobalSize;
204  GlobalComm.SumAll( &TotalSize, &GlobalSize, 1 );
205 
206  Epetra_Map GlobalMap( GlobalSize, TotalSize, &GlobalGIDs[0], BaseIndex, GlobalComm );
207 
208  int MaxIndices = BaseMatrix.MaxNumEntries();
209  vector<int> Indices(MaxIndices);
210  vector<double> Values(MaxIndices);
211  int NumIndices;
212 
213  Epetra_CrsGraph * GlobalGraph = new Epetra_CrsGraph( Copy,
214  dynamic_cast<Epetra_BlockMap&>(GlobalMap),
215  0 );
216 
217  for( int i = 0; i < NumBlockRows; ++i )
218  {
219  int StencilSize = RowStencil[i].size();
220  for( int j = 0; j < Size; ++j )
221  {
222  int GlobalRow = GlobalMap.GID(j+i*Size);
223 
224  BaseMatrix.ExtractMyRowCopy( j, MaxIndices, NumIndices, &Values[0], &Indices[0] );
225  for( int l = 0; l < NumIndices; ++l ) Indices[l] = BaseColMap.GID(Indices[l]);
226 
227  for( int k = 0; k < StencilSize; ++k )
228  {
229  int ColOffset = (RowIndices[i]+RowStencil[i][k]) * Offset;
230  if( k > 0 ) ColOffset -= (RowIndices[i]+RowStencil[i][k-1]) * Offset;
231 
232  for( int l = 0; l < NumIndices; ++l )
233  Indices[l] += ColOffset;
234 
235  GlobalGraph->InsertGlobalIndices( GlobalRow, NumIndices, &Indices[0] );
236  }
237  }
238  }
239 
240  GlobalGraph->FillComplete();
241 
242  return GlobalGraph;
243 }
244 
245 //==============================================================================
247  const Epetra_CrsGraph & BaseGraph,
248  const Epetra_CrsGraph & LocalBlockGraph,
249  const Epetra_Comm & GlobalComm )
250 {
251  const Epetra_BlockMap & BaseRowMap = BaseGraph.RowMap();
252  const Epetra_BlockMap & BaseColMap = BaseGraph.ColMap();
253  int ROffset = BlockUtility::CalculateOffset(BaseRowMap);
254  (void) ROffset; // Silence "unused variable" compiler warning.
255  int COffset = BlockUtility::CalculateOffset(BaseColMap);
256 
257  //Get Base Global IDs
258  const Epetra_BlockMap & BlockRowMap = LocalBlockGraph.RowMap();
259  const Epetra_BlockMap & BlockColMap = LocalBlockGraph.ColMap();
260 
261  int NumBlockRows = BlockRowMap.NumMyElements();
262  vector<int> RowIndices(NumBlockRows);
263  BlockRowMap.MyGlobalElements(&RowIndices[0]);
264 
265  int Size = BaseRowMap.NumMyElements();
266 
267  Epetra_Map *GlobalRowMap =
268  GenerateBlockMap(BaseRowMap, BlockRowMap, GlobalComm);
269 
270 
271  int MaxIndices = BaseGraph.MaxNumIndices();
272  vector<int> Indices(MaxIndices);
273 
274  Epetra_CrsGraph * GlobalGraph = new Epetra_CrsGraph( Copy,
275  dynamic_cast<Epetra_BlockMap&>(*GlobalRowMap),
276  0 );
277 
278  int NumBlockIndices, NumBaseIndices;
279  int *BlockIndices, *BaseIndices;
280  for( int i = 0; i < NumBlockRows; ++i )
281  {
282  LocalBlockGraph.ExtractMyRowView(i, NumBlockIndices, BlockIndices);
283 
284  for( int j = 0; j < Size; ++j )
285  {
286  int GlobalRow = GlobalRowMap->GID(j+i*Size);
287 
288  BaseGraph.ExtractMyRowView( j, NumBaseIndices, BaseIndices );
289  for( int k = 0; k < NumBlockIndices; ++k )
290  {
291  int ColOffset = BlockColMap.GID(BlockIndices[k]) * COffset;
292 
293  for( int l = 0; l < NumBaseIndices; ++l )
294  Indices[l] = BaseGraph.GCID(BaseIndices[l]) + ColOffset;
295 
296  GlobalGraph->InsertGlobalIndices( GlobalRow, NumBaseIndices, &Indices[0] );
297  }
298  }
299  }
300 
301  const Epetra_BlockMap & BaseDomainMap = BaseGraph.DomainMap();
302  const Epetra_BlockMap & BaseRangeMap = BaseGraph.RangeMap();
303  const Epetra_BlockMap & BlockDomainMap = LocalBlockGraph.DomainMap();
304  const Epetra_BlockMap & BlockRangeMap = LocalBlockGraph.RangeMap();
305 
306  Epetra_Map *GlobalDomainMap =
307  GenerateBlockMap(BaseDomainMap, BlockDomainMap, GlobalComm);
308  Epetra_Map *GlobalRangeMap =
309  GenerateBlockMap(BaseRangeMap, BlockRangeMap, GlobalComm);
310 
311  GlobalGraph->FillComplete(*GlobalDomainMap, *GlobalRangeMap);
312 
313  delete GlobalDomainMap;
314  delete GlobalRangeMap;
315  delete GlobalRowMap;
316 
317  return GlobalGraph;
318 }
319 
320 //==============================================================================
321 void BlockUtility::GenerateRowStencil(const Epetra_CrsGraph& LocalBlockGraph,
322  std::vector<int> RowIndices,
323  std::vector< std::vector<int> >& RowStencil)
324 {
325  // Get row indices
326  int NumMyRows = LocalBlockGraph.NumMyRows();
327  RowIndices.resize(NumMyRows);
328  const Epetra_BlockMap& RowMap = LocalBlockGraph.RowMap();
329  RowMap.MyGlobalElements(&RowIndices[0]);
330 
331  // Get stencil
332  RowStencil.resize(NumMyRows);
333  if (LocalBlockGraph.IndicesAreGlobal()) {
334  for (int i=0; i<NumMyRows; i++) {
335  int Row = RowIndices[i];
336  int NumCols = LocalBlockGraph.NumGlobalIndices(Row);
337  RowStencil[i].resize(NumCols);
338  LocalBlockGraph.ExtractGlobalRowCopy(Row, NumCols, NumCols,
339  &RowStencil[i][0]);
340  for (int k=0; k<NumCols; k++)
341  RowStencil[i][k] -= Row;
342  }
343  }
344  else {
345  for (int i=0; i<NumMyRows; i++) {
346  int NumCols = LocalBlockGraph.NumMyIndices(i);
347  RowStencil[i].resize(NumCols);
348  LocalBlockGraph.ExtractMyRowCopy(i, NumCols, NumCols,
349  &RowStencil[i][0]);
350  for (int k=0; k<NumCols; k++)
351  RowStencil[i][k] = LocalBlockGraph.GCID(RowStencil[i][k]) -
352  RowIndices[i];
353  }
354  }
355 }
356 
357 //==============================================================================
358 int BlockUtility::CalculateOffset(const Epetra_BlockMap & BaseMap)
359 {
360  int MaxGID = BaseMap.MaxAllGID();
361 
362 // int Offset = 1;
363 // while( Offset <= MaxGID ) Offset *= 10;
364 
365 // return Offset;
366 
367  return MaxGID+1;
368 }
369 
370 
371 } //namespace EpetraExt
static void GenerateRowStencil(const Epetra_CrsGraph &LocalBlockGraph, std::vector< int > RowIndices, std::vector< std::vector< int > > &RowStencil)
Generate stencil arrays from a local block graph.
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
static int CalculateOffset(const Epetra_BlockMap &BaseMap)
Routine for calculating Offset for creating unique global IDs for Block representation.
static Epetra_Map * GenerateBlockMap(const Epetra_BlockMap &BaseMap, const int *RowIndices, int num_indices, const Epetra_Comm &GlobalComm, int Offset=0)
static Epetra_CrsGraph * GenerateBlockGraph(const Epetra_CrsGraph &BaseGraph, const std::vector< std::vector< int > > &RowStencil, const std::vector< int > &RowIndices, const Epetra_Comm &GlobalComm)
BlockCrsMatrix constuctor.