-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexago_python.cpp
72 lines (57 loc) · 1.99 KB
/
exago_python.cpp
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
#include "exago_python.hpp"
static inline auto prefix() -> const char * { return CMAKE_INSTALL_PREFIX; }
int initialize(char *appname, MPI_Comm comm) {
PetscErrorCode ierr;
ierr = ExaGOInitialize(comm, nullptr, nullptr, appname, nullptr);
ExaGOCheckError(ierr);
return ierr;
}
int initialize_no_comm(char *appname) {
PetscErrorCode ierr;
MPI_Comm communicator;
ierr = ExaGOGetSelfCommunicator(&communicator);
ExaGOCheckError(ierr);
ierr = ExaGOInitialize(communicator, nullptr, nullptr, appname, nullptr);
ExaGOCheckError(ierr);
return ierr;
}
/*! Return a MPI communicator from mpi4py communicator object. */
MPI_Comm *get_mpi_comm(pybind11::object py_comm) {
auto comm_ptr = PyMPIComm_Get(py_comm.ptr());
if (!comm_ptr)
throw pybind11::error_already_set();
return comm_ptr;
}
PYBIND11_MODULE(exago, m) {
// Initialize mpi4py's C-API
if (import_mpi4py() < 0) {
// mpi4py calls the Python C API
// we let pybind11 give us the detailed traceback
throw pybind11::error_already_set();
}
m.doc() = "Python wrapper for ExaGO.";
/* Bindings for top-level utilities, such as initialization, finalization,
* and helpers for interacting with enums and macros */
// Initialize with pybind11 comm
m.def("initialize", [](char *appname, pybind11::object py_comm) {
auto comm = get_mpi_comm(py_comm);
return initialize(appname, *comm);
});
m.def("initialize", &initialize_no_comm);
m.def("finalize", &ExaGOFinalize);
m.def("prefix", &prefix);
extern void init_exago_pflow(pybind11::module & m);
init_exago_pflow(m);
#if defined(EXAGO_ENABLE_IPOPT) || defined(EXAGO_ENABLE_HIOP)
extern void init_exago_opflow(pybind11::module & m);
init_exago_opflow(m);
#endif
#if defined(EXAGO_ENABLE_IPOPT)
extern void init_exago_scopflow(pybind11::module & m);
init_exago_scopflow(m);
extern void init_exago_sopflow(pybind11::module & m);
init_exago_sopflow(m);
extern void init_exago_tcopflow(pybind11::module & m);
init_exago_tcopflow(m);
#endif
}