Reference documentation for deal.II version 9.1.0-pre
graph_coloring.h
1 
2 // ---------------------------------------------------------------------
3 //
4 // Copyright (C) 2013 - 2017 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE.md at
13 // the top level directory of deal.II.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef dealii_graph_coloring_h
18 # define dealii_graph_coloring_h
19 
20 
21 # include <deal.II/base/config.h>
22 
23 # include <deal.II/base/thread_management.h>
24 
25 # include <deal.II/lac/sparsity_tools.h>
26 
27 # include <boost/unordered_map.hpp>
28 # include <boost/unordered_set.hpp>
29 
30 # include <functional>
31 # include <set>
32 # include <vector>
33 
34 
35 DEAL_II_NAMESPACE_OPEN
36 
40 namespace GraphColoring
41 {
42  namespace internal
43  {
53  inline bool
54  have_nonempty_intersection(
55  const std::vector<types::global_dof_index> &indices1,
56  const std::vector<types::global_dof_index> &indices2)
57  {
58  // we assume that both arrays are sorted, so we can walk
59  // them in lockstep and see if we encounter an index that's
60  // in both arrays. once we reach the end of either array,
61  // we know that there is no intersection
62  std::vector<types::global_dof_index>::const_iterator p = indices1.begin(),
63  q = indices2.begin();
64  while ((p != indices1.end()) && (q != indices2.end()))
65  {
66  if (*p < *q)
67  ++p;
68  else if (*p > *q)
69  ++q;
70  else
71  // conflict found!
72  return true;
73  }
74 
75  // no conflict found!
76  return false;
77  }
78 
79 
110  template <typename Iterator>
111  std::vector<std::vector<Iterator>>
112  create_partitioning(
113  const Iterator & begin,
114  const typename identity<Iterator>::type &end,
115  const std::function<std::vector<types::global_dof_index>(
116  const Iterator &)> & get_conflict_indices)
117  {
118  // Number of iterators.
119  unsigned int n_iterators = 0;
120 
121  // Create a map from conflict indices to iterators
122  boost::unordered_map<types::global_dof_index, std::vector<Iterator>>
123  indices_to_iterators;
124  for (Iterator it = begin; it != end; ++it)
125  {
126  const std::vector<types::global_dof_index> conflict_indices =
127  get_conflict_indices(it);
128  const unsigned int n_conflict_indices = conflict_indices.size();
129  for (unsigned int i = 0; i < n_conflict_indices; ++i)
130  indices_to_iterators[conflict_indices[i]].push_back(it);
131  ++n_iterators;
132  }
133 
134  // create the very first zone which contains only the first
135  // iterator. then create the other zones. keep track of all the
136  // iterators that have already been assigned to a zone
137  std::vector<std::vector<Iterator>> zones(1,
138  std::vector<Iterator>(1, begin));
139  std::set<Iterator> used_it;
140  used_it.insert(begin);
141  while (used_it.size() != n_iterators)
142  {
143  // loop over the elements of the previous zone. for each element of
144  // the previous zone, get the conflict indices and from there get
145  // those iterators that are conflicting with the current element
146  typename std::vector<Iterator>::iterator previous_zone_it(
147  zones.back().begin());
148  typename std::vector<Iterator>::iterator previous_zone_end(
149  zones.back().end());
150  std::vector<Iterator> new_zone;
151  for (; previous_zone_it != previous_zone_end; ++previous_zone_it)
152  {
153  const std::vector<types::global_dof_index> conflict_indices =
154  get_conflict_indices(*previous_zone_it);
155 
156  const unsigned int n_conflict_indices(conflict_indices.size());
157  for (unsigned int i = 0; i < n_conflict_indices; ++i)
158  {
159  const std::vector<Iterator> &conflicting_elements =
160  indices_to_iterators[conflict_indices[i]];
161  for (unsigned int j = 0; j < conflicting_elements.size(); ++j)
162  {
163  // check that the iterator conflicting with the current
164  // one is not associated to a zone yet and if so, assign
165  // it to the current zone. mark it as used
166  //
167  // we can shortcut this test if the conflicting iterator
168  // is the current iterator
169  if ((conflicting_elements[j] != *previous_zone_it) &&
170  (used_it.count(conflicting_elements[j]) == 0))
171  {
172  new_zone.push_back(conflicting_elements[j]);
173  used_it.insert(conflicting_elements[j]);
174  }
175  }
176  }
177  }
178 
179  // If there are iterators in the new zone, then the zone is added to
180  // the partition. Otherwise, the graph is disconnected and we need to
181  // find an iterator on the other part of the graph. start the whole
182  // process again with the first iterator that hasn't been assigned to
183  // a zone yet
184  if (new_zone.size() != 0)
185  zones.push_back(new_zone);
186  else
187  for (Iterator it = begin; it != end; ++it)
188  if (used_it.count(it) == 0)
189  {
190  zones.push_back(std::vector<Iterator>(1, it));
191  used_it.insert(it);
192  break;
193  }
194  }
195 
196  return zones;
197  }
198 
199 
200 
220  template <typename Iterator>
221  void
222  make_dsatur_coloring(
223  std::vector<Iterator> & partition,
224  const std::function<std::vector<types::global_dof_index>(
225  const Iterator &)> & get_conflict_indices,
226  std::vector<std::vector<Iterator>> &partition_coloring)
227  {
228  partition_coloring.clear();
229 
230  // Number of zones composing the partitioning.
231  const unsigned int partition_size(partition.size());
232  std::vector<unsigned int> sorted_vertices(partition_size);
233  std::vector<int> degrees(partition_size);
234  std::vector<std::vector<types::global_dof_index>> conflict_indices(
235  partition_size);
236  std::vector<std::vector<unsigned int>> graph(partition_size);
237 
238  // Get the conflict indices associated to each iterator. The
239  // conflict_indices have to be sorted so we can more easily find conflicts
240  // later on
241  for (unsigned int i = 0; i < partition_size; ++i)
242  {
243  conflict_indices[i] = get_conflict_indices(partition[i]);
244  std::sort(conflict_indices[i].begin(), conflict_indices[i].end());
245  }
246 
247  // Compute the degree of each vertex of the graph using the
248  // intersection of the conflict indices.
249  for (unsigned int i = 0; i < partition_size; ++i)
250  for (unsigned int j = i + 1; j < partition_size; ++j)
251  // If the two iterators share indices then we increase the degree of
252  // the vertices and create an ''edge'' in the graph.
253  if (have_nonempty_intersection(conflict_indices[i],
254  conflict_indices[j]))
255  {
256  ++degrees[i];
257  ++degrees[j];
258  graph[i].push_back(j);
259  graph[j].push_back(i);
260  }
261 
262  // Sort the vertices by decreasing degree.
263  std::vector<int>::iterator degrees_it;
264  for (unsigned int i = 0; i < partition_size; ++i)
265  {
266  // Find the largest element.
267  degrees_it = std::max_element(degrees.begin(), degrees.end());
268  sorted_vertices[i] = degrees_it - degrees.begin();
269  // Put the largest element to -1 so it cannot be chosen again.
270  *degrees_it = -1;
271  }
272 
273  // Color the graph.
274  std::vector<boost::unordered_set<unsigned int>> colors_used;
275  for (unsigned int i = 0; i < partition_size; ++i)
276  {
277  const unsigned int current_vertex(sorted_vertices[i]);
278  bool new_color(true);
279  // Try to use an existing color, i.e., try to find a color which is
280  // not associated to one of the vertices linked to current_vertex.
281  // Loop over the color.
282  for (unsigned int j = 0; j < partition_coloring.size(); ++j)
283  {
284  // Loop on the vertices linked to current_vertex. If one vertex
285  // linked to current_vertex is already using the color j, this
286  // color cannot be used anymore.
287  bool unused_color(true);
288  for (unsigned int k = 0; k < graph[current_vertex].size(); ++k)
289  if (colors_used[j].count(graph[current_vertex][k]) == 1)
290  {
291  unused_color = false;
292  break;
293  }
294  if (unused_color)
295  {
296  partition_coloring[j].push_back(partition[current_vertex]);
297  colors_used[j].insert(current_vertex);
298  new_color = false;
299  break;
300  }
301  }
302  // Add a new color.
303  if (new_color)
304  {
305  partition_coloring.push_back(
306  std::vector<Iterator>(1, partition[current_vertex]));
307  boost::unordered_set<unsigned int> tmp;
308  tmp.insert(current_vertex);
309  colors_used.push_back(tmp);
310  }
311  }
312  }
313 
314 
315 
325  template <typename Iterator>
326  std::vector<std::vector<Iterator>>
327  gather_colors(
328  const std::vector<std::vector<std::vector<Iterator>>> &partition_coloring)
329  {
330  std::vector<std::vector<Iterator>> coloring;
331 
332  // Count the number of iterators in each color.
333  const unsigned int partition_size(partition_coloring.size());
334  std::vector<std::vector<unsigned int>> colors_counter(partition_size);
335  for (unsigned int i = 0; i < partition_size; ++i)
336  {
337  const unsigned int n_colors(partition_coloring[i].size());
338  colors_counter[i].resize(n_colors);
339  for (unsigned int j = 0; j < n_colors; ++j)
340  colors_counter[i][j] = partition_coloring[i][j].size();
341  }
342 
343  // Find the partition with the largest number of colors for the even
344  // partition.
345  unsigned int i_color(0);
346  unsigned int max_even_n_colors(0);
347  const unsigned int colors_size(colors_counter.size());
348  for (unsigned int i = 0; i < colors_size; i += 2)
349  {
350  if (max_even_n_colors < colors_counter[i].size())
351  {
352  max_even_n_colors = colors_counter[i].size();
353  i_color = i;
354  }
355  }
356  coloring.resize(max_even_n_colors);
357  for (unsigned int j = 0; j < colors_counter[i_color].size(); ++j)
358  coloring[j] = partition_coloring[i_color][j];
359 
360  for (unsigned int i = 0; i < partition_size; i += 2)
361  {
362  if (i != i_color)
363  {
364  boost::unordered_set<unsigned int> used_k;
365  for (unsigned int j = 0; j < colors_counter[i].size(); ++j)
366  {
367  // Find the color in the current partition with the largest
368  // number of iterators.
369  std::vector<unsigned int>::iterator it;
370  it = std::max_element(colors_counter[i].begin(),
371  colors_counter[i].end());
372  unsigned int min_iterators(static_cast<unsigned int>(-1));
373  unsigned int pos(0);
374  // Find the color of coloring with the least number of colors
375  // among the colors that have not been used yet.
376  for (unsigned int k = 0; k < max_even_n_colors; ++k)
377  if (used_k.count(k) == 0)
378  if (colors_counter[i_color][k] < min_iterators)
379  {
380  min_iterators = colors_counter[i_color][k];
381  pos = k;
382  }
383  colors_counter[i_color][pos] += *it;
384  // Concatenate the current color with the existing coloring.
385  coloring[pos].insert(
386  coloring[pos].end(),
387  partition_coloring[i][it - colors_counter[i].begin()]
388  .begin(),
389  partition_coloring[i][it - colors_counter[i].begin()]
390  .end());
391  used_k.insert(pos);
392  // Put the number of iterators to the current color to zero.
393  *it = 0;
394  }
395  }
396  }
397 
398  // If there is more than one partition, do the same thing that we did for
399  // the even partitions to the odd partitions
400  if (partition_size > 1)
401  {
402  unsigned int max_odd_n_colors(0);
403  for (unsigned int i = 1; i < partition_size; i += 2)
404  {
405  if (max_odd_n_colors < colors_counter[i].size())
406  {
407  max_odd_n_colors = colors_counter[i].size();
408  i_color = i;
409  }
410  }
411  coloring.resize(max_even_n_colors + max_odd_n_colors);
412  for (unsigned int j = 0; j < colors_counter[i_color].size(); ++j)
413  coloring[max_even_n_colors + j] = partition_coloring[i_color][j];
414 
415  for (unsigned int i = 1; i < partition_size; i += 2)
416  {
417  if (i != i_color)
418  {
419  boost::unordered_set<unsigned int> used_k;
420  for (unsigned int j = 0; j < colors_counter[i].size(); ++j)
421  {
422  // Find the color in the current partition with the
423  // largest number of iterators.
424  std::vector<unsigned int>::iterator it;
425  it = std::max_element(colors_counter[i].begin(),
426  colors_counter[i].end());
427  unsigned int min_iterators(static_cast<unsigned int>(-1));
428  unsigned int pos(0);
429  // Find the color of coloring with the least number of
430  // colors among the colors that have not been used yet.
431  for (unsigned int k = 0; k < max_odd_n_colors; ++k)
432  if (used_k.count(k) == 0)
433  if (colors_counter[i_color][k] < min_iterators)
434  {
435  min_iterators = colors_counter[i_color][k];
436  pos = k;
437  }
438  colors_counter[i_color][pos] += *it;
439  // Concatenate the current color with the existing
440  // coloring.
441  coloring[max_even_n_colors + pos].insert(
442  coloring[max_even_n_colors + pos].end(),
443  partition_coloring[i][it - colors_counter[i].begin()]
444  .begin(),
445  partition_coloring[i][it - colors_counter[i].begin()]
446  .end());
447  used_k.insert(pos);
448  // Put the number of iterators to the current color to
449  // zero.
450  *it = 0;
451  }
452  }
453  }
454  }
455 
456  return coloring;
457  }
458  } // namespace internal
459 
460 
540  template <typename Iterator>
541  std::vector<std::vector<Iterator>>
543  const Iterator & begin,
544  const typename identity<Iterator>::type & end,
545  const std::function<std::vector<types::global_dof_index>(
546  const typename identity<Iterator>::type &)> &get_conflict_indices)
547  {
548  Assert(begin != end,
549  ExcMessage(
550  "GraphColoring is not prepared to deal with empty ranges!"));
551 
552  // Create the partitioning.
553  std::vector<std::vector<Iterator>> partitioning =
554  internal::create_partitioning(begin, end, get_conflict_indices);
555 
556  // Color the iterators within each partition.
557  // Run the coloring algorithm on each zone in parallel
558  const unsigned int partitioning_size(partitioning.size());
559  std::vector<std::vector<std::vector<Iterator>>> partition_coloring(
560  partitioning_size);
561 
562  Threads::TaskGroup<> tasks;
563  for (unsigned int i = 0; i < partitioning_size; ++i)
564  tasks += Threads::new_task(&internal::make_dsatur_coloring<Iterator>,
565  partitioning[i],
566  get_conflict_indices,
567  partition_coloring[i]);
568  tasks.join_all();
569 
570  // Gather the colors together.
571  return internal::gather_colors(partition_coloring);
572  }
573 
580  unsigned int
581  color_sparsity_pattern(const SparsityPattern & sparsity_pattern,
582  std::vector<unsigned int> &color_indices);
583 
584 } // namespace GraphColoring
585 
586 DEAL_II_NAMESPACE_CLOSE
587 
588 
589 //---------------------------- graph_coloring.h ---------------------------
590 // end of #ifndef dealii_graph_coloring_h
591 #endif
592 //---------------------------- graph_coloring.h ---------------------------
Task< RT > new_task(const std::function< RT()> &function)
std::vector< std::vector< Iterator > > make_graph_coloring(const Iterator &begin, const typename identity< Iterator >::type &end, const std::function< std::vector< types::global_dof_index >(const typename identity< Iterator >::type &)> &get_conflict_indices)
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
unsigned int color_sparsity_pattern(const SparsityPattern &sparsity_pattern, std::vector< unsigned int > &color_indices)