Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_RebalanceColoring.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef _ZOLTAN2_REBALANCECOLORING_HPP_
11 #define _ZOLTAN2_REBALANCECOLORING_HPP_
12 
13 #include <Zoltan2_Standards.hpp>
14 
15 // Given a valid coloring of a graph, rebalance the colors
16 // such that either:
17 // (a) color classes are as balanced as possible, or
18 // (b) every color class has at least minSize vertices.
19 // This function can be called as a post-processing after any initial
20 // coloring.
21 // This is a greedy heuristic so there is no guarantee of success,
22 // though in practice we believe it will work well.
24  const lno_t nVtx,
25  ArrayView<const lno_t> edgeIds,
26  ArrayView<const offset_t> offsets,
27  ArrayRCP<int> colors,
28  const int balanceColors,
29  const lno_t minSize
30  )
31  {
32 #ifndef INCLUDE_ZOLTAN2_EXPERIMENTAL
33  Z2_THROW_EXPERIMENTAL("Zoltan2 rebalanceColoring is experimental and not tested");
34 #else
35  // Experimental: Not tested yet!
36  //
37  // Count size of each color class.
38  // No vertex weights, may add in future.
39  lno_t maxColor = 0;
40  Teuchos::Array<lno_t> colorSize(nVtx,0);
41  for (lno_t i=0; i < nVtx; i++){
42  if (colors[i] > maxColor)
43  maxColor = colors[i];
44  }
45  for (lno_t i=0; i < nVtx; i++){
46  colorSize[colors[i]]++;
47  }
48  lno_t targetSize = 0;
49  if (balanceColors > 0)
50  targetSize = nVtx/maxColor;
51  else
52  targetSize = minSize;
53 
54  // Vertex-centric version: Loop over vertices
55  // and move it to different color if needed.
56  Teuchos::Array<int> forbidden(maxDegree+2, 0);
57  for (lno_t i=0; i < nVtx; i++){
58  if (colorSize[colors[i]] > targetSize){
59  // Find first available color that is not full yet
60  for (offset_t j=offsets[i]; j<offsets[i+1]; j++){
61  lno_t nbor = edgeIds[j];
62  if (colors[nbor] > 0){
63  // Neighbors' colors are forbidden
64  forbidden[colors[nbor]] = i;
65  }
66  }
67  // Pick first (smallest) underfull color > 0.
68  // If no such color, just keep colors[i].
69  int newcolor = colors[i];
70  for (int c=1; c <= maxColor; c++){
71  if ((forbidden[c] != i) && (colorSize[c]<targetSize)){
72  newcolor = c;
73  break;
74  }
75  }
76 
77  // Move vertex i to underfull color.
78  colorSize[colors[i]]--;
79  colorSize[newcolor]++;
80  colors[i] = newcolor;
81  }
82  }
83 
84 #endif
85  }
86 
87 #endif
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
#define Z2_THROW_EXPERIMENTAL(mystr)
Throw an error when experimental code is requested but not compiled.
Gathering definitions used in software development.
int rebalanceColoring(const lno_t nVtx, ArrayView< const lno_t > edgeIds, ArrayView< const offset_t > offsets, ArrayRCP< int > colors, const int balanceColors, const lno_t minSize)