94#ifndef ZOLTAN2_EVALUATEPARTITION_HPP
95#define ZOLTAN2_EVALUATEPARTITION_HPP
113template <
typename Adapter>
118 typedef typename Adapter::base_adapter_t base_adapter_t;
119 typedef typename Adapter::lno_t lno_t;
120 typedef typename Adapter::part_t part_t;
121 typedef typename Adapter::scalar_t scalar_t;
123 typedef StridedData<lno_t, scalar_t> input_t;
125 part_t numGlobalParts_;
126 part_t targetGlobalParts_;
129 typedef BaseClassMetrics<scalar_t> base_metric_type;
130 typedef ArrayRCP<RCP<base_metric_type> > base_metric_array_type;
131 base_metric_array_type metricsBase_;
136 const RCP<
const Comm<int> > &problemComm,
137 const PartitioningSolution<Adapter> *soln,
139 <
typename Adapter::base_adapter_t> > &graphModel);
157 const RCP<
const Comm<int> > &problemComm,
158 const PartitioningSolution<Adapter> *soln,
160 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
162 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_() {
169 const RCP<const Environment> &_env,
170 const RCP<
const Comm<int> > &_problemComm,
171 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &_graph,
172 const ArrayView<const typename Adapter::part_t> &_partArray,
173 typename Adapter::part_t &_numGlobalParts,
174 ArrayRCP<RCP<BaseClassMetrics<typename Adapter::scalar_t> > > &_metricsBase,
175 ArrayRCP<typename Adapter::scalar_t> &_globalSums) {
176 globalWeightedByPart <Adapter>(_env, _problemComm, _graph,
177 _partArray, _numGlobalParts, _metricsBase, _globalSums);
193 const PartitioningSolution<Adapter> *soln,
194 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
196 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
198 Teuchos::RCP<const Comm<int> > problemComm = Tpetra::getDefaultComm();
215 const RCP<
const Comm<int> > &problemComm,
216 const PartitioningSolution<Adapter> *soln,
217 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
219 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
224#ifdef HAVE_ZOLTAN2_MPI
238 const PartitioningSolution<Adapter> *soln,
239 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &graphModel=
241 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
243 RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
244 Teuchos::opaqueWrapper(comm);
245 RCP<const Comm<int> > problemComm =
246 rcp<const Comm<int> >(
new Teuchos::MpiComm<int>(wrapper));
259 std::string metricType)
const {
263 int sizeOfArrayView = 0;
264 for(
auto n = 0; n < metricsBase_.size(); ++n) {
265 if( metricsBase_[n]->getMetricType() == metricType ) {
266 if (beginIndex == -1) {
272 if (sizeOfArrayView == 0) {
273 return ArrayView<RCP<base_metric_type> >();
275 return metricsBase_.view(beginIndex, sizeOfArrayView);
282 if( metrics.size() <= 0 ) {
283 throw std::logic_error(
"getObjectCountImbalance() was called "
284 "but no metrics data was generated for " +
287 return metrics[0]->getMetricValue(
"maximum imbalance");
298 if( metrics.size() <= 0 ) {
299 throw std::logic_error(
"getNormedImbalance() was called "
300 "but no metrics data was generated for " +
303 if( metrics.size() <= 1 ) {
304 throw std::logic_error(
"getNormedImbalance() was called "
305 "but the normed data does not exist." );
307 return metrics[1]->getMetricValue(
"maximum imbalance");
329 int weight0IndexStartsAtThisArrayIndex = ( metrics.size() > 2 ) ? 2 : 1;
330 int numberOfWeights = metrics.size() - weight0IndexStartsAtThisArrayIndex;
331 int indexInArray = weight0IndexStartsAtThisArrayIndex + weightIndex;
332 if( metrics.size() <= indexInArray ) {
333 throw std::logic_error(
"getWeightImbalance was called with weight index "+
334 std::to_string(weightIndex) +
335 " but the maximum weight available for " +
337 " is weight " + std::to_string(numberOfWeights-1) +
340 return metrics[indexInArray]->getMetricValue(
"maximum imbalance");
347 if( graphMetrics.size() < 1 ) {
348 throw std::logic_error(
"getMaxEdgeCut() was called "
349 "but no metrics data was generated for " +
352 return graphMetrics[0]->getMetricValue(
"global maximum");
359 int indexInArray = weightIndex + 1;
360 if( graphMetrics.size() <= 1 ) {
361 throw std::logic_error(
"getMaxWeightEdgeCut was called with "
362 "weight index " + std::to_string(weightIndex) +
363 " but no weights were available for " +
366 else if( graphMetrics.size() <= indexInArray ) {
370 throw std::logic_error(
"getMaxWeightEdgeCut was called with "
371 "weight index " + std::to_string(weightIndex) +
372 " but the maximum weight available for " +
375 std::to_string(graphMetrics.size() - 2) +
"." );
377 return graphMetrics[indexInArray]->getMetricValue(
"global maximum");
384 if( graphMetrics.size() < 1 ) {
385 throw std::logic_error(
"getTotalEdgeCut() was called but no metrics "
386 "data was generated for " +
389 return graphMetrics[0]->getMetricValue(
"global sum");
396 int indexInArray = weightIndex + 1;
397 if( graphMetrics.size() <= 1 ) {
401 throw std::logic_error(
"getTotalWeightEdgeCut was called with "
402 "weight index " + std::to_string(weightIndex) +
403 " but no weights were available for " +
406 else if( graphMetrics.size() <= indexInArray ) {
407 throw std::logic_error(
"getTotalWeightEdgeCut was called with "
408 "weight index " + std::to_string(weightIndex) +
409 " but the maximum weight available for " +
412 std::to_string(graphMetrics.size() - 2) +
"." );
414 return graphMetrics[indexInArray]->getMetricValue(
"global sum");
421 if( graphMetrics.size() < 1 ) {
422 throw std::logic_error(
"getTotalMessages() was called but no metrics "
423 "data was generated for " +
427 return graphMetrics[1]->getMetricValue(
"global sum");
435 if( graphMetrics.size() < 1 ) {
436 throw std::logic_error(
"getMaxMessages() was called but no metrics "
437 "data was generated for " +
441 return graphMetrics[1]->getMetricValue(
"global maximum");
450 ArrayView<RCP<base_metric_type>> graphMetrics =
452 if (graphMetrics.size() != 0) {
453 Zoltan2::printGraphMetrics<scalar_t, part_t>(os, targetGlobalParts_,
454 numGlobalParts_, graphMetrics);
460 Zoltan2::printImbalanceMetrics<scalar_t, part_t>(os, targetGlobalParts_,
467template <
typename Adapter>
471 const RCP<
const Comm<int> > &comm,
472 const PartitioningSolution<Adapter> *soln,
473 const RCP<
const GraphModel<typename Adapter::base_adapter_t> > &graphModel
476 RCP<const Comm<int> > problemComm;
477 if (comm == Teuchos::null) {
478 problemComm = Tpetra::getDefaultComm();
483 RCP<Environment> env;
490 env->debug(DETAILED_STATUS, std::string(
"Entering EvaluatePartition"));
491 env->timerStart(MACRO_TIMERS,
"Computing metrics");
494 size_t numLocalObjects = ia->getLocalNumIDs();
495 ArrayRCP<const part_t> parts;
499 parts = arcp(soln->getPartListView(), 0, numLocalObjects,
false);
500 env->localInputAssertion(__FILE__, __LINE__,
"parts not set",
505 const part_t *tmp = NULL;
506 ia->getPartsView(tmp);
508 parts = arcp(tmp, 0, numLocalObjects,
false);
511 part_t *procs =
new part_t[numLocalObjects];
512 for (
size_t i=0;i<numLocalObjects;i++) procs[i]=problemComm->getRank();
513 parts = arcp(procs, 0, numLocalObjects,
true);
516 ArrayView<const part_t> partArray = parts(0, numLocalObjects);
523 const Teuchos::ParameterEntry *pe = p->getEntryPtr(
"partitioning_objective");
525 std::string strChoice = pe->getValue<std::string>(&strChoice);
526 if (strChoice == std::string(
"multicriteria_minimize_total_weight"))
528 else if (strChoice == std::string(
"multicriteria_minimize_maximum_weight"))
532 const RCP<const base_adapter_t> bia =
533 rcp(
dynamic_cast<const base_adapter_t *
>(ia),
false);
536 imbalanceMetrics<Adapter>(env, problemComm, mcnorm, ia, soln, partArray,
538 numGlobalParts_, numNonEmpty_, metricsBase_);
543 targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
545 targetGlobalParts_ = problemComm->getSize();
547 env->timerStop(MACRO_TIMERS,
"Computing metrics");
554 env->timerStart(MACRO_TIMERS,
"Computing graph metrics");
559 std::bitset<NUM_MODEL_FLAGS> modelFlags;
563 RCP<const GraphModel<base_adapter_t> > graph = graphModel;
564 if (graphModel == Teuchos::null) {
565 graph = rcp(
new GraphModel<base_adapter_t>(bia, env, problemComm,
570 ArrayRCP<scalar_t> globalSums;
572 this->calculate_graph_metrics(env, problemComm, graph, partArray,
573 numGlobalParts_, metricsBase_, globalSums);
577 env->timerStop(MACRO_TIMERS,
"Computing graph metrics");
580 env->debug(DETAILED_STATUS, std::string(
"Exiting EvaluatePartition"));
Base class for the EvaluatePartition and EvaluateOrdering classes.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
#define GRAPH_METRICS_TYPE_NAME
#define IMBALANCE_METRICS_TYPE_NAME
Defines the PartitioningSolution class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
A class that computes and returns quality metrics.
scalar_t getMaxWeightEdgeCut(int weightIndex) const
getMaxWeightEdgeCuts weighted for the specified index
scalar_t getNormedImbalance() const
Return the object normed weight imbalance. Normed imbalance is only valid if there is at least 2 elem...
void printMetrics(std::ostream &os) const
Print all metrics.
EvaluatePartition(const Adapter *ia, ParameterList *p, const RCP< const Comm< int > > &problemComm, const PartitioningSolution< Adapter > *soln, bool force_evaluate, const RCP< const GraphModel< typename Adapter::base_adapter_t > > &graphModel=Teuchos::null)
Constructor where communicator is Teuchos default, and takes another parameter whether to evaluate me...
scalar_t getTotalMessages() const
getTotalMessages
scalar_t getWeightImbalance(int weightIndex) const
Return the imbalance for the requested weight.
scalar_t getMaxEdgeCut() const
Return the max cut for the requested weight.
ArrayView< RCP< base_metric_type > > getAllMetricsOfType(std::string metricType) const
Return the metric list for types matching the given metric type.
scalar_t getObjectCountImbalance() const
Return the object count imbalance.
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)
Constructor where Teuchos communicator is specified.
scalar_t getTotalWeightEdgeCut(int weightIndex) const
getTotalWeightEdgeCut weighted for the specified index
scalar_t getTotalEdgeCut() const
getTotalEdgeCut
EvaluatePartition(const Adapter *ia, ParameterList *p, const PartitioningSolution< Adapter > *soln, const RCP< const GraphModel< typename Adapter::base_adapter_t > > &graphModel=Teuchos::null)
Constructor where communicator is Teuchos default.
virtual ~EvaluatePartition()
virtual void calculate_graph_metrics(const RCP< const Environment > &_env, const RCP< const Comm< int > > &_problemComm, const RCP< const GraphModel< typename Adapter::base_adapter_t > > &_graph, const ArrayView< const typename Adapter::part_t > &_partArray, typename Adapter::part_t &_numGlobalParts, ArrayRCP< RCP< BaseClassMetrics< typename Adapter::scalar_t > > > &_metricsBase, ArrayRCP< typename Adapter::scalar_t > &_globalSums)
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)
scalar_t getMaxMessages() const
getMaxMessages
GraphModel defines the interface required for graph models.
Created by mbenlioglu on Aug 31, 2020.
void imbalanceMetrics(const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, multiCriteriaNorm mcNorm, const Adapter *ia, const PartitioningSolution< Adapter > *solution, const ArrayView< const typename Adapter::part_t > &partArray, const RCP< const GraphModel< typename Adapter::base_adapter_t > > &graphModel, typename Adapter::part_t &numExistingParts, typename Adapter::part_t &numNonemptyParts, ArrayRCP< RCP< BaseClassMetrics< typename Adapter::scalar_t > > > &metrics)
Compute imbalance metrics for a distribution.
@ BASIC_ASSERTION
fast typical checks for valid arguments
BaseAdapterType
An enum to identify general types of adapters.
@ GraphAdapterType
graph data
@ MatrixAdapterType
matrix data
@ MeshAdapterType
mesh data
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
@ normBalanceTotalMaximum
@ normMinimizeTotalWeight
@ normMinimizeMaximumWeight