1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright (C) 2017 Chris N. Richardson and Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "caster_mpi.h"
#include <array>
#include <dolfinx/graph/AdjacencyList.h>
#include <dolfinx/graph/ordering.h>
#include <dolfinx/graph/partition.h>
#include <dolfinx/graph/partitioners.h>
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/operators.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/function.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <vector>
namespace nb = nanobind;
namespace
{
/// Wrap a C++ graph partitioning function as a Python-ready function
template <typename Functor>
auto create_partitioner_py(Functor p_cpp)
{
return [p_cpp](dolfinx_wrappers::MPICommWrapper comm, int nparts,
const dolfinx::graph::AdjacencyList<std::int64_t>& local_graph,
bool ghosting)
{ return p_cpp(comm.get(), nparts, local_graph, ghosting); };
}
} // namespace
namespace dolfinx_wrappers
{
template <typename T>
void declare_adjacency_list(nb::module_& m, std::string type)
{
std::string pyclass_name = std::string("AdjacencyList_") + type;
nb::class_<dolfinx::graph::AdjacencyList<T>>(m, pyclass_name.c_str(),
"Adjacency List")
.def(
"__init__",
[](dolfinx::graph::AdjacencyList<T>* a,
nb::ndarray<const T, nb::ndim<1>, nb::c_contig> adj)
{
std::vector<T> data(adj.data(), adj.data() + adj.size());
new (a) dolfinx::graph::AdjacencyList<T>(
dolfinx::graph::regular_adjacency_list(std::move(data), 1));
},
nb::arg("adj").noconvert())
.def(
"__init__",
[](dolfinx::graph::AdjacencyList<T>* a,
nb::ndarray<const T, nb::ndim<2>, nb::c_contig> adj)
{
std::vector<T> data(adj.data(), adj.data() + adj.size());
new (a) dolfinx::graph::AdjacencyList<T>(
dolfinx::graph::regular_adjacency_list(std::move(data),
adj.shape(1)));
},
nb::arg("adj").noconvert())
.def(
"__init__",
[](dolfinx::graph::AdjacencyList<T>* a,
nb::ndarray<const T, nb::ndim<1>, nb::c_contig> array,
nb::ndarray<const std::int32_t, nb::ndim<1>, nb::c_contig> displ)
{
std::vector<T> data(array.data(), array.data() + array.size());
std::vector<std::int32_t> offsets(displ.data(),
displ.data() + displ.size());
new (a) dolfinx::graph::AdjacencyList<T>(std::move(data),
std::move(offsets));
},
nb::arg("data").noconvert(), nb::arg("offsets"))
.def(
"links",
[](const dolfinx::graph::AdjacencyList<T>& self, int i)
{
std::span<const T> link = self.links(i);
return nb::ndarray<const T, nb::numpy>(link.data(), {link.size()},
nb::handle());
},
nb::rv_policy::reference_internal, nb::arg("i"),
"Links (edges) of a node")
.def_prop_ro(
"array",
[](const dolfinx::graph::AdjacencyList<T>& self)
{
return nb::ndarray<const T, nb::numpy>(
self.array().data(), {self.array().size()}, nb::handle());
},
nb::rv_policy::reference_internal)
.def_prop_ro(
"offsets",
[](const dolfinx::graph::AdjacencyList<T>& self)
{
return nb::ndarray<const std::int32_t, nb::numpy>(
self.offsets().data(), {self.offsets().size()}, nb::handle());
},
nb::rv_policy::reference_internal)
.def_prop_ro("num_nodes", &dolfinx::graph::AdjacencyList<T>::num_nodes)
.def("__eq__", &dolfinx::graph::AdjacencyList<T>::operator==,
nb::is_operator())
.def("__repr__", &dolfinx::graph::AdjacencyList<T>::str)
.def("__len__", &dolfinx::graph::AdjacencyList<T>::num_nodes);
}
void graph(nb::module_& m)
{
declare_adjacency_list<std::int32_t>(m, "int32");
declare_adjacency_list<std::int64_t>(m, "int64");
using partition_fn
= std::function<dolfinx::graph::AdjacencyList<std::int32_t>(
MPICommWrapper, int,
const dolfinx::graph::AdjacencyList<std::int64_t>&, bool)>;
m.def(
"partitioner", []() -> partition_fn
{ return create_partitioner_py(dolfinx::graph::partition_graph); },
"Default graph partitioner");
#ifdef HAS_PTSCOTCH
m.def(
"partitioner_scotch",
[](double imbalance, int seed) -> partition_fn
{
return create_partitioner_py(dolfinx::graph::scotch::partitioner(
dolfinx::graph::scotch::strategy::none, imbalance, seed));
},
nb::arg("imbalance") = 0.025, nb::arg("seed") = 0,
"SCOTCH graph partitioner");
#endif
#ifdef HAS_PARMETIS
m.def(
"partitioner_parmetis",
[](double imbalance, std::array<int, 3> options) -> partition_fn
{
return create_partitioner_py(
dolfinx::graph::parmetis::partitioner(imbalance, options));
},
nb::arg("imbalance") = 1.02,
nb::arg("options") = std ::array<int, 3>({1, 0, 5}),
"ParMETIS graph partitioner");
#endif
#ifdef HAS_KAHIP
m.def(
"partitioner_kahip",
[](int mode = 1, int seed = 1, double imbalance = 0.03,
bool suppress_output = true) -> partition_fn
{
return create_partitioner_py(dolfinx::graph::kahip::partitioner(
mode, seed, imbalance, suppress_output));
},
nb::arg("mode") = 1, nb::arg("seed") = 1, nb::arg("imbalance") = 0.03,
nb::arg("suppress_output") = true, "KaHIP graph partitioner");
#endif
m.def("reorder_gps", &dolfinx::graph::reorder_gps, nb::arg("graph"));
}
} // namespace dolfinx_wrappers
|