Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_AlgSerialGreedy.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_ALGSERIALGREEDY_HPP_
46 #define _ZOLTAN2_ALGSERIALGREEDY_HPP_
47 
48 #include <Zoltan2_Algorithm.hpp>
49 #include <Zoltan2_GraphModel.hpp>
51 
55 
56 namespace Zoltan2{
57 
58 template <typename Adapter>
59 class AlgSerialGreedy : public Algorithm<Adapter>
60 {
61  private:
62  typedef typename Adapter::lno_t lno_t;
63  typedef typename Adapter::gno_t gno_t;
64  typedef typename Adapter::offset_t offset_t;
65  typedef typename Adapter::scalar_t scalar_t;
66  // Class member variables
67  RCP<GraphModel<typename Adapter::base_adapter_t> > model_;
68  RCP<Teuchos::ParameterList> pl_;
69  RCP<Environment> env_;
70  RCP<const Teuchos::Comm<int> > comm_;
71 
72  public:
75  const RCP<Teuchos::ParameterList> &pl,
76  const RCP<Environment> &env,
77  const RCP<const Teuchos::Comm<int> > &comm
78  ) : model_(model), pl_(pl), env_(env), comm_(comm)
79  {
80  }
81 
82  // Main entry point for graph coloring.
83  void color(
84  const RCP<ColoringSolution<Adapter> > &solution
85  )
86  {
87  HELLO;
88 
89  // Color local graph. Global coloring is supported in Zoltan (not Zoltan2).
90  // Get local graph.
91  ArrayView<const gno_t> edgeIds;
92  ArrayView<const offset_t> offsets;
93  ArrayView<StridedData<lno_t, scalar_t> > wgts; // Not used; needed by getLocalEdgeList
94 
95  const size_t nVtx = model_->getLocalNumVertices(); // Assume (0,nvtx-1)
96  model_->getEdgeList(edgeIds, offsets, wgts); // Don't need wgts
97 
98 #if 0
99  // Debug
100  cout << "Debug: Local graph from getLocalEdgeList" << endl;
101  cout << "rank " << comm_->getRank() << ": nVtx= " << nVtx << endl;
102  cout << "rank " << comm_->getRank() << ": edgeIds: " << edgeIds << endl;
103  cout << "rank " << comm_->getRank() << ": offsets: " << offsets << endl;
104 #endif
105 
106  // Get color array to fill.
107  // TODO: Allow user to input an old coloring.
108  ArrayRCP<int> colors = solution->getColorsRCP();
109  for (size_t i=0; i<nVtx; i++){
110  colors[i] = 0;
111  }
112 
113  // Let colorCrsGraph do the real work.
114  env_->timerStart(MACRO_TIMERS, "Coloring algorithm");
115  colorCrsGraph(nVtx, edgeIds, offsets, colors);
116  env_->timerStop(MACRO_TIMERS, "Coloring algorithm");
117  return;
118  }
119 
120  // Color graph given by two arrays. API may change. Expert users only!
122  const size_t nVtx,
123  ArrayView<const gno_t> edgeIds,
124  ArrayView<const offset_t> offsets,
125  ArrayRCP<int> colors
126  )
127  {
128  HELLO;
129 
130  // Find max degree, since (max degree)+1 is an upper bound.
131  offset_t maxDegree = 0;
132  for (size_t i=0; i<nVtx; i++){
133  if (offsets[i+1]-offsets[i] > maxDegree)
134  maxDegree = offsets[i+1]-offsets[i];
135  }
136 
137  // Greedy coloring.
138  // Use natural order for now.
139  // TODO: Support better orderings (e.g., Smallest-Last)
140  int maxColor = 0;
141 
142  // array of size #colors: forbidden[i]=v means color[v]=i so i is forbidden
143  Teuchos::Array<int> forbidden(maxDegree+2, 0);
144 
145  // LeastUsed: need array of size #colors
146  Teuchos::Array<lno_t> numVerticesWithColor(maxDegree+2, 0);
147 
148  // Get colorChoice from parameter list.
149  Teuchos::ParameterList &pl = env_->getParametersNonConst();
150  std::string colorChoice = pl.get<std::string>("color_choice", "FirstFit");
151 
152  for (size_t i=0; i<nVtx; i++){
153  //std::cout << "Debug: i= " << i << std::endl;
154  lno_t v=i; // TODO: Use ordering here.
155  for (offset_t j=offsets[v]; j<offsets[v+1]; j++){
156  gno_t nbor = edgeIds[j];
157  //std::cout << "Debug: nbor= " << nbor << ", color= " << colors[nbor] << std::endl;
158  if (colors[nbor] > 0){
159  // Neighbors' colors are forbidden
160  forbidden[colors[nbor]] = v;
161  }
162  }
163 
164  // Pick color for v
165 
166  // Keep colors[v] if possible, otherwise find valid color.
167  if ((colors[v]==0) || ((colors[v]>0) && forbidden[colors[v]] == v)){
168 
169  if (colorChoice.compare("FirstFit")){
170  // Pick first (smallest) available color > 0
171  for (int c=1; c <= maxColor+1; c++){
172  if (forbidden[c] != v){
173  colors[v] = c;
174  break;
175  }
176  }
177  }
178  else if (colorChoice.compare("Random")){
179  // Pick random available color.
180  // Truely random is slow, please consider RandomFast instead.
181  int numAvail = 0;
182  Teuchos::Array<int> avail(maxColor+1);
183  for (int c=1; c < maxColor+1; c++){
184  if (forbidden[c] != v){
185  avail[numAvail++] = c;
186  }
187  }
188  if (numAvail==0)
189  colors[v] = maxColor+1;
190  else
191  colors[v] = avail[rand()%numAvail];
192  }
193  else if (colorChoice.compare("RandomFast")){
194  // Pick random color, then find first available color after that.
195  bool foundColor = false;
196  int r = (rand() % maxColor) +1;
197  for (int c=r; c <= maxColor; c++){
198  if (forbidden[c] != v){
199  colors[v] = c;
200  foundColor = true;
201  break;
202  }
203  }
204  if (!foundColor){ // Look for colors in [1, r)
205  for (int c=1; c < r; c++){
206  if (forbidden[c] != v){
207  colors[v] = c;
208  foundColor = true;
209  break;
210  }
211  }
212  }
213  if (!foundColor) colors[v] = maxColor+1;
214  }
215  else if (colorChoice.compare("LeastUsed")){
216  // Pick least used available color.
217  // Simple linear algorithm; could maintain a priority queue but not sure any faster?
218  int leastUsedColor = 1;
219  for (int c=1; c <= maxColor; c++){
220  if (forbidden[c] != v){
221  if (numVerticesWithColor[c] < leastUsedColor){
222  leastUsedColor = c;
223  }
224  }
225  }
226  colors[v] = leastUsedColor;
227 
228  // Update color counts
229  numVerticesWithColor[colors[v]]++;
230  }
231 
232  if ((v==0) && colors[v]==0) colors[v]=1; // Corner case for first vertex
233 
234  // If we used a new color, increase maxColor.
235  if (colors[v] > maxColor){
236  maxColor = colors[v];
237  }
238  }
239  }
240 
241  return;
242  }
243 
246  static void getValidParameters(ParameterList & pl)
247  {
248  RCP<Teuchos::StringValidator> color_choice_Validator = Teuchos::rcp(
249  new Teuchos::StringValidator(
250  Teuchos::tuple<std::string>(
251  "FirstFit", "Random", "RandomFast", "LeastUsed" )));
252  pl.set("color_choice", "FirstFit", "selection criterion for coloring",
253  color_choice_Validator);
254  }
255 };
256 }
257 #endif
#define HELLO
Time an algorithm (or other entity) as a whole.
void color(const RCP< ColoringSolution< Adapter > > &solution)
Coloring method.
AlgSerialGreedy(const RCP< GraphModel< typename Adapter::base_adapter_t > > &model, const RCP< Teuchos::ParameterList > &pl, const RCP< Environment > &env, const RCP< const Teuchos::Comm< int > > &comm)
Algorithm defines the base class for all algorithms.
GraphModel defines the interface required for graph models.
Defines the ColoringSolution class.
Defines the GraphModel interface.
static void getValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
void colorCrsGraph(const size_t nVtx, ArrayView< const gno_t > edgeIds, ArrayView< const offset_t > offsets, ArrayRCP< int > colors)
The class containing coloring solution.