/usr/include/trilinos/EpetraExt_Permutation_impl.h is in libtrilinos-epetraext-dev 12.4.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | //@HEADER
// ***********************************************************************
//
// EpetraExt: Epetra Extended - Linear Algebra Services Package
// Copyright (2011) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
//@HEADER
#ifndef EpetraExt_PERMUTATION_IMPL_H
#define EpetraExt_PERMUTATION_IMPL_H
#include <EpetraExt_ConfigDefs.h>
#include <EpetraExt_Permutation.h>
#include <Epetra_Export.h>
#include <Epetra_Map.h>
#include <Epetra_Comm.h>
#include <Epetra_MultiVector.h>
#include <Epetra_CrsGraph.h>
#include <Epetra_CrsMatrix.h>
#include <Epetra_GIDTypeVector.h>
namespace EpetraExt {
/** Define some traits to make it easier to deal with template-parameters which
are objects to be permuted. Given a template parameter, we'll want to
have the following operations available:
<ul>
<li>determine the type
<li>construct an instance of it
<li>replace its row-map
<li>produce a column-permutation of it
</ul>
First the default definition, which catches all types "T", followed by some
specializations for anticipated types. Any type other than the types
specifically anticipated will be handled by this default definition,
allowing the Permutation class to abort or return NULL where appropriate.
We define these trait structs in this file rather than in a separate file
in an attempt to avoid some template-instantiation complications...
*/
template<class T>
struct Perm_traits {
/** return a std::string name for the object type */
static const char* typeName()
{ static const char name[] = "unknown"; return( name ); }
/** clone function accepts an example of the object being cloned, and enough
constructor arguments to be able to create any of these: CrsMatrix,
CrsGraph, Vector, MultiVector. And probably more later...
Why is an example object needed? For instance, if a MultiVector is
created, we may want to know how many vectors it should contain...
*/
static T* clone(T* example,
Epetra_DataAccess CV,
const Epetra_BlockMap& map,
int int_argument)
{ return( NULL ); }
/** replace the object's row-map (or if it's not a matrix, replace its only
map)
*/
static void replaceMap(T* obj, const Epetra_BlockMap& map)
{ std::cerr << "not implemented for unknown type"<<std::endl; }
/** return new object, which is a column-permutation of srcObj */
template<typename int_type>
static T*
produceColumnPermutation(TPermutation<T, int_type>* perm,
T* srcObj)
{ std::cerr << "not implemented for unknown type"<<std::endl; }
};//struct Perm_traits
/** A specialization of Perm_traits for the specific type
Epetra_CrsMatrix.
*/
template<>
struct Perm_traits<Epetra_CrsMatrix> {
/** typeName implementation */
static const char* typeName()
{ static const char name[] = "Epetra_CrsMatrix"; return( name ); }
/** clone implementation */
static Epetra_CrsMatrix* clone(Epetra_CrsMatrix* example,
Epetra_DataAccess CV,
const Epetra_BlockMap& map,
int rowLength)
{
//don't need the example object currently...
(void)example;
//we need a Epetra_Map, rather than a Epetra_BlockMap, to create a
//Epetra_CrsMatrix.
const Epetra_Map* pointmap =
dynamic_cast<const Epetra_Map*>(&map);
if (pointmap == NULL) {
std::cerr << "dynamic_cast<const Epetra_Map*> failed."<<std::endl;
return(NULL);
}
return( new Epetra_CrsMatrix(CV, *pointmap, rowLength) );
}
/** replaceMap implementation */
static void replaceMap(Epetra_CrsMatrix* mat, const Epetra_BlockMap& map)
{ mat->ReplaceRowMap(map); }
/** return new object, which is a column-permutation of srcObj */
template<typename int_type>
static Epetra_CrsMatrix*
TproduceColumnPermutation(TPermutation<Epetra_CrsMatrix, int_type>* perm,
Epetra_CrsMatrix* srcObj)
{
//First we need to export this permutation to match the column-map of the
//object being column-permuted. (We need to have locally available all
//elements of the permutation corresponding to the local columns of the
//object being permuted.)
const Epetra_Map& origColMap = srcObj->ColMap();
TPermutation<Epetra_CrsMatrix, int_type>* colperm =
new TPermutation<Epetra_CrsMatrix, int_type>(origColMap);
colperm->PutValue(0);
Epetra_Export p_exporter(perm->Map(), origColMap);
colperm->Export(*perm, p_exporter, Add);
const Epetra_Map& origRowMap = srcObj->RowMap();
int numMyRows = origRowMap.NumMyElements();
int_type* myGlobalRows = 0;
origRowMap.MyGlobalElementsPtr(myGlobalRows);
//Create the new object, giving it the same map as the original object.
Epetra_CrsMatrix* result = new Epetra_CrsMatrix(Copy, origRowMap, 1);
for(int i=0; i<numMyRows; ++i) {
int_type globalRow = myGlobalRows[i];
int len = srcObj->NumGlobalEntries(globalRow);
int numIndices;
double* src_values = new double[len];
int_type* src_indices = new int_type[len];
int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices,
src_values, src_indices);
if (err < 0 || numIndices != len) {
std::cerr<<"Perm_traits<CrsMatrix>::produceColumnPermutation err("<<err<<") row "
<<globalRow<<", len "<<len<<", numIndices "<<numIndices<<std::endl;
}
int_type* pindices = new int_type[len];
const Epetra_BlockMap& pmap = colperm->Map();
int_type* p = colperm->Values();
for(int j=0; j<len; ++j) {
int_type old_col = src_indices[j];
int lid = pmap.LID(old_col);
if (lid<0) {
std::cerr << "Perm_traits<CrsMatrix>::permuteColumnIndices GID("<<old_col
<<") not found"<<std::endl;
break;
}
pindices[j] = p[lid];
}
err = result->InsertGlobalValues(globalRow, len, src_values, pindices);
if (err < 0) {
std::cerr << "Perm_traits<CrsMatrix>::permuteColumnIndices err("<<err
<<") row "<<globalRow<<std::endl;
}
delete [] pindices;
delete [] src_indices;
delete [] src_values;
}
result->FillComplete();
delete colperm;
return(result);
}
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
/** return new object, which is a column-permutation of srcObj */
static Epetra_CrsMatrix*
produceColumnPermutation(TPermutation<Epetra_CrsMatrix, int>* perm,
Epetra_CrsMatrix* srcObj)
{
return TproduceColumnPermutation<int>(perm, srcObj);
}
#endif
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
/** return new object, which is a column-permutation of srcObj */
static Epetra_CrsMatrix*
produceColumnPermutation(TPermutation<Epetra_CrsMatrix, long long>* perm,
Epetra_CrsMatrix* srcObj)
{
return TproduceColumnPermutation<long long>(perm, srcObj);
}
#endif
};//struct Perm_traits<Epetra_CrsMatrix>
/** A specialization of Perm_traits for the specific type
Epetra_CrsGraph.
*/
template<>
struct Perm_traits<Epetra_CrsGraph> {
/** typeName implementation */
static const char* typeName()
{ static const char name[] = "Epetra_CrsGraph"; return( name ); }
/** clone implementation */
static Epetra_CrsGraph* clone(Epetra_CrsGraph* example,
Epetra_DataAccess CV,
const Epetra_BlockMap& map,
int rowLength)
{
//don't need the example object currently...
(void)example;
return( new Epetra_CrsGraph(CV, map, rowLength) );
}
/** replaceMap implementation */
static void replaceMap(Epetra_CrsGraph* graph, const Epetra_BlockMap& map)
{ graph->ReplaceRowMap(map); }
/** return new object which is a column-permutation of srcObj */
template<typename int_type>
static Epetra_CrsGraph*
TproduceColumnPermutation(TPermutation<Epetra_CrsGraph, int_type>* perm,
Epetra_CrsGraph* srcObj)
{
//First we need to export this permutation to match the column-map of the
//object being column-permuted. (We need to have locally available all
//elements of the permutation corresponding to the local columns of the
//object being permuted.)
const Epetra_BlockMap& origColMap = srcObj->ColMap();
TPermutation<Epetra_CrsGraph, int_type>* colperm =
new TPermutation<Epetra_CrsGraph, int_type>(origColMap);
colperm->PutValue(0);
Epetra_Export p_exporter(perm->Map(), origColMap);
colperm->Export(*perm, p_exporter, Add);
const Epetra_BlockMap& origRowMap = srcObj->RowMap();
int numMyRows = origRowMap.NumMyElements();
int_type* myGlobalRows = 0;
origRowMap.MyGlobalElementsPtr(myGlobalRows);
//Create the new object, giving it the same map as the original object.
Epetra_CrsGraph* result = new Epetra_CrsGraph(Copy, origRowMap, 1);
for(int i=0; i<numMyRows; ++i) {
int_type globalRow = myGlobalRows[i];
int len = srcObj->NumGlobalIndices(globalRow);
int numIndices;
int_type* src_indices = new int_type[len];
int err = srcObj->ExtractGlobalRowCopy(globalRow, len, numIndices, src_indices);
if (err < 0 || numIndices != len) {
std::cerr<<"Perm_traits<CrsGraph>::produceColumnPermutation err("<<err<<") row "
<<globalRow<<", len "<<len<<", numIndices "<<numIndices<<std::endl;
}
int_type* pindices = new int_type[len];
const Epetra_BlockMap& pmap = colperm->Map();
int_type* p = colperm->Values();
for(int j=0; j<len; ++j) {
int_type old_col = src_indices[j];
int lid = pmap.LID(old_col);
if (lid<0) {
std::cerr << "Perm_traits<CrsGraph>::permuteColumnIndices GID("<<old_col
<<") not found"<<std::endl;
break;
}
pindices[j] = p[lid];
}
err = result->InsertGlobalIndices(globalRow, len, pindices);
if (err < 0) {
std::cerr << "Perm_traits<CrsGraph>::produceColumnPermutation err("<<err
<<") row "<<globalRow<<std::endl;
}
delete [] pindices;
delete [] src_indices;
}
result->FillComplete();
delete colperm;
return(result);
}
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
/** return new object which is a column-permutation of srcObj */
static Epetra_CrsGraph*
produceColumnPermutation(TPermutation<Epetra_CrsGraph, int>* perm,
Epetra_CrsGraph* srcObj)
{
return TproduceColumnPermutation<int>(perm, srcObj);
}
#endif
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
/** return new object which is a column-permutation of srcObj */
static Epetra_CrsGraph*
produceColumnPermutation(TPermutation<Epetra_CrsGraph, long long>* perm,
Epetra_CrsGraph* srcObj)
{
return TproduceColumnPermutation<long long>(perm, srcObj);
}
#endif
};//struct Perm_traits<Epetra_CrsGraph>
/** A specialization of Perm_traits for the specific type
Epetra_MultiVector.
*/
template<>
struct Perm_traits<Epetra_MultiVector> {
/** typeName implementation */
static const char* typeName()
{ static const char name[] = "Epetra_MultiVector"; return( name ); }
/** clone implementation */
static Epetra_MultiVector* clone(Epetra_MultiVector* example,
Epetra_DataAccess CV,
const Epetra_BlockMap& map,
int numVectors)
{
return( new Epetra_MultiVector(map, example->NumVectors()) );
}
/** replaceMap implementation */
static void replaceMap(Epetra_MultiVector* mvec, const Epetra_BlockMap& map)
{ mvec->ReplaceMap(map); }
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
/** permute column-indices within a specified row, if applicable*/
static Epetra_MultiVector*
produceColumnPermutation(Permutation<Epetra_MultiVector>* perm,
Epetra_MultiVector* srcObj)
{
std::cerr << "col-permutation not implemented for Epetra_MultiVector"<<std::endl;
return(NULL);
}
#endif
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
/** permute column-indices within a specified row, if applicable*/
static Epetra_MultiVector*
produceColumnPermutation(Permutation64<Epetra_MultiVector>* perm,
Epetra_MultiVector* srcObj)
{
std::cerr << "col-permutation not implemented for Epetra_MultiVector"<<std::endl;
return(NULL);
}
#endif
};//struct Perm_traits<Epetra_CrsGraph>
//-------------------------------------------------------------------------
//Now the method definitions for the EpetraExt::Permutation class.
//-------------------------------------------------------------------------
template<typename T, typename int_type>
TPermutation<T, int_type>::TPermutation(Epetra_DataAccess CV,
const Epetra_BlockMap& map,
int_type* permutation)
: Epetra_GIDTypeVector<int_type>::impl(CV, map, permutation),
newObj_(NULL),
origObj_(NULL)
{
if (!isTypeSupported()) {
std::cerr << "unsupported type for permutation, aborting" << std::endl;
abort();
}
}
template<typename T, typename int_type>
TPermutation<T, int_type>::TPermutation(const Epetra_BlockMap& map)
: Epetra_GIDTypeVector<int_type>::impl(map),
newObj_(NULL),
origObj_(NULL)
{
if (!isTypeSupported()) {
std::cerr << "unsupported type for permutation, aborting" << std::endl;
abort();
}
}
template<typename T, typename int_type>
TPermutation<T, int_type>::TPermutation(const TPermutation& src)
: Epetra_GIDTypeVector<int_type>::impl((const typename Epetra_GIDTypeVector<int_type>::impl&)src),
newObj_(NULL),
origObj_(NULL)
{
if (!isTypeSupported()) {
std::cerr << "unsupported type for permutation, aborting" << std::endl;
abort();
}
}
template<typename T, typename int_type>
TPermutation<T, int_type>::~TPermutation()
{
if (newObj_ != NULL) delete newObj_;
}
template<typename T, typename int_type>
bool TPermutation<T, int_type>::isTypeSupported()
{
const char* type_name = Perm_traits<T>::typeName();
if (!strcmp(type_name, "unknown")) {
return(false);
}
return( true );
}
template<typename T, typename int_type>
typename TPermutation<T, int_type>::OutputRef
TPermutation<T, int_type>::operator()( typename TPermutation<T, int_type>::InputRef orig )
{
//In this function we're going to produce a new object which is a
//row-permutation of the input object (orig).
//
//Our permutation inherits IntVector, and the permutation is defined by the
//contents of the integer vector 'p', such that if p[i] = j then row i of
//the input object becomes row j of the permuted object.
//
//The permutation is accomplished by creating a map defined by the
//permutation, then using an Epetra_Export operation to move data from the
//input object into the permuted object.
//
//The permutation may be global. In other words, the rows of the object may
//be arbitrarily rearranged, including across processors.
//
origObj_ = &orig;
//The 'Map()' accessor returns Epetra_DistObject::Map() for CrsGraph and
//CrsMatrix, which turns out to be the RowMap() for those objects. For
//MultiVector it returns the correct object because MultiVectors only have
//one map.
const Epetra_BlockMap& origMap = orig.Map();
//Create an Epetra_Map representing the permutation.
Epetra_Map* pmap = new Epetra_Map((int_type) Epetra_DistObject::Map().NumGlobalPoints64(),
Epetra_DistObject::Map().NumMyPoints(),
Epetra_GIDTypeVector<int_type>::impl::Values(),
(int_type) Epetra_DistObject::Map().IndexBase64(),
Epetra_DistObject::Map().Comm());
TPermutation* p = this;
//Next check that the maps are compatible. If they aren't, we'll redistribute
//the permutation to match the distribution of the input object.
if (!pmap->PointSameAs(origMap)) {
Epetra_Export p_exporter(Epetra_DistObject::Map(), origMap);
TPermutation* newp = new TPermutation(origMap);
newp->Export(*p, p_exporter, Add);
p = newp;
delete pmap;
pmap = new Epetra_Map((int_type) p->Map().NumGlobalPoints64(),
p->Map().NumMyPoints(),
p->Values(),
(int_type) p->Map().IndexBase64(),
p->Map().Comm());
}
//Create the new object, initially giving it the map defined by the
//permutation.
newObj_ = Perm_traits<T>::clone(origObj_, Copy, *pmap, 1);
//Create an exporter which will export data from the original object to the
//permuted object.
Epetra_Export exporter(origMap, *pmap);
//Now export the original object to the permuted object.
newObj_->Export(orig, exporter, Add);
//Now, since the export operation moved not only row-contents but also
//row-numbering, we need to replace the permuted row-numbering with the
//original row-numbering. We do this by replacing the permuted map with
//the original row-map.
Perm_traits<T>::replaceMap(newObj_, origMap);
delete pmap;
if (p != this) {
delete p; //delete "newp" created if the PointSameAs test failed above
}
return( *newObj_ );
}
template<typename T, typename int_type>
typename TPermutation<T, int_type>::OutputRef
TPermutation<T, int_type>::operator()( typename TPermutation<T, int_type>::InputRef orig,
bool column_permutation )
{
origObj_ = &orig;
newObj_ = NULL;
if (!column_permutation) {
return( operator()(orig) );
}
if (strcmp("Epetra_CrsMatrix", Perm_traits<T>::typeName()) &&
strcmp("Epetra_CrsGraph", Perm_traits<T>::typeName())) {
std::cerr << "Permutation: column-permutation only implemented for"
<< "CrsMatrix and CrsGraph." << std::endl;
assert(0);
}
newObj_ = Perm_traits<T>::produceColumnPermutation(this, &orig);
return( *newObj_ );
}
} // namespace EpetraExt
#endif //EpetraExt_PERMUTATION_IMPL_H
|