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 
48 #include <Zoltan2_GraphModel.hpp>
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 
63 namespace Zoltan2 {
64 template <typename Adapter>
65 class AlgParMETIS : public Algorithm<Adapter>
66 {
67 public:
68  AlgParMETIS(const RCP<const Environment> &env,
69  const RCP<const Comm<int> > &problemComm,
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 
95 extern "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 
109 namespace Zoltan2 {
110 
111 template <typename Adapter>
112 class AlgParMETIS : public Algorithm<Adapter>
113 {
114 public:
115 
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;
121 
122  typedef idx_t pm_idx_t;
123  typedef real_t pm_real_t;
124 
135  AlgParMETIS(const RCP<const Environment> &env__,
136  const RCP<const Comm<int> > &problemComm__,
137  const RCP<graphModel_t> &model__) :
138  env(env__), problemComm(problemComm__),
139  model(model__)
140  { }
141 
142  void partition(const RCP<PartitioningSolution<Adapter> > &solution);
143 
144 private:
145 
146  const RCP<const Environment> env;
147  const RCP<const Comm<int> > problemComm;
148  const RCP<GraphModel<typename Adapter::base_adapter_t> > model;
149 
150  void scale_weights(size_t n, ArrayView<StridedData<lno_t, scalar_t> > &fwgts,
151  pm_idx_t *iwgts);
152 };
153 
154 
156 template <typename Adapter>
158  const RCP<PartitioningSolution<Adapter> > &solution
159 )
160 {
161  HELLO;
162 
163  size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
164 
165  int me = problemComm->getRank();
166  int np = problemComm->getSize();
167 
168  // Get vertex info
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);
173  pm_idx_t pm_nVtx;
175 
176  pm_idx_t *pm_vwgts = NULL;
177  if (nVwgt) {
178  pm_vwgts = new pm_idx_t[nVtx*nVwgt];
179  scale_weights(nVtx, vwgts, pm_vwgts);
180  }
181 
182  // Get edge info
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);
188 
189  pm_idx_t *pm_ewgts = NULL;
190  if (nEwgt) {
191  pm_ewgts = new pm_idx_t[nEdge*nEwgt];
192  scale_weights(nEdge, ewgts, pm_ewgts);
193  }
194 
195  // Convert index types for edges, if needed
196  pm_idx_t *pm_offsets;
198  pm_idx_t *pm_adjs;
199  pm_idx_t pm_dummy_adj;
200  if (nEdge)
202  else
203  pm_adjs = &pm_dummy_adj; // ParMETIS does not like NULL pm_adjs;
204 
205 
206  // Build vtxdist
207  pm_idx_t *pm_vtxdist;
208  ArrayView<size_t> vtxdist;
209  model->getVertexDist(vtxdist);
211 
212  // ParMETIS does not like processors having no vertices.
213  // Inspect vtxdist and remove from communicator procs that have no vertices
214  RCP<Comm<int> > subcomm;
215  MPI_Comm mpicomm; // Note: mpicomm is valid only while subcomm is in scope
216 
217  if (np > 1) {
218  int nKeep = 0;
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];
224  nKeep++;
225  }
226  }
227  pm_vtxdist[nKeep] = pm_vtxdist[np];
228  if (nKeep < np) {
229  subcomm = problemComm->createSubcommunicator(keepRanks.view(0,nKeep));
230  if (subcomm != Teuchos::null)
231  mpicomm = Teuchos::getRawMpiComm(*subcomm);
232  else
233  mpicomm = MPI_COMM_NULL;
234  }
235  else {
236  mpicomm = Teuchos::getRawMpiComm(*problemComm);
237  }
238  }
239  else {
240  mpicomm = Teuchos::getRawMpiComm(*problemComm);
241  }
242 
243  // Create array for ParMETIS to return results in.
244  pm_idx_t *pm_partList = NULL;
245  if (nVtx) pm_partList = new pm_idx_t[nVtx];
246 
247  if (mpicomm != MPI_COMM_NULL) {
248  // If in ParMETIS' communicator (i.e., have vertices), call ParMETIS
249 
250  // Get target part sizes
251  pm_idx_t pm_nCon = (nVwgt == 0 ? 1 : pm_idx_t(nVwgt));
252  pm_real_t *pm_partsizes = new pm_real_t[numGlobalParts*pm_nCon];
253  for (pm_idx_t dim = 0; dim < pm_nCon; dim++) {
254  if (!solution->criteriaHasUniformPartSizes(dim))
255  for (size_t i=0; i<numGlobalParts; i++)
256  pm_partsizes[i*pm_nCon+dim] =
257  pm_real_t(solution->getCriteriaPartSize(dim,i));
258  else
259  for (size_t i=0; i<numGlobalParts; i++)
260  pm_partsizes[i*pm_nCon+dim] = pm_real_t(1.)/pm_real_t(numGlobalParts);
261  }
262 
263  // Get imbalance tolerances
264  double tolerance = 1.1;
265  const Teuchos::ParameterList &pl = env->getParameters();
266  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("imbalance_tolerance");
267  if (pe) tolerance = pe->getValue<double>(&tolerance);
268 
269  // ParMETIS requires tolerance to be greater than 1.0;
270  // fudge it if condition is not met
271  if (tolerance <= 1.0) {
272  if (me == 0)
273  std::cerr << "Warning: ParMETIS requires imbalance_tolerance > 1.0; "
274  << "to comply, Zoltan2 reset imbalance_tolerance to 1.01."
275  << std::endl;
276  tolerance = 1.01;
277  }
278 
279  pm_real_t *pm_imbTols = new pm_real_t[pm_nCon];
280  for (pm_idx_t dim = 0; dim < pm_nCon; dim++)
281  pm_imbTols[dim] = pm_real_t(tolerance);
282 
283  // Other ParMETIS parameters?
284  std::string parmetis_method("PARTKWAY");
285  pm_idx_t pm_wgtflag = 2*(nVwgt > 0) + (nEwgt > 0);
286  pm_idx_t pm_numflag = 0;
287 
288  pm_idx_t pm_nPart;
289  TPL_Traits<pm_idx_t,size_t>::ASSIGN_TPL_T(pm_nPart, numGlobalParts);
290 
291  if (parmetis_method == "PARTKWAY") {
292 
293  pm_idx_t pm_edgecut = -1;
294  pm_idx_t pm_options[METIS_NOPTIONS];
295  pm_options[0] = 1; // Use non-default options for some ParMETIS options
296  for (int i = 0; i < METIS_NOPTIONS; i++)
297  pm_options[i] = 0; // Default options
298  pm_options[2] = 15; // Matches default value used in Zoltan
299 
300  ParMETIS_V3_PartKway(pm_vtxdist, pm_offsets, pm_adjs, pm_vwgts, pm_ewgts,
301  &pm_wgtflag, &pm_numflag, &pm_nCon, &pm_nPart,
302  pm_partsizes, pm_imbTols, pm_options,
303  &pm_edgecut, pm_partList, &mpicomm);
304  }
305  else if (parmetis_method == "ADAPTIVE_REPART") {
306  // Get object sizes
307  std::cout << "NOT READY FOR ADAPTIVE_REPART YET" << std::endl;
308  exit(-1);
309  }
310  else if (parmetis_method == "PART_GEOM") {
311  // Get coordinate info, too.
312  std::cout << "NOT READY FOR PART_GEOM YET" << std::endl;
313  exit(-1);
314  }
315 
316  // Clean up
317  delete [] pm_partsizes;
318  delete [] pm_imbTols;
319  }
320 
321  // Load answer into the solution.
322 
323  ArrayRCP<part_t> partList;
324  if (nVtx) {
326  partList = ArrayRCP<part_t>((part_t *)pm_partList, 0, nVtx, true);
327  }
328  else {
329  // TODO Probably should have a TPL_Traits function to do the following
330  partList = ArrayRCP<part_t>(new part_t[nVtx], 0, nVtx, true);
331  for (size_t i = 0; i < nVtx; i++) {
332  partList[i] = part_t(pm_partList[i]);
333  }
334  delete [] pm_partList;
335  }
336  }
337 
338  solution->setParts(partList);
339 
340  env->memory("Zoltan2-ParMETIS: After creating solution");
341 
342  // Clean up copies made due to differing data sizes.
345  if (nEdge)
347 
348  if (nVwgt) delete [] pm_vwgts;
349  if (nEwgt) delete [] pm_ewgts;
350 }
351 
353 // Scale and round scalar_t weights (typically float or double) to
354 // ParMETIS' idx_t (typically int or long).
355 // subject to sum(weights) <= max_wgt_sum.
356 // Scale only if deemed necessary.
357 //
358 // Note that we use ceil() instead of round() to avoid
359 // rounding to zero weights.
360 // Based on Zoltan's scale_round_weights, mode 1
361 
362 template <typename Adapter>
364  size_t n,
365  ArrayView<StridedData<typename Adapter::lno_t,
366  typename Adapter::scalar_t> > &fwgts,
367  pm_idx_t *iwgts
368 )
369 {
370  const double INT_EPSILON = 1e-5;
371  const int nWgt = fwgts.size();
372 
373  int *nonint_local = new int[nWgt+nWgt];
374  int *nonint = nonint_local + nWgt;
375 
376  double *sum_wgt_local = new double[nWgt*4];
377  double *max_wgt_local = sum_wgt_local + nWgt;
378  double *sum_wgt = max_wgt_local + nWgt;
379  double *max_wgt = sum_wgt + nWgt;
380 
381  for (int i = 0; i < nWgt; i++) {
382  nonint_local[i] = 0;
383  sum_wgt_local[i] = 0.;
384  max_wgt_local[i] = 0;
385  }
386 
387  // Compute local sums of the weights
388  // Check whether all weights are integers
389  for (int j = 0; j < nWgt; j++) {
390  for (size_t i = 0; i < n; i++) {
391  double fw = double(fwgts[j][i]);
392  if (!nonint_local[j]) {
393  pm_idx_t tmp = (pm_idx_t) floor(fw + .5); /* Nearest int */
394  if (fabs((double)tmp-fw) > INT_EPSILON) {
395  nonint_local[j] = 1;
396  }
397  }
398  sum_wgt_local[j] += fw;
399  if (fw > max_wgt_local[j]) max_wgt_local[j] = fw;
400  }
401  }
402 
403  Teuchos::reduceAll<int,int>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
404  nonint_local, nonint);
405  Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_SUM, nWgt,
406  sum_wgt_local, sum_wgt);
407  Teuchos::reduceAll<int,double>(*problemComm, Teuchos::REDUCE_MAX, nWgt,
408  max_wgt_local, max_wgt);
409 
410  const double max_wgt_sum = double(std::numeric_limits<pm_idx_t>::max()/8);
411  for (int j = 0; j < nWgt; j++) {
412  double scale = 1.;
413 
414  // Scaling needed if weights are not integers or weights'
415  // range is not sufficient
416  if (nonint[j] || (max_wgt[j]<=INT_EPSILON) || (sum_wgt[j]>max_wgt_sum)) {
417  /* Calculate scale factor */
418  if (sum_wgt[j] != 0.) scale = max_wgt_sum/sum_wgt[j];
419  }
420 
421  /* Convert weights to positive integers using the computed scale factor */
422  for (size_t i = 0; i < n; i++)
423  iwgts[i*nWgt+j] = (pm_idx_t) ceil(double(fwgts[j][i])*scale);
424  }
425  delete [] nonint_local;
426  delete [] sum_wgt_local;
427 }
428 
429 } // namespace Zoltan2
430 
431 #endif // PARMETIS VERSION 4 OR HIGHER CHECK
432 
433 #endif // HAVE_ZOLTAN2_MPI
434 
435 #endif // HAVE_ZOLTAN2_PARMETIS
436 
437 #endif
#define HELLO
static void DELETE_TPL_T_ARRAY(tpl_t **a)
Defines the PartitioningSolution class.
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
A PartitioningSolution is a solution to a partitioning problem.
Adapter::part_t part_t
The StridedData class manages lists of weights or coordinates.
Algorithm defines the base class for all algorithms.
static void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView< zno_t > &b)
virtual void partition(const RCP< PartitioningSolution< Adapter > > &solution)
Partitioning method.
GraphModel defines the interface required for graph models.
static void ASSIGN_TPL_T(tpl_t &a, zno_t b)
Defines the GraphModel interface.
A gathering of useful namespace methods.