/usr/include/trilinos/Zoltan2_MetricUtility.hpp is in libtrilinos-zoltan2-dev 12.10.1-3.
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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | // @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
/*! \file Zoltan2_MetricFunctions.hpp
* \functions which were with the metric classes but do not explicitly depend on them
*/
#ifndef ZOLTAN2_METRICFUNCTIONS_HPP
#define ZOLTAN2_METRICFUNCTIONS_HPP
#include <Zoltan2_StridedData.hpp>
namespace Zoltan2{
///////////////////////////////////////////////////////////////////
// Namespace methods to compute metric values
///////////////////////////////////////////////////////////////////
/*! \brief Find min, max and sum of metric values.
* \param v a list of values
* \param stride the value such that \c v[offset + stride*i]
* will be included in the calculation for all possible i.
* \param offset the offset at which calculation will begin.
* \param min on return, min will hold the minimum of the values.
* \param max on return, max will hold the maximum of the values.
* \param sum on return, sum will hold the sum of the values.
*/
template <typename scalar_t>
void getStridedStats(const ArrayView<scalar_t> &v, int stride,
int offset, scalar_t &min, scalar_t &max, scalar_t &sum)
{
if (v.size() < 1) return;
min = max = sum = v[offset];
for (int i=offset+stride; i < v.size(); i += stride){
if (v[i] < min) min = v[i];
else if (v[i] > max) max = v[i];
sum += v[i];
}
}
/*! \brief Find max and sum of graph metric values.
* \param v a list of values
* \param stride the value such that \c v[offset + stride*i]
* will be included in the calculation for all possible i.
* \param offset the offset at which calculation will begin.
* \param max on return, max will hold the maximum of the values.
* \param sum on return, sum will hold the sum of the values.
*/
template <typename scalar_t>
void getStridedStats(const ArrayView<scalar_t> &v, int stride,
int offset, scalar_t &max, scalar_t &sum)
{
if (v.size() < 1) return;
max = sum = v[offset];
for (int i=offset+stride; i < v.size(); i += stride){
if (v[i] > max) max = v[i];
sum += v[i];
}
}
/*! \brief Compute the total weight in each part on this process.
*
* \param env the Environment for error messages
* \param numberOfParts the number of Parts with respect to
* which weights should be computed.
* \param parts the part Id for each object, which may range
* from 0 to one less than \c numberOfParts
* \param vwgts \c vwgts[w] is the StridedData object
* representing weight index
* \c w. vwgts[w][i] is the \c w'th weight for object \c i.
* \param mcNorm the multiCriteria norm, to be used if the number of weights
* is greater than one.
* \param weights on return, \c weights[p] is the total weight for part
\c p. \c weights is allocated by the caller
*
* \todo - Zoltan_norm() in Zoltan may scale the weight. Do we ever need this?
*/
template <typename scalar_t, typename lno_t, typename part_t>
void normedPartWeights(
const RCP<const Environment> &env,
part_t numberOfParts,
const ArrayView<const part_t> &parts,
const ArrayView<StridedData<lno_t, scalar_t> > &vwgts,
multiCriteriaNorm mcNorm,
scalar_t *weights)
{
env->localInputAssertion(__FILE__, __LINE__, "parts or weights",
numberOfParts > 0 && vwgts.size() > 0, BASIC_ASSERTION);
int numObjects = parts.size();
int vwgtDim = vwgts.size();
memset(weights, 0, sizeof(scalar_t) * numberOfParts);
if (numObjects == 0)
return;
if (vwgtDim == 0) {
for (lno_t i=0; i < numObjects; i++){
weights[parts[i]]++;
}
}
else if (vwgtDim == 1){
for (lno_t i=0; i < numObjects; i++){
weights[parts[i]] += vwgts[0][i];
}
}
else{
switch (mcNorm){
case normMinimizeTotalWeight: /*!< 1-norm = Manhattan norm */
for (int wdim=0; wdim < vwgtDim; wdim++){
for (lno_t i=0; i < numObjects; i++){
weights[parts[i]] += vwgts[wdim][i];
}
} // next weight index
break;
case normBalanceTotalMaximum: /*!< 2-norm = sqrt of sum of squares */
for (lno_t i=0; i < numObjects; i++){
scalar_t ssum = 0;
for (int wdim=0; wdim < vwgtDim; wdim++)
ssum += (vwgts[wdim][i] * vwgts[wdim][i]);
weights[parts[i]] += sqrt(ssum);
}
break;
case normMinimizeMaximumWeight: /*!< inf-norm = maximum norm */
for (lno_t i=0; i < numObjects; i++){
scalar_t max = 0;
for (int wdim=0; wdim < vwgtDim; wdim++)
if (vwgts[wdim][i] > max)
max = vwgts[wdim][i];
weights[parts[i]] += max;
}
break;
default:
env->localBugAssertion(__FILE__, __LINE__, "invalid norm", false,
BASIC_ASSERTION);
break;
}
}
}
/*! \brief Compute the imbalance
* \param numExistingParts the max Part ID + 1, which is the
* length of the \c vals array.
* \param targetNumParts the number of parts desired, which is the
* length of the \c psizes array if it is defined.
* \param psizes if part sizes are not uniform then <tt> psizes[p]</tt>
* is the part size for part \c p, for \c p ranging from zero
* to one less than \c targetNumParts. Part sizes must sum to one.
* If part sizes are uniform, \c psizes should be NULL.
* \param sumVals is the sum of the values in the \c vals list.
* \param vals <tt> vals[p] </tt> is the amount in part \c p, for \c p
* ranging from zero to one less than \c numExistingParts.
* \param min on return, min will be the minimum (best)
* imbalance of all the parts.
* \param max on return, max will be the maximum imbalance of all the parts.
* \param avg on return avg will be the average imbalance across the parts.
*
* Imbalance is a value between zero and one. If \c target is the desired
* amount in part \c p and \c actual is the actual amount in part \c p, then
* the imbalance is:
\code
abs(target - actual) / target
\endcode
*
* If the part is supposed to be empty (\c target is zero), then no
* imbalance is computed for that part. If \c actual for that part
* is non-zero, then other parts are too small and the imbalance will
* be found in those other parts.
*/
template <typename scalar_t, typename part_t>
void computeImbalances(
part_t numExistingParts, // Max Part ID + 1
part_t targetNumParts, // comm.size() or requested global # parts from soln
const scalar_t *psizes,
scalar_t sumVals,
const scalar_t *vals,
scalar_t &min,
scalar_t &max,
scalar_t &avg)
{
min = sumVals;
max = avg = 0;
if (sumVals <= 0 || targetNumParts < 1 || numExistingParts < 1)
return;
if (targetNumParts==1) {
min = max = avg = 0; // 0 imbalance
return;
}
if (!psizes){
scalar_t target = sumVals / targetNumParts;
for (part_t p=0; p < numExistingParts; p++){
scalar_t diff = vals[p] - target;
scalar_t adiff = (diff >= 0 ? diff : -diff);
scalar_t tmp = diff / target;
scalar_t atmp = adiff / target;
avg += atmp;
if (tmp > max) max = tmp;
if (tmp < min) min = tmp;
}
part_t emptyParts = targetNumParts - numExistingParts;
if (emptyParts > 0){
if (max < 1.0)
max = 1.0; // target divided by target
avg += emptyParts;
}
}
else{
for (part_t p=0; p < targetNumParts; p++){
if (psizes[p] > 0){
if (p < numExistingParts){
scalar_t target = sumVals * psizes[p];
scalar_t diff = vals[p] - target;
scalar_t adiff = (diff >= 0 ? diff : -diff);
scalar_t tmp = diff / target;
scalar_t atmp = adiff / target;
avg += atmp;
if (tmp > max) max = tmp;
if (tmp < min) min = tmp;
}
else{
if (max < 1.0)
max = 1.0; // target divided by target
avg += 1.0;
}
}
}
}
avg /= targetNumParts;
}
/*! \brief Compute the imbalance in the case of multiple part sizes.
*
* \param numExistingParts the max Part ID + 1, which is the
* length of the \c vals array.
* \param targetNumParts the number of parts desired, which is the
* length of the \c psizes array if it is defined.
* \param numSizes the number of part size arrays
* \param psizes is an array of \c numSizes pointers to part size arrays.
* If the part sizes for index \c w are uniform, then
* <tt>psizes[w]</tt> should be NULL. Otherwise it should
* point to \c targetNumParts sizes, and the sizes for each
* index should sum to one.
* \param sumVals is the sum of the values in the \c vals list.
* \param vals <tt> vals[p] </tt> is the amount in part \c p, for \c p
* ranging from zero to one less than \c numExistingParts.
* \param min on return, min will be the minimum (best) imbalance
* of all the parts.
* \param max on return, max will be the maximum imbalance of all the parts.
* \param avg on return avg will be the average imbalance across the parts.
*
* Imbalance is a value between zero and one. If \c target is the desired
* amount in part \c p and \c actual is the actual amount in part \c p, then
* the imbalance is:
\code
abs(target - actual) / target
\endcode
*
* If the part is supposed to be empty (\c target is zero), then no
* imbalance is computed for that part. If \c actual for that part
* is non-zero, then other parts are too small and the imbalance will
* be found in those other parts.
*/
template <typename scalar_t, typename part_t>
void computeImbalances(
part_t numExistingParts,
part_t targetNumParts,
int numSizes,
ArrayView<ArrayRCP<scalar_t> > psizes,
scalar_t sumVals,
const scalar_t *vals,
scalar_t &min,
scalar_t &max,
scalar_t &avg)
{
min = sumVals;
max = avg = 0;
if (sumVals <= 0 || targetNumParts < 1 || numExistingParts < 1)
return;
if (targetNumParts==1) {
min = max = avg = 0; // 0 imbalance
return;
}
bool allUniformParts = true;
for (int i=0; i < numSizes; i++){
if (psizes[i].size() != 0){
allUniformParts = false;
break;
}
}
if (allUniformParts){
computeImbalances<scalar_t, part_t>(numExistingParts, targetNumParts, NULL,
sumVals, vals, min, max, avg);
return;
}
double uniformSize = 1.0 / targetNumParts;
std::vector<double> sizeVec(numSizes);
for (int i=0; i < numSizes; i++){
sizeVec[i] = uniformSize;
}
for (part_t p=0; p < numExistingParts; p++){
// If we have objects in parts that should have 0 objects,
// we don't compute an imbalance. It means that other
// parts have too few objects, and the imbalance will be
// picked up there.
if (p >= targetNumParts)
break;
// Vector of target amounts: T
double targetNorm = 0;
for (int i=0; i < numSizes; i++) {
if (psizes[i].size() > 0)
sizeVec[i] = psizes[i][p];
sizeVec[i] *= sumVals;
targetNorm += (sizeVec[i] * sizeVec[i]);
}
targetNorm = sqrt(targetNorm);
// If part is supposed to be empty, we don't compute an
// imbalance. Same argument as above.
if (targetNorm > 0){
// Vector of actual amounts: A
std::vector<double> actual(numSizes);
double actualNorm = 0.;
for (int i=0; i < numSizes; i++) {
actual[i] = vals[p] * -1.0;
actual[i] += sizeVec[i];
actualNorm += (actual[i] * actual[i]);
}
actualNorm = sqrt(actualNorm);
// |A - T| / |T|
scalar_t imbalance = actualNorm / targetNorm;
if (imbalance < min)
min = imbalance;
else if (imbalance > max)
max = imbalance;
avg += imbalance;
}
}
part_t numEmptyParts = 0;
for (part_t p=numExistingParts; p < targetNumParts; p++){
bool nonEmptyPart = false;
for (int i=0; !nonEmptyPart && i < numSizes; i++)
if (psizes[i].size() > 0 && psizes[i][p] > 0.0)
nonEmptyPart = true;
if (nonEmptyPart){
// The partition has no objects for this part, which
// is supposed to be non-empty.
numEmptyParts++;
}
}
if (numEmptyParts > 0){
avg += numEmptyParts;
if (max < 1.0)
max = 1.0; // target divided by target
}
avg /= targetNumParts;
}
/*! \brief Compute the norm of the vector of weights.
*/
template <typename scalar_t>
scalar_t normedWeight(ArrayView <scalar_t> weights,
multiCriteriaNorm norm)
{
size_t dim = weights.size();
if (dim == 0)
return 0.0;
else if (dim == 1)
return weights[0];
scalar_t nweight = 0;
switch (norm){
case normMinimizeTotalWeight: /*!< 1-norm = Manhattan norm */
for (size_t i=0; i <dim; i++)
nweight += weights[i];
break;
case normBalanceTotalMaximum: /*!< 2-norm = sqrt of sum of squares */
for (size_t i=0; i <dim; i++)
nweight += (weights[i] * weights[i]);
nweight = sqrt(nweight);
break;
case normMinimizeMaximumWeight: /*!< inf-norm = maximum norm */
nweight = weights[0];
for (size_t i=1; i <dim; i++)
if (weights[i] > nweight)
nweight = weights[i];
break;
default:
Environment env; // default environment
env.localBugAssertion(__FILE__, __LINE__, "invalid norm", false,
BASIC_ASSERTION);
break;
}
return nweight;
}
/*! \brief Compute the norm of the vector of weights stored as StridedData.
*/
template<typename lno_t, typename scalar_t>
scalar_t normedWeight(ArrayView<StridedData<lno_t, scalar_t> > weights,
lno_t idx, multiCriteriaNorm norm)
{
size_t dim = weights.size();
if (dim < 1)
return 0;
Array<scalar_t> vec(dim, 1.0);
for (size_t i=0; i < dim; i++)
if (weights[i].size() > 0)
vec[dim] = weights[i][idx];
return normedWeight(vec, norm);
}
} //namespace Zoltan2
#endif
|