/usr/include/trilinos/Zoltan2_AlgParMETIS.hpp is in libtrilinos-zoltan2-dev 12.4.2-2.
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 | // @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__),
mpicomm(TeuchosConst2MPI(problemComm__)),
model(model__)
{ }
void partition(const RCP<PartitioningSolution<Adapter> > &solution);
private:
const RCP<const Environment> env;
const RCP<const Comm<int> > problemComm;
MPI_Comm mpicomm;
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 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_TPL_T(pm_nVtx, nVtx, env);
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,lno_t>::ASSIGN_TPL_T_ARRAY(&pm_offsets, offsets, env);
pm_idx_t *pm_adjs;
TPL_Traits<pm_idx_t,gno_t>::ASSIGN_TPL_T_ARRAY(&pm_adjs, adjgnos, env);
// Build vtxdist
pm_idx_t *pm_vtxdist = new pm_idx_t[np+1];
pm_vtxdist[0] = 0;
Teuchos::gatherAll(*problemComm, 1, &pm_nVtx, np, &(pm_vtxdist[1]));
for (int i = 2; i <= np; i++)
pm_vtxdist[i] += pm_vtxdist[i-1];
// Create array for ParMETIS to return results in.
// Note: ParMETIS does not like NULL arrays,
// so add 1 to always have non-null.
// See Zoltan bug 4299.
pm_idx_t *pm_partList = new pm_idx_t[nVtx+1];
// Get target part sizes and imbalance tolerances
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);
}
pm_real_t *pm_imbTols = new pm_real_t[pm_nCon];
for (pm_idx_t dim = 0; dim < pm_nCon; dim++)
pm_imbTols[dim] = 1.05; // TODO: GET THE PARAMETER
std::string parmetis_method("PARTKWAY");
pm_idx_t pm_wgtflag = 2*(nVwgt > 0) + (nEwgt > 0);
pm_idx_t pm_numflag = 0;
pm_idx_t pm_nPart;
TPL_Traits<pm_idx_t,size_t>::ASSIGN_TPL_T(pm_nPart, numGlobalParts, env);
if (parmetis_method == "PARTKWAY") {
pm_idx_t pm_edgecut = -1;
pm_idx_t pm_options[3];
pm_options[0] = 0; // Use default options
pm_options[1] = 0; // Debug level (ignored if pm_options[0] == 0)
pm_options[2] = 0; // Seed (ignored if pm_options[0] == 0)
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
std::cout << "NOT READY FOR ADAPTIVE_REPART YET" << std::endl;
exit(-1);
}
else if (parmetis_method == "PART_GEOM") {
// Get coordinate info, too.
std::cout << "NOT READY FOR PART_GEOM YET" << std::endl;
exit(-1);
}
// Clean up
delete [] pm_vtxdist;
delete [] pm_partsizes;
delete [] pm_imbTols;
// Load answer into the solution.
ArrayRCP<part_t> partList;
if (TPL_Traits<pm_idx_t, part_t>::OK_TO_CAST_TPL_T()) {
partList = ArrayRCP<part_t>((part_t *)pm_partList, 0, nVtx, true);
}
else {
// TODO Probably should have a TPL_Traits function to do the following
partList = ArrayRCP<part_t>(new part_t[nVtx], 0, nVtx, true);
for (size_t i = 0; i < nVtx; i++) {
partList[i] = part_t(pm_partList[i]);
}
delete [] 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,lno_t>::DELETE_TPL_T_ARRAY(&pm_offsets);
TPL_Traits<pm_idx_t,gno_t>::DELETE_TPL_T_ARRAY(&pm_adjs);
if (nVwgt) delete [] pm_vwgts;
if (nEwgt) delete [] pm_ewgts;
}
/////////////////////////////////////////////////////////////////////////////
// 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
|