26#ifndef DUMUX_SCOTCH_BACKEND_HH
27#define DUMUX_SCOTCH_BACKEND_HH
33#include <dune/common/exceptions.hh>
45#warning "PTSCOTCH was not found on your system. Dumux::ScotchBackend won't do anything."
57template<
class IndexType =
int>
62 using Graph = std::vector<std::vector<IndexType>>;
64 ScotchGraph(
const Graph& graph)
67 const SCOTCH_Num numNodes = graph.size();
71 vertTab_.reserve(numNodes + 1);
72 edgeTab_.reserve(20*numNodes);
77 SCOTCH_Num numEdges = 0;
78 vertTab_.push_back(0);
79 for (
auto vertex = graph.begin(); vertex != graph.end(); ++vertex)
81 numEdges +=
vertex->size();
82 vertTab_.push_back(vertTab_.back() +
vertex->size());
83 edgeTab_.insert(edgeTab_.end(),
vertex->begin(),
vertex->end());
87 vertTab_.shrink_to_fit();
88 edgeTab_.shrink_to_fit();
90 if (SCOTCH_graphInit(&scotchGraph_) != 0)
91 DUNE_THROW(Dune::Exception,
"Error initializing SCOTCH graph!");
94 if (SCOTCH_graphCheck(&scotchGraph_) != 0)
95 DUNE_THROW(Dune::Exception,
"Error within SCOTCH graph's consistency!");
98 const SCOTCH_Num baseValue = 0;
99 if (SCOTCH_graphBuild(&scotchGraph_, baseValue, numNodes, vertTab_.data(), vertTab_.data()+1, NULL, NULL, numEdges, edgeTab_.data(), NULL))
100 DUNE_THROW(Dune::Exception,
"Error building SCOTCH graph!");
106 SCOTCH_graphExit(&scotchGraph_);
111 {
return &scotchGraph_; }
114 SCOTCH_Graph scotchGraph_;
116 std::vector<SCOTCH_Num> vertTab_;
117 std::vector<SCOTCH_Num> edgeTab_;
125class ScotchGraphOrderStrategy
128 ScotchGraphOrderStrategy(
const std::string& strategy =
"")
130 if (SCOTCH_stratInit(&strategy_) != 0)
131 DUNE_THROW(Dune::Exception,
"Error initializing SCOTCH strategy!");
134 if (!strategy.empty())
135 SCOTCH_stratGraphOrder(&strategy_, strategy.c_str());
139 ~ScotchGraphOrderStrategy()
141 SCOTCH_stratExit(&strategy_);
146 {
return &strategy_; }
149 SCOTCH_Strat strategy_;
160template<
class IndexType>
165 using Graph = std::vector<std::vector<IndexType>>;
170 std::size_t numPasses = 5)
173 std::string strategy =
"g{pass= " + std::to_string(numPasses) +
"}";
179 std::string scotchStrategy =
"")
181 std::vector<int> permutation, inversePermutation;
188 std::vector<int>& permutation,
189 std::vector<int>& inversePermutation,
190 std::string scotchStrategy =
"")
193 ScotchGraph<IndexType> scotchGraph(graph);
194 ScotchGraphOrderStrategy strategy(scotchStrategy);
197 const auto graphSize = graph.size();
198 std::vector<SCOTCH_Num> permutationIndices(graphSize);
199 std::vector<SCOTCH_Num> inversePermutationIndices(graphSize);
202 SCOTCH_randomReset();
205 if (SCOTCH_graphOrder(
206 scotchGraph.data(), strategy.data(), permutationIndices.data(),
207 inversePermutationIndices.data(), NULL, NULL, NULL
209 DUNE_THROW(Dune::Exception,
"Error reordering SCOTCH graph!");
212 permutation.resize(graphSize);
213 inversePermutation.resize(graphSize);
214 std::copy(permutationIndices.begin(), permutationIndices.end(),
215 permutation.begin());
216 std::copy(inversePermutationIndices.begin(), inversePermutationIndices.end(),
217 inversePermutation.begin());
Definition: scotchbackend.hh:162
static std::vector< int > computeReordering(const Graph &graph, std::string scotchStrategy="")
Compute graph re-ordering.
Definition: scotchbackend.hh:178
std::vector< std::vector< IndexType > > Graph
the graph type
Definition: scotchbackend.hh:165
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition: scotchbackend.hh:169
static void computeReordering(const Graph &graph, std::vector< int > &permutation, std::vector< int > &inversePermutation, std::string scotchStrategy="")
Compute graph re-ordering.
Definition: scotchbackend.hh:187