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