14#ifndef DUMUX_SCOTCH_BACKEND_HH 
   15#define DUMUX_SCOTCH_BACKEND_HH 
   21#include <dune/common/exceptions.hh> 
   33#warning "PTSCOTCH was not found on your system. Dumux::ScotchBackend won't do anything." 
   44template<
class IndexType = 
int>
 
   49    using Graph = std::vector<std::vector<IndexType>>;
 
   51    ScotchGraph(
const Graph& graph)
 
   54        const SCOTCH_Num numNodes = graph.size();
 
   58        vertTab_.reserve(numNodes + 1);
 
   59        edgeTab_.reserve(20*numNodes);
 
   64        SCOTCH_Num numEdges = 0;
 
   65        vertTab_.push_back(0);
 
   66        for (
auto vertex = graph.begin(); vertex != graph.end(); ++vertex)
 
   68            numEdges += 
vertex->size();
 
   69            vertTab_.push_back(vertTab_.back() + 
vertex->size());
 
   70            edgeTab_.insert(edgeTab_.end(), 
vertex->begin(), 
vertex->end());
 
   74        vertTab_.shrink_to_fit();
 
   75        edgeTab_.shrink_to_fit();
 
   77        if (SCOTCH_graphInit(&scotchGraph_) != 0)
 
   78            DUNE_THROW(Dune::Exception, 
"Error initializing SCOTCH graph!");
 
   81        if (SCOTCH_graphCheck(&scotchGraph_) != 0)
 
   82            DUNE_THROW(Dune::Exception, 
"Error within SCOTCH graph's consistency!");
 
   85        const SCOTCH_Num baseValue = 0; 
 
   86        if (SCOTCH_graphBuild(&scotchGraph_, baseValue, numNodes, vertTab_.data(), vertTab_.data()+1, NULL, NULL, numEdges, edgeTab_.data(), NULL))
 
   87            DUNE_THROW(Dune::Exception, 
"Error building SCOTCH graph!");
 
   93        SCOTCH_graphExit(&scotchGraph_);
 
   98    { 
return &scotchGraph_; }
 
  101    SCOTCH_Graph scotchGraph_;
 
  103    std::vector<SCOTCH_Num> vertTab_;
 
  104    std::vector<SCOTCH_Num> edgeTab_;
 
  111class ScotchGraphOrderStrategy
 
  114    ScotchGraphOrderStrategy(
const std::string& strategy = 
"")
 
  116        if (SCOTCH_stratInit(&strategy_) != 0)
 
  117            DUNE_THROW(Dune::Exception, 
"Error initializing SCOTCH strategy!");
 
  120        if (!strategy.empty())
 
  121            SCOTCH_stratGraphOrder(&strategy_, strategy.c_str());
 
  125    ~ScotchGraphOrderStrategy()
 
  127        SCOTCH_stratExit(&strategy_);
 
  132    { 
return &strategy_; }
 
  135    SCOTCH_Strat strategy_;
 
  145template<
class IndexType>
 
  150    using Graph = std::vector<std::vector<IndexType>>;
 
  155                                                 std::size_t numPasses = 5)
 
  158        std::string strategy = 
"g{pass= " + std::to_string(numPasses) + 
"}";
 
 
  164                                              std::string scotchStrategy = 
"")
 
  166        std::vector<int> permutation, inversePermutation;
 
 
  173                                   std::vector<int>& permutation,
 
  174                                   std::vector<int>& inversePermutation,
 
  175                                   std::string scotchStrategy = 
"")
 
  178        ScotchGraph<IndexType> scotchGraph(graph);
 
  179        ScotchGraphOrderStrategy strategy(scotchStrategy);
 
  182        const auto graphSize = graph.size();
 
  183        std::vector<SCOTCH_Num> permutationIndices(graphSize);
 
  184        std::vector<SCOTCH_Num> inversePermutationIndices(graphSize);
 
  187        SCOTCH_randomReset();
 
  190        if (SCOTCH_graphOrder(
 
  191            scotchGraph.data(), strategy.data(), permutationIndices.data(),
 
  192            inversePermutationIndices.data(), NULL, NULL, NULL
 
  194            DUNE_THROW(Dune::Exception, 
"Error reordering SCOTCH graph!");
 
  197        permutation.resize(graphSize);
 
  198        inversePermutation.resize(graphSize);
 
  199        std::copy(permutationIndices.begin(), permutationIndices.end(),
 
  200                  permutation.begin());
 
  201        std::copy(inversePermutationIndices.begin(), inversePermutationIndices.end(),
 
  202                  inversePermutation.begin());
 
 
 
A reordering backend using the scotch library.
Definition scotchbackend.hh:147
static std::vector< int > computeReordering(const Graph &graph, std::string scotchStrategy="")
Compute graph re-ordering.
Definition scotchbackend.hh:163
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition scotchbackend.hh:154
std::vector< std::vector< IndexType > > Graph
the graph type
Definition scotchbackend.hh:150
static void computeReordering(const Graph &graph, std::vector< int > &permutation, std::vector< int > &inversePermutation, std::string scotchStrategy="")
Compute graph re-ordering.
Definition scotchbackend.hh:172