EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_MapColoring.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
42 #include <EpetraExt_MapColoring.h>
43 
46 
47 #include <Epetra_CrsGraph.h>
48 #include <Epetra_IntVector.h>
49 #include <Epetra_MapColoring.h>
50 #include <Epetra_Map.h>
51 #include <Epetra_Comm.h>
52 #include <Epetra_Util.h>
53 #include <Epetra_Import.h>
54 #include <Epetra_Export.h>
55 
56 #include <Epetra_Time.h>
57 
58 #include <vector>
59 #include <set>
60 #include <map>
61 
62 using std::vector;
63 using std::set;
64 using std::map;
65 
66 namespace EpetraExt {
67 
71 {
72  Epetra_Time timer( orig.Comm() );
73 
74  origObj_ = &orig;
75 
76  const Epetra_BlockMap & RowMap = orig.RowMap();
77  int nRows = RowMap.NumMyElements();
78  const Epetra_BlockMap & ColMap = orig.ColMap();
79  int nCols = ColMap.NumMyElements();
80 
81  int MyPID = RowMap.Comm().MyPID();
82 
83  if( verbosity_ > 1 ) std::cout << "RowMap:\n" << RowMap;
84  if( verbosity_ > 1 ) std::cout << "ColMap:\n" << ColMap;
85 
86  Epetra_MapColoring * ColorMap = 0;
87 
88  if( algo_ == GREEDY || algo_ == LUBY )
89  {
90 
91  Epetra_CrsGraph * base = &( const_cast<Epetra_CrsGraph&>(orig) );
92 
93  int NumIndices;
94  int * Indices;
95 
96  double wTime1 = timer.WallTime();
97 
98  // For parallel applications, add in boundaries to coloring
99  bool distributedGraph = RowMap.DistributedGlobal();
100  if( distributedGraph )
101  {
102  base = new Epetra_CrsGraph( Copy, ColMap, ColMap, 0 );
103 
104  for( int i = 0; i < nRows; ++i )
105  {
106  orig.ExtractMyRowView( i, NumIndices, Indices );
107  base->InsertMyIndices( i, NumIndices, Indices );
108 
109  //Do this with a single insert
110  //Is this the right thing?
111  for( int j = 0; j < NumIndices; ++j )
112  if( Indices[j] >= nRows )
113  base->InsertMyIndices( Indices[j], 1, &i );
114  }
115  base->FillComplete();
116  }
117 
118  if( verbosity_ > 1 ) std::cout << "Base Graph:\n" << *base << std::endl;
119 
120  double wTime2 = timer.WallTime();
121  if( verbosity_ > 0 )
122  std::cout << "EpetraExt::MapColoring [INSERT BOUNDARIES] Time: " << wTime2-wTime1 << std::endl;
123 
124  //Generate Local Distance-1 Adjacency Graph
125  //(Transpose of orig excluding non-local column indices)
126  EpetraExt::CrsGraph_Transpose transposeTransform( true );
127  Epetra_CrsGraph & Adj1 = transposeTransform( *base );
128  if( verbosity_ > 1 ) std::cout << "Adjacency 1 Graph!\n" << Adj1;
129 
130  wTime1 = timer.WallTime();
131  if( verbosity_ > 0 )
132  std::cout << "EpetraExt::MapColoring [TRANSPOSE GRAPH] Time: " << wTime1-wTime2 << std::endl;
133 
134  int Delta = Adj1.MaxNumIndices();
135  if( verbosity_ > 0 ) std::cout << std::endl << "Delta: " << Delta << std::endl;
136 
137  //Generation of Local Distance-2 Adjacency Graph
138  Epetra_CrsGraph * Adj2 = &Adj1;
139  if( !distance1_ )
140  {
141  Adj2 = new Epetra_CrsGraph( Copy, ColMap, ColMap, 0 );
142  int NumAdj1Indices;
143  int * Adj1Indices;
144  for( int i = 0; i < nCols; ++i )
145  {
146  Adj1.ExtractMyRowView( i, NumAdj1Indices, Adj1Indices );
147 
148  set<int> Cols;
149  for( int j = 0; j < NumAdj1Indices; ++j )
150  {
151  base->ExtractMyRowView( Adj1Indices[j], NumIndices, Indices );
152  for( int k = 0; k < NumIndices; ++k )
153  if( Indices[k] < nCols ) Cols.insert( Indices[k] );
154  }
155  int nCols2 = Cols.size();
156  std::vector<int> ColVec( nCols2 );
157  set<int>::iterator iterIS = Cols.begin();
158  set<int>::iterator iendIS = Cols.end();
159  for( int j = 0 ; iterIS != iendIS; ++iterIS, ++j ) ColVec[j] = *iterIS;
160  Adj2->InsertMyIndices( i, nCols2, &ColVec[0] );
161  }
162  Adj2->FillComplete();
163 
164  if( verbosity_ > 1 ) std::cout << "Adjacency 2 Graph!\n" << *Adj2;
165 
166  wTime2 = timer.WallTime();
167  if( verbosity_ > 0 )
168  std::cout << "EpetraExt::MapColoring [GEN DIST-2 GRAPH] Time: " << wTime2-wTime1 << std::endl;
169  }
170 
171  wTime2 = timer.WallTime();
172 
173  ColorMap = new Epetra_MapColoring( ColMap );
174 
175  std::vector<int> rowOrder( nCols );
176  if( reordering_ == 0 || reordering_ == 1 )
177  {
178  std::multimap<int,int> adjMap;
179  typedef std::multimap<int,int>::value_type adjMapValueType;
180  for( int i = 0; i < nCols; ++i )
181  adjMap.insert( adjMapValueType( Adj2->NumMyIndices(i), i ) );
182  std::multimap<int,int>::iterator iter = adjMap.begin();
183  std::multimap<int,int>::iterator end = adjMap.end();
184  if( reordering_ == 0 ) //largest first (less colors)
185  {
186  for( int i = 1; iter != end; ++iter, ++i )
187  rowOrder[ nCols - i ] = (*iter).second;
188  }
189  else //smallest first (better balance)
190  {
191  for( int i = 0; iter != end; ++iter, ++i )
192  rowOrder[ i ] = (*iter).second;
193  }
194  }
195  else if( reordering_ == 2 ) //random
196  {
197  for( int i = 0; i < nCols; ++i )
198  rowOrder[ i ] = i;
199 #ifndef TFLOP
200  std::random_shuffle( rowOrder.begin(), rowOrder.end() );
201 #endif
202  }
203 
204  wTime1 = timer.WallTime();
205  if( verbosity_ > 0 )
206  std::cout << "EpetraExt::MapColoring [REORDERING] Time: " << wTime1-wTime2 << std::endl;
207 
208  //Application of Greedy Algorithm to generate Color Map
209  if( algo_ == GREEDY )
210  {
211  for( int col = 0; col < nCols; ++col )
212  {
213  Adj2->ExtractMyRowView( rowOrder[col], NumIndices, Indices );
214 
215  set<int> usedColors;
216  int color;
217  for( int i = 0; i < NumIndices; ++i )
218  {
219  color = (*ColorMap)[ Indices[i] ];
220  if( color > 0 ) usedColors.insert( color );
221  color = 0;
222  int testcolor = 1;
223  while( !color )
224  {
225  if( !usedColors.count( testcolor ) ) color = testcolor;
226  ++testcolor;
227  }
228  }
229  (*ColorMap)[ rowOrder[col] ] = color;
230  }
231 
232  wTime2 = timer.WallTime();
233  if( verbosity_ > 0 )
234  std::cout << "EpetraExt::MapColoring [GREEDY COLORING] Time: " << wTime2-wTime1 << std::endl;
235  if( verbosity_ > 0 )
236  std::cout << "Num GREEDY Colors: " << ColorMap->NumColors() << std::endl;
237  }
238  else if( algo_ == LUBY )
239  {
240  //Assign Random Keys To Rows
241  Epetra_Util util;
242  std::vector<int> Keys(nCols);
243  std::vector<int> State(nCols,-1);
244 
245  for( int col = 0; col < nCols; ++col )
246  Keys[col] = util.RandomInt();
247 
248  int NumRemaining = nCols;
249  int CurrentColor = 1;
250 
251  while( NumRemaining > 0 )
252  {
253  //maximal independent set
254  while( NumRemaining > 0 )
255  {
256  NumRemaining = 0;
257 
258  //zero out everyone less than neighbor
259  for( int i = 0; i < nCols; ++i )
260  {
261  int col = rowOrder[i];
262  if( State[col] < 0 )
263  {
264  Adj2->ExtractMyRowView( col, NumIndices, Indices );
265  int MyKey = Keys[col];
266  for( int j = 0; j < NumIndices; ++j )
267  if( col != Indices[j] && State[ Indices[j] ] < 0 )
268  {
269  if( MyKey > Keys[ Indices[j] ] ) State[ Indices[j] ] = 0;
270  else State[ col ] = 0;
271  }
272  }
273  }
274 
275  //assign -1's to current color
276  for( int col = 0; col < nCols; ++col )
277  {
278  if( State[col] < 0 )
279  State[col] = CurrentColor;
280  }
281 
282  //reinstate any zero not neighboring current color
283  for( int col = 0; col < nCols; ++col )
284  if( State[col] == 0 )
285  {
286  Adj2->ExtractMyRowView( col, NumIndices, Indices );
287  bool flag = false;
288  for( int i = 0; i < NumIndices; ++i )
289  if( col != Indices[i] && State[ Indices[i] ] == CurrentColor )
290  {
291  flag = true;
292  break;
293  }
294  if( !flag )
295  {
296  State[col] = -1;
297  ++NumRemaining;
298  }
299  }
300  }
301 
302  //Reset Status for all non-colored nodes
303  for( int col = 0; col < nCols; ++col )
304  if( State[col] == 0 )
305  {
306  State[col] = -1;
307  ++NumRemaining;
308  }
309 
310  if( verbosity_ > 2 )
311  {
312  std::cout << "Finished Color: " << CurrentColor << std::endl;
313  std::cout << "NumRemaining: " << NumRemaining << std::endl;
314  }
315 
316  //New color
317  ++CurrentColor;
318  }
319 
320  for( int col = 0; col < nCols; ++col )
321  (*ColorMap)[col] = State[col]-1;
322 
323  wTime2 = timer.WallTime();
324  if( verbosity_ > 0 )
325  std::cout << "EpetraExt::MapColoring [LUBI COLORING] Time: " << wTime2-wTime1 << std::endl;
326  if( verbosity_ > 0 )
327  std::cout << "Num LUBI Colors: " << ColorMap->NumColors() << std::endl;
328 
329  }
330  else
331  abort(); //UNKNOWN ALGORITHM
332 
333  if( distributedGraph ) delete base;
334  if( !distance1_ ) delete Adj2;
335  }
336  else
337  {
338  //Generate Parallel Adjacency-2 Graph
339  EpetraExt::CrsGraph_Overlap OverlapTrans(1);
340  Epetra_CrsGraph & OverlapGraph = OverlapTrans( orig );
341 
342  if( verbosity_ > 1 ) std::cout << "OverlapGraph:\n" << OverlapGraph;
343 
344  Epetra_CrsGraph * Adj2 = &orig;
345 
346  int NumIndices;
347  int * Indices;
348  if( !distance1_ )
349  {
350  Adj2 = new Epetra_CrsGraph( Copy, RowMap, OverlapGraph.ColMap(), 0 );
351  int NumAdj1Indices;
352  int * Adj1Indices;
353  for( int i = 0; i < nRows; ++i )
354  {
355  OverlapGraph.ExtractMyRowView( i, NumAdj1Indices, Adj1Indices );
356 
357  set<int> Cols;
358  for( int j = 0; j < NumAdj1Indices; ++j )
359  {
360  int GID = OverlapGraph.LRID( OverlapGraph.GCID( Adj1Indices[j] ) );
361  OverlapGraph.ExtractMyRowView( GID, NumIndices, Indices );
362  for( int k = 0; k < NumIndices; ++k ) Cols.insert( Indices[k] );
363  }
364  int nCols2 = Cols.size();
365  std::vector<int> ColVec( nCols2 );
366  set<int>::iterator iterIS = Cols.begin();
367  set<int>::iterator iendIS = Cols.end();
368  for( int j = 0 ; iterIS != iendIS; ++iterIS, ++j ) ColVec[j] = *iterIS;
369  Adj2->InsertMyIndices( i, nCols2, &ColVec[0] );
370  }
371  int flag = Adj2->FillComplete();
372  assert( flag == 0 );
373  RowMap.Comm().Barrier();
374  if( verbosity_ > 1 ) std::cout << "Adjacency 2 Graph!\n" << *Adj2;
375  }
376 
377  //collect GIDs on boundary
378  std::vector<int> boundaryGIDs;
379  std::vector<int> interiorGIDs;
380  for( int row = 0; row < nRows; ++row )
381  {
382  Adj2->ExtractMyRowView( row, NumIndices, Indices );
383  bool testFlag = false;
384  for( int i = 0; i < NumIndices; ++i )
385  if( Indices[i] >= nRows ) testFlag = true;
386  if( testFlag ) boundaryGIDs.push_back( Adj2->GRID(row) );
387  else interiorGIDs.push_back( Adj2->GRID(row) );
388  }
389 
390  int LocalBoundarySize = boundaryGIDs.size();
391 
392  Epetra_Map BoundaryMap( -1, boundaryGIDs.size(),
393  LocalBoundarySize ? &boundaryGIDs[0]: 0,
394  0, RowMap.Comm() );
395  if( verbosity_ > 1 ) std::cout << "BoundaryMap:\n" << BoundaryMap;
396 
397  int BoundarySize = BoundaryMap.NumGlobalElements();
398  Epetra_MapColoring BoundaryColoring( BoundaryMap );
399 
400  if( algo_ == PSEUDO_PARALLEL )
401  {
402  Epetra_Map BoundaryIndexMap( BoundarySize, LocalBoundarySize, 0, RowMap.Comm() );
403  if( verbosity_ > 1) std::cout << "BoundaryIndexMap:\n" << BoundaryIndexMap;
404 
405  Epetra_IntVector bGIDs( View, BoundaryIndexMap, &boundaryGIDs[0] );
406  if( verbosity_ > 1) std::cout << "BoundaryGIDs:\n" << bGIDs;
407 
408  int NumLocalBs = 0;
409  if( !RowMap.Comm().MyPID() ) NumLocalBs = BoundarySize;
410 
411  Epetra_Map LocalBoundaryIndexMap( BoundarySize, NumLocalBs, 0, RowMap.Comm() );
412  if( verbosity_ > 1) std::cout << "LocalBoundaryIndexMap:\n" << LocalBoundaryIndexMap;
413 
414  Epetra_IntVector lbGIDs( LocalBoundaryIndexMap );
415  Epetra_Import lbImport( LocalBoundaryIndexMap, BoundaryIndexMap );
416  lbGIDs.Import( bGIDs, lbImport, Insert );
417  if( verbosity_ > 1) std::cout << "LocalBoundaryGIDs:\n" << lbGIDs;
418 
419  Epetra_Map LocalBoundaryMap( BoundarySize, NumLocalBs, lbGIDs.Values(), 0, RowMap.Comm() );
420  if( verbosity_ > 1) std::cout << "LocalBoundaryMap:\n" << LocalBoundaryMap;
421 
422  Epetra_CrsGraph LocalBoundaryGraph( Copy, LocalBoundaryMap, LocalBoundaryMap, 0 );
423  Epetra_Import LocalBoundaryImport( LocalBoundaryMap, Adj2->RowMap() );
424  LocalBoundaryGraph.Import( *Adj2, LocalBoundaryImport, Insert );
425  LocalBoundaryGraph.FillComplete();
426  if( verbosity_ > 1 ) std::cout << "LocalBoundaryGraph:\n " << LocalBoundaryGraph;
427 
428  EpetraExt::CrsGraph_MapColoring BoundaryTrans( GREEDY, reordering_, distance1_, verbosity_ );
429  Epetra_MapColoring & LocalBoundaryColoring = BoundaryTrans( LocalBoundaryGraph );
430  if( verbosity_ > 1 ) std::cout << "LocalBoundaryColoring:\n " << LocalBoundaryColoring;
431 
432  Epetra_Export BoundaryExport( LocalBoundaryMap, BoundaryMap );
433  BoundaryColoring.Export( LocalBoundaryColoring, BoundaryExport, Insert );
434  }
435  else if( algo_ == JONES_PLASSMAN )
436  {
437  /* Alternative Distrib. Memory Coloring of Boundary based on JonesPlassman(sic) paper
438  * 1.Random number assignment to all boundary nodes using GID as seed to function
439  * (This allows any processor to compute adj. off proc values with a local computation)
440  * 2.Find all nodes greater than any neighbor off processor, color them.
441  * 3.Send colored node info to neighbors
442  * 4.Constrained color all nodes with all off proc neighbors smaller or colored.
443  * 5.Goto 3
444  */
445 
446  std::vector<int> OverlapBoundaryGIDs( boundaryGIDs );
447  for( int i = nRows; i < Adj2->ColMap().NumMyElements(); ++i )
448  OverlapBoundaryGIDs.push_back( Adj2->ColMap().GID(i) );
449 
450  int OverlapBoundarySize = OverlapBoundaryGIDs.size();
451  Epetra_Map BoundaryColMap( -1, OverlapBoundarySize,
452  OverlapBoundarySize ? &OverlapBoundaryGIDs[0] : 0,
453  0, RowMap.Comm() );
454 
455  Epetra_CrsGraph BoundaryGraph( Copy, BoundaryMap, BoundaryColMap, 0 );
456  Epetra_Import BoundaryImport( BoundaryMap, Adj2->RowMap() );
457  BoundaryGraph.Import( *Adj2, BoundaryImport, Insert );
458  BoundaryGraph.FillComplete();
459  if( verbosity_ > 1) std::cout << "BoundaryGraph:\n" << BoundaryGraph;
460 
461  Epetra_Import ReverseOverlapBoundaryImport( BoundaryMap, BoundaryColMap );
462  Epetra_Import OverlapBoundaryImport( BoundaryColMap, BoundaryMap );
463 
464  int Colored = 0;
465  int GlobalColored = 0;
466  int Level = 0;
467  Epetra_MapColoring OverlapBoundaryColoring( BoundaryColMap );
468 
469  //Setup random integers for boundary nodes
470  Epetra_IntVector BoundaryValues( BoundaryMap );
471  Epetra_Util Util;
472  Util.SetSeed( 47954118 * (MyPID+1) );
473  for( int i=0; i < LocalBoundarySize; ++i )
474  {
475  int val = Util.RandomInt();
476  if( val < 0 ) val *= -1;
477  BoundaryValues[i] = val;
478  }
479 
480  //Get Random Values for External Boundary
481  Epetra_IntVector OverlapBoundaryValues( BoundaryColMap );
482  OverlapBoundaryValues.Import( BoundaryValues, OverlapBoundaryImport, Insert );
483 
484  while( GlobalColored < BoundarySize )
485  {
486  //Find current "Level" of boundary indices to color
487  int NumIndices;
488  int * Indices;
489  std::vector<int> LevelIndices;
490  for( int i = 0; i < LocalBoundarySize; ++i )
491  {
492  if( !OverlapBoundaryColoring[i] )
493  {
494  //int MyVal = PRAND(BoundaryColMap.GID(i));
495  int MyVal = OverlapBoundaryValues[i];
496  BoundaryGraph.ExtractMyRowView( i, NumIndices, Indices );
497  bool ColorFlag = true;
498  int Loc = 0;
499  while( Loc<NumIndices && Indices[Loc]<LocalBoundarySize ) ++Loc;
500  for( int j = Loc; j < NumIndices; ++j )
501  if( (OverlapBoundaryValues[Indices[j]]>MyVal)
502  && !OverlapBoundaryColoring[Indices[j]] )
503  {
504  ColorFlag = false;
505  break;
506  }
507  if( ColorFlag ) LevelIndices.push_back(i);
508  }
509  }
510 
511  if( verbosity_ > 1 )
512  {
513  std::cout << MyPID << " Level Indices: ";
514  int Lsize = (int) LevelIndices.size();
515  for( int i = 0; i < Lsize; ++i ) std::cout << LevelIndices[i] << " ";
516  std::cout << std::endl;
517  }
518 
519  //Greedy coloring of current level
520  set<int> levelColors;
521  int Lsize = (int) LevelIndices.size();
522  for( int i = 0; i < Lsize; ++i )
523  {
524  BoundaryGraph.ExtractMyRowView( LevelIndices[i], NumIndices, Indices );
525 
526  set<int> usedColors;
527  int color;
528  for( int j = 0; j < NumIndices; ++j )
529  {
530  color = OverlapBoundaryColoring[ Indices[j] ];
531  if( color > 0 ) usedColors.insert( color );
532  color = 0;
533  int testcolor = 1;
534  while( !color )
535  {
536  if( !usedColors.count( testcolor ) ) color = testcolor;
537  ++testcolor;
538  }
539  }
540  OverlapBoundaryColoring[ LevelIndices[i] ] = color;
541  levelColors.insert( color );
542  }
543 
544  if( verbosity_ > 2 ) std::cout << MyPID << " Level: " << Level << " Count: " << LevelIndices.size() << " NumColors: " << levelColors.size() << std::endl;
545 
546  if( verbosity_ > 2 ) std::cout << "Current Level Boundary Coloring:\n" << OverlapBoundaryColoring;
547 
548  //Update off processor coloring info
549  BoundaryColoring.Import( OverlapBoundaryColoring, ReverseOverlapBoundaryImport, Insert );
550  OverlapBoundaryColoring.Import( BoundaryColoring, OverlapBoundaryImport, Insert );
551  Colored += LevelIndices.size();
552  Level++;
553 
554  RowMap.Comm().SumAll( &Colored, &GlobalColored, 1 );
555  if( verbosity_ > 2 ) std::cout << "Num Globally Colored: " << GlobalColored << " from Num Global Boundary Nodes: " << BoundarySize << std::endl;
556  }
557  }
558 
559  if( verbosity_ > 1 ) std::cout << "BoundaryColoring:\n " << BoundaryColoring;
560 
561  Epetra_MapColoring RowColorMap( RowMap );
562 
563  //Add Boundary Colors
564  for( int i = 0; i < LocalBoundarySize; ++i )
565  {
566  int GID = BoundaryMap.GID(i);
567  RowColorMap(GID) = BoundaryColoring(GID);
568  }
569 
570  Epetra_MapColoring Adj2ColColorMap( Adj2->ColMap() );
571  Epetra_Import Adj2Import( Adj2->ColMap(), RowMap );
572  Adj2ColColorMap.Import( RowColorMap, Adj2Import, Insert );
573 
574  if( verbosity_ > 1 ) std::cout << "RowColoringMap:\n " << RowColorMap;
575  if( verbosity_ > 1 ) std::cout << "Adj2ColColorMap:\n " << Adj2ColColorMap;
576 
577  std::vector<int> rowOrder( nRows );
578  if( reordering_ == 0 || reordering_ == 1 )
579  {
580  std::multimap<int,int> adjMap;
581  typedef std::multimap<int,int>::value_type adjMapValueType;
582  for( int i = 0; i < nRows; ++i )
583  adjMap.insert( adjMapValueType( Adj2->NumMyIndices(i), i ) );
584  std::multimap<int,int>::iterator iter = adjMap.begin();
585  std::multimap<int,int>::iterator end = adjMap.end();
586  if( reordering_ == 0 ) //largest first (less colors)
587  {
588  for( int i = 1; iter != end; ++iter, ++i )
589  rowOrder[nRows-i] = (*iter).second;
590  }
591  else //smallest first (better balance)
592  {
593  for( int i = 0; iter != end; ++iter, ++i )
594  rowOrder[i] = (*iter).second;
595  }
596  }
597  else if( reordering_ == 2 ) //random
598  {
599  for( int i = 0; i < nRows; ++i )
600  rowOrder[i] = i;
601 #ifdef TFLOP
602  random_shuffle( rowOrder.begin(), rowOrder.end() );
603 #endif
604  }
605 
606  //Constrained greedy coloring of interior
607  set<int> InteriorColors;
608  for( int row = 0; row < nRows; ++row )
609  {
610  if( !RowColorMap[ rowOrder[row] ] )
611  {
612  Adj2->ExtractMyRowView( rowOrder[row], NumIndices, Indices );
613 
614  set<int> usedColors;
615  int color;
616  for( int i = 0; i < NumIndices; ++i )
617  {
618  color = Adj2ColColorMap[ Indices[i] ];
619  if( color > 0 ) usedColors.insert( color );
620  color = 0;
621  int testcolor = 1;
622  while( !color )
623  {
624  if( !usedColors.count( testcolor ) ) color = testcolor;
625  ++testcolor;
626  }
627  }
628  Adj2ColColorMap[ rowOrder[row] ] = color;
629  InteriorColors.insert( color );
630  }
631  }
632  if( verbosity_ > 1 ) std::cout << MyPID << " Num Interior Colors: " << InteriorColors.size() << std::endl;
633  if( verbosity_ > 1 ) std::cout << "RowColorMap after Greedy:\n " << RowColorMap;
634 
635  ColorMap = new Epetra_MapColoring( ColMap );
636  Epetra_Import ColImport( ColMap, Adj2->ColMap() );
637  ColorMap->Import( Adj2ColColorMap, ColImport, Insert );
638 
639  if( !distance1_ ) delete Adj2;
640  }
641 
642  if( verbosity_ > 0 ) std::cout << MyPID << " ColorMap Color Count: " << ColorMap->NumColors() << std::endl;
643  if( verbosity_ > 1 ) std::cout << "ColorMap!\n" << *ColorMap;
644 
645  newObj_ = ColorMap;
646 
647  return *ColorMap;
648 }
649 
650 } // namespace EpetraExt
Transform to generate the explicit transpose of a Epetra_CrsGraph.
Given an input Epetra_CrsGraph, a &quot;overlapped&quot; Epetra_CrsGraph is generated including rows associated...
CrsGraph_MapColoring::NewTypeRef operator()(CrsGraph_MapColoring::OriginalTypeRef orig)
Generates the Epetra_MapColoring object from an input Epetra_CrsGraph.
Map Coloring of independent columns in a Graph.