| 1 |
//@HEADER |
| 2 |
//************************************************************************ |
| 3 |
// |
| 4 |
// Isorropia: Partitioning and Load Balancing Package |
| 5 |
// Copyright (2006) Sandia Corporation |
| 6 |
// |
| 7 |
//Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive |
| 8 |
//license for use of this work by or on behalf of the U.S. Government. |
| 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 |
//************************************************************************ |
| 38 |
//@HEADER |
| 39 |
|
| 40 |
#include <Isorropia_EpetraCostDescriber.hpp> |
| 41 |
#include <Isorropia_Exception.hpp> |
| 42 |
|
| 43 |
#ifdef USE_UTILS |
| 44 |
#include "ispatest_lbeval_utils.hpp" |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef HAVE_EPETRA |
| 48 |
#include <Epetra_BlockMap.h> |
| 49 |
#include <Epetra_Map.h> |
| 50 |
#include <Epetra_Vector.h> |
| 51 |
#include <Epetra_CrsMatrix.h> |
| 52 |
#include <Epetra_RowMatrix.h> |
| 53 |
#include <Epetra_MultiVector.h> |
| 54 |
#include <Epetra_Import.h> |
| 55 |
#include <Epetra_CrsGraph.h> |
| 56 |
#include <Epetra_Comm.h> |
| 57 |
|
| 58 |
namespace Isorropia { |
| 59 |
|
| 60 |
namespace Epetra { |
| 61 |
|
| 62 |
CostDescriber::CostDescriber() |
| 63 |
: vertex_weights_(), |
| 64 |
graph_edge_weights_(), |
| 65 |
graph_self_edges_(), |
| 66 |
paramlist_(), |
| 67 |
hg_edge_gids_(NULL), |
| 68 |
hg_edge_weights_(NULL), |
| 69 |
num_hg_edge_weights_(0), |
| 70 |
numGlobalVertexWeights_(0), |
| 71 |
numGlobalGraphEdgeWeights_(0), |
| 72 |
numGlobalHypergraphEdgeWeights_(0) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
CostDescriber::~CostDescriber() |
| 77 |
{ |
| 78 |
free_hg_edge_weights_(); |
| 79 |
} |
| 80 |
|
| 81 |
CostDescriber::CostDescriber(const CostDescriber &costs): |
| 82 |
vertex_weights_(), |
| 83 |
graph_edge_weights_(), |
| 84 |
graph_self_edges_(costs.graph_self_edges_), |
| 85 |
paramlist_(costs.paramlist_), |
| 86 |
hg_edge_gids_(NULL), |
| 87 |
hg_edge_weights_(NULL), |
| 88 |
num_hg_edge_weights_(costs.num_hg_edge_weights_), |
| 89 |
numGlobalVertexWeights_(costs.numGlobalVertexWeights_), |
| 90 |
numGlobalGraphEdgeWeights_(costs.numGlobalGraphEdgeWeights_), |
| 91 |
numGlobalHypergraphEdgeWeights_(costs.numGlobalHypergraphEdgeWeights_) |
| 92 |
|
| 93 |
{ |
| 94 |
if (costs.haveVertexWeights()){ |
| 95 |
Teuchos::RCP<Epetra_Vector> vwgts = Teuchos::rcp(new Epetra_Vector(*costs.vertex_weights_)); |
| 96 |
setVertexWeights(vwgts); |
| 97 |
} |
| 98 |
|
| 99 |
if (costs.haveGraphEdgeWeights()){ |
| 100 |
Teuchos::RCP<Epetra_CrsMatrix> ewgts = Teuchos::rcp(new Epetra_CrsMatrix(*costs.graph_edge_weights_)); |
| 101 |
setGraphEdgeWeights(ewgts); |
| 102 |
} |
| 103 |
|
| 104 |
int n = num_hg_edge_weights_; |
| 105 |
|
| 106 |
if (n > 0){ |
| 107 |
allocate_hg_edge_weights_(n); |
| 108 |
memcpy(hg_edge_gids_, costs.hg_edge_gids_, sizeof(int) * n); |
| 109 |
memcpy(hg_edge_weights_, costs.hg_edge_weights_, sizeof(float) * n); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void CostDescriber::_transformWeights(const Epetra_Import &importer) |
| 114 |
{ |
| 115 |
const Epetra_BlockMap &target_map = importer.TargetMap(); |
| 116 |
|
| 117 |
// same map as a concrete type |
| 118 |
|
| 119 |
Epetra_Map new_map(target_map.NumGlobalElements(), target_map.NumMyElements(), |
| 120 |
target_map.MyGlobalElements(), target_map.IndexBase(), target_map.Comm()); |
| 121 |
|
| 122 |
if (numGlobalVertexWeights_ > 0){ |
| 123 |
Teuchos::RCP<Epetra_Vector> redistWeights = Teuchos::rcp(new Epetra_Vector(target_map)); |
| 124 |
redistWeights->Import(*vertex_weights_, importer, Insert); |
| 125 |
setVertexWeights(redistWeights); |
| 126 |
} |
| 127 |
|
| 128 |
if (numGlobalGraphEdgeWeights_ > 0){ |
| 129 |
|
| 130 |
int myOldRows = graph_edge_weights_->NumMyRows(); |
| 131 |
int myNewRows = target_map.NumMyElements(); |
| 132 |
|
| 133 |
double *nnz = new double [myOldRows]; |
| 134 |
for (int i=0; i < myOldRows; i++){ |
| 135 |
nnz[i] = graph_edge_weights_->NumMyEntries(i); |
| 136 |
} |
| 137 |
|
| 138 |
Epetra_Vector oldRowSizes(Copy, graph_edge_weights_->RowMap(), nnz); |
| 139 |
|
| 140 |
if (myOldRows) |
| 141 |
delete [] nnz; |
| 142 |
|
| 143 |
Epetra_Vector newRowSizes(target_map); |
| 144 |
|
| 145 |
newRowSizes.Import(oldRowSizes, importer, Insert); |
| 146 |
|
| 147 |
int *rowSize=0; |
| 148 |
if(myNewRows){ |
| 149 |
rowSize = new int [myNewRows]; |
| 150 |
for (int i=0; i< myNewRows; i++){ |
| 151 |
rowSize[i] = static_cast<int>(newRowSizes[i]); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Teuchos::RCP<Epetra_CrsMatrix> new_graph_edge_weights = |
| 156 |
Teuchos::rcp(new Epetra_CrsMatrix(Copy, new_map, rowSize, true)); |
| 157 |
|
| 158 |
if (myNewRows) |
| 159 |
delete [] rowSize; |
| 160 |
|
| 161 |
new_graph_edge_weights->Import(*graph_edge_weights_, importer, Insert); |
| 162 |
|
| 163 |
new_graph_edge_weights->FillComplete(); |
| 164 |
|
| 165 |
setGraphEdgeWeights(new_graph_edge_weights); |
| 166 |
} |
| 167 |
|
| 168 |
// hg_edge_weights are independent of matrix row partitioning, so |
| 169 |
// there is no need to transform them |
| 170 |
} |
| 171 |
|
| 172 |
#ifdef USE_UTILS |
| 173 |
int CostDescriber::compareBeforeAndAfterHypergraph( |
| 174 |
const Epetra_RowMatrix &in_m, const Epetra_RowMatrix &out_m, |
| 175 |
const Epetra_Import &importer, |
| 176 |
std::vector<double> &balance, std::vector<double> &cutn, std::vector<double> &cutl) const |
| 177 |
{ |
| 178 |
|
| 179 |
CostDescriber costs(*this); |
| 180 |
|
| 181 |
double goalWeight = 1.0 / in_m.Comm().NumProc(); |
| 182 |
|
| 183 |
double bal, n, l; |
| 184 |
|
| 185 |
// Before |
| 186 |
|
| 187 |
int rc = ispatest::compute_hypergraph_metrics(in_m, costs, goalWeight, bal, n, l); |
| 188 |
|
| 189 |
if (rc != 0) |
| 190 |
return 1; |
| 191 |
|
| 192 |
balance[0] = bal; |
| 193 |
cutn[0] = n; |
| 194 |
cutl[0] = l; |
| 195 |
|
| 196 |
// After |
| 197 |
|
| 198 |
costs._transformWeights(importer); |
| 199 |
|
| 200 |
rc = ispatest::compute_hypergraph_metrics(out_m, costs, goalWeight, bal, n, l); |
| 201 |
|
| 202 |
if (rc != 0) |
| 203 |
return 1; |
| 204 |
|
| 205 |
balance[1] = bal; |
| 206 |
cutn[1] = n; |
| 207 |
cutl[1] = l; |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
int CostDescriber::compareBeforeAndAfterGraph( |
| 213 |
const Epetra_RowMatrix &in_m, const Epetra_RowMatrix &out_m, |
| 214 |
const Epetra_Import &importer, |
| 215 |
std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt, |
| 216 |
std::vector<double> &cutn, std::vector<double> &cutl) const |
| 217 |
{ |
| 218 |
return _compareBeforeAndAfterGraph(&in_m, &out_m, NULL, NULL, importer, |
| 219 |
balance, numCuts, cutWgt, |
| 220 |
cutn, cutl); |
| 221 |
} |
| 222 |
|
| 223 |
int CostDescriber::compareBeforeAndAfterGraph( |
| 224 |
const Epetra_CrsGraph &in_g, const Epetra_CrsGraph &out_g, |
| 225 |
const Epetra_Import &importer, |
| 226 |
std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt, |
| 227 |
std::vector<double> &cutn, std::vector<double> &cutl) const |
| 228 |
{ |
| 229 |
return _compareBeforeAndAfterGraph(NULL, NULL, &in_g, &out_g, importer, |
| 230 |
balance, numCuts, cutWgt, |
| 231 |
cutn, cutl); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
int CostDescriber::_compareBeforeAndAfterGraph( |
| 236 |
const Epetra_RowMatrix *in_m, const Epetra_RowMatrix *out_m, |
| 237 |
const Epetra_CrsGraph *in_g, const Epetra_CrsGraph *out_g, |
| 238 |
const Epetra_Import &importer, |
| 239 |
std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt, |
| 240 |
std::vector<double> &cutn, std::vector<double> &cutl) const |
| 241 |
{ |
| 242 |
CostDescriber costs(*this); |
| 243 |
|
| 244 |
double goalWeight = 1.0 / in_m->Comm().NumProc(); |
| 245 |
|
| 246 |
double bal,wgt, n, l; |
| 247 |
int ncuts, rc; |
| 248 |
|
| 249 |
// Before |
| 250 |
|
| 251 |
if (in_m){ |
| 252 |
rc = ispatest::compute_graph_metrics(*in_m, costs, goalWeight, bal, ncuts, wgt, n, l); |
| 253 |
} |
| 254 |
else{ |
| 255 |
rc = ispatest::compute_graph_metrics(*in_g, costs, goalWeight, bal, ncuts, wgt, n, l); |
| 256 |
} |
| 257 |
|
| 258 |
if (rc != 0) |
| 259 |
return 1; |
| 260 |
|
| 261 |
balance[0] = bal; |
| 262 |
numCuts[0] = ncuts; |
| 263 |
cutWgt[0] = wgt; |
| 264 |
cutn[0] = n; |
| 265 |
cutl[0] = l; |
| 266 |
|
| 267 |
// After |
| 268 |
|
| 269 |
costs._transformWeights(importer); |
| 270 |
|
| 271 |
if (in_m){ |
| 272 |
rc = ispatest::compute_graph_metrics(*out_m, costs, goalWeight, bal, ncuts, wgt, n, l); |
| 273 |
} |
| 274 |
else{ |
| 275 |
rc = ispatest::compute_graph_metrics(*out_g, costs, goalWeight, bal, ncuts, wgt, n, l); |
| 276 |
} |
| 277 |
|
| 278 |
if (rc != 0) |
| 279 |
return 1; |
| 280 |
|
| 281 |
balance[1] = bal; |
| 282 |
numCuts[1] = ncuts; |
| 283 |
cutWgt[1] = wgt; |
| 284 |
cutn[1] = n; |
| 285 |
cutl[1] = l; |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int CostDescriber::compareBeforeAndAfterImbalance(const Epetra_MultiVector &mv, const Epetra_Import &importer, |
| 291 |
std::vector<double> &min, std::vector<double> &max, std::vector<double> &avg) const |
| 292 |
{ |
| 293 |
CostDescriber costs(*this); |
| 294 |
double goalWeight = 1.0 / mv.Comm().NumProc(); |
| 295 |
double a, b, c; |
| 296 |
|
| 297 |
if (!costs.haveVertexWeights()){ |
| 298 |
Epetra_Vector unitWgts(mv.Map()); |
| 299 |
unitWgts.PutScalar(1.0); |
| 300 |
costs.setVertexWeights(Teuchos::rcp(&unitWgts)); |
| 301 |
} |
| 302 |
|
| 303 |
// Before |
| 304 |
|
| 305 |
int rc = ispatest::compute_balance(costs.getVertexWeights(), goalWeight, a, b, c); |
| 306 |
|
| 307 |
if (rc != 0) |
| 308 |
return 1; |
| 309 |
|
| 310 |
min[0] = a; |
| 311 |
max[0] = b; |
| 312 |
avg[0] = c; |
| 313 |
|
| 314 |
// After |
| 315 |
|
| 316 |
costs._transformWeights(importer); |
| 317 |
|
| 318 |
rc = ispatest::compute_balance(costs.getVertexWeights(), goalWeight, a, b, c); |
| 319 |
|
| 320 |
if (rc != 0) |
| 321 |
return 1; |
| 322 |
|
| 323 |
min[1] = a; |
| 324 |
max[1] = b; |
| 325 |
avg[1] = c; |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |
| 329 |
#endif |
| 330 |
|
| 331 |
void CostDescriber::setParameters(const Teuchos::ParameterList& paramlist) |
| 332 |
{ |
| 333 |
paramlist_ = paramlist; |
| 334 |
} |
| 335 |
|
| 336 |
/** Supply a vector of vertex (row) weights. If rows are distributed, then |
| 337 |
each process must supply a weight for each of its rows. (Alternatively |
| 338 |
the application can supply no vertex weights at all.) The weights should |
| 339 |
be in the same order as the rows in the Epetra object being partitioned. |
| 340 |
*/ |
| 341 |
void CostDescriber::setVertexWeights(Teuchos::RCP<const Epetra_Vector> vwts) |
| 342 |
{ |
| 343 |
if (vertex_weights_.get() != 0){ |
| 344 |
vertex_weights_.release(); |
| 345 |
} |
| 346 |
vertex_weights_ = vwts; |
| 347 |
} |
| 348 |
|
| 349 |
void |
| 350 |
CostDescriber::setGraphEdgeWeights(Teuchos::RCP<const Epetra_CrsMatrix> gewts) |
| 351 |
{ |
| 352 |
if (graph_edge_weights_.get() != 0){ |
| 353 |
graph_edge_weights_.release(); |
| 354 |
graph_self_edges_.clear(); |
| 355 |
} |
| 356 |
graph_edge_weights_ = gewts; |
| 357 |
|
| 358 |
if (gewts->NumMyDiagonals() > 0){ |
| 359 |
|
| 360 |
// Save list of self edges - we omit them in the Zoltan query functions |
| 361 |
|
| 362 |
const Epetra_Map &rowmap = gewts->RowMap(); |
| 363 |
|
| 364 |
Epetra_Vector diag(rowmap); |
| 365 |
|
| 366 |
gewts->ExtractDiagonalCopy(diag); |
| 367 |
|
| 368 |
int nvals = gewts->NumMyRows(); |
| 369 |
double *entry; |
| 370 |
diag.ExtractView(&entry); |
| 371 |
for (int i=0; i<nvals; i++){ |
| 372 |
if (entry[i] != 0){ |
| 373 |
graph_self_edges_.insert( rowmap.GID(i)); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
void |
| 380 |
CostDescriber::setHypergraphEdgeWeights(Teuchos::RCP<const Epetra_Vector> hgewts) |
| 381 |
{ |
| 382 |
free_hg_edge_weights_(); |
| 383 |
const Epetra_BlockMap& map = hgewts->Map(); |
| 384 |
|
| 385 |
int numWeights = map.NumMyElements(); |
| 386 |
|
| 387 |
if (numWeights > 0){ |
| 388 |
allocate_hg_edge_weights_(numWeights); |
| 389 |
map.MyGlobalElements(hg_edge_gids_); |
| 390 |
double *v; |
| 391 |
int stride; |
| 392 |
hgewts->ExtractView(&v, &stride); |
| 393 |
for (int i=0; i<numWeights; i++){ |
| 394 |
hg_edge_weights_[i] = (float)v[i]; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
void |
| 400 |
CostDescriber::setHypergraphEdgeWeights(int numHGedges, const int *hgGIDs, const float *hgEwgts) |
| 401 |
{ |
| 402 |
free_hg_edge_weights_(); |
| 403 |
if (numHGedges > 0){ |
| 404 |
allocate_hg_edge_weights_(numHGedges); |
| 405 |
for (int i=0; i<numHGedges; i++){ |
| 406 |
hg_edge_weights_[i] = hgEwgts[i]; |
| 407 |
hg_edge_gids_[i] = hgGIDs[i]; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
void |
| 413 |
CostDescriber::setHypergraphEdgeWeights(int numHGedges, const int *hgGIDs, const double *hgEwgts) |
| 414 |
{ |
| 415 |
free_hg_edge_weights_(); |
| 416 |
if (numHGedges > 0){ |
| 417 |
allocate_hg_edge_weights_(numHGedges); |
| 418 |
for (int i=0; i<numHGedges; i++){ |
| 419 |
hg_edge_weights_[i] = (float)hgEwgts[i]; |
| 420 |
hg_edge_gids_[i] = hgGIDs[i]; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
bool CostDescriber::haveVertexWeights() const |
| 426 |
{ |
| 427 |
const int n = getNumVertices(); |
| 428 |
return( n > 0); |
| 429 |
} |
| 430 |
|
| 431 |
int CostDescriber::getNumVertices() const |
| 432 |
{ |
| 433 |
return( vertex_weights_.get()==0 ? 0 : vertex_weights_->MyLength() ); |
| 434 |
} |
| 435 |
|
| 436 |
int CostDescriber::getVertexWeights(std::map<int, float> &wgtMap) const |
| 437 |
{ |
| 438 |
double *wgts; |
| 439 |
|
| 440 |
if (Teuchos::is_null(vertex_weights_)){ |
| 441 |
wgtMap.clear(); |
| 442 |
return 0; |
| 443 |
} |
| 444 |
|
| 445 |
const Epetra_BlockMap& map = vertex_weights_->Map(); |
| 446 |
const int length = map.NumMyElements(); |
| 447 |
|
| 448 |
if (length < 1) return 0; |
| 449 |
|
| 450 |
int *global_ids = map.MyGlobalElements(); |
| 451 |
vertex_weights_->ExtractView(&wgts); |
| 452 |
|
| 453 |
for(int i=0; i<length; ++i) { |
| 454 |
wgtMap[global_ids[i]] = (float)wgts[i]; |
| 455 |
} |
| 456 |
return length; |
| 457 |
} |
| 458 |
|
| 459 |
void CostDescriber::getVertexWeights(int numVertices, |
| 460 |
int* global_ids, |
| 461 |
float* weights) const |
| 462 |
{ |
| 463 |
if (getNumVertices() == 0){ |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
const Epetra_BlockMap& map = vertex_weights_->Map(); |
| 468 |
|
| 469 |
if (numVertices != map.NumMyElements()) { |
| 470 |
throw Isorropia::Exception("CostDescriber::getVertexWeights: wrong numVertices"); |
| 471 |
} |
| 472 |
|
| 473 |
map.MyGlobalElements(global_ids); |
| 474 |
|
| 475 |
double* vals = vertex_weights_->Values(); |
| 476 |
for(int i=0; i<numVertices; ++i) { |
| 477 |
weights[i] = vals[i]; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
bool CostDescriber::haveGraphEdgeWeights() const |
| 482 |
{ |
| 483 |
int n = 0; |
| 484 |
if (graph_edge_weights_.get()){ |
| 485 |
n = graph_edge_weights_->NumMyNonzeros(); |
| 486 |
} |
| 487 |
return( n > 0); |
| 488 |
} |
| 489 |
|
| 490 |
int CostDescriber::getNumGraphEdges(int vertex_global_id) const |
| 491 |
{ |
| 492 |
int n = 0; |
| 493 |
if (graph_edge_weights_.get() != 0) { |
| 494 |
int lrid = graph_edge_weights_->LRID(vertex_global_id); |
| 495 |
if (lrid >= 0){ |
| 496 |
n = graph_edge_weights_->NumMyEntries(lrid); |
| 497 |
|
| 498 |
if (graph_self_edges_.size() > 0){ |
| 499 |
std::set<int>::const_iterator it = graph_self_edges_.find(vertex_global_id); |
| 500 |
if (it != graph_self_edges_.end()){ |
| 501 |
n--; // don't count self edges |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return n; |
| 509 |
} |
| 510 |
|
| 511 |
// ONLY called if graph_self_edge_ is not null |
| 512 |
|
| 513 |
int CostDescriber::getEdges(int vertexGID, int len, int *nborGID, float *weights) const |
| 514 |
{ |
| 515 |
const Epetra_Map &colmap = graph_edge_weights_->ColMap(); |
| 516 |
const Epetra_Map &rowmap = graph_edge_weights_->RowMap(); |
| 517 |
|
| 518 |
int vertexLID = rowmap.LID(vertexGID); |
| 519 |
int numRealEdges = getNumGraphEdges(vertexGID); //excluding self edges |
| 520 |
|
| 521 |
if (numRealEdges < 1){ |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
if (len < numRealEdges ){ |
| 526 |
throw Isorropia::Exception("CostDescriber::getEdges: length of allocated arrays"); |
| 527 |
} |
| 528 |
|
| 529 |
int self_edge = 0; |
| 530 |
std::set<int>::const_iterator it = graph_self_edges_.find(vertexGID); |
| 531 |
if (it != graph_self_edges_.end()){ |
| 532 |
self_edge = 1; |
| 533 |
} |
| 534 |
|
| 535 |
int *viewIds=NULL; |
| 536 |
double *viewWgts=NULL; |
| 537 |
int numedges; // including self edges |
| 538 |
|
| 539 |
int rc = graph_edge_weights_->ExtractMyRowView(vertexLID, numedges, |
| 540 |
viewWgts, viewIds); |
| 541 |
|
| 542 |
if (rc){ |
| 543 |
throw Isorropia::Exception("CostDescriber::getEdges: Extract matrix row view"); |
| 544 |
} |
| 545 |
|
| 546 |
if (numedges != (numRealEdges + self_edge)){ |
| 547 |
throw Isorropia::Exception("CostDescriber::getEdges: Extract matrix count"); |
| 548 |
} |
| 549 |
|
| 550 |
int nextID = 0; |
| 551 |
|
| 552 |
for (int j=0; j < numedges; j++){ |
| 553 |
int gid = colmap.GID(viewIds[j]); |
| 554 |
if (gid == vertexGID) continue; // skip the self edges |
| 555 |
|
| 556 |
nborGID[nextID] = gid; |
| 557 |
weights[nextID] = (float)viewWgts[j]; |
| 558 |
|
| 559 |
nextID++; |
| 560 |
} |
| 561 |
|
| 562 |
return nextID; |
| 563 |
} |
| 564 |
|
| 565 |
int CostDescriber::getGraphEdgeVertices(std::set<int> &gids) const |
| 566 |
{ |
| 567 |
gids.clear(); |
| 568 |
int ngids = 0; |
| 569 |
|
| 570 |
if (haveGraphEdgeWeights()){ |
| 571 |
const Epetra_Map &rowmap = graph_edge_weights_->RowMap(); |
| 572 |
ngids = rowmap.NumMyElements(); |
| 573 |
for (int i=0; i<ngids; i++){ |
| 574 |
gids.insert(rowmap.GID(i)); |
| 575 |
} |
| 576 |
} |
| 577 |
return ngids; |
| 578 |
} |
| 579 |
|
| 580 |
int CostDescriber::getGraphEdgeWeights(int vertex_global_id, std::map<int, float> &wgtMap) const |
| 581 |
{ |
| 582 |
int rowlen = getNumGraphEdges(vertex_global_id); |
| 583 |
|
| 584 |
if (rowlen < 1){ |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
float *wgt = new float [rowlen]; |
| 589 |
int *nborGID = new int [rowlen]; |
| 590 |
|
| 591 |
int numEdges = getEdges(vertex_global_id, rowlen, nborGID, wgt); |
| 592 |
|
| 593 |
for (int i=0; i<numEdges; i++){ |
| 594 |
wgtMap[nborGID[i]] = wgt[i]; |
| 595 |
} |
| 596 |
|
| 597 |
if (rowlen > 0){ |
| 598 |
delete [] nborGID; |
| 599 |
delete [] wgt; |
| 600 |
} |
| 601 |
return numEdges; |
| 602 |
} |
| 603 |
|
| 604 |
void CostDescriber::getGraphEdgeWeights(int vertex_global_id, |
| 605 |
int num_neighbors, |
| 606 |
int* neighbor_global_ids, |
| 607 |
float* weights) const |
| 608 |
{ |
| 609 |
int rowlen = getNumGraphEdges(vertex_global_id); |
| 610 |
|
| 611 |
if (rowlen < 1){ |
| 612 |
return; |
| 613 |
} |
| 614 |
|
| 615 |
if (rowlen > num_neighbors) { |
| 616 |
throw Isorropia::Exception("CostDescriber::getGraphEdgeWeights: wrong num_neighbors"); |
| 617 |
} |
| 618 |
|
| 619 |
getEdges(vertex_global_id, num_neighbors, neighbor_global_ids, weights); |
| 620 |
} |
| 621 |
|
| 622 |
bool CostDescriber::haveHypergraphEdgeWeights() const |
| 623 |
{ |
| 624 |
return(num_hg_edge_weights_ > 0); |
| 625 |
} |
| 626 |
|
| 627 |
int CostDescriber::getNumHypergraphEdgeWeights() const |
| 628 |
{ |
| 629 |
return num_hg_edge_weights_; |
| 630 |
} |
| 631 |
|
| 632 |
void CostDescriber::getHypergraphEdgeWeights(int numEdges, |
| 633 |
int* global_ids, |
| 634 |
float* weights) const |
| 635 |
{ |
| 636 |
if (numEdges != num_hg_edge_weights_) { |
| 637 |
throw Isorropia::Exception("CostDescriber::getHypergraphEdgeWeights: wrong numEdges"); |
| 638 |
} |
| 639 |
|
| 640 |
for(int i=0; i<numEdges; ++i) { |
| 641 |
weights[i] = hg_edge_weights_[i]; |
| 642 |
global_ids[i] = hg_edge_gids_[i]; |
| 643 |
} |
| 644 |
} |
| 645 |
int CostDescriber::getHypergraphEdgeWeights(std::map<int, float> &wgtMap) const |
| 646 |
{ |
| 647 |
int nEdges = num_hg_edge_weights_; |
| 648 |
if (nEdges < 1) return 0; |
| 649 |
|
| 650 |
for(int i=0; i<nEdges; ++i) { |
| 651 |
wgtMap[hg_edge_gids_[i]] = hg_edge_weights_[i]; |
| 652 |
} |
| 653 |
return nEdges; |
| 654 |
} |
| 655 |
|
| 656 |
void CostDescriber::getCosts(std::map<int, float> &vertexWeights, |
| 657 |
std::map<int, std::map<int, float > > &graphEdgeWeights, |
| 658 |
std::map<int, float> &hypergraphEdgeWeights) const |
| 659 |
{ |
| 660 |
if (haveVertexWeights()){ |
| 661 |
getVertexWeights(vertexWeights); |
| 662 |
} |
| 663 |
|
| 664 |
if (haveHypergraphEdgeWeights()){ |
| 665 |
getHypergraphEdgeWeights(hypergraphEdgeWeights); |
| 666 |
} |
| 667 |
|
| 668 |
if (haveGraphEdgeWeights()){ |
| 669 |
std::set<int> vgids; |
| 670 |
// int ngids = getGraphEdgeVertices(vgids); |
| 671 |
std::set<int>::iterator curr; |
| 672 |
std::set<int>::iterator end = vgids.end(); |
| 673 |
curr = vgids.begin(); |
| 674 |
while (curr != end){ |
| 675 |
std::map<int, float> nborMap; |
| 676 |
getGraphEdgeWeights(*curr, nborMap); |
| 677 |
graphEdgeWeights[*curr] = nborMap; |
| 678 |
curr++; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
bool CostDescriber::haveGlobalVertexWeights() const |
| 684 |
{ |
| 685 |
return (numGlobalVertexWeights_ > 0); |
| 686 |
} |
| 687 |
void CostDescriber::setNumGlobalVertexWeights(int num) |
| 688 |
{ |
| 689 |
numGlobalVertexWeights_ = num; |
| 690 |
} |
| 691 |
bool CostDescriber::haveGlobalGraphEdgeWeights() const |
| 692 |
{ |
| 693 |
return (numGlobalGraphEdgeWeights_ > 0); |
| 694 |
} |
| 695 |
void CostDescriber::setNumGlobalGraphEdgeWeights(int num) |
| 696 |
{ |
| 697 |
numGlobalGraphEdgeWeights_ = num; |
| 698 |
} |
| 699 |
bool CostDescriber::haveGlobalHypergraphEdgeWeights() const |
| 700 |
{ |
| 701 |
return (numGlobalHypergraphEdgeWeights_ > 0); |
| 702 |
} |
| 703 |
void CostDescriber::setNumGlobalHypergraphEdgeWeights(int num) |
| 704 |
{ |
| 705 |
numGlobalHypergraphEdgeWeights_ = num; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
void CostDescriber::allocate_hg_edge_weights_(int n) |
| 711 |
{ |
| 712 |
free_hg_edge_weights_(); |
| 713 |
if (n > 0){ |
| 714 |
hg_edge_gids_ = new int [n]; |
| 715 |
hg_edge_weights_ = new float [n]; |
| 716 |
num_hg_edge_weights_ = n; |
| 717 |
} |
| 718 |
} |
| 719 |
void CostDescriber::free_hg_edge_weights_() |
| 720 |
{ |
| 721 |
if (hg_edge_gids_){ |
| 722 |
delete [] hg_edge_gids_; |
| 723 |
delete [] hg_edge_weights_; |
| 724 |
hg_edge_gids_ = NULL; |
| 725 |
hg_edge_weights_ = NULL; |
| 726 |
num_hg_edge_weights_ = 0; |
| 727 |
} |
| 728 |
} |
| 729 |
void CostDescriber::show_cd(std::ostream &os) const |
| 730 |
{ |
| 731 |
int nv = getNumVertices(); |
| 732 |
int nhge = getNumHypergraphEdgeWeights(); |
| 733 |
|
| 734 |
int *gids = NULL; |
| 735 |
if (nv){ |
| 736 |
os << "Vertices and weights" << std::endl << " "; |
| 737 |
gids = new int [nv]; |
| 738 |
float *w = new float [nv]; |
| 739 |
|
| 740 |
getVertexWeights(nv, gids, w); |
| 741 |
for (int j=0; j<nv; j++){ |
| 742 |
os << gids[j] << " (" << w[j] << ") "; |
| 743 |
} |
| 744 |
os << std::endl; |
| 745 |
delete [] w; |
| 746 |
} |
| 747 |
else{ |
| 748 |
os << "No vertex weights" << std::endl; |
| 749 |
} |
| 750 |
if (gids && haveGraphEdgeWeights()){ |
| 751 |
os << "Graph edge (non zero) weights for each vertex (row)" << std::endl; |
| 752 |
for (int i=0; i < nv; i++){ |
| 753 |
int vid = gids[i]; |
| 754 |
std::map<int, float> wgts; |
| 755 |
|
| 756 |
getGraphEdgeWeights(vid, wgts); |
| 757 |
|
| 758 |
os << " Vertex (row) GID " << vid << std::endl << " "; |
| 759 |
std::map<int, float>::iterator curr; |
| 760 |
|
| 761 |
for(curr = wgts.begin(); curr != wgts.end(); curr++){ |
| 762 |
os << curr->first << " (" << curr->second << ") "; |
| 763 |
} |
| 764 |
os << std::endl; |
| 765 |
} |
| 766 |
} |
| 767 |
else{ |
| 768 |
os << "No graph edge weights" << std::endl; |
| 769 |
} |
| 770 |
if (nhge){ |
| 771 |
int *colgids = new int [nhge]; |
| 772 |
float *wgts = new float [nhge]; |
| 773 |
|
| 774 |
getHypergraphEdgeWeights(nhge, colgids, wgts); |
| 775 |
|
| 776 |
os << "Hypergraph Edge (column) weights" << std::endl << " "; |
| 777 |
|
| 778 |
for (int j=0; j < nhge; j++){ |
| 779 |
os << colgids[j] << " (" << wgts[j] << ") "; |
| 780 |
} |
| 781 |
os << std::endl; |
| 782 |
|
| 783 |
delete [] colgids; |
| 784 |
delete [] wgts; |
| 785 |
} |
| 786 |
else{ |
| 787 |
os << "No hypergraph edge weights" << std::endl; |
| 788 |
} |
| 789 |
|
| 790 |
if (gids) delete [] gids; |
| 791 |
|
| 792 |
nv = numGlobalVertexWeights_; |
| 793 |
int nge = numGlobalGraphEdgeWeights_; |
| 794 |
nhge = numGlobalHypergraphEdgeWeights_; |
| 795 |
|
| 796 |
if (paramlist_.begin() == paramlist_.end()){ |
| 797 |
os << "No parameters set" << std::endl; |
| 798 |
} |
| 799 |
else{ |
| 800 |
os << "Have some parameters set" << std::endl; |
| 801 |
} |
| 802 |
|
| 803 |
if (haveGlobalVertexWeights()){ |
| 804 |
os << "Number of global vertices " << nv << std::endl; |
| 805 |
} |
| 806 |
else{ |
| 807 |
os << "Don't know number of global vertices " << std::endl; |
| 808 |
} |
| 809 |
|
| 810 |
if (haveGlobalGraphEdgeWeights()){ |
| 811 |
os << "Number of global graph edge weights " << nge << std::endl; |
| 812 |
} |
| 813 |
else{ |
| 814 |
os << "Don't know number of global graph edge weights " << std::endl; |
| 815 |
} |
| 816 |
|
| 817 |
if (haveGlobalHypergraphEdgeWeights()){ |
| 818 |
os << "Number of global hypergraph edge weights " << nhge << std::endl; |
| 819 |
} |
| 820 |
else{ |
| 821 |
os << "Don't know number of global hypergraph edge weights " << std::endl; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
std::ostream& operator <<(std::ostream& os, const Isorropia::Epetra::CostDescriber &cd) |
| 826 |
{ |
| 827 |
cd.show_cd(os); |
| 828 |
return os; |
| 829 |
} |
| 830 |
|
| 831 |
}//namespace Epetra |
| 832 |
}//namespace Isorropia |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
#endif |
| 837 |
|