| Build Status |
|---|
Metis.jl is a Julia wrapper to the Metis library which is a library for partitioning unstructured graphs, partitioning meshes, and computing fill-reducing orderings of sparse matrices.
Metis.partition calculates graph partitions. As an example, here we partition
a small graph into two, three and four parts, and visualize the result:
![]() |
![]() |
![]() |
|---|---|---|
Metis.partition(g, 2) |
Metis.partition(g, 3) |
Metis.partition(g, 4) |
Metis.partition calls METIS_PartGraphKway or METIS_PartGraphRecursive from the Metis
C API, depending on the optional keyword argument alg:
alg = Metis.Algorithm.KWAY(default): multilevel k-way partitioning (METIS_PartGraphKway).alg = Metis.Algorithm.RECURSIVE: multilevel recursive bisection (METIS_PartGraphRecursive).
Metis.partition uses edge weights when the graph has them:
- Graphs from SimpleWeightedGraphs.jl use their weights by default.
- The values of a sparse matrix
Sare ignored by default. To use them as edge weights, callMetis.partition(Metis.graph(S; weights = true), n).weights = trueisn't supported forHermitianorSymmetricwrapped matrices.
The weights must be positive integers, and each edge must have the same weight in both
directions, so a sparse matrix must be symmetric. Metis.graph throws an ArgumentError
otherwise. Metis.separator and Metis.permutation don't use edge weights.
Metis requires every edge to be stored in both directions. Metis.graph checks this for
sparse matrices and directed graphs, and throws an ArgumentError otherwise, e.g. for a
directed graph with a one-way edge or a sparse matrix with a stored zero at (i, j) but no
entry at (j, i) (remove those with SparseArrays.dropzeros). Pass check = false to
skip the check, for example for a matrix that is known to be symmetric.
Metis.separator calculates a vertex separator
of a graph. Metis.separator calls METIS_ComputeVertexSeparator from the Metis C API.
As an example, here we calculate a vertex separator (green) of a small graph:
![]() |
|---|
Metis.separator(g) |
Metis.separator returns a vector with one label per vertex: 1 and 2 for the two parts
and 3 for the separator. No edge joins part 1 and part 2. A part or the separator can
be empty, in which case its label is missing from the result.
Metis.permutation calculates the fill reducing permutation
for a sparse matrix. Metis.permutation calls METIS_NodeND from the Metis
C API. As an example, we calculate the fill reducing permutation
for a sparse matrix S originating from a typical (small) FEM problem, and
visualize the sparsity pattern for the original matrix and the permuted matrix:
perm, iperm = Metis.permutation(S)⠛⣤⢠⠄⠀⣌⠃⢠⠀⠐⠈⠀⠀⠀⠀⠉⠃⠀⠀⠀⠀⠀⠀⠀⠀⠘⠀⠂⠔⠀ |
⣕⢝⠀⠀⢸⠔⡵⢊⡀⠂⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣑⠑ |
|---|---|
S (5% stored values) |
S[perm,perm] (5% stored values) |
We can also visualize the sparsity pattern of the Cholesky factorization of the same matrix. It is here clear that using the fill reducing permutation results in a sparser factorization:
⠙⢤⢠⡄⠀⣜⠃⢠⠀⠐⠘⠀⠀⠀⠀⠛⠃⠀⠀⠀⠀⠀⠀⠀⠀⠘⠀⠂⡔⠀ |
⠑⢝⠀⠀⢸⠔⡵⢊⡀⡂⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣕⢕ |
|---|---|
chol(S) (16% stored values) |
chol(S[perm,perm]) (6% stored values) |
Metis options can be passed to Metis.partition, Metis.permutation and Metis.separator
with the options keyword argument and Metis.Options, e.g. to use a different random seed
and a larger load imbalance:
Metis.partition(g, 4; options = Metis.Options(seed = 42, ufactor = 100))See the docstring of Metis.Options for the available options, and the
Metis manual for their meaning. Setting options in the global
Metis.options array instead is deprecated.
The Metis C library has global state, so Metis.partition, Metis.permutation and
Metis.separator hold a lock while calling it. They can be called from several threads,
also on the same Metis.Graph, but the Metis calls run one at a time. The input graph is
never modified, and the results are the same as for sequential calls.
When calling the C API directly, don't make concurrent calls, and note that with 1-based numbering Metis temporarily converts the input arrays to 0-based in place during a call.
For more fine tuned usage of Metis consider calling the C API directly.
The wrappers aren't exported, so call them with the module prefix, e.g. Metis.METIS_NodeND.
The following functions are currently exposed:
METIS_PartGraphRecursiveMETIS_PartGraphKwayMETIS_PartMeshNodalMETIS_PartMeshDualMETIS_MeshToNodalMETIS_MeshToDualMETIS_NodeNDMETIS_SetDefaultOptionsMETIS_Free
all with the same arguments and argument order as described in the Metis manual.
The following functions are also exposed. The manual doesn't describe them, so see
metis.h (included in METIS_jll) for their arguments, which the wrappers follow:
METIS_ComputeVertexSeparatorMETIS_NodeNDPMETIS_NodeRefine



