Zoltan2
Zoltan2_AlgParMETIS.hpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// Zoltan2: A package of combinatorial algorithms for scientific computing
6// Copyright 2012 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Karen Devine (kddevin@sandia.gov)
39// Erik Boman (egboman@sandia.gov)
40// Siva Rajamanickam (srajama@sandia.gov)
41//
42// ***********************************************************************
43//
44// @HEADER
45#ifndef _ZOLTAN2_ALGPARMETIS_HPP_
46#define _ZOLTAN2_ALGPARMETIS_HPP_
47
49#include <Zoltan2_Algorithm.hpp>
51#include <Zoltan2_Util.hpp>
52
57
58#ifndef HAVE_ZOLTAN2_PARMETIS
59
60// Error handling for when ParMETIS is requested
61// but Zoltan2 not built with ParMETIS.
62
63namespace Zoltan2 {
64template <typename Adapter, typename Model=GraphModel<typename Adapter::base_adapter_t>>
65class AlgParMETIS : public Algorithm<Adapter>
66{
67public:
68 AlgParMETIS(const RCP<const Environment> &/* env */,
69 const RCP<const Comm<int> > &/* problemComm */,
70 const RCP<Model> &/* model */
71 )
72 {
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.");
76 }
77};
78}
79
80#endif
81
84
85#ifdef HAVE_ZOLTAN2_PARMETIS
86
87#ifndef HAVE_ZOLTAN2_MPI
88
89// ParMETIS requires compilation with MPI.
90// If MPI is not available, make compilation fail.
91#error "TPL ParMETIS requires compilation with MPI. Configure with -DTPL_ENABLE_MPI:BOOL=ON or -DZoltan2_ENABLE_ParMETIS:BOOL=OFF"
92
93#else
94
95extern "C" {
96#include "parmetis.h"
97}
98
99#if (PARMETIS_MAJOR_VERSION < 4)
100
101// Zoltan2 requires ParMETIS v4.x.
102// Make compilation fail for earlier versions of ParMETIS.
103#error "Specified version of ParMETIS is not compatible with Zoltan2; upgrade to ParMETIS v4 or later, or build Zoltan2 without ParMETIS."
104
105#else
106
107// MPI and ParMETIS version requirements are met. Proceed.
108
109namespace Zoltan2 {
110
111template <typename Adapter, typename Model=GraphModel<typename Adapter::base_adapter_t>>
112class AlgParMETIS : public Algorithm<Adapter>
113{
114public:
115
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::offset_t offset_t;
120 typedef typename Adapter::scalar_t scalar_t;
121 typedef typename Adapter::part_t part_t;
122
123 typedef idx_t pm_idx_t;
124 typedef real_t pm_real_t;
125
136 AlgParMETIS(const RCP<const Environment> &env__,
137 const RCP<const Comm<int> > &problemComm__,
138 const RCP<Model> &model__) :
139 env(env__), problemComm(problemComm__),
140 model(model__)
141 { }
142
143 void partition(const RCP<PartitioningSolution<Adapter> > &solution);
144
145private:
146
147 const RCP<const Environment> env;
148 const RCP<const Comm<int> > problemComm;
149 const RCP<Model> model;
150
151 void scale_weights(size_t n, ArrayView<StridedData<lno_t, scalar_t> > &fwgts,
152 pm_idx_t *iwgts);
153};
154
155
157 template <typename Adapter, typename Model>
159 const RCP<PartitioningSolution<Adapter> > &solution
160)
161{
162 HELLO;
163
164 size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
165
166 int me = problemComm->getRank();
167 int np = problemComm->getSize();
168
169 // Get vertex info
170 ArrayView<const gno_t> vtxgnos;
171 ArrayView<StridedData<lno_t, scalar_t> > vwgts;
172 int nVwgt = model->getNumWeightsPerVertex();
173 size_t nVtx = model->getVertexList(vtxgnos, vwgts);
174 pm_idx_t pm_nVtx;
176
177 pm_idx_t *pm_vwgts = NULL;
178 if (nVwgt) {
179 pm_vwgts = new pm_idx_t[nVtx*nVwgt];
180 scale_weights(nVtx, vwgts, pm_vwgts);
181 }
182
183 // Get edge info
184 ArrayView<const gno_t> adjgnos;
185 ArrayView<const offset_t> offsets;
186 ArrayView<StridedData<lno_t, scalar_t> > ewgts;
187 int nEwgt = model->getNumWeightsPerEdge();
188 size_t nEdge = model->getEdgeList(adjgnos, offsets, ewgts);
189
190 pm_idx_t *pm_ewgts = NULL;
191 if (nEwgt) {
192 pm_ewgts = new pm_idx_t[nEdge*nEwgt];
193 scale_weights(nEdge, ewgts, pm_ewgts);
194 }
195
196 // Convert index types for edges, if needed
197 pm_idx_t *pm_offsets;
199 pm_idx_t *pm_adjs;
200 pm_idx_t pm_dummy_adj;
201 if (nEdge)
203 else
204 pm_adjs = &pm_dummy_adj; // ParMETIS does not like NULL pm_adjs;
205
206
207 // Build vtxdist
208 pm_idx_t *pm_vtxdist;
209 ArrayView<size_t> vtxdist;
210 model->getVertexDist(vtxdist);
211 TPL_Traits<pm_idx_t,size_t>::ASSIGN_ARRAY(&pm_vtxdist, vtxdist);
212
213 // ParMETIS does not like processors having no vertices.
214 // Inspect vtxdist and remove from communicator procs that have no vertices
215 RCP<Comm<int> > subcomm;
216 MPI_Comm mpicomm; // Note: mpicomm is valid only while subcomm is in scope
217
218 int nKeep = 0;
219 if (np > 1) {
220 Array<int> keepRanks(np);
221 for (int i = 0; i < np; i++) {
222 if ((pm_vtxdist[i+1] - pm_vtxdist[i]) > 0) {
223 keepRanks[nKeep] = i;
224 pm_vtxdist[nKeep] = pm_vtxdist[i];
225 nKeep++;
226 }
227 }
228 pm_vtxdist[nKeep] = pm_vtxdist[np];
229 if (nKeep < np) {
230 subcomm = problemComm->createSubcommunicator(keepRanks.view(0,nKeep));
231 if (subcomm != Teuchos::null)
232 mpicomm = Teuchos::getRawMpiComm(*subcomm);
233 else
234 mpicomm = MPI_COMM_NULL;
235 }
236 else {
237 mpicomm = Teuchos::getRawMpiComm(*problemComm);
238 }
239 }
240 else {
241 mpicomm = Teuchos::getRawMpiComm(*problemComm);
242 }
243
244 // Create array for ParMETIS to return results in.
245 pm_idx_t *pm_partList = NULL;
246 if (nVtx) pm_partList = new pm_idx_t[nVtx];
247 for (size_t i = 0; i < nVtx; i++) pm_partList[i] = 0;
248 int pm_return = METIS_OK;
249
250 if (mpicomm != MPI_COMM_NULL) {
251 // If in ParMETIS' communicator (i.e., have vertices), call ParMETIS
252
253 // Get target part sizes
254 pm_idx_t pm_nCon = (nVwgt == 0 ? 1 : pm_idx_t(nVwgt));
255 pm_real_t *pm_partsizes = new pm_real_t[numGlobalParts*pm_nCon];
256 for (pm_idx_t dim = 0; dim < pm_nCon; dim++) {
257 if (!solution->criteriaHasUniformPartSizes(dim))
258 for (size_t i=0; i<numGlobalParts; i++)
259 pm_partsizes[i*pm_nCon+dim] =
260 pm_real_t(solution->getCriteriaPartSize(dim,i));
261 else
262 for (size_t i=0; i<numGlobalParts; i++)
263 pm_partsizes[i*pm_nCon+dim] = pm_real_t(1.)/pm_real_t(numGlobalParts);
264 }
265
266 // Get imbalance tolerances
267 double tolerance = 1.1;
268 const Teuchos::ParameterList &pl = env->getParameters();
269 const Teuchos::ParameterEntry *pe = pl.getEntryPtr("imbalance_tolerance");
270 if (pe) tolerance = pe->getValue<double>(&tolerance);
271
272 // ParMETIS requires tolerance to be greater than 1.0;
273 // fudge it if condition is not met
274 if (tolerance <= 1.0) {
275 if (me == 0)
276 std::cerr << "Warning: ParMETIS requires imbalance_tolerance > 1.0; "
277 << "to comply, Zoltan2 reset imbalance_tolerance to 1.01."
278 << std::endl;
279 tolerance = 1.01;
280 }
281
282 pm_real_t *pm_imbTols = new pm_real_t[pm_nCon];
283 for (pm_idx_t dim = 0; dim < pm_nCon; dim++)
284 pm_imbTols[dim] = pm_real_t(tolerance);
285
286 std::string parmetis_method("PARTKWAY");
287 pe = pl.getEntryPtr("partitioning_approach");
288 if (pe){
289 std::string approach;
290 approach = pe->getValue<std::string>(&approach);
291 if ((approach == "repartition") || (approach == "maximize_overlap")) {
292 if (nKeep > 1)
293 // ParMETIS_V3_AdaptiveRepart requires two or more processors
294 parmetis_method = "ADAPTIVE_REPART";
295 else
296 // Probably best to do PartKway if nKeep == 1;
297 // I think REFINE_KWAY won't give a good answer in most use cases
298 // parmetis_method = "REFINE_KWAY";
299 parmetis_method = "PARTKWAY";
300 }
301 }
302
303 // Other ParMETIS parameters?
304 pm_idx_t pm_wgtflag = 2*(nVwgt > 0) + (nEwgt > 0);
305 pm_idx_t pm_numflag = 0;
306 pm_idx_t pm_edgecut = -1;
307 pm_idx_t pm_options[METIS_NOPTIONS];
308 pm_options[0] = 1; // Use non-default options for some ParMETIS options
309 for (int i = 1; i < METIS_NOPTIONS; i++)
310 pm_options[i] = 0; // Default options
311 pm_options[2] = 15; // Matches default value used in Zoltan
312
313 pm_idx_t pm_nPart;
314 TPL_Traits<pm_idx_t,size_t>::ASSIGN(pm_nPart, numGlobalParts);
315
316 if (parmetis_method == "PARTKWAY") {
317 pm_return = ParMETIS_V3_PartKway(pm_vtxdist, pm_offsets, pm_adjs,
318 pm_vwgts, pm_ewgts, &pm_wgtflag,
319 &pm_numflag, &pm_nCon, &pm_nPart,
320 pm_partsizes, pm_imbTols, pm_options,
321 &pm_edgecut, pm_partList, &mpicomm);
322 }
323 else if (parmetis_method == "ADAPTIVE_REPART") {
324 // Get object sizes: pm_vsize
325 // TODO: get pm_vsize info from input adapter or graph model
326 // TODO: This is just a placeholder
327 pm_idx_t *pm_vsize = new pm_idx_t[nVtx];
328 for (size_t i = 0; i < nVtx; i++) pm_vsize[i] = 1;
329
330 pm_real_t itr = 100.; // Same default as in Zoltan
331 pm_return = ParMETIS_V3_AdaptiveRepart(pm_vtxdist, pm_offsets, pm_adjs,
332 pm_vwgts,
333 pm_vsize, pm_ewgts, &pm_wgtflag,
334 &pm_numflag, &pm_nCon, &pm_nPart,
335 pm_partsizes, pm_imbTols,
336 &itr, pm_options, &pm_edgecut,
337 pm_partList, &mpicomm);
338 delete [] pm_vsize;
339 }
340 // else if (parmetis_method == "REFINE_KWAY") {
341 // We do not currently have an execution path that calls REFINE_KWAY.
342 // pm_return = ParMETIS_V3_RefineKway(pm_vtxdist, pm_offsets, pm_adjs,
343 // pm_vwgts, pm_ewgts, &pm_wgtflag,
344 // &pm_numflag, &pm_nCon, &pm_nPart,
345 // pm_partsizes, pm_imbTols, pm_options,
346 // &pm_edgecut, pm_partList, &mpicomm);
347 // }
348 else {
349 // We should not reach this condition.
350 throw std::logic_error("\nInvalid ParMETIS method requested.\n");
351 }
352
353 // Clean up
354 delete [] pm_partsizes;
355 delete [] pm_imbTols;
356 }
357
358 // Load answer into the solution.
359
360 ArrayRCP<part_t> partList;
361 if (nVtx)
362 TPL_Traits<part_t, pm_idx_t>::SAVE_ARRAYRCP(&partList, pm_partList, nVtx);
364
365 solution->setParts(partList);
366
367 env->memory("Zoltan2-ParMETIS: After creating solution");
368
369 // Clean up copies made due to differing data sizes.
372 if (nEdge)
374
375 if (nVwgt) delete [] pm_vwgts;
376 if (nEwgt) delete [] pm_ewgts;
377
378 if (pm_return != METIS_OK) {
379 throw std::runtime_error(
380 "\nParMETIS returned an error; no valid partition generated.\n"
381 "Look for 'PARMETIS ERROR' in your output for more details.\n");
382 }
383}
384
386// Scale and round scalar_t weights (typically float or double) to
387// ParMETIS' idx_t (typically int or long).
388// subject to sum(weights) <= max_wgt_sum.
389// Scale only if deemed necessary.
390//
391// Note that we use ceil() instead of round() to avoid
392// rounding to zero weights.
393// Based on Zoltan's scale_round_weights, mode 1
394
395 template <typename Adapter, typename Model>
396 void AlgParMETIS<Adapter, Model>::scale_weights(
397 size_t n,
398 ArrayView<StridedData<typename Adapter::lno_t,
399 typename Adapter::scalar_t> > &fwgts,
400 pm_idx_t *iwgts
401)
402{
403 const double INT_EPSILON = 1e-5;
404 const int nWgt = fwgts.size();
405
406 int *nonint_local = new int[nWgt+nWgt];
407 int *nonint = nonint_local + nWgt;
408
409 double *sum_wgt_local = new double[nWgt*4];
410 double *max_wgt_local = sum_wgt_local + nWgt;
411 double *sum_wgt = max_wgt_local + nWgt;
412 double *max_wgt = sum_wgt + nWgt;
413
414 for (int i = 0; i < nWgt; i++) {
415 nonint_local[i] = 0;
416 sum_wgt_local[i] = 0.;
417 max_wgt_local[i] = 0;
418 }
419
420 // Compute local sums of the weights
421 // Check whether all weights are integers
422 for (int j = 0; j < nWgt; j++) {
423 for (size_t i = 0; i < n; i++) {
424 double fw = double(fwgts[j][i]);
425 if (!nonint_local[j]) {
426 pm_idx_t tmp = (pm_idx_t) floor(fw + .5); /* Nearest int */
427 if (fabs((double)tmp-fw) > INT_EPSILON) {
428 nonint_local[j] = 1;
429 }
430 }
431 sum_wgt_local[j] += fw;
432 if (fw > max_wgt_local[j]) max_wgt_local[j] = fw;
433 }
434 }
435
436 Teuchos::reduceAll<int,int>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
437 nonint_local, nonint);
438 Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_SUM, nWgt,
439 sum_wgt_local, sum_wgt);
440 Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
441 max_wgt_local, max_wgt);
442
443 const double max_wgt_sum = double(std::numeric_limits<pm_idx_t>::max()/8);
444 for (int j = 0; j < nWgt; j++) {
445 double scale = 1.;
446
447 // Scaling needed if weights are not integers or weights'
448 // range is not sufficient
449 if (nonint[j] || (max_wgt[j]<=INT_EPSILON) || (sum_wgt[j]>max_wgt_sum)) {
450 /* Calculate scale factor */
451 if (sum_wgt[j] != 0.) scale = max_wgt_sum/sum_wgt[j];
452 }
453
454 /* Convert weights to positive integers using the computed scale factor */
455 for (size_t i = 0; i < n; i++)
456 iwgts[i*nWgt+j] = (pm_idx_t) ceil(double(fwgts[j][i])*scale);
457 }
458 delete [] nonint_local;
459 delete [] sum_wgt_local;
460}
461
462} // namespace Zoltan2
463
464#endif // PARMETIS VERSION 4 OR HIGHER CHECK
465
466#endif // HAVE_ZOLTAN2_MPI
467
468#endif // HAVE_ZOLTAN2_PARMETIS
469
470#endif
Defines the GraphModel interface.
Defines the PartitioningSolution class.
#define HELLO
A gathering of useful namespace methods.
AlgParMETIS(const RCP< const Environment > &, const RCP< const Comm< int > > &, const RCP< Model > &)
Algorithm defines the base class for all algorithms.
virtual void partition(const RCP< PartitioningSolution< Adapter > > &)
Partitioning method.
Adapter::part_t part_t
Adapter::scalar_t scalar_t
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:17
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:18
Created by mbenlioglu on Aug 31, 2020.
SparseMatrixAdapter_t::part_t part_t
static void DELETE_ARRAY(first_t **a)
static void ASSIGN_ARRAY(first_t **a, ArrayView< second_t > &b)
static void ASSIGN(first_t &a, second_t b)
static void SAVE_ARRAYRCP(ArrayRCP< first_t > *a, second_t *b, size_t size)