3#ifndef DUNE_GRID_UTILITY_PARMETISGRIDPARTITIONER_HH
4#define DUNE_GRID_UTILITY_PARMETISGRIDPARTITIONER_HH
13#include <dune/common/parallel/mpihelper.hh>
14#include <dune/common/exceptions.hh>
16#include <dune/geometry/referenceelements.hh>
28#ifdef PARMETIS_MAJOR_VERSION
37 template<
class Gr
idView>
38 struct ParMetisGridPartitioner {
41#if PARMETIS_MAJOR_VERSION > 3
42 typedef idx_t idx_type;
43 typedef ::real_t real_type;
46 typedef float real_type;
62 static std::vector<unsigned> partition(
const GridView& gv,
const Dune::MPIHelper& mpihelper) {
63 const unsigned numElements = gv.size(0);
65 std::vector<unsigned> part(numElements);
71 idx_type ncommonnodes = 2;
72 idx_type options[4] = {0, 0, 0, 0};
74 idx_type nparts = mpihelper.size();
75 std::vector<real_type> tpwgts(ncon*nparts, 1./nparts);
76 std::vector<real_type> ubvec(ncon, 1.05);
79 std::vector<idx_type> elmdist(nparts+1);
81 std::fill(elmdist.begin()+1, elmdist.end(), gv.size(0));
85 std::vector<idx_type> eptr, eind;
87 eptr.push_back(numVertices);
90 const size_t curNumVertices = referenceElement<double, dimension>(element.type()).size(dimension);
92 numVertices += curNumVertices;
93 eptr.push_back(numVertices);
95 for (
size_t k = 0; k < curNumVertices; ++k)
96 eind.push_back(gv.indexSet().subIndex(element, k, dimension));
100 if (0 == mpihelper.rank()) {
101 MPI_Comm comm = Dune::MPIHelper::getLocalCommunicator();
103#if PARMETIS_MAJOR_VERSION >= 4
106 ParMETIS_V3_PartMeshKway(elmdist.data(), eptr.data(), eind.data(), NULL, &wgtflag, &numflag,
107 &ncon, &ncommonnodes, &nparts, tpwgts.data(), ubvec.data(),
108 options, &edgecut,
reinterpret_cast<idx_type*
>(part.data()), &comm);
110#if PARMETIS_MAJOR_VERSION >= 4
112 DUNE_THROW(Dune::Exception,
"ParMETIS returned an error code.");
130 static std::vector<unsigned> repartition(
const GridView& gv,
const Dune::MPIHelper& mpihelper, real_type itr = 1000) {
133 GlobalIndexSet<GridView> globalIndex(gv,0);
135 int numElements = std::distance(gv.template begin<0, Interior_Partition>(),
136 gv.template end<0, Interior_Partition>());
138 std::vector<unsigned> interiorPart(numElements);
141 idx_type wgtflag = 0;
142 idx_type numflag = 0;
144 idx_type options[4] = {0, 0, 0, 0};
146 idx_type nparts = mpihelper.size();
147 std::vector<real_type> tpwgts(ncon*nparts, 1./nparts);
148 std::vector<real_type> ubvec(ncon, 1.05);
150 MPI_Comm comm = Dune::MPIHelper::getCommunicator();
153 std::vector<int> offset(gv.comm().size());
154 std::fill(offset.begin(), offset.end(), 0);
156 gv.comm().template allgather<int>(&numElements, 1, offset.data());
159 std::vector<idx_type> vtxdist(gv.comm().size()+1);
162 for (
unsigned int i=1; i<vtxdist.size(); ++i)
163 vtxdist[i] = vtxdist[i-1] + offset[i-1];
166 std::vector<idx_type> xadj, adjncy;
170 size_t numNeighbors = 0;
172 for (
const auto& in : intersections(gv, element)) {
174 adjncy.push_back(globalIndex.index(in.outside()));
180 xadj.push_back(xadj.back() + numNeighbors);
183#if PARMETIS_MAJOR_VERSION >= 4
186 ParMETIS_V3_AdaptiveRepart(vtxdist.data(), xadj.data(), adjncy.data(), NULL, NULL, NULL,
187 &wgtflag, &numflag, &ncon, &nparts, tpwgts.data(), ubvec.data(),
188 &itr, options, &edgecut,
reinterpret_cast<idx_type*
>(interiorPart.data()), &comm);
190#if PARMETIS_MAJOR_VERSION >= 4
192 DUNE_THROW(Dune::Exception,
"ParMETIS returned error code " << OK);
201 typedef MultipleCodimMultipleGeomTypeMapper<GridView> ElementMapper;
204 std::vector<unsigned int> part(gv.size(0));
205 std::fill(part.begin(), part.end(), 0);
208 part[elementMapper.index(element)] = interiorPart[c++];
217#warning "You seem to be using the ParMETIS emulation layer of scotch, which does not work with this file."
221#warning "PARMETIS was not found, please check your configuration"
Mapper for multiple codim and multiple geometry types.
Provides a globally unique index for all entities of a distributed Dune grid.
static constexpr int dimension
The dimension of the grid.
Definition: common/gridview.hh:148
MCMGLayout mcmgElementLayout()
layout for elements (codim-0 entities)
Definition: mcmgmapper.hh:97
Include standard header files.
Definition: agrid.hh:60
constexpr Interior interior
PartitionSet for the interior partition.
Definition: partitionset.hh:272