Debian Codegraph @

python/src/common.cpp

raw
1 // Copyright (C) 2017 Chris Richardson and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <vector>
22 #include <pybind11/pybind11.h>
23 #include <pybind11/stl.h>
24
25 #include <dolfin/common/MPI.h>
26 #include <dolfin/common/constants.h>
27 #include <dolfin/common/defines.h>
28 #include <dolfin/common/SubSystemsManager.h>
29 #include <dolfin/common/Timer.h>
30 #include <dolfin/common/Variable.h>
31 #include <dolfin/common/timing.h>
32 #include <dolfin/log/Table.h>
33
34 #include "casters.h"
35 #include "MPICommWrapper.h"
36
37 namespace py = pybind11;
38
39 namespace dolfin_wrappers
40 {
41 // Interface for dolfin/common
42 void common(py::module& m)
43 {
44 // dolfin::Variable
45 py::class_<dolfin::Variable, std::shared_ptr<dolfin::Variable>>
46 (m, "Variable", "Variable base class")
47 .def("id", &dolfin::Variable::id)
48 .def("name", &dolfin::Variable::name)
49 .def("label", &dolfin::Variable::label)
50 .def("rename", &dolfin::Variable::rename)
51 .def_readwrite("parameters", &dolfin::Variable::parameters);
52
53 // From dolfin/common/defines.h
54 m.def("has_debug", &dolfin::has_debug);
55 m.def("has_hdf5", &dolfin::has_hdf5);
56 m.def("has_hdf5_parallel", &dolfin::has_hdf5_parallel);
57 m.def("has_mpi", &dolfin::has_mpi);
58 m.def("has_mpi4py", []()
59 {
60 #ifdef HAS_PYBIND11_MPI4PY
61 return true;
62 #else
63 return false;
64 #endif
65 }, "Return `True` if DOLFIN is configured with mpi4py");
66 m.def("has_parmetis", &dolfin::has_parmetis);
67 m.def("has_scotch", &dolfin::has_scotch);
68 m.def("has_petsc", &dolfin::has_petsc,
69 "Return `True` if DOLFIN is configured with PETSc");
70 m.def("has_slepc", &dolfin::has_slepc,
71 "Return `True` if DOLFIN is configured with SLEPc");
72 m.def("has_sundials", &dolfin::has_sundials,
73 "Return `True` if DOLFIN is configured with SUNDIALS");
74 m.def("has_petsc4py", []()
75 {
76 #ifdef HAS_PYBIND11_PETSC4PY
77 return true;
78 #else
79 return false;
80 #endif
81 }, "Return `True` if DOLFIN is configured with petsc4py");
82 m.def("has_slepc4py", []()
83 {
84 #ifdef HAS_PYBIND11_SLEPC4PY
85 return true;
86 #else
87 return false;
88 #endif
89 }, "Return `True` if DOLFIN is configured with slepc4py");
90 m.def("git_commit_hash", &dolfin::git_commit_hash,
91 "Returns git hash for this build.");
92 m.def("sizeof_la_index", &dolfin::sizeof_la_index);
93
94 m.attr("DOLFIN_EPS") = DOLFIN_EPS;
95 m.attr("DOLFIN_EPS_LARGE") = DOLFIN_EPS_LARGE;
96 m.attr("DOLFIN_PI") = DOLFIN_PI;
97
98 // dolfin::Timer
99 py::class_<dolfin::Timer, std::shared_ptr<dolfin::Timer>>
100 (m, "Timer", "Timer class")
101 .def(py::init<>())
102 .def(py::init<std::string>())
103 .def("start", &dolfin::Timer::start, "Start timer")
104 .def("stop", &dolfin::Timer::stop, "Stop timer")
105 .def("resume", &dolfin::Timer::resume)
106 .def("elapsed", &dolfin::Timer::elapsed);
107
108 // dolfin::Timer enums
109 py::enum_<dolfin::TimingClear>(m, "TimingClear")
110 .value("clear", dolfin::TimingClear::clear)
111 .value("keep", dolfin::TimingClear::keep);
112 py::enum_<dolfin::TimingType>(m, "TimingType")
113 .value("wall", dolfin::TimingType::wall)
114 .value("system", dolfin::TimingType::system)
115 .value("user", dolfin::TimingType::user);
116
117 // dolfin/common free functions
118 m.def("timing", &dolfin::timing);
119 m.def("timings", [](dolfin::TimingClear clear, std::vector<dolfin::TimingType> type)
120 {
121 std::set<dolfin::TimingType> _type(type.begin(), type.end());
122 return dolfin::timings(clear, _type);
123 });
124 m.def("list_timings", [](dolfin::TimingClear clear, std::vector<dolfin::TimingType> type)
125 {
126 std::set<dolfin::TimingType> _type(type.begin(), type.end());
127 dolfin::list_timings(clear, _type);
128 });
129 m.def("dump_timings_to_xml", &dolfin::dump_timings_to_xml);
130
131 // dolfin::SubSystemsManager
132 py::class_<dolfin::SubSystemsManager, std::unique_ptr<dolfin::SubSystemsManager, py::nodelete>>
133 (m, "SubSystemsManager")
134 .def_static("init_petsc", (void (*)()) &dolfin::SubSystemsManager::init_petsc)
135 .def_static("init_petsc", [](std::vector<std::string> args)
136 {
137 std::vector<char*> argv(args.size());
138 for (std::size_t i = 0; i < args.size(); ++i)
139 argv[i] = const_cast<char*>(args[i].data());
140 dolfin::SubSystemsManager::init_petsc(args.size(), argv.data());
141 })
142 .def_static("finalize", &dolfin::SubSystemsManager::finalize)
143 .def_static("responsible_mpi", &dolfin::SubSystemsManager::responsible_mpi)
144 .def_static("responsible_petsc", &dolfin::SubSystemsManager::responsible_petsc)
145 .def_static("mpi_initialized", &dolfin::SubSystemsManager::mpi_initialized)
146 .def_static("mpi_finalized", &dolfin::SubSystemsManager::mpi_finalized);
147
148 }
149
150 // Interface for MPI
151 void mpi(py::module& m)
152 {
153
154 #ifndef HAS_PYBIND11_MPI4PY
155 // Expose the MPICommWrapper directly since we cannot cast it to
156 // mpi4py
157 py::class_<MPICommWrapper>(m, "MPICommWrapper",
158 "DOLFIN is compiled without support for mpi4py. This object can be "
159 "passed into DOLFIN as an MPI communicator, but is not an mpi4py comm.")
160 .def("underlying_comm", [](MPICommWrapper self)
161 { return (std::uintptr_t) self.get(); },
162 "Return the underlying MPI_Comm cast to std::uintptr_t. "
163 "The return value may or may not make sense depending on the MPI implementation.");
164 #endif
165
166 // dolfin::MPI
167 py::class_<dolfin::MPI>(m, "MPI", "MPI utilities")
168 .def_property_readonly_static("comm_world", [](py::object)
169 { return MPICommWrapper(MPI_COMM_WORLD); })
170 .def_property_readonly_static("comm_self", [](py::object)
171 { return MPICommWrapper(MPI_COMM_SELF); })
172 .def_property_readonly_static("comm_null", [](py::object)
173 { return MPICommWrapper(MPI_COMM_NULL); })
174 .def_static("init", (void (*)()) &dolfin::SubSystemsManager::init_mpi,
175 "Initialise MPI")
176 .def_static("init",
177 [](std::vector<std::string> args, int required_thread_level)->int
178 {
179 std::vector<char*> argv(args.size());
180 for (std::size_t i = 0; i < args.size(); ++i)
181 argv[i] = const_cast<char*>(args[i].data());
182 return dolfin::SubSystemsManager::
183 init_mpi(args.size(), argv.data(), required_thread_level);
184 },
185 "Initialise MPI with command-line args and required level "
186 "of thread support. Return provided thread level.")
187 .def_static("responsible", &dolfin::SubSystemsManager::responsible_mpi,
188 "Return true if DOLFIN initialised MPI (and is therefore "
189 "responsible for finalization)")
190 .def_static("initialized", &dolfin::SubSystemsManager::mpi_initialized,
191 "Check if MPI has been initialised")
192 .def_static("finalized", &dolfin::SubSystemsManager::mpi_finalized,
193 "Check if MPI has been finalized")
194 .def_static("barrier", [](const MPICommWrapper comm)
195 { return dolfin::MPI::barrier(comm.get()); })
196 .def_static("rank", [](const MPICommWrapper comm)
197 { return dolfin::MPI::rank(comm.get()); })
198 .def_static("size", [](const MPICommWrapper comm)
199 { return dolfin::MPI::size(comm.get()); })
200 .def_static("local_range", [](MPICommWrapper comm, std::int64_t N)
201 { return dolfin::MPI::local_range(comm.get(), N); })
202 // templated for double
203 .def_static("max", [](const MPICommWrapper comm, double value)
204 { return dolfin::MPI::max(comm.get(), value); })
205 .def_static("min", [](const MPICommWrapper comm, double value)
206 { return dolfin::MPI::min(comm.get(), value); })
207 .def_static("sum", [](const MPICommWrapper comm, double value)
208 { return dolfin::MPI::sum(comm.get(), value); })
209 // templated for dolfin::Table
210 .def_static("max", [](const MPICommWrapper comm, dolfin::Table value)
211 { return dolfin::MPI::max(comm.get(), value); })
212 .def_static("min", [](const MPICommWrapper comm, dolfin::Table value)
213 { return dolfin::MPI::min(comm.get(), value); })
214 .def_static("sum", [](const MPICommWrapper comm, dolfin::Table value)
215 { return dolfin::MPI::sum(comm.get(), value); })
216 .def_static("avg", [](const MPICommWrapper comm, dolfin::Table value)
217 { return dolfin::MPI::avg(comm.get(), value); });
218 }
219 }