File: common.cpp

package info (click to toggle)
fenics-dolfinx 1%3A0.9.0-11
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,376 kB
  • sloc: cpp: 33,701; python: 22,338; makefile: 230; sh: 171; xml: 55
file content (217 lines) | stat: -rw-r--r-- 8,461 bytes parent folder | download | duplicates (4)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (C) 2017-2019 Chris Richardson and Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier:    LGPL-3.0-or-later

#include <complex>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <vector>

#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/vector.h>

#include <dolfinx/common/IndexMap.h>
#include <dolfinx/common/Scatterer.h>
#include <dolfinx/common/Table.h>
#include <dolfinx/common/Timer.h>
#include <dolfinx/common/defines.h>
#include <dolfinx/common/log.h>
#include <dolfinx/common/timing.h>
#include <dolfinx/common/utils.h>

#include "MPICommWrapper.h"
#include "array.h"
#include "caster_mpi.h"

namespace nb = nanobind;

namespace dolfinx_wrappers
{

/// Return true if DOLFINx is compiled with petsc4py
consteval bool has_petsc4py()
{
#ifdef HAS_PETSC4PY
  return true;
#else
  return false;
#endif
}

// Interface for dolfinx/common
void common(nb::module_& m)
{
  // From dolfinx/common/defines.h
  m.attr("git_commit_hash") = dolfinx::git_commit_hash();
  m.attr("has_adios2") = dolfinx::has_adios2();
  m.attr("has_complex_ufcx_kernels") = dolfinx::has_complex_ufcx_kernels();
  m.attr("has_debug") = dolfinx::has_debug();
  m.attr("has_kahip") = dolfinx::has_kahip();
  m.attr("has_parmetis") = dolfinx::has_parmetis();
  m.attr("has_petsc") = dolfinx::has_petsc();
  m.attr("has_petsc4py") = has_petsc4py();
  m.attr("has_ptscotch") = dolfinx::has_ptscotch();
  m.attr("has_slepc") = dolfinx::has_slepc();
  m.attr("ufcx_signature") = dolfinx::ufcx_signature();
  m.attr("version") = dolfinx::version();

  nb::enum_<dolfinx::Table::Reduction>(m, "Reduction")
      .value("max", dolfinx::Table::Reduction::max)
      .value("min", dolfinx::Table::Reduction::min)
      .value("average", dolfinx::Table::Reduction::average);

  // dolfinx::common::IndexMap
  nb::class_<dolfinx::common::IndexMap>(m, "IndexMap")
      .def(
          "__init__",
          [](dolfinx::common::IndexMap* self, MPICommWrapper comm,
             std::int32_t local_size)
          { new (self) dolfinx::common::IndexMap(comm.get(), local_size); },
          nb::arg("comm"), nb::arg("local_size"))
      .def(
          "__init__",
          [](dolfinx::common::IndexMap* self, MPICommWrapper comm,
             std::int32_t local_size,
             nb::ndarray<const std::int64_t, nb::ndim<1>, nb::c_contig> ghosts,
             nb::ndarray<const int, nb::ndim<1>, nb::c_contig> ghost_owners)
          {
            new (self) dolfinx::common::IndexMap(
                comm.get(), local_size, std::span(ghosts.data(), ghosts.size()),
                std::span(ghost_owners.data(), ghost_owners.size()));
          },
          nb::arg("comm"), nb::arg("local_size"), nb::arg("ghosts"),
          nb::arg("ghost_owners"))
      .def(
          "__init__",
          [](dolfinx::common::IndexMap* self, MPICommWrapper comm,
             std::int32_t local_size,
             std::array<nb::ndarray<const int, nb::ndim<1>, nb::c_contig>, 2>
                 dest_src,
             nb::ndarray<const std::int64_t, nb::ndim<1>, nb::c_contig> ghosts,
             nb::ndarray<const int, nb::ndim<1>, nb::c_contig> ghost_owners)
          {
            std::array<std::vector<int>, 2> ranks;
            ranks[0].assign(dest_src[0].data(),
                            dest_src[0].data() + dest_src[0].size());
            ranks[1].assign(dest_src[1].data(),
                            dest_src[1].data() + dest_src[1].size());
            new (self) dolfinx::common::IndexMap(
                comm.get(), local_size, ranks,
                std::span(ghosts.data(), ghosts.size()),
                std::span(ghost_owners.data(), ghost_owners.size()));
          },
          nb::arg("comm"), nb::arg("local_size"), nb::arg("dest_src"),
          nb::arg("ghosts"), nb::arg("ghost_owners"))
      .def_prop_ro(
          "comm", [](const dolfinx::common::IndexMap& self)
          { return MPICommWrapper(self.comm()); }, nb::keep_alive<0, 1>())
      .def_prop_ro("size_local", &dolfinx::common::IndexMap::size_local)
      .def_prop_ro("size_global", &dolfinx::common::IndexMap::size_global)
      .def_prop_ro("num_ghosts", &dolfinx::common::IndexMap::num_ghosts)
      .def_prop_ro("local_range", &dolfinx::common::IndexMap::local_range,
                   "Range of indices owned by this map")
      .def("index_to_dest_ranks",
           &dolfinx::common::IndexMap::index_to_dest_ranks)
      .def("imbalance", &dolfinx::common::IndexMap::imbalance,
           "Imbalance of the current IndexMap.")
      .def_prop_ro(
          "ghosts",
          [](const dolfinx::common::IndexMap& self)
          {
            std::span ghosts = self.ghosts();
            return nb::ndarray<const std::int64_t, nb::numpy>(
                ghosts.data(), {ghosts.size()}, nb::handle());
          },
          nb::rv_policy::reference_internal, "Return list of ghost indices")
      .def_prop_ro(
          "owners",
          [](const dolfinx::common::IndexMap& self)
          {
            std::span owners = self.owners();
            return nb::ndarray<nb::numpy, const int, nb::ndim<1>>(
                owners.data(), {owners.size()}, nb::handle());
          },
          nb::rv_policy::reference_internal)
      .def(
          "local_to_global",
          [](const dolfinx::common::IndexMap& self,
             nb::ndarray<const std::int32_t, nb::ndim<1>, nb::c_contig> local)
          {
            std::vector<std::int64_t> global(local.size());
            self.local_to_global(std::span(local.data(), local.size()), global);
            return dolfinx_wrappers::as_nbarray(std::move(global));
          },
          nb::arg("local"))
      .def(
          "global_to_local",
          [](const dolfinx::common::IndexMap& self,
             nb::ndarray<const std::int64_t, nb::ndim<1>, nb::c_contig> global)
          {
            std::vector<std::int32_t> local(global.size());
            self.global_to_local(std::span(global.data(), global.size()),
                                 local);
            return dolfinx_wrappers::as_nbarray(std::move(local));
          },
          nb::arg("global"));
  // dolfinx::common::Timer
  nb::class_<dolfinx::common::Timer>(m, "Timer", "Timer class")
      .def(nb::init<std::optional<std::string>>(), nb::arg("task").none())
      .def("start", &dolfinx::common::Timer::start, "Start timer")
      .def("stop", &dolfinx::common::Timer::stop, "Stop timer")
      .def("resume", &dolfinx::common::Timer::resume)
      .def("elapsed", &dolfinx::common::Timer::elapsed);

  // dolfinx::common::Timer enum
  nb::enum_<dolfinx::TimingType>(m, "TimingType")
      .value("wall", dolfinx::TimingType::wall)
      .value("system", dolfinx::TimingType::system)
      .value("user", dolfinx::TimingType::user);

  m.def("timing", &dolfinx::timing);

  m.def(
      "list_timings",
      [](MPICommWrapper comm, std::vector<dolfinx::TimingType> type,
         dolfinx::Table::Reduction reduction)
      {
        std::set<dolfinx::TimingType> _type(type.begin(), type.end());
        dolfinx::list_timings(comm.get(), _type, reduction);
      },
      nb::arg("comm"), nb::arg("type"), nb::arg("reduction"));

  m.def(
      "init_logging",
      [](std::vector<std::string> args)
      {
        std::vector<char*> argv(args.size() + 1, nullptr);
        for (std::size_t i = 0; i < args.size(); ++i)
          argv[i] = const_cast<char*>(args[i].data());
        dolfinx::init_logging(args.size(), argv.data());
      },
      nb::arg("args"));

  m.def(
      "create_sub_index_map",
      [](const dolfinx::common::IndexMap& imap,
         nb::ndarray<const std::int32_t, nb::ndim<1>, nb::c_contig> indices,
         bool allow_owner_change)
      {
        auto [map, submap_to_map] = dolfinx::common::create_sub_index_map(
            imap, std::span(indices.data(), indices.size()),
            dolfinx::common::IndexMapOrder::any, allow_owner_change);
        return std::pair(std::move(map), dolfinx_wrappers::as_nbarray(
                                             std::move(submap_to_map)));
      },
      nb::arg("index_map"), nb::arg("indices"), nb::arg("allow_owner_change"));
}
} // namespace dolfinx_wrappers