/usr/include/trilinos/Zoltan2_EvaluatePartition.hpp is in libtrilinos-zoltan2-dev 12.10.1-3.
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 | // @HEADER
//
// ***********************************************************************
//
// Zoltan2: A package of combinatorial algorithms for scientific computing
// Copyright 2012 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 Karen Devine (kddevin@sandia.gov)
// Erik Boman (egboman@sandia.gov)
// Siva Rajamanickam (srajama@sandia.gov)
//
// ***********************************************************************
//
// @HEADER
// @HEADER
//
// ***********************************************************************
//
// Zoltan2: A package of combinatorial algorithms for scientific computing
// Copyright 2012 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 Karen Devine (kddevin@sandia.gov)
// Erik Boman (egboman@sandia.gov)
// Siva Rajamanickam (srajama@sandia.gov)
//
// ***********************************************************************
//
// @HEADER
/*! \file Zoltan2_EvaluatePartition.hpp
* \brief Defines the EvaluatePartition class.
*/
#ifndef ZOLTAN2_EVALUATEPARTITION_HPP
#define ZOLTAN2_EVALUATEPARTITION_HPP
#include <Zoltan2_Metric.hpp>
#include <Zoltan2_PartitioningSolution.hpp>
namespace Zoltan2{
/*! \brief A class that computes and returns quality metrics.
* \todo For some problems it will be necessary to build the
* Model again in order to compute metrics. For now
* we don't have any problems like that.
\todo write a unit test for this class
*/
template <typename Adapter>
class EvaluatePartition {
private:
typedef typename Adapter::base_adapter_t base_adapter_t;
typedef typename Adapter::lno_t lno_t;
typedef typename Adapter::part_t part_t;
typedef typename Adapter::scalar_t scalar_t;
typedef StridedData<lno_t, scalar_t> input_t;
part_t numGlobalParts_; // desired
part_t targetGlobalParts_; // actual
part_t numNonEmpty_; // of actual
typedef BaseClassMetrics<scalar_t> base_metric_type;
typedef ArrayRCP<RCP<base_metric_type> > base_metric_array_type;
base_metric_array_type metricsBase_;
void sharedConstructor(const Adapter *ia,
ParameterList *p,
const RCP<const Comm<int> > &problemComm,
const PartitioningSolution<Adapter> *soln,
const RCP<const GraphModel
<typename Adapter::base_adapter_t> > &graphModel);
public:
/*! \brief Constructor where communicator is Teuchos default.
\param ia the problem input adapter
\param p the parameter list
\param soln the solution
\param graphModel the graph model
The constructor does global communication to compute the metrics.
The rest of the methods are local.
*/
EvaluatePartition(
const Adapter *ia,
ParameterList *p,
const PartitioningSolution<Adapter> *soln,
const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
Teuchos::null):
numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
{
RCP<const Comm<int> > problemComm = DefaultComm<int>::getComm();
sharedConstructor(ia, p, problemComm, soln, graphModel);
}
/*! \brief Constructor where Teuchos communicator is specified
\param ia the problem input adapter
\param p the parameter list
\param problemComm the problem communicator
\param soln the solution
\param graphModel the graph model
The constructor does global communication to compute the metrics.
The rest of the methods are local.
*/
EvaluatePartition(
const Adapter *ia,
ParameterList *p,
const RCP<const Comm<int> > &problemComm,
const PartitioningSolution<Adapter> *soln,
const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
Teuchos::null):
numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
{
sharedConstructor(ia, p, problemComm, soln, graphModel);
}
#ifdef HAVE_ZOLTAN2_MPI
/*! \brief Constructor for MPI builds
\param ia the problem input adapter
\param p the parameter list
\param comm the problem communicator
\param soln the solution
\param graphModel the graph model
The constructor does global communication to compute the metrics.
The rest of the methods are local.
*/
EvaluatePartition(
const Adapter *ia,
ParameterList *p,
MPI_Comm comm,
const PartitioningSolution<Adapter> *soln,
const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
Teuchos::null):
numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
{
RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
Teuchos::opaqueWrapper(comm);
RCP<const Comm<int> > problemComm =
rcp<const Comm<int> >(new Teuchos::MpiComm<int>(wrapper));
sharedConstructor(ia, p, problemComm, soln, graphModel);
}
#endif
/*! \brief Return the full metric list which will be mixed types.
*/
base_metric_array_type getAllMetrics() const {
return metricsBase_;
}
/*! \get the unique class names which are actually used
*/
std::vector<std::string> getMetricTypes() const {
std::set<std::string> temporarySet;
std::vector<std::string> returnVector; // Michel did this because he wants
// set behavior but preserve the
// ordering and a vector is
// convenient later since we may
// also be looking at the vector
// set of all possible names from
// BaseClassMetrics
// other option is to use only types
// that are currently being used
for( int n = 0; n < metricsBase_.size(); ++n ) {
std::string checkName = metricsBase_[n]->getMetricType();
if (temporarySet.find(checkName) == temporarySet.end()) {
temporarySet.insert(checkName);
returnVector.push_back(checkName);
}
}
return returnVector;
}
/*! \brief Return the metric list for types matching the given metric type.
*/
ArrayView<RCP<base_metric_type> > getAllMetricsOfType(std::string metricType) const
{
// find the beginning and the end of the contiguous block
// the list is an ArrayRCP and must preserve any ordering
int beginIndex = -1;
int sizeOfArrayView = 0;
for(typename base_metric_array_type::size_type n = 0;
n < metricsBase_.size(); ++n) {
if( metricsBase_[n]->getMetricType() == metricType ) {
if (beginIndex == -1) {
beginIndex = int(n);
}
++sizeOfArrayView;
}
}
if (sizeOfArrayView == 0) {
return ArrayView<RCP<base_metric_type> >(); // empty array view
}
return metricsBase_.view(beginIndex, sizeOfArrayView);
}
/*! \brief Return the object count imbalance.
*/
scalar_t getObjectCountImbalance() const {
ArrayView<RCP<base_metric_type> > metrics =
getAllMetricsOfType(IMBALANCE_METRICS_TYPE_NAME);
if( metrics.size() <= 0 ) {
throw std::logic_error("getObjectCountImbalance() was called "
"but no metrics data was generated for " +
std::string(IMBALANCE_METRICS_TYPE_NAME) + "." );
}
return metrics[0]->getMetricValue("maximum imbalance");
}
/*! \brief Return the object normed weight imbalance.
* Normed imbalance is only valid if there is at least 2 elements -
* the second one is the normed imbalance.
* If we have weights (which start at the second element) the spec is to
* have this return that element.
*/
scalar_t getNormedImbalance() const{
ArrayView<RCP<base_metric_type> > metrics =
getAllMetricsOfType(IMBALANCE_METRICS_TYPE_NAME);
if( metrics.size() <= 0 ) {
throw std::logic_error("getNormedImbalance() was called "
"but no metrics data was generated for " +
std::string(IMBALANCE_METRICS_TYPE_NAME) + "." );
}
if( metrics.size() <= 1 ) {
throw std::logic_error("getNormedImbalance() was called "
"but the normed data does not exist." );
}
return metrics[1]->getMetricValue("maximum imbalance");
}
/*! \brief Return the imbalance for the requested weight.
*/
scalar_t getWeightImbalance(int weightIndex) const {
// In this case we could have
// Option 1
// object count
// Option 2
// object count
// weight 0
// Option 3
// object count
// normed imbalance
// weight 0
// weight 1
// if we have multiple weights (meaning array size if 2 or greater) than
// the weights begin on index 2
// if we have one weight 0 (option 2) then the weights begin on index 1
ArrayView<RCP<base_metric_type> > metrics =
getAllMetricsOfType(IMBALANCE_METRICS_TYPE_NAME);
int weight0IndexStartsAtThisArrayIndex = ( metrics.size() > 2 ) ? 2 : 1;
int numberOfWeights = metrics.size() - weight0IndexStartsAtThisArrayIndex;
int indexInArray = weight0IndexStartsAtThisArrayIndex + weightIndex;
if( metrics.size() <= indexInArray ) {
throw std::logic_error("getWeightImbalance was called with weight index "+
std::to_string(weightIndex) +
" but the maximum weight available for " +
std::string(IMBALANCE_METRICS_TYPE_NAME) +
" is weight " + std::to_string(numberOfWeights-1) +
"." );
}
return metrics[indexInArray]->getMetricValue("maximum imbalance");
}
/*! \brief Return the max cut for the requested weight.
*/
scalar_t getMaxEdgeCut() const{
ArrayView<RCP<base_metric_type> > graphMetrics =
getAllMetricsOfType(GRAPH_METRICS_TYPE_NAME);
if( graphMetrics.size() < 1 ) {
throw std::logic_error("getMaxEdgeCut() was called "
"but no metrics data was generated for " +
std::string(GRAPH_METRICS_TYPE_NAME) + "." );
}
return graphMetrics[0]->getMetricValue("global maximum");
}
/*! \brief getMaxWeightEdgeCuts weighted for the specified index
*/
scalar_t getMaxWeightEdgeCut(int weightIndex) const{
ArrayView<RCP<base_metric_type> > graphMetrics =
getAllMetricsOfType(GRAPH_METRICS_TYPE_NAME);
int indexInArray = weightIndex + 1; // changed this - it used to start at 0
if( graphMetrics.size() <= 1 ) {
throw std::logic_error("getMaxWeightEdgeCut was called with "
"weight index " + std::to_string(weightIndex) +
" but no weights were available for " +
std::string(GRAPH_METRICS_TYPE_NAME) + "." );
}
else if( graphMetrics.size() <= indexInArray ) {
// the size() - 2 is because weight 0 starts at array element 1
// (so if the array size is 2, the maximum specified weight index is
// weight 0 ( 2-2 = 0 )
throw std::logic_error("getMaxWeightEdgeCut was called with "
"weight index " + std::to_string(weightIndex) +
" but the maximum weight available for " +
std::string(GRAPH_METRICS_TYPE_NAME) +
" is weight " +
std::to_string(graphMetrics.size() - 2) + "." );
}
return graphMetrics[indexInArray]->getMetricValue("global maximum");
}
/*! \brief getTotalEdgeCut
*/
scalar_t getTotalEdgeCut() const{
ArrayView<RCP<base_metric_type> > graphMetrics =
getAllMetricsOfType(GRAPH_METRICS_TYPE_NAME);
if( graphMetrics.size() < 1 ) {
throw std::logic_error("getTotalEdgeCut() was called but no metrics "
"data was generated for " +
std::string(GRAPH_METRICS_TYPE_NAME) + "." );
}
return graphMetrics[0]->getMetricValue("global sum");
}
/*! \brief getTotalWeightEdgeCut weighted for the specified index
*/
scalar_t getTotalWeightEdgeCut(int weightIndex) const{
ArrayView<RCP<base_metric_type> > graphMetrics =
getAllMetricsOfType(GRAPH_METRICS_TYPE_NAME);
int indexInArray = weightIndex + 1; // changed this; it used to start at 0
if( graphMetrics.size() <= 1 ) {
// the size() - 2 is because weight 0 starts at array element 1 (so if
// the array size is 2, the maximum specified weight index is
// weight 0 ( 2-2 = 0 )
throw std::logic_error("getTotalWeightEdgeCut was called with "
"weight index " + std::to_string(weightIndex) +
" but no weights were available for " +
std::string(GRAPH_METRICS_TYPE_NAME) + "." );
}
else if( graphMetrics.size() <= indexInArray ) {
throw std::logic_error("getTotalWeightEdgeCut was called with "
"weight index " + std::to_string(weightIndex) +
" but the maximum weight available for " +
std::string(GRAPH_METRICS_TYPE_NAME) +
" is weight " +
std::to_string(graphMetrics.size() - 2) + "." );
}
return graphMetrics[indexInArray]->getMetricValue("global sum");
}
/*! \brief Print all the metrics based on the metric object type
*/
void printMetrics(std::ostream &os, bool bIncludeUnusedTypes = true) const {
std::vector<std::string> types =
(bIncludeUnusedTypes ?
BaseClassMetrics<scalar_t>::static_allMetricNames_ :
getMetricTypes());
for( auto metricType : types ) {
printMetrics (os, metricType);
}
}
/*! \brief Print all metrics of type metricType based on the metric object type
*/
void printMetrics(std::ostream &os, std::string metricType) const {
// Might need changing. The issue here is that they each have unique
// parameters to pass.
ArrayView<RCP<base_metric_type> > metrics = getAllMetricsOfType(metricType);
if (metrics.size() != 0) {
// this could be a critical decision - do we want a blank table with
// headers when the list is empty - for debugging that is probably better
// but it's very messy to have lots of empty tables in the logs
if( metricType == GRAPH_METRICS_TYPE_NAME ) {
Zoltan2::printGraphMetrics<scalar_t, part_t>(os, targetGlobalParts_,
numGlobalParts_, metrics);
}
else if( metricType == IMBALANCE_METRICS_TYPE_NAME ) {
Zoltan2::printImbalanceMetrics<scalar_t, part_t>(os, targetGlobalParts_,
numGlobalParts_,
numNonEmpty_, metrics);
}
}
}
};
// sharedConstructor
template <typename Adapter>
void EvaluatePartition<Adapter>::sharedConstructor(
const Adapter *ia,
ParameterList *p,
const RCP<const Comm<int> > &comm,
const PartitioningSolution<Adapter> *soln,
const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graphModel
)
{
RCP<const Comm<int> > problemComm;
if (comm == Teuchos::null) {
problemComm = DefaultComm<int>::getComm();//communicator is Teuchos default
} else {
problemComm = comm;
}
RCP<Environment> env;
try{
env = rcp(new Environment(*p, problemComm));
}
Z2_FORWARD_EXCEPTIONS
env->debug(DETAILED_STATUS, std::string("Entering EvaluatePartition"));
env->timerStart(MACRO_TIMERS, "Computing metrics");
// Parts to which objects are assigned.
size_t numLocalObjects = ia->getLocalNumIDs();
ArrayRCP<const part_t> parts;
if (soln) {
// User provided a partitioning solution; use it.
parts = arcp(soln->getPartListView(), 0, numLocalObjects, false);
env->localInputAssertion(__FILE__, __LINE__, "parts not set",
((numLocalObjects == 0) || soln->getPartListView()), BASIC_ASSERTION);
} else {
// User did not provide a partitioning solution;
// Use input adapter's partition.
const part_t *tmp = NULL;
ia->getPartsView(tmp);
if (tmp != NULL)
parts = arcp(tmp, 0, numLocalObjects, false);
else {
// User has not provided input parts in input adapter
part_t *procs = new part_t[numLocalObjects];
for (size_t i=0;i<numLocalObjects;i++) procs[i]=problemComm->getRank();
parts = arcp(procs, 0, numLocalObjects, true);
}
}
ArrayView<const part_t> partArray = parts(0, numLocalObjects);
// When we add parameters for which weights to use, we
// should check those here. For now we compute metrics
// using all weights.
multiCriteriaNorm mcnorm = normBalanceTotalMaximum;
const Teuchos::ParameterEntry *pe = p->getEntryPtr("partitioning_objective");
if (pe){
std::string strChoice = pe->getValue<std::string>(&strChoice);
if (strChoice == std::string("multicriteria_minimize_total_weight"))
mcnorm = normMinimizeTotalWeight;
else if (strChoice == std::string("multicriteria_minimize_maximum_weight"))
mcnorm = normMinimizeMaximumWeight;
}
const RCP<const base_adapter_t> bia =
rcp(dynamic_cast<const base_adapter_t *>(ia), false);
try{
imbalanceMetrics<Adapter>(env, problemComm, mcnorm, ia, soln, partArray,
graphModel,
numGlobalParts_, numNonEmpty_, metricsBase_);
}
Z2_FORWARD_EXCEPTIONS;
if (soln)
targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
else
targetGlobalParts_ = problemComm->getSize();
env->timerStop(MACRO_TIMERS, "Computing metrics");
BaseAdapterType inputType = bia->adapterType();
if (inputType == GraphAdapterType ||
inputType == MatrixAdapterType ||
inputType == MeshAdapterType){
env->timerStart(MACRO_TIMERS, "Computing graph metrics");
// When we add parameters for which weights to use, we
// should check those here. For now we compute graph metrics
// using all weights.
std::bitset<NUM_MODEL_FLAGS> modelFlags;
// Create a GraphModel based on input data.
RCP<const GraphModel<base_adapter_t> > graph;
if (graphModel == Teuchos::null)
graph = rcp(new GraphModel<base_adapter_t>(bia, env, problemComm,
modelFlags));
else
graph = graphModel;
// compute weighted cuts
ArrayRCP<scalar_t> globalSums;
try {
globalWeightedCutsByPart<Adapter>(env,
problemComm, graph, partArray,
numGlobalParts_, metricsBase_,
globalSums);
}
Z2_FORWARD_EXCEPTIONS;
env->timerStop(MACRO_TIMERS, "Computing graph metrics");
}
env->debug(DETAILED_STATUS,
std::string("Exiting EvaluatePartition"));
}
} // namespace Zoltan2
#endif
|