41#ifndef EpetraExt_PERMUTATION_IMPL_H
42#define EpetraExt_PERMUTATION_IMPL_H
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#include <Epetra_GIDTypeVector.h>
80 {
static const char name[] =
"unknown";
return( name ); }
99 { std::cerr <<
"not implemented for unknown type"<<std::endl; }
102 template<
typename int_type>
106 { std::cerr <<
"not implemented for unknown type"<<std::endl; }
120 {
static const char name[] =
"Epetra_CrsMatrix";
return( name ); }
137 if (pointmap == NULL) {
138 std::cerr <<
"dynamic_cast<const Epetra_Map*> failed."<<std::endl;
148 { mat->ReplaceRowMap(map); }
151 template<
typename int_type>
161 const Epetra_Map& origColMap = srcObj->ColMap();
163 TPermutation<Epetra_CrsMatrix, int_type>* colperm =
164 new TPermutation<Epetra_CrsMatrix, int_type>(origColMap);
165 colperm->PutValue(0);
168 colperm->Export(*perm, p_exporter,
Add);
170 const Epetra_Map& origRowMap = srcObj->RowMap();
172 int_type* myGlobalRows = 0;
173 origRowMap.MyGlobalElementsPtr(myGlobalRows);
179 for(
int i=0; i<numMyRows; ++i) {
180 int_type globalRow = myGlobalRows[i];
181 int len = srcObj->NumGlobalEntries(globalRow);
184 double* src_values =
new double[len];
185 int_type* src_indices =
new int_type[len];
186 int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices,
187 src_values, src_indices);
188 if (err < 0 || numIndices != len) {
189 std::cerr<<
"Perm_traits<CrsMatrix>::produceColumnPermutation err("<<err<<
") row "
190 <<globalRow<<
", len "<<len<<
", numIndices "<<numIndices<<std::endl;
193 int_type* pindices =
new int_type[len];
196 int_type* p = colperm->Values();
198 for(
int j=0; j<len; ++j) {
199 int_type old_col = src_indices[j];
201 int lid = pmap.
LID(old_col);
203 std::cerr <<
"Perm_traits<CrsMatrix>::permuteColumnIndices GID("<<old_col
204 <<
") not found"<<std::endl;
208 pindices[j] = p[lid];
211 err = result->InsertGlobalValues(globalRow, len, src_values, pindices);
213 std::cerr <<
"Perm_traits<CrsMatrix>::permuteColumnIndices err("<<err
214 <<
") row "<<globalRow<<std::endl;
218 delete [] src_indices;
219 delete [] src_values;
222 result->FillComplete();
229#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
235 return TproduceColumnPermutation<int>(perm, srcObj);
239#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
245 return TproduceColumnPermutation<long long>(perm, srcObj);
260 {
static const char name[] =
"Epetra_CrsGraph";
return( name ); }
278 { graph->ReplaceRowMap(map); }
281 template<
typename int_type>
293 TPermutation<Epetra_CrsGraph, int_type>* colperm =
294 new TPermutation<Epetra_CrsGraph, int_type>(origColMap);
295 colperm->PutValue(0);
298 colperm->Export(*perm, p_exporter,
Add);
302 int_type* myGlobalRows = 0;
303 origRowMap.MyGlobalElementsPtr(myGlobalRows);
309 for(
int i=0; i<numMyRows; ++i) {
310 int_type globalRow = myGlobalRows[i];
311 int len = srcObj->NumGlobalIndices(globalRow);
314 int_type* src_indices =
new int_type[len];
315 int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices, src_indices);
316 if (err < 0 || numIndices != len) {
317 std::cerr<<
"Perm_traits<CrsGraph>::produceColumnPermutation err("<<err<<
") row "
318 <<globalRow<<
", len "<<len<<
", numIndices "<<numIndices<<std::endl;
321 int_type* pindices =
new int_type[len];
324 int_type* p = colperm->Values();
326 for(
int j=0; j<len; ++j) {
327 int_type old_col = src_indices[j];
329 int lid = pmap.
LID(old_col);
331 std::cerr <<
"Perm_traits<CrsGraph>::permuteColumnIndices GID("<<old_col
332 <<
") not found"<<std::endl;
336 pindices[j] = p[lid];
339 err = result->InsertGlobalIndices(globalRow, len, pindices);
341 std::cerr <<
"Perm_traits<CrsGraph>::produceColumnPermutation err("<<err
342 <<
") row "<<globalRow<<std::endl;
346 delete [] src_indices;
349 result->FillComplete();
356#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
362 return TproduceColumnPermutation<int>(perm, srcObj);
366#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
372 return TproduceColumnPermutation<long long>(perm, srcObj);
386 {
static const char name[] =
"Epetra_MultiVector";
return( name ); }
401 { mvec->ReplaceMap(map); }
403#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
409 std::cerr <<
"col-permutation not implemented for Epetra_MultiVector"<<std::endl;
413#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
419 std::cerr <<
"col-permutation not implemented for Epetra_MultiVector"<<std::endl;
430template<
typename T,
typename int_type>
433 int_type* permutation)
438 if (!isTypeSupported()) {
439 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
444template<
typename T,
typename int_type>
450 if (!isTypeSupported()) {
451 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
456template<
typename T,
typename int_type>
462 if (!isTypeSupported()) {
463 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
468template<
typename T,
typename int_type>
471 if (newObj_ != NULL)
delete newObj_;
474template<
typename T,
typename int_type>
478 if (!strcmp(type_name,
"unknown")) {
485template<
typename T,
typename int_type>
486typename TPermutation<T, int_type>::OutputRef
487TPermutation<T, int_type>::operator()(
typename TPermutation<T, int_type>::InputRef orig )
521 TPermutation* p =
this;
526 if (!pmap->PointSameAs(origMap)) {
528 TPermutation* newp =
new TPermutation(origMap);
529 newp->Export(*p, p_exporter, Add);
533 pmap =
new Epetra_Map((int_type) p->Map().NumGlobalPoints64(),
534 p->Map().NumMyPoints(),
536 (int_type) p->Map().IndexBase64(),
543 newObj_ = Perm_traits<T>::clone(origObj_, Copy, *pmap, 1);
552 newObj_->Export(orig, exporter, Add);
559 Perm_traits<T>::replaceMap(newObj_, origMap);
570template<
typename T,
typename int_type>
571typename TPermutation<T, int_type>::OutputRef
572TPermutation<T, int_type>::operator()(
typename TPermutation<T, int_type>::InputRef orig,
573 bool column_permutation )
578 if (!column_permutation) {
579 return(
operator()(orig) );
582 if (strcmp(
"Epetra_CrsMatrix", Perm_traits<T>::typeName()) &&
583 strcmp(
"Epetra_CrsGraph", Perm_traits<T>::typeName())) {
584 std::cerr <<
"Permutation: column-permutation only implemented for"
585 <<
"CrsMatrix and CrsGraph." << std::endl;
589 newObj_ = Perm_traits<T>::produceColumnPermutation(
this, &orig);
Permutation stores and describes a permutation matrix P.
TPermutation(Epetra_DataAccess CV, const Epetra_BlockMap &map, int_type *permutation)
Constructor.
virtual ~TPermutation()
Destructor.
int NumMyElements() const
const Epetra_BlockMap & Map() const
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
static void replaceMap(Epetra_CrsGraph *graph, const Epetra_BlockMap &map)
replaceMap implementation
static Epetra_CrsGraph * produceColumnPermutation(TPermutation< Epetra_CrsGraph, long long > *perm, Epetra_CrsGraph *srcObj)
return new object which is a column-permutation of srcObj
static Epetra_CrsGraph * clone(Epetra_CrsGraph *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int rowLength)
clone implementation
static Epetra_CrsGraph * TproduceColumnPermutation(TPermutation< Epetra_CrsGraph, int_type > *perm, Epetra_CrsGraph *srcObj)
return new object which is a column-permutation of srcObj
static Epetra_CrsGraph * produceColumnPermutation(TPermutation< Epetra_CrsGraph, int > *perm, Epetra_CrsGraph *srcObj)
return new object which is a column-permutation of srcObj
static const char * typeName()
typeName implementation
static Epetra_CrsMatrix * produceColumnPermutation(TPermutation< Epetra_CrsMatrix, long long > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj
static Epetra_CrsMatrix * TproduceColumnPermutation(TPermutation< Epetra_CrsMatrix, int_type > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj
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
static Epetra_CrsMatrix * produceColumnPermutation(TPermutation< Epetra_CrsMatrix, int > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj
static const char * typeName()
typeName implementation
static void replaceMap(Epetra_MultiVector *mvec, const Epetra_BlockMap &map)
replaceMap implementation
static const char * typeName()
typeName implementation
static Epetra_MultiVector * clone(Epetra_MultiVector *example, Epetra_DataAccess, const Epetra_BlockMap &map, int)
clone implementation
static Epetra_MultiVector * produceColumnPermutation(Permutation64< Epetra_MultiVector > *, Epetra_MultiVector *)
permute column-indices within a specified row, if applicable
static Epetra_MultiVector * produceColumnPermutation(Permutation< Epetra_MultiVector > *, Epetra_MultiVector *)
permute column-indices within a specified row, if applicable
Define some traits to make it easier to deal with template-parameters which are objects to be permute...
static void replaceMap(T *obj, const Epetra_BlockMap &map)
replace the object's row-map (or if it's not a matrix, replace its only map)
static T * produceColumnPermutation(TPermutation< T, int_type > *perm, T *srcObj)
return new object, which is a column-permutation of srcObj
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()
return a std::string name for the object type