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 ); }
91 const Epetra_BlockMap& map,
98 static void replaceMap(T* obj,
const Epetra_BlockMap& map)
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 ); }
124 static Epetra_CrsMatrix*
clone(Epetra_CrsMatrix* example,
125 Epetra_DataAccess CV,
126 const Epetra_BlockMap& map,
135 const Epetra_Map* pointmap =
136 dynamic_cast<const Epetra_Map*
>(&map);
137 if (pointmap == NULL) {
138 std::cerr <<
"dynamic_cast<const Epetra_Map*> failed."<<std::endl;
142 return(
new Epetra_CrsMatrix(CV, *pointmap, rowLength) );
147 static void replaceMap(Epetra_CrsMatrix* mat,
const Epetra_BlockMap& map)
148 { mat->ReplaceRowMap(map); }
151 template<
typename int_type>
152 static Epetra_CrsMatrix*
154 Epetra_CrsMatrix* srcObj)
161 const Epetra_Map& origColMap = srcObj->ColMap();
165 colperm->PutValue(0);
167 Epetra_Export p_exporter(perm->Map(), origColMap);
168 colperm->Export(*perm, p_exporter, Add);
170 const Epetra_Map& origRowMap = srcObj->RowMap();
171 int numMyRows = origRowMap.NumMyElements();
172 int_type* myGlobalRows = 0;
173 origRowMap.MyGlobalElementsPtr(myGlobalRows);
177 Epetra_CrsMatrix* result =
new Epetra_CrsMatrix(Copy, origRowMap, 1);
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];
195 const Epetra_BlockMap& pmap = colperm->Map();
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 231 static Epetra_CrsMatrix*
233 Epetra_CrsMatrix* srcObj)
235 return TproduceColumnPermutation<int>(perm, srcObj);
239 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 241 static Epetra_CrsMatrix*
243 Epetra_CrsMatrix* srcObj)
245 return TproduceColumnPermutation<long long>(perm, srcObj);
260 {
static const char name[] =
"Epetra_CrsGraph";
return( name ); }
264 static Epetra_CrsGraph*
clone(Epetra_CrsGraph* example,
265 Epetra_DataAccess CV,
266 const Epetra_BlockMap& map,
272 return(
new Epetra_CrsGraph(CV, map, rowLength) );
277 static void replaceMap(Epetra_CrsGraph* graph,
const Epetra_BlockMap& map)
278 { graph->ReplaceRowMap(map); }
281 template<
typename int_type>
282 static Epetra_CrsGraph*
284 Epetra_CrsGraph* srcObj)
291 const Epetra_BlockMap& origColMap = srcObj->ColMap();
295 colperm->PutValue(0);
297 Epetra_Export p_exporter(perm->Map(), origColMap);
298 colperm->Export(*perm, p_exporter, Add);
300 const Epetra_BlockMap& origRowMap = srcObj->RowMap();
301 int numMyRows = origRowMap.NumMyElements();
302 int_type* myGlobalRows = 0;
303 origRowMap.MyGlobalElementsPtr(myGlobalRows);
307 Epetra_CrsGraph* result =
new Epetra_CrsGraph(Copy, origRowMap, 1);
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];
323 const Epetra_BlockMap& pmap = colperm->Map();
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 358 static Epetra_CrsGraph*
360 Epetra_CrsGraph* srcObj)
362 return TproduceColumnPermutation<int>(perm, srcObj);
366 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 368 static Epetra_CrsGraph*
370 Epetra_CrsGraph* srcObj)
372 return TproduceColumnPermutation<long long>(perm, srcObj);
386 {
static const char name[] =
"Epetra_MultiVector";
return( name ); }
390 static Epetra_MultiVector*
clone(Epetra_MultiVector* example,
391 Epetra_DataAccess CV,
392 const Epetra_BlockMap& map,
395 return(
new Epetra_MultiVector(map, example->NumVectors()) );
400 static void replaceMap(Epetra_MultiVector* mvec,
const Epetra_BlockMap& map)
401 { mvec->ReplaceMap(map); }
403 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 405 static Epetra_MultiVector*
407 Epetra_MultiVector* srcObj)
409 std::cerr <<
"col-permutation not implemented for Epetra_MultiVector"<<std::endl;
413 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 415 static Epetra_MultiVector*
417 Epetra_MultiVector* srcObj)
419 std::cerr <<
"col-permutation not implemented for Epetra_MultiVector"<<std::endl;
430 template<
typename T,
typename int_type>
432 const Epetra_BlockMap& map,
433 int_type* permutation)
434 : Epetra_GIDTypeVector<int_type>::impl(CV, map, permutation),
438 if (!isTypeSupported()) {
439 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
444 template<
typename T,
typename int_type>
446 : Epetra_GIDTypeVector<int_type>::impl(map),
450 if (!isTypeSupported()) {
451 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
456 template<
typename T,
typename int_type>
458 : Epetra_GIDTypeVector<int_type>::impl((const typename Epetra_GIDTypeVector<int_type>::impl&)src),
462 if (!isTypeSupported()) {
463 std::cerr <<
"unsupported type for permutation, aborting" << std::endl;
468 template<
typename T,
typename int_type>
471 if (newObj_ != NULL)
delete newObj_;
474 template<
typename T,
typename int_type>
478 if (!strcmp(type_name,
"unknown")) {
485 template<
typename T,
typename int_type>
511 const Epetra_BlockMap& origMap = orig.Map();
515 Epetra_Map* pmap =
new Epetra_Map((int_type) Epetra_DistObject::Map().NumGlobalPoints64(),
516 Epetra_DistObject::Map().NumMyPoints(),
517 Epetra_GIDTypeVector<int_type>::impl::Values(),
518 (int_type) Epetra_DistObject::Map().IndexBase64(),
519 Epetra_DistObject::Map().Comm());
526 if (!pmap->PointSameAs(origMap)) {
527 Epetra_Export p_exporter(Epetra_DistObject::Map(), 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(),
548 Epetra_Export exporter(origMap, *pmap);
552 newObj_->Export(orig, exporter, Add);
570 template<
typename T,
typename int_type>
573 bool column_permutation )
578 if (!column_permutation) {
579 return(
operator()(orig) );
584 std::cerr <<
"Permutation: column-permutation only implemented for" 585 <<
"CrsMatrix and CrsGraph." << std::endl;
596 #endif //EpetraExt_PERMUTATION_IMPL_H 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 * TproduceColumnPermutation(TPermutation< Epetra_CrsGraph, int_type > *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 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 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
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
static Epetra_CrsMatrix * produceColumnPermutation(TPermutation< Epetra_CrsMatrix, int > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj
virtual ~TPermutation()
Destructor.
static Epetra_MultiVector * clone(Epetra_MultiVector *example, Epetra_DataAccess CV, const Epetra_BlockMap &map, int numVectors)
clone implementation
Permutation stores and describes a permutation matrix P.
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_CrsGraph * produceColumnPermutation(TPermutation< Epetra_CrsGraph, int > *perm, Epetra_CrsGraph *srcObj)
return new object which is a column-permutation of srcObj
static Epetra_MultiVector * produceColumnPermutation(Permutation< Epetra_MultiVector > *perm, Epetra_MultiVector *srcObj)
permute column-indices within a specified row, if applicable
OutputRef operator()(InputRef orig)
This method creates a new object which is a permuted copy of the input argument.
static const char * typeName()
typeName implementation
static T * produceColumnPermutation(TPermutation< T, int_type > *perm, T *srcObj)
return new object, which is a column-permutation of srcObj
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)
TPermutation(Epetra_DataAccess CV, const Epetra_BlockMap &map, int_type *permutation)
Constructor.
EpetraExt::SameTypeTransform< T >::TransformTypeRef InputRef
static Epetra_MultiVector * produceColumnPermutation(Permutation64< Epetra_MultiVector > *perm, Epetra_MultiVector *srcObj)
permute column-indices within a specified row, if applicable
static const char * typeName()
return a std::string name for the object type
Define some traits to make it easier to deal with template-parameters which are objects to be permute...
EpetraExt::SameTypeTransform< T >::TransformTypeRef OutputRef
static void replaceMap(Epetra_CrsGraph *graph, const Epetra_BlockMap &map)
replaceMap implementation
static Epetra_CrsMatrix * TproduceColumnPermutation(TPermutation< Epetra_CrsMatrix, int_type > *perm, Epetra_CrsMatrix *srcObj)
return new object, which is a column-permutation of srcObj