ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Metis.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Metis.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
44/*--------------------------------------------------------------------------*/
45/*----------------------------- DEFINITIONS --------------------------------*/
46/*--------------------------------------------------------------------------*/
47
48#ifndef __ADS_interop_metis_Metis
49 #define __ADS_interop_metis_Metis
50/* self-identification: #endif constant */
51
52/*--------------------------------------------------------------------------*/
53/*------------------------------ INCLUDES ----------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56#if ! __has_include( <metis.h> )
57 #error "the interoperation with METIS needs the header of METIS"
58#endif
59
60#include "ADS/Contract.h"
62#include "ADS/interop/Csr.h"
63
64#include <metis.h>
65
66#include <cstddef>
67#include <stdexcept>
68#include <string>
69#include <vector>
70
71/*--------------------------------------------------------------------------*/
72/*--------------------------- NAMESPACE ------------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75namespace ADS::metis
76{
77
78/*--------------------------------------------------------------------------*/
79/*------------------------------ THE OPTIONS -------------------------------*/
80/*--------------------------------------------------------------------------*/
81
83struct Options
84{
85 bool recursive = false;
86 bool weights = true;
87 double imbalance = 1.03;
88 int seed = 0;
89 };
90
91/*--------------------------------------------------------------------------*/
92/*----------------------------- THE PARTITION ------------------------------*/
93/*--------------------------------------------------------------------------*/
94
97
102template< class G >
103[[nodiscard]] Partition partition( const G & g , const Input & I , int k ,
104 const Options & o = Options() )
105{
106 ADS_PRE( ( ! g.directed() ) && ( k >= 1 ) && ( k <= g.n() ) );
107 Partition P;
108 P.k = k;
109 if( g.n() == 0 )
110 return( P );
111 idx_t n = idx_t( g.n() ) , ncon = 1 , parts = idx_t( k ) , cut = 0;
112 real_t ub = real_t( o.imbalance );
113 idx_t opt[ METIS_NOPTIONS ];
114 METIS_SetDefaultOptions( opt );
115 opt[ METIS_OPTION_NUMBERING ] = 0;
116 opt[ METIS_OPTION_SEED ] = idx_t( o.seed );
117 std::vector< idx_t > part( std::size_t( g.n() ) , 0 );
118 // METIS reads the arrays and writes none of them
119 auto * xadj = const_cast< idx_t * >( I.offsets() );
120 auto * adjncy = const_cast< idx_t * >( I.heads() );
121 auto * adjwgt = o.weights ? const_cast< idx_t * >( I.weights() ) : nullptr;
122 int r = METIS_OK;
123 if( k > 1 )
124 r = ( o.recursive ? METIS_PartGraphRecursive : METIS_PartGraphKway )(
125 &n , &ncon , xadj , adjncy , nullptr , nullptr , adjwgt , &parts ,
126 nullptr , &ub ,
127 opt , &cut , part.data() );
128 if( r != METIS_OK )
129 throw( std::runtime_error( "ADS::metis::partition: METIS returned " +
130 std::to_string( r ) ) );
131 P.part.assign( part.begin() , part.end() );
132 P.cut = static_cast< long long >( cut );
133 return( P );
134 }
135
139template< class G >
140[[nodiscard]] Partition partition( const G & g , int k ,
141 const Options & o = Options() )
142{
143 const Input I( g , o.weights );
144 return( partition( g , I , k , o ) );
145 }
146
147/*--------------------------------------------------------------------------*/
148
149} // end( namespace ADS::metis )
150
151/*--------------------------------------------------------------------------*/
152
153#endif /* Metis.h included */
154
155/*--------------------------------------------------------------------------*/
156/*---------------------------- End File Metis.h ----------------------------*/
157/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The compressed rows of a StaticGraph handed to a library that takes them as plain arrays,...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
the offsets and the heads of a graph as arrays of X, and its weights as an array of Y,...
Definition Csr.h:72
const X * offsets(void) const
the offsets of the rows
Definition Csr.h:125
const X * heads(void) const
the heads of the arcs, row by row
Definition Csr.h:127
const Y * weights(void) const
the weights of the arcs in the places of the heads, null if the graph is unweighted or they were not ...
Definition Csr.h:130
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Partition partition(const G &g, const Input &I, int k, const Options &o=Options())
the partition of the nodes of into parts by METIS, on the input of (whose arrays it reads in plac...
Definition Metis.h:103
CsrInput< idx_t, idx_t > Input
the arrays of a graph for METIS: in place where the layouts agree
Definition Metis.h:96
Definition Metis.h:76
a partition of the nodes of a graph into parts
Definition Csr.h:150
int k
the number of the parts
Definition Csr.h:153
long long cut
the weight of the edges between two parts
Definition Csr.h:152
std::vector< int > part
the part of each node, in
Definition Csr.h:151
the options of a partition by METIS
Definition Metis.h:84
int seed
the seed of METIS
Definition Metis.h:88
bool recursive
recursive bisection instead of k-way
Definition Metis.h:85
bool weights
the weights of the arcs, if any, as adjwgt
Definition Metis.h:86
double imbalance
the largest part over the mean, ubvec
Definition Metis.h:87