dune-assembler 2.12
Loading...
Searching...
No Matches
gridcoloring.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_ASSEMBLER_PARALLEL_GRIDCOLORING_HH
5#define DUNE_ASSEMBLER_PARALLEL_GRIDCOLORING_HH
6
7#include <algorithm>
8#include <cstddef>
9#include <iostream>
10#include <unordered_map>
11#include <utility>
12#include <vector>
13
15#include <dune/common/timer.hh>
16
18
21
24
26
27
28
58template <class GridView, class ElementMapper, class size_type=uint32_t>
59auto vertexBasedElementAdjacency (const GridView& gridView, const ElementMapper& elementMapper, bool verbose=false)
60{
61 static const auto dim = GridView::dimension;
62
64
65 // Compute a map of vertex index to vector of adjacent elements indices.
66 auto vertexCliques = Impl::FlatBlockedVector<size_type>(subEntityMapper.size());
67
68 // Precompute and reserve block sizes
69 for (const auto& element : elements(gridView))
70 for (std::size_t i = 0; i<element.subEntities(dim); ++i)
71 ++vertexCliques.capacity(subEntityMapper.subIndex(element, i, dim));
72 vertexCliques.initialize();
73
74 for (const auto& element : elements(gridView))
75 {
76 auto elementIndex = elementMapper.index(element);
77 for (std::size_t i = 0; i<element.subEntities(dim); ++i)
78 vertexCliques[subEntityMapper.subIndex(element, i, dim)].push_back(elementIndex);
79 }
80
81 return cliqueListToAdjacency(std::move(vertexCliques).toRange(), elementMapper.size(), verbose);
82}
83
84
85
112template <class GridView, class size_type=uint32_t>
113auto vertexBasedElementAdjacency (const GridView& gridView, bool verbose=false)
114{
116 return vertexBasedElementAdjacency(gridView, elementMapper, verbose);
117}
118
119
120
144template <class GridView, class SubEntityLayout, class ElementMapper, class size_type=uint32_t>
146{
147 static const auto dim = GridView::dimension;
148
150
152 for (const auto& element : elements(gridView))
153 {
154 auto elementIndex = elementMapper.index(element);
155 auto re = Dune::referenceElement<double,dim>(element.type());
156 for (std::size_t codim = 1; codim <= dim; ++codim)
157 for (std::size_t i = 0; i<re.size(codim); ++i)
158 if (subEntityLayout(re.type(i, codim), GridView::dimension))
159 subEntityCliques[subEntityMapper.subIndex(element, i, codim)].push_back(elementIndex);
160 }
161
163}
164
165
166
185template <class GridView, class SubEntityLayout, class size_type=uint32_t>
191
192
193
209template <class GridView, class size_type>
211{
212 size_type colorCount = *std::max_element(coloring.begin(), coloring.end()) + 1;
213
214 using ElementSeed = typename GridView::Grid::template Codim<0>::EntitySeed;
216
219 for (const auto& element : elements(gridView))
220 coloredElementSeedRange[coloring[elementMapper.index(element)]].push_back(element.seed());
221
222 // For convenience transform from range of seeds to range of entities
223 // This is safe because transformedRangeView() stores copies of passed
224 // r-values. Furthermore it transforms random access ranges to random
225 // access ranges, such that the resulting entity range can be split
226 // cheaply.
227 auto seedToElement = [gridPtr = &gridView.grid()] (const auto& seed) {
228 return gridPtr->entity(seed);
229 };
232 });
233}
234
235
236
272template <class GridView, class size_type=uint32_t>
273auto coloredElementRange (const GridView& gridView, bool verbose=false)
274{
276
277 timer.reset();
279 if (verbose)
280 std::cout << "Building adjacency map took " << timer.elapsed() << "s." << std::endl;
281
282 timer.reset();
284 if (verbose)
285 std::cout << "Building coloring took " << timer.elapsed() << "s." << std::endl;
286
287 timer.reset();
289 if (verbose)
290 {
291 std::cout << "Building colored element range took " << timer.elapsed() << "s." << std::endl;
292 std::cout << "Number of used colors is " << coloredElementRange.size() << std::endl;
293 std::size_t elementCount = 0;
294 for (const auto& color : coloredElementRange)
295 elementCount += color.size();
296 std::cout << "Total number of elements in the range is " << elementCount << std::endl;
297 }
298
299 return coloredElementRange;
300}
301
302
303
316template <class ElementRange, class Basis>
317bool checkOverlapFree (const ElementRange& elementRange, const Basis& basis) {
318
319 bool overlapFree = true;
320
322
323 using MI = typename Basis::MultiIndex;
325
326 auto localView = basis.localView();
327 for (const auto& element : elementRange)
328 {
329 localView.bind(element);
330
331 // Check if any of the DOFs of the present element showed up earlier.
332 for (size_t i=0; i<localView.tree().size(); ++i)
333 {
334 auto localIndex = localView.tree().localIndex(i);
335 auto globalIndex = localView.index(localIndex);
336 auto it = processedDOFs.find(globalIndex);
337 if (it != processedDOFs.end())
338 {
339 std::cout << "Overlap detected! Index [" << globalIndex << "] shows up in element "
340 << it->second << " and " << elementMapper.index(element) << std::endl;
341 overlapFree =false;
342 }
343 }
344
345 for (size_t i=0; i<localView.tree().size(); ++i)
346 {
347 auto localIndex = localView.tree().localIndex(i);
348 auto globalIndex = localView.index(localIndex);
349 processedDOFs[globalIndex] = elementMapper.index(element);
350 }
351 }
352 return overlapFree;
353}
354
355
356} // end namespace Dune::Assembler::Experimental
357
358#endif // DUNE_ASSEMBLER_PARALLEL_GRIDCOLORING_HH
Definition barrier.hh:11
auto coloredElementRange(const GridView &gridView, const std::vector< size_type > &coloring)
Compute a colored range of the elements in the gridView from coloring.
Definition gridcoloring.hh:210
auto cliqueListToAdjacency(const CliqueList &cliqueList, std::size_t size, bool verbose=false)
Compute an adjacency list from a clique list.
Definition graphutilities.hh:206
auto subEntityBasedElementAdjacency(const GridView &gridView, const SubEntityLayout &subEntityLayout, const ElementMapper &elementMapper)
Compute a layout based adjacency list.
Definition gridcoloring.hh:145
auto vertexBasedElementAdjacency(const GridView &gridView, const ElementMapper &elementMapper, bool verbose=false)
Compute a vertex based adjacency list.
Definition gridcoloring.hh:59
auto computeColoring(const Adjacency &adjacency, SeedNodesContainer &&seedNodes)
Compute a graph coloring from neighboring information.
Definition graphcoloring.hh:48
bool checkOverlapFree(const ElementRange &elementRange, const Basis &basis)
Check if given element range is overlap free.
Definition gridcoloring.hh:317
void seed(const Vertex &vertex)
auto transformedRangeView(R &&range, F &&f)
size_type dim() const
const Grid & grid() const
MCMGLayout mcmgElementLayout()
RowIterator end()
RowIterator begin()
T endl(T... args)
T max_element(T... args)