EpetraExt  Development
EpetraExt_BlockCrsMatrix.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 
43 #include "EpetraExt_BlockUtility.h"
44 #include "Epetra_Map.h"
45 
46 namespace EpetraExt {
47 
48 using std::vector;
49 
50 //==============================================================================
52  const Epetra_CrsGraph & BaseGraph,
53  const vector<int> & RowStencil,
54  int RowIndex,
55  const Epetra_Comm & GlobalComm )
56  : Epetra_CrsMatrix( Copy, *(BlockUtility::GenerateBlockGraph( BaseGraph, vector< vector<int> >(1,RowStencil), vector<int>(1,RowIndex), GlobalComm )) ),
57  BaseGraph_( BaseGraph ),
58  RowStencil_( vector< vector<int> >(1,RowStencil) ),
59  RowIndices_( vector<int>(1,RowIndex) ),
60  ROffset_(BlockUtility::CalculateOffset(BaseGraph.RowMap())),
61  COffset_(BlockUtility::CalculateOffset(BaseGraph.ColMap()))
62 {
63 }
64 
65 //==============================================================================
67  const Epetra_CrsGraph & BaseGraph,
68  const vector< vector<int> > & RowStencil,
69  const vector<int> & RowIndices,
70  const Epetra_Comm & GlobalComm )
71  : Epetra_CrsMatrix( Copy, *(BlockUtility::GenerateBlockGraph( BaseGraph, RowStencil, RowIndices, GlobalComm )) ),
72  BaseGraph_( BaseGraph ),
73  RowStencil_( RowStencil ),
74  RowIndices_( RowIndices ),
75  ROffset_(BlockUtility::CalculateOffset(BaseGraph.RowMap())),
76  COffset_(BlockUtility::CalculateOffset(BaseGraph.ColMap()))
77 {
78 }
79 
80 //==============================================================================
82  const Epetra_CrsGraph & BaseGraph,
83  const Epetra_CrsGraph & LocalBlockGraph,
84  const Epetra_Comm & GlobalComm )
85  : Epetra_CrsMatrix( Copy, *(BlockUtility::GenerateBlockGraph( BaseGraph, LocalBlockGraph, GlobalComm )) ),
86  BaseGraph_( BaseGraph ),
87  RowStencil_( ),
88  RowIndices_( ),
89  ROffset_(BlockUtility::CalculateOffset(BaseGraph.RowMap())),
90  COffset_(BlockUtility::CalculateOffset(BaseGraph.ColMap()))
91 {
93 }
94 
95 //==============================================================================
97  const Epetra_RowMatrix & BaseMatrix,
98  const vector< vector<int> > & RowStencil,
99  const vector<int> & RowIndices,
100  const Epetra_Comm & GlobalComm )
101  : Epetra_CrsMatrix( Copy, *(BlockUtility::GenerateBlockGraph( BaseMatrix, RowStencil, RowIndices, GlobalComm )) ),
102  BaseGraph_( Copy, BaseMatrix.RowMatrixRowMap(), 1 ), //Junk to satisfy constructor
103  RowStencil_( RowStencil ),
104  RowIndices_( RowIndices ),
105  ROffset_(BlockUtility::CalculateOffset(BaseMatrix.RowMatrixRowMap())),
106  COffset_(BlockUtility::CalculateOffset(BaseMatrix.RowMatrixColMap()))
107 {
108 }
109 
110 //==============================================================================
112  : Epetra_CrsMatrix( dynamic_cast<const Epetra_CrsMatrix &>( Matrix ) ),
113  BaseGraph_( Matrix.BaseGraph_ ),
114  RowStencil_( Matrix.RowStencil_ ),
115  RowIndices_( Matrix.RowIndices_ ),
116  ROffset_( Matrix.ROffset_ ),
117  COffset_( Matrix.COffset_ )
118 {
119 }
120 
121 //==============================================================================
123 {
124 }
125 
126 //==============================================================================
127 void BlockCrsMatrix::LoadBlock(const Epetra_RowMatrix & BaseMatrix, const int Row, const int Col)
128 {
129  int RowOffset = RowIndices_[Row] * ROffset_;
130  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
131 
132 // const Epetra_CrsGraph & BaseGraph = BaseMatrix.Graph();
133  const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
134  const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
135 
136  // This routine copies entries of a BaseMatrix into big BlockCrsMatrix
137  // It performs the following operation on the global IDs row-by-row
138  // this->val[i+rowOffset][j+ColOffset] = BaseMatrix.val[i][j]
139 
140  int MaxIndices = BaseMatrix.MaxNumEntries();
141  vector<int> Indices(MaxIndices);
142  vector<double> Values(MaxIndices);
143  int NumIndices;
144  int ierr=0;
145 
146  for (int i=0; i<BaseMap.NumMyElements(); i++) {
147  BaseMatrix.ExtractMyRowCopy( i, MaxIndices, NumIndices, &Values[0], &Indices[0] );
148 
149  // Convert to BlockMatrix Global numbering scheme
150  for( int l = 0; l < NumIndices; ++l )
151  Indices[l] = ColOffset + BaseColMap.GID(Indices[l]);
152 
153  int BaseRow = BaseMap.GID(i);
154  ierr = this->ReplaceGlobalValues(BaseRow + RowOffset, NumIndices, &Values[0], &Indices[0]);
155  if (ierr != 0) cout << "WARNING BlockCrsMatrix::LoadBlock ReplaceGlobalValues err = " << ierr <<
156  "\n\t Row " << BaseRow + RowOffset << "Col start" << Indices[0] << endl;
157 
158  }
159 }
160 
161 //==============================================================================
162  void BlockCrsMatrix::SumIntoBlock(double alpha, const Epetra_RowMatrix & BaseMatrix, const int Row, const int Col)
163 {
164  int RowOffset = RowIndices_[Row] * ROffset_;
165  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
166 
167 // const Epetra_CrsGraph & BaseGraph = BaseMatrix.Graph();
168  const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
169  const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
170 
171  // This routine copies entries of a BaseMatrix into big BlockCrsMatrix
172  // It performs the following operation on the global IDs row-by-row
173  // this->val[i+rowOffset][j+ColOffset] = BaseMatrix.val[i][j]
174 
175  int MaxIndices = BaseMatrix.MaxNumEntries();
176  vector<int> Indices(MaxIndices);
177  vector<double> Values(MaxIndices);
178  int NumIndices;
179  int ierr=0;
180 
181  for (int i=0; i<BaseMap.NumMyElements(); i++) {
182  BaseMatrix.ExtractMyRowCopy( i, MaxIndices, NumIndices, &Values[0], &Indices[0] );
183 
184  // Convert to BlockMatrix Global numbering scheme
185  for( int l = 0; l < NumIndices; ++l ) {
186  Indices[l] = ColOffset + BaseColMap.GID(Indices[l]);
187  Values[l] *= alpha;
188  }
189 
190  int BaseRow = BaseMap.GID(i);
191  ierr = this->SumIntoGlobalValues(BaseRow + RowOffset, NumIndices, &Values[0], &Indices[0]);
192  if (ierr != 0) cout << "WARNING BlockCrsMatrix::SumIntoBlock SumIntoGlobalValues err = " << ierr <<
193  "\n\t Row " << BaseRow + RowOffset << "Col start" << Indices[0] << endl;
194 
195  }
196 }
197 
198 //==============================================================================
199  void BlockCrsMatrix::SumIntoGlobalBlock(double alpha, const Epetra_RowMatrix & BaseMatrix, const int Row, const int Col)
200 {
201  int RowOffset = Row * ROffset_;
202  int ColOffset = Col * COffset_;
203 
204 // const Epetra_CrsGraph & BaseGraph = BaseMatrix.Graph();
205  const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
206  const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
207 
208  // This routine copies entries of a BaseMatrix into big BlockCrsMatrix
209  // It performs the following operation on the global IDs row-by-row
210  // this->val[i+rowOffset][j+ColOffset] = BaseMatrix.val[i][j]
211 
212  int MaxIndices = BaseMatrix.MaxNumEntries();
213  vector<int> Indices(MaxIndices);
214  vector<double> Values(MaxIndices);
215  int NumIndices;
216  int ierr=0;
217 
218  for (int i=0; i<BaseMap.NumMyElements(); i++) {
219  BaseMatrix.ExtractMyRowCopy( i, MaxIndices, NumIndices, &Values[0], &Indices[0] );
220 
221  // Convert to BlockMatrix Global numbering scheme
222  for( int l = 0; l < NumIndices; ++l ) {
223  Indices[l] = ColOffset + BaseColMap.GID(Indices[l]);
224  Values[l] *= alpha;
225  }
226 
227  int BaseRow = BaseMap.GID(i);
228  ierr = this->SumIntoGlobalValues(BaseRow + RowOffset, NumIndices, &Values[0], &Indices[0]);
229  if (ierr != 0) cout << "WARNING BlockCrsMatrix::SumIntoBlock SumIntoGlobalValues err = " << ierr <<
230  "\n\t Row " << BaseRow + RowOffset << "Col start" << Indices[0] << endl;
231 
232  }
233 }
234 
235 //==============================================================================
236 void BlockCrsMatrix::BlockSumIntoGlobalValues(const int BaseRow, int NumIndices,
237  double* Values, const int* Indices, const int Row, const int Col)
238 //All arguments could be const, except some were not set as const in CrsMatrix
239 {
240  int RowOffset = RowIndices_[Row] * ROffset_;
241  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
242 
243  // Convert to BlockMatrix Global numbering scheme
244  vector<int> OffsetIndices(NumIndices);
245  for( int l = 0; l < NumIndices; ++l ) OffsetIndices[l] = Indices[l] + ColOffset;
246 
247  int ierr = this->SumIntoGlobalValues(BaseRow + RowOffset, NumIndices,
248  Values, &OffsetIndices[0]);
249 
250  if (ierr != 0) cout << "WARNING BlockCrsMatrix::BlockSumIntoGlobalValues err = "
251  << ierr << "\n\t Row " << BaseRow + RowOffset << "Col start" << Indices[0] << endl;
252 }
253 
254 //==============================================================================
255 void BlockCrsMatrix::BlockReplaceGlobalValues(const int BaseRow, int NumIndices,
256  double* Values, const int* Indices, const int Row, const int Col)
257 //All arguments could be const, except some were not set as const in CrsMatrix
258 {
259  int RowOffset = RowIndices_[Row] * ROffset_;
260  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
261 
262  // Convert to BlockMatrix Global numbering scheme
263  vector<int> OffsetIndices(NumIndices);
264  for( int l = 0; l < NumIndices; ++l ) OffsetIndices[l] = Indices[l] + ColOffset;
265 
266  int ierr = this->ReplaceGlobalValues(BaseRow + RowOffset, NumIndices,
267  Values, &OffsetIndices[0]);
268 
269  if (ierr != 0) cout << "WARNING BlockCrsMatrix::BlockReplaceGlobalValues err = "
270  << ierr << "\n\t Row " << BaseRow + RowOffset << "Col start" << Indices[0] << endl;
271 }
272 
273 //==============================================================================
275  int& NumEntries,
276  double*& Values,
277  const int Row,
278  const int Col)
279 //All arguments could be const, except some were not set as const in CrsMatrix
280 {
281  int RowOffset = RowIndices_[Row] * ROffset_;
282  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
283 
284  // Get the whole row
285  int ierr = this->ExtractGlobalRowView(BaseRow + RowOffset, NumEntries,
286  Values);
287 
288  // Adjust for just this block column
289  Values += ColOffset;
290  NumEntries -= ColOffset;
291 
292  if (ierr != 0) cout << "WARNING BlockCrsMatrix::BlockExtractGlobalRowView err = "
293  << ierr << "\n\t Row " << BaseRow + RowOffset << "Col " << Col+ColOffset << endl;
294 }
295 
296 //==============================================================================
297 void BlockCrsMatrix::ExtractBlock(Epetra_CrsMatrix & BaseMatrix, const int Row, const int Col)
298 {
299  int RowOffset = RowIndices_[Row] * ROffset_;
300  int ColOffset = (RowIndices_[Row] + RowStencil_[Row][Col]) * COffset_;
301 
302 // const Epetra_CrsGraph & BaseGraph = BaseMatrix.Graph();
303  const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
304  //const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
305 
306  // This routine extracts entries of a BaseMatrix from a big BlockCrsMatrix
307  // It performs the following operation on the global IDs row-by-row
308  // BaseMatrix.val[i][j] = this->val[i+rowOffset][j+ColOffset]
309 
310  int MaxIndices = BaseMatrix.MaxNumEntries();
311  vector<int> Indices(MaxIndices);
312  vector<double> Values(MaxIndices);
313  int NumIndices;
314  int indx,icol;
315  double* BlkValues;
316  int *BlkIndices;
317  int BlkNumIndices;
318  int ierr=0;
319  (void) ierr; // Forestall compiler warning for unused variable.
320 
321  for (int i=0; i<BaseMap.NumMyElements(); i++) {
322 
323  // Get pointers to values and indices of whole block matrix row
324  int BaseRow = BaseMap.GID(i);
325  int myBlkBaseRow = this->RowMatrixRowMap().LID(BaseRow + RowOffset);
326  ierr = this->ExtractMyRowView(myBlkBaseRow, BlkNumIndices, BlkValues, BlkIndices);
327 
328  NumIndices = 0;
329  // Grab columns with global indices in correct range for this block
330  for( int l = 0; l < BlkNumIndices; ++l ) {
331  icol = this->RowMatrixColMap().GID(BlkIndices[l]);
332  indx = icol - ColOffset;
333  if (indx >= 0 && indx < COffset_) {
334  Indices[NumIndices] = indx;
335  Values[NumIndices] = BlkValues[l];
336  NumIndices++;
337  }
338  }
339 
340  //Load this row into base matrix
341  BaseMatrix.ReplaceGlobalValues(BaseRow, NumIndices, &Values[0], &Indices[0] );
342 
343  }
344 }
345 
346 } //namespace EpetraExt
void LoadBlock(const Epetra_RowMatrix &BaseMatrix, const int Row, const int Col)
Routine for loading a base matrices values into the large Block Matrix The Row and Col arguments are ...
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.
void BlockReplaceGlobalValues(const int BaseRow, int NumIndices, double *Values, const int *Indices, const int Row, const int Col)
void ExtractBlock(Epetra_CrsMatrix &BaseMatrix, const int Row, const int Col)
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
void BlockExtractGlobalRowView(const int BaseRow, int &NumEntries, double *&Values, const int Row, const int Col)
static int CalculateOffset(const Epetra_BlockMap &BaseMap)
Routine for calculating Offset for creating unique global IDs for Block representation.
void SumIntoGlobalBlock(double alpha, const Epetra_RowMatrix &BaseMatrix, const int Row, const int Col)
Routine for summing base matrices values into the large Block Matrix The Row and Col arguments are gl...
BlockCrsMatrix(const Epetra_CrsGraph &BaseGraph, const std::vector< int > &RowStencil, int RowIndex, const Epetra_Comm &GlobalComm)
BlockCrsMatrix constuctor with one block row per processor.
void SumIntoBlock(double alpha, const Epetra_RowMatrix &BaseMatrix, const int Row, const int Col)
Routine for summing base matrices values into the large Block Matrix The Row and Col arguments are in...
void BlockSumIntoGlobalValues(const int BaseRow, int NumIndices, double *Values, const int *Indices, const int Row, const int Col)
Sum Entries into Block matrix using base-matrix numbering plus block Row and Col The Row and Col argu...
std::vector< std::vector< int > > RowStencil_
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.