50 #ifndef _ZOLTAN2_PARTITIONINGPROBLEM_HPP_
51 #define _ZOLTAN2_PARTITIONINGPROBLEM_HPP_
65 #ifdef HAVE_ZOLTAN2_OVIS
90 template<
typename Adapter>
96 typedef typename Adapter::gid_t
gid_t;
102 #ifdef HAVE_ZOLTAN2_MPI
103 typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
110 #ifdef HAVE_ZOLTAN2_MPI
138 void solve(
bool updateInputData=
true );
145 return *(solution_.getRawPtr());
158 if (!metrics_.is_null())
159 metrics_->getWeightImbalance(imb, dim);
169 if (metrics_.is_null()){
170 ArrayRCP<const MetricValues<scalar_t> > emptyMetrics;
174 return metrics_->getMetrics();
183 if (metrics_.is_null())
184 os <<
"No metrics available." << endl;
186 metrics_->printMetrics(os);
268 scalar_t *partSizes,
bool makeCopy=
true) ;
277 if (timer_.getRawPtr() != NULL)
278 this->
env_->setTimer(timer_);
291 void initializeProblem();
293 void createPartitioningProblem(
bool newData);
295 RCP<PartitioningSolution<Adapter> > solution_;
297 RCP<MachineRepresentation <typename Adapter::base_adapter_t::scalar_t> > machine_;
299 RCP<Comm<int> > problemComm_;
300 RCP<const Comm<int> > problemCommConst_;
302 #ifdef HAVE_ZOLTAN2_MPI
311 std::string algorithm_;
313 int numberOfWeights_;
324 ArrayRCP<ArrayRCP<partId_t> > partIds_;
325 ArrayRCP<ArrayRCP<scalar_t> > partSizes_;
326 int numberOfCriteria_;
330 ArrayRCP<int> levelNumberParts_;
335 RCP<TimerManager> timer_;
339 bool metricsRequested_;
340 RCP<const PartitioningSolutionQuality<Adapter> > metrics_;
344 #ifdef HAVE_ZOLTAN2_MPI
345 template <
typename Adapter>
347 ParameterList *p, MPI_Comm comm):
348 Problem<Adapter>(A,p,comm), solution_(),
349 problemComm_(), problemCommConst_(),
351 graphFlags_(), idFlags_(), coordFlags_(), algorithm_(),
352 numberOfWeights_(), partIds_(), partSizes_(),
353 numberOfCriteria_(), levelNumberParts_(), hierarchical_(false),
354 timer_(), metricsRequested_(false), metrics_()
366 template <
typename Adapter>
369 Problem<Adapter>(A,p), solution_(),
370 problemComm_(), problemCommConst_(),
372 graphFlags_(), idFlags_(), coordFlags_(), algorithm_(),
374 partIds_(), partSizes_(), numberOfCriteria_(),
375 levelNumberParts_(), hierarchical_(false), timer_(),
376 metricsRequested_(false), metrics_()
381 template <
typename Adapter>
386 this->env_->debug(
DETAILED_STATUS,
"PartitioningProblem::initializeProblem");
388 if (getenv(
"DEBUGME")){
389 std::cout << getpid() << std::endl;
393 #ifdef HAVE_ZOLTAN2_OVIS
394 ovis_enabled(this->comm_->getRank());
399 problemComm_ = this->comm_->duplicate();
400 problemCommConst_ = rcp_const_cast<
const Comm<int> > (problemComm_);
403 #ifdef HAVE_ZOLTAN2_MPI
407 mpiComm_ = Teuchos2MPI(problemComm_);
414 numberOfWeights_ = this->inputAdapter_->getNumberOfWeightsPerObject();
416 numberOfCriteria_ = (numberOfWeights_ > 1) ? numberOfWeights_ : 1;
418 inputType_ = this->inputAdapter_->inputAdapterType();
423 ArrayRCP<partId_t> *noIds =
new ArrayRCP<partId_t> [numberOfCriteria_];
424 ArrayRCP<scalar_t> *noSizes =
new ArrayRCP<scalar_t> [numberOfCriteria_];
426 partIds_ = arcp(noIds, 0, numberOfCriteria_,
true);
427 partSizes_ = arcp(noSizes, 0, numberOfCriteria_,
true);
431 msg << problemComm_->getSize() <<
" procs,"
432 << numberOfWeights_ <<
" user-defined weights, "
433 << this->inputAdapter_->inputAdapterName()
434 <<
" input adapter type.\n";
438 this->env_->memory(
"After initializeProblem");
441 template <
typename Adapter>
443 int criteria,
int len,
partId_t *partIds,
scalar_t *partSizes,
bool makeCopy)
445 this->env_->localInputAssertion(__FILE__, __LINE__,
"invalid length",
448 this->env_->localInputAssertion(__FILE__, __LINE__,
"invalid criteria",
452 partIds_[criteria] = ArrayRCP<partId_t>();
453 partSizes_[criteria] = ArrayRCP<scalar_t>();
457 this->env_->localInputAssertion(__FILE__, __LINE__,
"invalid arrays",
466 bool own_memory =
false;
471 this->env_->localMemoryAssertion(__FILE__, __LINE__, len, z2_partSizes);
472 memcpy(z2_partIds, partIds, len *
sizeof(
partId_t));
473 memcpy(z2_partSizes, partSizes, len *
sizeof(
scalar_t));
477 z2_partIds = partIds;
478 z2_partSizes = partSizes;
481 partIds_[criteria] = arcp(z2_partIds, 0, len, own_memory);
482 partSizes_[criteria] = arcp(z2_partSizes, 0, len, own_memory);
485 template <
typename Adapter>
495 createPartitioningProblem(updateInputData);
507 RCP<const IdentifierMap<user_t> > idMap =
508 this->baseModel_->getIdentifierMap();
512 this->env_->timerStart(
MACRO_TIMERS,
"create solution");
516 this->envConst_, problemCommConst_, idMap, numberOfWeights_,
517 partIds_.view(0, numberOfCriteria_),
518 partSizes_.view(0, numberOfCriteria_));
522 solution_ = rcp(soln);
526 this->env_->memory(
"After creating Solution");
531 if (algorithm_ ==
string(
"scotch")){
532 AlgPTScotch<Adapter>(this->envConst_, problemComm_,
533 #ifdef HAVE_ZOLTAN2_MPI
536 this->graphModel_, solution_);
538 else if (algorithm_ ==
string(
"block")){
539 AlgBlock<Adapter>(this->envConst_, problemComm_,
540 this->identifierModel_, solution_);
542 else if (algorithm_ ==
string(
"rcb")){
543 AlgRCB<Adapter>(this->envConst_, problemComm_,
544 this->coordinateModel_, solution_);
546 else if (algorithm_ ==
string(
"multijagged")){
547 AlgPQJagged<Adapter>(this->envConst_, problemComm_,
548 this->coordinateModel_, solution_);
551 throw std::logic_error(
"partitioning algorithm not supported yet");
559 const Teuchos::ParameterEntry *pe = this->envConst_->getParameters().getEntryPtr(
"mapping_type");
560 int mapping_type = -1;
562 mapping_type = pe->getValue(&mapping_type);
566 if (mapping_type == 0){
571 problemComm_.getRawPtr(),
572 machine_.getRawPtr(),
573 this->coordinateModel_.getRawPtr(),
574 solution_.getRawPtr(),
575 this->envConst_.getRawPtr()
582 else if (mapping_type == 1){
587 #ifdef KDDKDD_SHOULD_NEVER_CHANGE_PROBLEMCOMM
588 #ifdef HAVE_ZOLTAN2_MPI
594 RCP<const mpiWrapper_t > wrappedComm = rcp(
new mpiWrapper_t(mpiComm_));
595 problemComm_ = rcp(
new Teuchos::MpiComm<int>(wrappedComm));
596 problemCommConst_ = rcp_const_cast<
const Comm<int> > (problemComm_);
601 if (metricsRequested_){
605 psq_t *quality = NULL;
606 RCP<const ps_t> solutionConst = rcp_const_cast<
const ps_t>(solution_);
607 RCP<const Adapter> adapter = rcp(this->inputAdapter_,
false);
610 quality =
new psq_t(this->envConst_, problemCommConst_, adapter,
615 metrics_ = rcp(quality);
621 template <
typename Adapter>
626 "PartitioningProblem::createPartitioningProblem");
629 using Teuchos::ParameterList;
638 modelFlag_t previousIdentifierModelFlags = idFlags_;
639 modelFlag_t previousCoordinateModelFlags = coordFlags_;
668 string defString(
"default");
673 const Teuchos::ParameterEntry *pe = pl.getEntryPtr(
"compute_metrics");
675 yesNo = pe->getValue<
int>(&yesNo);
676 metricsRequested_ =
true;
681 string model(defString);
682 pe = pl.getEntryPtr(
"model");
684 model = pe->getValue<
string>(&model);
688 string algorithm(defString);
689 pe = pl.getEntryPtr(
"algorithm");
691 algorithm = pe->getValue<
string>(&algorithm);
695 bool needConsecutiveGlobalIds =
false;
696 bool removeSelfEdges=
false;
702 if (algorithm != defString){
704 if (algorithm ==
string(
"block") ||
705 algorithm == string(
"random") ||
706 algorithm == string(
"cyclic") ){
709 algorithm_ = algorithm;
710 needConsecutiveGlobalIds =
true;
712 else if (algorithm ==
string(
"rcb") ||
713 algorithm == string(
"rib") ||
714 algorithm == string(
"multijagged") ||
715 algorithm == string(
"hsfc")){
718 algorithm_ = algorithm;
720 else if (algorithm ==
string(
"metis") ||
721 algorithm == string(
"parmetis") ||
722 algorithm == string(
"scotch") ||
723 algorithm == string(
"ptscotch")){
726 algorithm_ = algorithm;
727 removeSelfEdges =
true;
728 needConsecutiveGlobalIds =
true;
730 else if (algorithm ==
string(
"patoh") ||
731 algorithm == string(
"phg")){
737 algorithm_ = algorithm;
738 needConsecutiveGlobalIds =
true;
742 throw std::logic_error(
"parameter list algorithm is invalid");
745 else if (model != defString){
747 if (model ==
string(
"hypergraph")){
749 if (problemComm_->getSize() > 1)
750 algorithm_ =
string(
"phg");
752 algorithm_ = string(
"patoh");
753 needConsecutiveGlobalIds =
true;
755 else if (model ==
string(
"graph")){
757 #ifdef HAVE_ZOLTAN2_SCOTCH
758 if (problemComm_->getSize() > 1)
759 algorithm_ =
string(
"ptscotch");
761 algorithm_ = string(
"scotch");
762 removeSelfEdges =
true;
763 needConsecutiveGlobalIds =
true;
765 #ifdef HAVE_ZOLTAN2_PARMETIS
766 if (problemComm_->getSize() > 1)
767 algorithm_ =
string(
"parmetis");
769 algorithm_ = string(
"metis");
770 removeSelfEdges =
true;
771 needConsecutiveGlobalIds =
true;
773 if (problemComm_->getSize() > 1)
774 algorithm_ =
string(
"phg");
776 algorithm_ = string(
"patoh");
777 removeSelfEdges =
true;
778 needConsecutiveGlobalIds =
true;
782 else if (model ==
string(
"geometry")){
784 algorithm_ = string(
"rcb");
786 else if (model ==
string(
"ids")){
788 algorithm_ = string(
"block");
789 needConsecutiveGlobalIds =
true;
804 if (problemComm_->getSize() > 1)
805 algorithm_ =
string(
"phg");
807 algorithm_ = string(
"patoh");
812 if (problemComm_->getSize() > 1)
813 algorithm_ =
string(
"phg");
815 algorithm_ = string(
"patoh");
819 if(algorithm_ !=
string(
"multijagged"))
820 algorithm_ = string(
"rcb");
825 algorithm_ = string(
"block");
829 throw std::logic_error(
"input type is invalid");
835 Array<int> valueList;
836 pe = pl.getEntryPtr(
"topology");
839 valueList = pe->getValue<Array<int> >(&valueList);
841 if (!Zoltan2::noValuesAreInRangeList<int>(valueList)){
842 int *n =
new int [valueList.size() + 1];
843 levelNumberParts_ = arcp(n, 0, valueList.size() + 1,
true);
844 int procsPerNode = 1;
845 for (
int i=0; i < valueList.size(); i++){
846 levelNumberParts_[i+1] = valueList[i];
847 procsPerNode *= valueList[i];
850 levelNumberParts_[0] = env.
numProcs_ / procsPerNode;
853 levelNumberParts_[0]++;
857 levelNumberParts_.clear();
860 hierarchical_ = levelNumberParts_.size() > 0;
864 string objectOfInterest(defString);
865 pe = pl.getEntryPtr(
"objects_to_partition");
867 objectOfInterest = pe->getValue<
string>(&objectOfInterest);
876 string symParameter(defString);
877 pe = pl.getEntryPtr(
"symmetrize_graph");
879 symParameter = pe->getValue<
string>(&symParameter);
880 if (symParameter ==
string(
"transpose"))
882 else if (symParameter ==
string(
"bipartite"))
887 pe = pl.getEntryPtr(
"subset_graph");
889 sgParameter = pe->getValue<
int>(&sgParameter);
891 if (sgParameter == 1)
899 if (needConsecutiveGlobalIds)
905 if (objectOfInterest == defString ||
906 objectOfInterest ==
string(
"matrix_rows") )
908 else if (objectOfInterest ==
string(
"matrix_columns"))
910 else if (objectOfInterest ==
string(
"matrix_nonzeros"))
915 if (objectOfInterest == defString ||
916 objectOfInterest ==
string(
"mesh_nodes") )
918 else if (objectOfInterest ==
string(
"mesh_elements"))
926 if (needConsecutiveGlobalIds)
933 if (needConsecutiveGlobalIds)
939 (modelType_ != previousModel) ||
940 (graphFlags_ != previousGraphModelFlags) ||
941 (coordFlags_ != previousCoordinateModelFlags) ||
942 (idFlags_ != previousIdentifierModelFlags) ) {
954 switch (modelType_) {
957 this->graphModel_ = rcp(
new GraphModel<base_adapter_t>(
958 this->baseInputAdapter_, this->envConst_, problemComm_, graphFlags_));
960 this->baseModel_ = rcp_implicit_cast<
const Model<base_adapter_t> >(
969 this->coordinateModel_ = rcp(
new CoordinateModel<base_adapter_t>(
970 this->baseInputAdapter_, this->envConst_, problemComm_, coordFlags_));
1027 this->baseModel_ = rcp_implicit_cast<
const Model<base_adapter_t> >(
1028 this->coordinateModel_);
1032 this->identifierModel_ = rcp(
new IdentifierModel<base_adapter_t>(
1033 this->baseInputAdapter_, this->envConst_, problemComm_, idFlags_));
1035 this->baseModel_ = rcp_implicit_cast<
const Model<base_adapter_t> >(
1036 this->identifierModel_);
1040 cout << __func__ <<
" Invalid model" << modelType_ << endl;
1044 this->env_->memory(
"After creating Model");
use columns as graph vertices
~PartitioningProblem()
Destructor.
ArrayRCP< const MetricValues< scalar_t > > getMetrics() const
Get the array of metrics Metrics were only computed if user requested metrics with a parameter...
Time an algorithm (or other entity) as a whole.
Adapter::scalar_t scalar_t
use mesh nodes as vertices
ModelType
An identifier for the general type of model.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
algorithm requires consecutive ids
model must symmetrize input
model must symmetrize input
zoltan2_partId_t partId_t
void resetParameters(ParameterList *params)
Reset the list of parameters.
Defines the PartitioningSolution class.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
sub-steps, each method's entry and exit
const RCP< const Environment > & getEnvironment() const
Get the current Environment. Useful for testing.
void resetParameters(ParameterList *params)
Reset the list of parameters.
int numProcs_
number of processes (relative to comm_)
InputAdapterType
An enum to identify general types of input adapters.
use matrix rows as graph vertices
Teuchos::ParameterList & getParametersNonConst()
Returns a reference to a non-const copy of the parameters.
A class that computes and returns quality metrics.
Defines the IdentifierModel interface.
A PartitioningSolution is a solution to a partitioning problem.
use nonzeros as graph vertices
Defines the PartitioningSolutionQuality class.
void localBugAssertion(const char *file, int lineNum, const char *msg, bool ok, AssertionLevel level) const
Test for valid library behavior on local process only.
Problem base class from which other classes (PartitioningProblem, ColoringProblem, OrderingProblem, MatchingProblem, etc.) derive.
Defines the Problem base class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
MachineRepresentation Class Finds the coordinate of the processor. Used to find the processor coordin...
void printMetrics(ostream &os) const
Print the array of metrics.
Adapter::base_adapter_t base_adapter_t
Define IntegerRangeList validator.
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
use mesh elements as vertices
PartitioningProblem(Adapter *A, Teuchos::ParameterList *p)
Constructor where communicator is the Teuchos default.
algorithm requires no self edges
void setPartSizes(int len, partId_t *partIds, scalar_t *partSizes, bool makeCopy=true)
Set or reset relative sizes for the partitions that Zoltan2 will create.
Defines the GraphModel interface.
void setPartSizesForCritiera(int criteria, int len, partId_t *partIds, scalar_t *partSizes, bool makeCopy=true)
Set or reset the relative sizes (per weight) for the partitions that Zoltan2 will create...
plain identifier input, just a list of Ids
void solve(bool updateInputData=true)
Direct the problem to create a solution.
RCP< const Environment > envConst_
const scalar_t getImbalance(int dim=0) const
Returns the imbalance of the solution.