EpetraExt  Development
EpetraExt_Permutation_impl.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 #ifndef EpetraExt_PERMUTATION_IMPL_H
42 #define EpetraExt_PERMUTATION_IMPL_H
43 
44 #include <EpetraExt_ConfigDefs.h>
45 
46 #include <EpetraExt_Permutation.h>
47 
48 #include <Epetra_Export.h>
49 #include <Epetra_Map.h>
50 #include <Epetra_Comm.h>
51 #include <Epetra_MultiVector.h>
52 #include <Epetra_CrsGraph.h>
53 #include <Epetra_CrsMatrix.h>
54 
55 namespace EpetraExt {
56 
75 template<class T>
76 struct Perm_traits {
78  static const char* typeName()
79  { static const char name[] = "unknown"; return( name ); }
80 
88  static T* clone(T* example,
89  Epetra_DataAccess CV,
90  const Epetra_BlockMap& map,
91  int int_argument)
92  { return( NULL ); }
93 
97  static void replaceMap(T* obj, const Epetra_BlockMap& map)
98  { cerr << "not implemented for unknown type"<<endl; }
99 
101  static T*
103  T* srcObj)
104  { cerr << "not implemented for unknown type"<<endl; }
105 
106 };//struct Perm_traits
107 
108 
109 
113 template<>
114 struct Perm_traits<Epetra_CrsMatrix> {
115 
117  static const char* typeName()
118  { static const char name[] = "Epetra_CrsMatrix"; return( name ); }
119 
120 
122  static Epetra_CrsMatrix* clone(Epetra_CrsMatrix* example,
123  Epetra_DataAccess CV,
124  const Epetra_BlockMap& map,
125  int rowLength)
126  {
127  //don't need the example object currently...
128  (void)example;
129 
130  //we need a Epetra_Map, rather than a Epetra_BlockMap, to create a
131  //Epetra_CrsMatrix.
132 
133  const Epetra_Map* pointmap =
134  dynamic_cast<const Epetra_Map*>(&map);
135  if (pointmap == NULL) {
136  cerr << "dynamic_cast<const Epetra_Map*> failed."<<endl;
137  return(NULL);
138  }
139 
140  return( new Epetra_CrsMatrix(CV, *pointmap, rowLength) );
141  }
142 
143 
145  static void replaceMap(Epetra_CrsMatrix* mat, const Epetra_BlockMap& map)
146  { mat->ReplaceRowMap(map); }
147 
149  static Epetra_CrsMatrix*
151  Epetra_CrsMatrix* srcObj)
152  {
153  //First we need to export this permutation to match the column-map of the
154  //object being column-permuted. (We need to have locally available all
155  //elements of the permutation corresponding to the local columns of the
156  //object being permuted.)
157 
158  const Epetra_Map& origColMap = srcObj->ColMap();
159 
161  new Permutation<Epetra_CrsMatrix>(origColMap);
162  colperm->PutValue(0);
163 
164  Epetra_Export p_exporter(perm->Map(), origColMap);
165  colperm->Export(*perm, p_exporter, Add);
166 
167  const Epetra_Map& origRowMap = srcObj->RowMap();
168  int numMyRows = origRowMap.NumMyElements();
169  const int* myGlobalRows = origRowMap.MyGlobalElements();
170 
171  //Create the new object, giving it the same map as the original object.
172 
173  Epetra_CrsMatrix* result = new Epetra_CrsMatrix(Copy, origRowMap, 1);
174 
175  for(int i=0; i<numMyRows; ++i) {
176  int globalRow = myGlobalRows[i];
177  int len = srcObj->NumGlobalEntries(globalRow);
178 
179  int numIndices;
180  double* src_values = new double[len];
181  int* src_indices = new int[len];
182  int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices,
183  src_values, src_indices);
184  if (err < 0 || numIndices != len) {
185  cerr<<"Perm_traits<CrsMatrix>::produceColumnPermutation err("<<err<<") row "
186  <<globalRow<<", len "<<len<<", numIndices "<<numIndices<<endl;
187  }
188 
189  int* pindices = new int[len];
190 
191  const Epetra_BlockMap& pmap = colperm->Map();
192  int* p = colperm->Values();
193 
194  for(int j=0; j<len; ++j) {
195  int old_col = src_indices[j];
196 
197  int lid = pmap.LID(old_col);
198  if (lid<0) {
199  cerr << "Perm_traits<CrsMatrix>::permuteColumnIndices GID("<<old_col
200  <<") not found"<<endl;
201  break;
202  }
203 
204  pindices[j] = p[lid];
205  }
206 
207  err = result->InsertGlobalValues(globalRow, len, src_values, pindices);
208  if (err < 0) {
209  cerr << "Perm_traits<CrsMatrix>::permuteColumnIndices err("<<err
210  <<") row "<<globalRow<<endl;
211  }
212 
213  delete [] pindices;
214  delete [] src_indices;
215  delete [] src_values;
216  }
217 
218  result->FillComplete();
219 
220  delete colperm;
221 
222  return(result);
223  }
224 
225 };//struct Perm_traits<Epetra_CrsMatrix>
226 
227 
228 
232 template<>
233 struct Perm_traits<Epetra_CrsGraph> {
234 
236  static const char* typeName()
237  { static const char name[] = "Epetra_CrsGraph"; return( name ); }
238 
239 
241  static Epetra_CrsGraph* clone(Epetra_CrsGraph* example,
242  Epetra_DataAccess CV,
243  const Epetra_BlockMap& map,
244  int rowLength)
245  {
246  //don't need the example object currently...
247  (void)example;
248 
249  return( new Epetra_CrsGraph(CV, map, rowLength) );
250  }
251 
252 
254  static void replaceMap(Epetra_CrsGraph* graph, const Epetra_BlockMap& map)
255  { graph->ReplaceRowMap(map); }
256 
258  static Epetra_CrsGraph*
260  Epetra_CrsGraph* srcObj)
261  {
262  //First we need to export this permutation to match the column-map of the
263  //object being column-permuted. (We need to have locally available all
264  //elements of the permutation corresponding to the local columns of the
265  //object being permuted.)
266 
267  const Epetra_BlockMap& origColMap = srcObj->ColMap();
268 
270  new Permutation<Epetra_CrsGraph>(origColMap);
271  colperm->PutValue(0);
272 
273  Epetra_Export p_exporter(perm->Map(), origColMap);
274  colperm->Export(*perm, p_exporter, Add);
275 
276  const Epetra_BlockMap& origRowMap = srcObj->RowMap();
277  int numMyRows = origRowMap.NumMyElements();
278  const int* myGlobalRows = origRowMap.MyGlobalElements();
279 
280  //Create the new object, giving it the same map as the original object.
281 
282  Epetra_CrsGraph* result = new Epetra_CrsGraph(Copy, origRowMap, 1);
283 
284  for(int i=0; i<numMyRows; ++i) {
285  int globalRow = myGlobalRows[i];
286  int len = srcObj->NumGlobalIndices(globalRow);
287 
288  int numIndices;
289  int* src_indices = new int[len];
290  int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices, src_indices);
291  if (err < 0 || numIndices != len) {
292  cerr<<"Perm_traits<CrsGraph>::produceColumnPermutation err("<<err<<") row "
293  <<globalRow<<", len "<<len<<", numIndices "<<numIndices<<endl;
294  }
295 
296  int* pindices = new int[len];
297 
298  const Epetra_BlockMap& pmap = colperm->Map();
299  int* p = colperm->Values();
300 
301  for(int j=0; j<len; ++j) {
302  int old_col = src_indices[j];
303 
304  int lid = pmap.LID(old_col);
305  if (lid<0) {
306  cerr << "Perm_traits<CrsGraph>::permuteColumnIndices GID("<<old_col
307  <<") not found"<<endl;
308  break;
309  }
310 
311  pindices[j] = p[lid];
312  }
313 
314  err = result->InsertGlobalIndices(globalRow, len, pindices);
315  if (err < 0) {
316  cerr << "Perm_traits<CrsGraph>::produceColumnPermutation err("<<err
317  <<") row "<<globalRow<<endl;
318  }
319 
320  delete [] pindices;
321  delete [] src_indices;
322  }
323 
324  result->FillComplete();
325 
326  delete colperm;
327 
328  return(result);
329  }
330 
331 };//struct Perm_traits<Epetra_CrsGraph>
332 
333 
337 template<>
338 struct Perm_traits<Epetra_MultiVector> {
339 
341  static const char* typeName()
342  { static const char name[] = "Epetra_MultiVector"; return( name ); }
343 
344 
346  static Epetra_MultiVector* clone(Epetra_MultiVector* example,
347  Epetra_DataAccess CV,
348  const Epetra_BlockMap& map,
349  int numVectors)
350  {
351  return( new Epetra_MultiVector(map, example->NumVectors()) );
352  }
353 
354 
356  static void replaceMap(Epetra_MultiVector* mvec, const Epetra_BlockMap& map)
357  { mvec->ReplaceMap(map); }
358 
360  static Epetra_MultiVector*
362  Epetra_MultiVector* srcObj)
363  {
364  cerr << "col-permutation not implemented for Epetra_MultiVector"<<endl;
365  return(NULL);
366  }
367 
368 };//struct Perm_traits<Epetra_CrsGraph>
369 
370 
371 //-------------------------------------------------------------------------
372 //Now the method definitions for the EpetraExt::Permutation class.
373 //-------------------------------------------------------------------------
374 
375 template<typename T>
376 Permutation<T>::Permutation(Epetra_DataAccess CV,
377  const Epetra_BlockMap& map,
378  int* permutation)
379  : Epetra_IntVector(CV, map, permutation),
380  newObj_(NULL),
381  origObj_(NULL)
382 {
383  if (!isTypeSupported()) {
384  cerr << "unsupported type for permutation, aborting" << endl;
385  abort();
386  }
387 }
388 
389 template<typename T>
390 Permutation<T>::Permutation(const Epetra_BlockMap& map)
391  : Epetra_IntVector(map),
392  newObj_(NULL),
393  origObj_(NULL)
394 {
395  if (!isTypeSupported()) {
396  cerr << "unsupported type for permutation, aborting" << endl;
397  abort();
398  }
399 }
400 
401 template<typename T>
403  : Epetra_IntVector((const Epetra_IntVector&)src),
404  newObj_(NULL),
405  origObj_(NULL)
406 {
407  if (!isTypeSupported()) {
408  cerr << "unsupported type for permutation, aborting" << endl;
409  abort();
410  }
411 }
412 
413 template<typename T>
415 {
416  if (newObj_ != NULL) delete newObj_;
417 }
418 
419 template<typename T>
421 {
422  const char* type_name = Perm_traits<T>::typeName();
423  if (!strcmp(type_name, "unknown")) {
424  return(false);
425  }
426 
427  return( true );
428 }
429 
430 template<typename T>
433 {
434  //In this function we're going to produce a new object which is a
435  //row-permutation of the input object (orig).
436  //
437  //Our permutation inherits IntVector, and the permutation is defined by the
438  //contents of the integer vector 'p', such that if p[i] = j then row i of
439  //the input object becomes row j of the permuted object.
440  //
441  //The permutation is accomplished by creating a map defined by the
442  //permutation, then using an Epetra_Export operation to move data from the
443  //input object into the permuted object.
444  //
445  //The permutation may be global. In other words, the rows of the object may
446  //be arbitrarily rearranged, including across processors.
447  //
448 
449  origObj_ = &orig;
450 
451  //The 'Map()' accessor returns Epetra_DistObject::Map() for CrsGraph and
452  //CrsMatrix, which turns out to be the RowMap() for those objects. For
453  //MultiVector it returns the correct object because MultiVectors only have
454  //one map.
455 
456  const Epetra_BlockMap& origMap = orig.Map();
457 
458  //Create an Epetra_Map representing the permutation.
459 
460  Epetra_Map* pmap = new Epetra_Map(Map().NumGlobalPoints(),
461  Map().NumMyPoints(),
462  Values(),
463  Map().IndexBase(),
464  Map().Comm());
465 
466  Permutation* p = this;
467 
468  //Next check that the maps are compatible. If they aren't, we'll redistribute
469  //the permutation to match the distribution of the input object.
470 
471  if (!pmap->PointSameAs(origMap)) {
472  Epetra_Export p_exporter(Map(), origMap);
473  Permutation* newp = new Permutation(origMap);
474  newp->Export(*p, p_exporter, Add);
475  p = newp;
476 
477  delete pmap;
478  pmap = new Epetra_Map(p->Map().NumGlobalPoints(),
479  p->Map().NumMyPoints(),
480  p->Values(),
481  p->Map().IndexBase(),
482  p->Map().Comm());
483  }
484 
485  //Create the new object, initially giving it the map defined by the
486  //permutation.
487 
488  newObj_ = Perm_traits<T>::clone(origObj_, Copy, *pmap, 1);
489 
490  //Create an exporter which will export data from the original object to the
491  //permuted object.
492 
493  Epetra_Export exporter(origMap, *pmap);
494 
495  //Now export the original object to the permuted object.
496 
497  newObj_->Export(orig, exporter, Add);
498 
499  //Now, since the export operation moved not only row-contents but also
500  //row-numbering, we need to replace the permuted row-numbering with the
501  //original row-numbering. We do this by replacing the permuted map with
502  //the original row-map.
503 
504  Perm_traits<T>::replaceMap(newObj_, origMap);
505 
506  delete pmap;
507 
508  if (p != this) {
509  delete p; //delete "newp" created if the PointSameAs test failed above
510  }
511 
512  return( *newObj_ );
513 }
514 
515 template<typename T>
518  bool column_permutation )
519 {
520  origObj_ = &orig;
521  newObj_ = NULL;
522 
523  if (!column_permutation) {
524  return( operator()(orig) );
525  }
526 
527  if (strcmp("Epetra_CrsMatrix", Perm_traits<T>::typeName()) &&
528  strcmp("Epetra_CrsGraph", Perm_traits<T>::typeName())) {
529  cerr << "Permutation: column-permutation only implemented for"
530  << "CrsMatrix and CrsGraph." << endl;
531  assert(0);
532  }
533 
534  newObj_ = Perm_traits<T>::produceColumnPermutation(this, &orig);
535 
536  return( *newObj_ );
537 }
538 
539 } // namespace EpetraExt
540 
541 #endif //EpetraExt_PERMUTATION_IMPL_H
virtual ~Permutation()
Destructor.
static Epetra_CrsGraph * clone(Epetra_CrsGraph *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int rowLength)
clone implementation
static void replaceMap(Epetra_MultiVector *mvec, const Epetra_BlockMap &map)
replaceMap implementation
static T * clone(T *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int int_argument)
clone function accepts an example of the object being cloned, and enough constructor arguments to be ...
static const char * typeName()
typeName implementation
static T * produceColumnPermutation(Permutation< T > *perm, T *srcObj)
return new object, which is a column-permutation of srcObj
static const char * typeName()
typeName implementation
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
static Epetra_MultiVector * clone(Epetra_MultiVector *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int numVectors)
clone implementation
static void replaceMap(Epetra_CrsMatrix *mat, const Epetra_BlockMap &map)
replaceMap implementation
static Epetra_CrsMatrix * clone(Epetra_CrsMatrix *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int rowLength)
clone implementation
Permutation stores and describes a permutation matrix P.
static Epetra_MultiVector * produceColumnPermutation(Permutation< Epetra_MultiVector > *perm, Epetra_MultiVector *srcObj)
permute column-indices within a specified row, if applicable
static const char * typeName()
typeName implementation
Permutation(Epetra_DataAccess CV, const Epetra_BlockMap &map, int *permutation)
Constructor.
EpetraExt::SameTypeTransform< T >::TransformTypeRef OutputRef
OutputRef operator()(InputRef orig)
This method creates a new object which is a permuted copy of the input argument.
static void replaceMap(T *obj, const Epetra_BlockMap &map)
replace the object&#39;s row-map (or if it&#39;s not a matrix, replace its only map)
EpetraExt::SameTypeTransform< T >::TransformTypeRef InputRef
static const char * typeName()
return a string name for the object type
Define some traits to make it easier to deal with template-parameters which are objects to be permute...
static Epetra_CrsMatrix * produceColumnPermutation(Permutation< Epetra_CrsMatrix > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj
static void replaceMap(Epetra_CrsGraph *graph, const Epetra_BlockMap &map)
replaceMap implementation
static Epetra_CrsGraph * produceColumnPermutation(Permutation< Epetra_CrsGraph > *perm, Epetra_CrsGraph *srcObj)
return new object which is a column-permutation of srcObj