14 #ifndef ZOLTAN2_EVALUATEPARTITION_HPP
15 #define ZOLTAN2_EVALUATEPARTITION_HPP
33 template <
typename Adapter>
41 typedef typename Adapter::scalar_t scalar_t;
45 part_t numGlobalParts_;
46 part_t targetGlobalParts_;
50 typedef ArrayRCP<RCP<base_metric_type> > base_metric_array_type;
51 base_metric_array_type metricsBase_;
56 const RCP<
const Comm<int> > &problemComm,
77 const RCP<
const Comm<int> > &problemComm,
82 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_() {
89 const RCP<const Environment> &_env,
90 const RCP<
const Comm<int> > &_problemComm,
92 const ArrayView<const typename Adapter::part_t> &_partArray,
95 ArrayRCP<typename Adapter::scalar_t> &_globalSums) {
96 globalWeightedByPart <Adapter>(_env, _problemComm, _graph,
97 _partArray, _numGlobalParts, _metricsBase, _globalSums);
116 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
118 Teuchos::RCP<const Comm<int> > problemComm = Tpetra::getDefaultComm();
135 const RCP<
const Comm<int> > &problemComm,
139 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
144 #ifdef HAVE_ZOLTAN2_MPI
161 numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0), metricsBase_()
163 RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
164 Teuchos::opaqueWrapper(comm);
165 RCP<const Comm<int> > problemComm =
166 rcp<const Comm<int> >(
new Teuchos::MpiComm<int>(wrapper));
179 std::string metricType)
const {
183 int sizeOfArrayView = 0;
184 for(
auto n = 0; n < metricsBase_.size(); ++n) {
185 if( metricsBase_[n]->getMetricType() == metricType ) {
186 if (beginIndex == -1) {
192 if (sizeOfArrayView == 0) {
193 return ArrayView<RCP<base_metric_type> >();
195 return metricsBase_.view(beginIndex, sizeOfArrayView);
202 if( metrics.size() <= 0 ) {
203 throw std::logic_error(
"getObjectCountImbalance() was called "
204 "but no metrics data was generated for " +
207 return metrics[0]->getMetricValue(
"maximum imbalance");
218 if( metrics.size() <= 0 ) {
219 throw std::logic_error(
"getNormedImbalance() was called "
220 "but no metrics data was generated for " +
223 if( metrics.size() <= 1 ) {
224 throw std::logic_error(
"getNormedImbalance() was called "
225 "but the normed data does not exist." );
227 return metrics[1]->getMetricValue(
"maximum imbalance");
249 int weight0IndexStartsAtThisArrayIndex = ( metrics.size() > 2 ) ? 2 : 1;
250 int numberOfWeights = metrics.size() - weight0IndexStartsAtThisArrayIndex;
251 int indexInArray = weight0IndexStartsAtThisArrayIndex + weightIndex;
252 if( metrics.size() <= indexInArray ) {
253 throw std::logic_error(
"getWeightImbalance was called with weight index "+
254 std::to_string(weightIndex) +
255 " but the maximum weight available for " +
257 " is weight " + std::to_string(numberOfWeights-1) +
260 return metrics[indexInArray]->getMetricValue(
"maximum imbalance");
267 if( graphMetrics.size() < 1 ) {
268 throw std::logic_error(
"getMaxEdgeCut() was called "
269 "but no metrics data was generated for " +
272 return graphMetrics[0]->getMetricValue(
"global maximum");
279 int indexInArray = weightIndex + 1;
280 if( graphMetrics.size() <= 1 ) {
281 throw std::logic_error(
"getMaxWeightEdgeCut was called with "
282 "weight index " + std::to_string(weightIndex) +
283 " but no weights were available for " +
286 else if( graphMetrics.size() <= indexInArray ) {
290 throw std::logic_error(
"getMaxWeightEdgeCut was called with "
291 "weight index " + std::to_string(weightIndex) +
292 " but the maximum weight available for " +
295 std::to_string(graphMetrics.size() - 2) +
"." );
297 return graphMetrics[indexInArray]->getMetricValue(
"global maximum");
304 if( graphMetrics.size() < 1 ) {
305 throw std::logic_error(
"getTotalEdgeCut() was called but no metrics "
306 "data was generated for " +
309 return graphMetrics[0]->getMetricValue(
"global sum");
316 int indexInArray = weightIndex + 1;
317 if( graphMetrics.size() <= 1 ) {
321 throw std::logic_error(
"getTotalWeightEdgeCut was called with "
322 "weight index " + std::to_string(weightIndex) +
323 " but no weights were available for " +
326 else if( graphMetrics.size() <= indexInArray ) {
327 throw std::logic_error(
"getTotalWeightEdgeCut was called with "
328 "weight index " + std::to_string(weightIndex) +
329 " but the maximum weight available for " +
332 std::to_string(graphMetrics.size() - 2) +
"." );
334 return graphMetrics[indexInArray]->getMetricValue(
"global sum");
341 if( graphMetrics.size() < 1 ) {
342 throw std::logic_error(
"getTotalMessages() was called but no metrics "
343 "data was generated for " +
347 return graphMetrics[1]->getMetricValue(
"global sum");
355 if( graphMetrics.size() < 1 ) {
356 throw std::logic_error(
"getMaxMessages() was called but no metrics "
357 "data was generated for " +
361 return graphMetrics[1]->getMetricValue(
"global maximum");
370 ArrayView<RCP<base_metric_type>> graphMetrics =
372 if (graphMetrics.size() != 0) {
373 Zoltan2::printGraphMetrics<scalar_t, part_t>(os, targetGlobalParts_,
374 numGlobalParts_, graphMetrics);
379 if (imbalanceMetrics.size() != 0) {
380 Zoltan2::printImbalanceMetrics<scalar_t, part_t>(os, targetGlobalParts_,
387 template <
typename Adapter>
391 const RCP<
const Comm<int> > &comm,
396 RCP<const Comm<int> > problemComm;
397 if (comm == Teuchos::null) {
398 problemComm = Tpetra::getDefaultComm();
403 RCP<Environment> env;
410 env->debug(
DETAILED_STATUS, std::string(
"Entering EvaluatePartition"));
414 size_t numLocalObjects = ia->getLocalNumIDs();
415 ArrayRCP<const part_t> parts;
420 env->localInputAssertion(__FILE__, __LINE__,
"parts not set",
425 const part_t *tmp = NULL;
426 ia->getPartsView(tmp);
428 parts = arcp(tmp, 0, numLocalObjects,
false);
431 part_t *procs =
new part_t[numLocalObjects];
432 for (
size_t i=0;i<numLocalObjects;i++) procs[i]=problemComm->getRank();
433 parts = arcp(procs, 0, numLocalObjects,
true);
436 ArrayView<const part_t> partArray = parts(0, numLocalObjects);
443 const Teuchos::ParameterEntry *pe = p->getEntryPtr(
"partitioning_objective");
445 std::string strChoice = pe->getValue<std::string>(&strChoice);
446 if (strChoice == std::string(
"multicriteria_minimize_total_weight"))
448 else if (strChoice == std::string(
"multicriteria_minimize_maximum_weight"))
452 const RCP<const base_adapter_t> bia =
453 rcp(dynamic_cast<const base_adapter_t *>(ia),
false);
456 imbalanceMetrics<Adapter>(env, problemComm, mcnorm, ia, soln, partArray,
458 numGlobalParts_, numNonEmpty_, metricsBase_);
465 targetGlobalParts_ = problemComm->getSize();
474 env->timerStart(
MACRO_TIMERS,
"Computing graph metrics");
479 std::bitset<NUM_MODEL_FLAGS> modelFlags;
483 RCP<const GraphModel<base_adapter_t> > graph = graphModel;
484 if (graphModel == Teuchos::null) {
490 ArrayRCP<scalar_t> globalSums;
492 this->calculate_graph_metrics(env, problemComm, graph, partArray,
493 numGlobalParts_, metricsBase_, globalSums);
497 env->timerStop(
MACRO_TIMERS,
"Computing graph metrics");
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.
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Time an algorithm (or other entity) as a whole.
void printMetrics(std::ostream &os) const
Print all metrics.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
ArrayView< RCP< base_metric_type > > getAllMetricsOfType(std::string metricType) const
Return the metric list for types matching the given metric type.
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)
scalar_t getTotalEdgeCut() const
getTotalEdgeCut
Defines the PartitioningSolution class.
sub-steps, each method's entry and exit
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.
#define IMBALANCE_METRICS_TYPE_NAME
SparseMatrixAdapter_t::part_t part_t
scalar_t getNormedImbalance() const
Return the object normed weight imbalance. Normed imbalance is only valid if there is at least 2 elem...
A PartitioningSolution is a solution to a partitioning problem.
const part_t * getPartListView() const
Returns the part list corresponding to the global ID list.
BaseAdapterType
An enum to identify general types of adapters.
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.
The StridedData class manages lists of weights or coordinates.
map_t::local_ordinal_type lno_t
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Base class for the EvaluatePartition and EvaluateOrdering classes.
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)
#define GRAPH_METRICS_TYPE_NAME
GraphModel defines the interface required for graph models.
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
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.
A class that computes and returns quality metrics.
virtual ~EvaluatePartition()
scalar_t getTotalWeightEdgeCut(int weightIndex) const
getTotalWeightEdgeCut weighted for the specified index
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 getMaxWeightEdgeCut(int weightIndex) const
getMaxWeightEdgeCuts weighted for the specified index
size_t getTargetGlobalNumberOfParts() const
Returns the global number of parts desired in the solution.
scalar_t getObjectCountImbalance() const
Return the object count imbalance.
scalar_t getTotalMessages() const
getTotalMessages
scalar_t getMaxMessages() const
getMaxMessages