45 #ifndef _ZOLTAN2_ALGPARMETIS_HPP_ 46 #define _ZOLTAN2_ALGPARMETIS_HPP_ 58 #ifndef HAVE_ZOLTAN2_PARMETIS 64 template <
typename Adapter>
69 const RCP<
const Comm<int> > &problemComm,
73 throw std::runtime_error(
74 "BUILD ERROR: ParMETIS requested but not compiled into Zoltan2.\n" 75 "Please set CMake flag Zoltan2_ENABLE_ParMETIS:BOOL=ON.");
85 #ifdef HAVE_ZOLTAN2_PARMETIS 87 #ifndef HAVE_ZOLTAN2_MPI 91 #error "TPL ParMETIS requires compilation with MPI. Configure with -DTPL_ENABLE_MPI:BOOL=ON or -DZoltan2_ENABLE_ParMETIS:BOOL=OFF" 99 #if (PARMETIS_MAJOR_VERSION < 4) 103 #error "Specified version of ParMETIS is not compatible with Zoltan2; upgrade to ParMETIS v4 or later, or build Zoltan2 without ParMETIS." 111 template <
typename Adapter>
112 class AlgParMETIS :
public Algorithm<Adapter>
116 typedef GraphModel<typename Adapter::base_adapter_t> graphModel_t;
117 typedef typename Adapter::lno_t
lno_t;
118 typedef typename Adapter::gno_t
gno_t;
119 typedef typename Adapter::scalar_t
scalar_t;
120 typedef typename Adapter::part_t
part_t;
122 typedef idx_t pm_idx_t;
123 typedef real_t pm_real_t;
136 const RCP<
const Comm<int> > &problemComm__,
137 const RCP<graphModel_t> &model__) :
138 env(env__), problemComm(problemComm__),
142 void partition(
const RCP<PartitioningSolution<Adapter> > &solution);
146 const RCP<const Environment> env;
147 const RCP<const Comm<int> > problemComm;
148 const RCP<GraphModel<typename Adapter::base_adapter_t> > model;
150 void scale_weights(
size_t n, ArrayView<StridedData<lno_t, scalar_t> > &fwgts,
156 template <
typename Adapter>
158 const RCP<PartitioningSolution<Adapter> > &solution
163 size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
165 int me = problemComm->getRank();
166 int np = problemComm->getSize();
169 ArrayView<const gno_t> vtxgnos;
170 ArrayView<StridedData<lno_t, scalar_t> > vwgts;
171 int nVwgt = model->getNumWeightsPerVertex();
172 size_t nVtx = model->getVertexList(vtxgnos, vwgts);
176 pm_idx_t *pm_vwgts = NULL;
178 pm_vwgts =
new pm_idx_t[nVtx*nVwgt];
179 scale_weights(nVtx, vwgts, pm_vwgts);
183 ArrayView<const gno_t> adjgnos;
184 ArrayView<const lno_t> offsets;
185 ArrayView<StridedData<lno_t, scalar_t> > ewgts;
186 int nEwgt = model->getNumWeightsPerEdge();
187 size_t nEdge = model->getEdgeList(adjgnos, offsets, ewgts);
189 pm_idx_t *pm_ewgts = NULL;
191 pm_ewgts =
new pm_idx_t[nEdge*nEwgt];
192 scale_weights(nEdge, ewgts, pm_ewgts);
196 pm_idx_t *pm_offsets;
199 pm_idx_t pm_dummy_adj;
203 pm_adjs = &pm_dummy_adj;
207 pm_idx_t *pm_vtxdist;
208 ArrayView<size_t> vtxdist;
209 model->getVertexDist(vtxdist);
214 RCP<Comm<int> > subcomm;
219 Array<int> keepRanks(np);
220 for (
int i = 0; i < np; i++) {
221 if ((pm_vtxdist[i+1] - pm_vtxdist[i]) > 0) {
222 keepRanks[nKeep] = i;
223 pm_vtxdist[nKeep] = pm_vtxdist[i];
227 pm_vtxdist[nKeep] = pm_vtxdist[np];
229 subcomm = problemComm->createSubcommunicator(keepRanks.view(0,nKeep));
230 if (subcomm != Teuchos::null)
231 mpicomm = Teuchos::getRawMpiComm(*subcomm);
233 mpicomm = MPI_COMM_NULL;
236 mpicomm = Teuchos::getRawMpiComm(*problemComm);
240 mpicomm = Teuchos::getRawMpiComm(*problemComm);
244 pm_idx_t *pm_partList = NULL;
245 if (nVtx) pm_partList =
new pm_idx_t[nVtx];
246 for (
size_t i = 0; i < nVtx; i++) pm_partList[i] = 0;
247 int pm_return = METIS_OK;
249 if (mpicomm != MPI_COMM_NULL) {
253 pm_idx_t pm_nCon = (nVwgt == 0 ? 1 : pm_idx_t(nVwgt));
254 pm_real_t *pm_partsizes =
new pm_real_t[numGlobalParts*pm_nCon];
255 for (pm_idx_t dim = 0; dim < pm_nCon; dim++) {
256 if (!solution->criteriaHasUniformPartSizes(dim))
257 for (
size_t i=0; i<numGlobalParts; i++)
258 pm_partsizes[i*pm_nCon+dim] =
259 pm_real_t(solution->getCriteriaPartSize(dim,i));
261 for (
size_t i=0; i<numGlobalParts; i++)
262 pm_partsizes[i*pm_nCon+dim] = pm_real_t(1.)/pm_real_t(numGlobalParts);
266 double tolerance = 1.1;
267 const Teuchos::ParameterList &pl = env->getParameters();
268 const Teuchos::ParameterEntry *pe = pl.getEntryPtr(
"imbalance_tolerance");
269 if (pe) tolerance = pe->getValue<
double>(&tolerance);
273 if (tolerance <= 1.0) {
275 std::cerr <<
"Warning: ParMETIS requires imbalance_tolerance > 1.0; " 276 <<
"to comply, Zoltan2 reset imbalance_tolerance to 1.01." 281 pm_real_t *pm_imbTols =
new pm_real_t[pm_nCon];
282 for (pm_idx_t dim = 0; dim < pm_nCon; dim++)
283 pm_imbTols[dim] = pm_real_t(tolerance);
285 std::string parmetis_method(
"PARTKWAY");
286 pe = pl.getEntryPtr(
"partitioning_approach");
288 std::string approach;
289 approach = pe->getValue<std::string>(&approach);
290 if ((approach ==
"repartition") || (approach ==
"maximize_overlap")) {
293 parmetis_method =
"ADAPTIVE_REPART";
298 parmetis_method =
"PARTKWAY";
303 pm_idx_t pm_wgtflag = 2*(nVwgt > 0) + (nEwgt > 0);
304 pm_idx_t pm_numflag = 0;
305 pm_idx_t pm_edgecut = -1;
306 pm_idx_t pm_options[METIS_NOPTIONS];
308 for (
int i = 0; i < METIS_NOPTIONS; i++)
315 if (parmetis_method ==
"PARTKWAY") {
316 pm_return = ParMETIS_V3_PartKway(pm_vtxdist, pm_offsets, pm_adjs,
317 pm_vwgts, pm_ewgts, &pm_wgtflag,
318 &pm_numflag, &pm_nCon, &pm_nPart,
319 pm_partsizes, pm_imbTols, pm_options,
320 &pm_edgecut, pm_partList, &mpicomm);
322 else if (parmetis_method ==
"ADAPTIVE_REPART") {
326 pm_idx_t *pm_vsize =
new pm_idx_t[nVtx];
327 for (
size_t i = 0; i < nVtx; i++) pm_vsize[i] = 1;
329 pm_real_t itr = 100.;
330 pm_return = ParMETIS_V3_AdaptiveRepart(pm_vtxdist, pm_offsets, pm_adjs,
332 pm_vsize, pm_ewgts, &pm_wgtflag,
333 &pm_numflag, &pm_nCon, &pm_nPart,
334 pm_partsizes, pm_imbTols,
335 &itr, pm_options, &pm_edgecut,
336 pm_partList, &mpicomm);
349 throw std::logic_error(
"\nInvalid ParMETIS method requested.\n");
353 delete [] pm_partsizes;
354 delete [] pm_imbTols;
359 ArrayRCP<part_t> partList;
364 solution->setParts(partList);
366 env->memory(
"Zoltan2-ParMETIS: After creating solution");
374 if (nVwgt)
delete [] pm_vwgts;
375 if (nEwgt)
delete [] pm_ewgts;
377 if (pm_return != METIS_OK) {
378 throw std::runtime_error(
379 "\nParMETIS returned an error; no valid partition generated.\n" 380 "Look for 'PARMETIS ERROR' in your output for more details.\n");
394 template <
typename Adapter>
395 void AlgParMETIS<Adapter>::scale_weights(
397 ArrayView<StridedData<
typename Adapter::lno_t,
398 typename Adapter::scalar_t> > &fwgts,
402 const double INT_EPSILON = 1e-5;
403 const int nWgt = fwgts.size();
405 int *nonint_local =
new int[nWgt+nWgt];
406 int *nonint = nonint_local + nWgt;
408 double *sum_wgt_local =
new double[nWgt*4];
409 double *max_wgt_local = sum_wgt_local + nWgt;
410 double *sum_wgt = max_wgt_local + nWgt;
411 double *max_wgt = sum_wgt + nWgt;
413 for (
int i = 0; i < nWgt; i++) {
415 sum_wgt_local[i] = 0.;
416 max_wgt_local[i] = 0;
421 for (
int j = 0; j < nWgt; j++) {
422 for (
size_t i = 0; i < n; i++) {
423 double fw = double(fwgts[j][i]);
424 if (!nonint_local[j]) {
425 pm_idx_t tmp = (pm_idx_t) floor(fw + .5);
426 if (fabs((
double)tmp-fw) > INT_EPSILON) {
430 sum_wgt_local[j] += fw;
431 if (fw > max_wgt_local[j]) max_wgt_local[j] = fw;
435 Teuchos::reduceAll<int,int>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
436 nonint_local, nonint);
437 Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_SUM, nWgt,
438 sum_wgt_local, sum_wgt);
439 Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
440 max_wgt_local, max_wgt);
442 const double max_wgt_sum = double(std::numeric_limits<pm_idx_t>::max()/8);
443 for (
int j = 0; j < nWgt; j++) {
448 if (nonint[j] || (max_wgt[j]<=INT_EPSILON) || (sum_wgt[j]>max_wgt_sum)) {
450 if (sum_wgt[j] != 0.) scale = max_wgt_sum/sum_wgt[j];
454 for (
size_t i = 0; i < n; i++)
455 iwgts[i*nWgt+j] = (pm_idx_t) ceil(
double(fwgts[j][i])*scale);
457 delete [] nonint_local;
458 delete [] sum_wgt_local;
463 #endif // PARMETIS VERSION 4 OR HIGHER CHECK 465 #endif // HAVE_ZOLTAN2_MPI 467 #endif // HAVE_ZOLTAN2_PARMETIS
Defines the PartitioningSolution class.
static void SAVE_ARRAYRCP(ArrayRCP< first_t > *a, second_t *b, size_t size)
AlgParMETIS(const RCP< const Environment > &env, const RCP< const Comm< int > > &problemComm, const RCP< GraphModel< typename Adapter::base_adapter_t > > &model)
Adapter::scalar_t scalar_t
Algorithm defines the base class for all algorithms.
static void ASSIGN(first_t &a, second_t b)
virtual void partition(const RCP< PartitioningSolution< Adapter > > &solution)
Partitioning method.
GraphModel defines the interface required for graph models.
static void ASSIGN_ARRAY(first_t **a, ArrayView< second_t > &b)
Defines the GraphModel interface.
A gathering of useful namespace methods.
static void DELETE_ARRAY(first_t **a)