6#include <kernel/adjacency/graph.hpp>
7#include <kernel/geometry/parti_scotch.hpp>
8#include <kernel/util/dist.hpp>
9#include <kernel/util/math.hpp>
12#if defined(FEAT_HAVE_SCOTCH) || defined(DOXYGEN)
23 std::vector<SCOTCH_Num> vertloctab;
24 std::vector<SCOTCH_Num> edgeloctab;
25 std::vector<SCOTCH_Num> veloloctab;
40 SCOTCH_dgraphExit(inner);
41 SCOTCH_dgraphFree(inner);
58 static constexpr SCOTCH_Num baseval(0);
61 scotch.vertloctab.clear();
64 scotch.edgeloctab.clear();
69 for(
Index i(0); i < graph.get_num_nodes_domain(); ++i)
71 scotch.vertloctab.push_back(SCOTCH_Num(scotch.edgeloctab.size()));
72 for(
Index k(dom_ptr[i]); k < dom_ptr[i+1]; ++k)
74 if(img_idx[k] != first_elem + i)
76 scotch.edgeloctab.push_back(SCOTCH_Num(img_idx[k]));
80 scotch.vertloctab.push_back(SCOTCH_Num(scotch.edgeloctab.size()));
86 SCOTCH_Num*
const vendloctab = scotch.vertloctab.data() + 1;
89 const auto vertlocnbr = SCOTCH_Num(graph.get_num_nodes_domain());
91 XASSERT(vertlocnbr == SCOTCH_Num(scotch.vertloctab.size() - 1));
96 const SCOTCH_Num vertlocmax = vertlocnbr;
99 const auto edgelocnbr = SCOTCH_Num(scotch.edgeloctab.size());
101 XASSERT(scotch.vertloctab.back() == edgelocnbr);
105 const SCOTCH_Num edgelocsiz = edgelocnbr;
107 XASSERT(scotch.vertloctab.front() == baseval);
110 scotch.veloloctab.clear();
115 XASSERT(weights.size() == std::size_t(vertlocnbr));
117 scotch.veloloctab.resize(std::size_t(vertlocnbr));
125 Real wsum = std::accumulate(weights.begin(), weights.end(),
Real(0));
126 XASSERTM(wsum > 1e-3,
"weight sum appears to be zero");
129 Real scale = 1E+9 / wsum;
130 for(std::size_t i(0); i < std::size_t(vertlocnbr); ++i)
132 scotch.veloloctab[i] = SCOTCH_Num(scale * weights[i]);
137 SCOTCH_Num*
const vlblloctab =
nullptr;
140 SCOTCH_Num*
const edgegsttab =
nullptr;
144 SCOTCH_Num*
const edloloctab =
nullptr;
149 return SCOTCH_dgraphBuild(
154 scotch.vertloctab.data(),
156 scotch.veloloctab.empty() ?
nullptr : scotch.veloloctab.data(),
160 scotch.edgeloctab.data(),
166 void PartiScotch::_create_subgraph(
const Adjacency::Graph& faces_at_elem,
const Index num_parts)
196 XASSERTM(
Index(z_size) <= num_parts,
"thou shall not create less partitions than thou hast ranks");
206 const Index num_elems = faces_at_elem.get_num_nodes_domain();
207 const Index num_faces = faces_at_elem.get_num_nodes_image();
212 Index my_num_elems = end_elem - first_elem;
214 const Index* dom_ptr = faces_at_elem.get_domain_ptr();
215 const Index* img_idx = faces_at_elem.get_image_idx();
218 const Index first_ptr = dom_ptr[first_elem];
219 const Index my_num_idx = dom_ptr[end_elem] - first_ptr;
220 Adjacency::Graph faces_at_my_elem(my_num_elems, num_faces, my_num_idx);
222 Index* my_dom_ptr = faces_at_my_elem.get_domain_ptr();
223 Index* my_img_idx = faces_at_my_elem.get_image_idx();
224 for(
Index i(0); i <= my_num_elems; ++i)
226 my_dom_ptr[i] = dom_ptr[first_elem + i] - first_ptr;
228 for(
Index i(0); i < my_num_idx; ++i)
230 my_img_idx[i] = img_idx[first_ptr + i];
240 this->
_parts.resize(std::size_t(my_num_elems));
243 bool PartiScotch::_execute(
const std::vector<Real>& weights)
249 return this->_broadcast_coloring(
true);
256 return this->_broadcast_coloring(
false);
260 if(SCOTCH_dgraphCheck(scotch_graph.inner) != 0)
262 return this->_broadcast_coloring(
false);
266 SCOTCH_Strat partitioning_strategy;
268 if(SCOTCH_stratInit(&partitioning_strategy) != 0)
270 return this->_broadcast_coloring(
false);
277 std::vector<SCOTCH_Num> partloctab(
_subgraph.get_num_nodes_domain());
278 if(SCOTCH_dgraphPart(scotch_graph.inner, SCOTCH_Num(
_num_parts), &partitioning_strategy, partloctab.data()) != 0)
280 SCOTCH_stratFree(&partitioning_strategy);
281 return this->_broadcast_coloring(
false);
284 SCOTCH_stratFree(&partitioning_strategy);
288 _parts[std::size_t(i)] =
Index(partloctab[std::size_t(i)]);
292 return this->_broadcast_coloring(this->_gather_coloring());
296 bool PartiScotch::_gather_coloring()
303 Index max_local_elems(0);
305 max_local_elems += 1;
308 auto send_buf = std::vector<Index>(std::size_t(max_local_elems));
312 send_buf[std::size_t(i + 1)] =
_parts[std::size_t(i)];
316 std::vector<Index> recv_buf(scotch_rank == 0 ? std::size_t(max_local_elems * scotch_size) : std::size_t(0));
317 _work_comm.
gather(send_buf.data(), max_local_elems, recv_buf.data(), max_local_elems, 0);
330 for(
Index i(0), k(0); i < scotch_size; ++i)
332 const Index off = i * max_local_elems;
333 const Index n = recv_buf[std::size_t(off)];
334 for(
Index j(1); j <= n; ++j, ++k)
336 col[k] = recv_buf[std::size_t(off + j)];
341 std::vector<int> color_counts(std::size_t(this->
_num_parts), 0);
345 ++color_counts[std::size_t(col[i])];
349 if(color_counts[std::size_t(i)] == 0)
359 bool PartiScotch::_broadcast_coloring(
bool ok)
361 int was_ok(ok ? 0 : 1);
372 this->
_coloring = Adjacency::Coloring(this->_global_num_elements, this->_num_parts);
383void scotch_not_linked() {}
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Index * get_coloring()
Returns the coloring array.
Adjacency Graph implementation.
Index * get_domain_ptr()
Returns the domain pointer array.
Index * get_image_idx()
Returns the image node index array.
void bcast(void *buffer, std::size_t count, const Datatype &datatype, int root) const
Blocking broadcast.
Comm comm_create_range_incl(int count, int first=0, int stride=1) const
Creates a new sub-communicator from a strided range of ranks.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
int size() const
Returns the size of this communicator.
void gather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype, int root) const
Blocking gather.
const MPI_Comm & mpi_comm() const
Comm comm_dup() const
Creates a copy of this communicator.
int rank() const
Returns the rank of this process in this communicator.
Index _min_elems
Mininum number of elements on each rank.
Index _local_num_elements
Local number of elements.
Dist::Comm _work_comm
Communicator for performing partitioning.
Adjacency::Coloring _coloring
Elements-at-rank coloring.
Adjacency::Graph _subgraph
Subgraph for this rank.
Index _first_elem
First element of this process.
int _max_work_procs
Maximum number of ranks to use for partitioning
Index _global_num_elements
Global number of elements.
std::vector< Index > _parts
Element partitioning.
Index _num_parts
Desired number of partitions.
const Dist::Comm & _comm
Main communicator.
@ injectify_sorted
Render-Injectified mode, sort image indices.
@ transpose
Render-Transpose mode.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
void mini(T_ &xmin, T_ x)
Updates the minimum of a set of values.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
double Real
Real data type.
std::uint64_t Index
Index data type.
RAII-wrapper for Scotch graphs.