This repository was archived by the owner on Mar 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpyembed.hpp
More file actions
188 lines (152 loc) · 5.53 KB
/
Copy pathpyembed.hpp
File metadata and controls
188 lines (152 loc) · 5.53 KB
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
/*************************************************************************
* Copyright (C) 2018-2019 Blue Brain Project
*
* This file is part of NMODL distributed under the terms of the GNU
* Lesser General Public License. See top-level LICENSE file for details.
*************************************************************************/
#pragma once
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include "pybind11/embed.h"
namespace nmodl {
namespace pybind_wrappers {
struct PythonExecutor {
virtual ~PythonExecutor() {}
virtual void operator()() = 0;
};
struct SolveLinearSystemExecutor: public PythonExecutor {
// input
std::vector<std::string> eq_system;
std::vector<std::string> state_vars;
std::set<std::string> vars;
bool small_system;
bool elimination;
// This is used only if elimination is true. It gives the root for the tmp variables
std::string tmp_unique_prefix;
std::set<std::string> function_calls;
// output
// returns a vector of solutions, i.e. new statements to add to block:
std::vector<std::string> solutions;
// and a vector of new local variables that need to be declared in the block:
std::vector<std::string> new_local_vars;
// may also return a python exception message:
std::string exception_message;
// executor function
virtual void operator()() override;
};
struct SolveNonLinearSystemExecutor: public PythonExecutor {
// input
std::vector<std::string> eq_system;
std::vector<std::string> state_vars;
std::set<std::string> vars;
std::set<std::string> function_calls;
// output
// returns a vector of solutions, i.e. new statements to add to block:
std::vector<std::string> solutions;
// returns if the system is linear or not.
bool linear;
// may also return a python exception message:
std::string exception_message;
// executor function
virtual void operator()() override;
};
struct DiffeqSolverExecutor: public PythonExecutor {
// input
std::string node_as_nmodl;
std::string dt_var;
std::set<std::string> vars;
bool use_pade_approx;
std::set<std::string> function_calls;
std::string method;
// output
// returns solution, i.e. new statement to add to block:
std::string solution;
// may also return a python exception message:
std::string exception_message;
// executor function
virtual void operator()() override;
};
struct AnalyticDiffExecutor: public PythonExecutor {
// input
std::vector<std::string> expressions;
std::set<std::string> used_names_in_block;
// output
// returns solution, i.e. new statement to add to block:
std::string solution;
// may also return a python exception message:
std::string exception_message;
// executor function
virtual void operator()() override;
};
SolveLinearSystemExecutor* create_sls_executor_func();
SolveNonLinearSystemExecutor* create_nsls_executor_func();
DiffeqSolverExecutor* create_des_executor_func();
AnalyticDiffExecutor* create_ads_executor_func();
void destroy_sls_executor_func(SolveLinearSystemExecutor* exec);
void destroy_nsls_executor_func(SolveNonLinearSystemExecutor* exec);
void destroy_des_executor_func(DiffeqSolverExecutor* exec);
void destroy_ads_executor_func(AnalyticDiffExecutor* exec);
void initialize_interpreter_func();
void finalize_interpreter_func();
struct pybind_wrap_api {
decltype(&initialize_interpreter_func) initialize_interpreter;
decltype(&finalize_interpreter_func) finalize_interpreter;
decltype(&create_sls_executor_func) create_sls_executor;
decltype(&create_nsls_executor_func) create_nsls_executor;
decltype(&create_des_executor_func) create_des_executor;
decltype(&create_ads_executor_func) create_ads_executor;
decltype(&destroy_sls_executor_func) destroy_sls_executor;
decltype(&destroy_nsls_executor_func) destroy_nsls_executor;
decltype(&destroy_des_executor_func) destroy_des_executor;
decltype(&destroy_ads_executor_func) destroy_ads_executor;
};
/**
* A singleton class handling access to the pybind_wrap_api struct
*
* This class manages the runtime loading of the libpython so/dylib file and the python binding
* wrapper library and provides access to the API wrapper struct that can be used to access the
* pybind11 embedded python functionality.
*/
class EmbeddedPythonLoader {
public:
/**
* Construct (if not already done) and get the only instance of this class
*
* @return the EmbeddedPythonLoader singleton instance
*/
static EmbeddedPythonLoader& get_instance() {
static EmbeddedPythonLoader instance;
return instance;
}
EmbeddedPythonLoader(const EmbeddedPythonLoader&) = delete;
void operator=(const EmbeddedPythonLoader&) = delete;
/**
* Get a pointer to the pybind_wrap_api struct
*
* Get access to the container struct for the pointers to the functions in the wrapper library.
* @return a pybind_wrap_api pointer
*/
const pybind_wrap_api* api();
~EmbeddedPythonLoader() {
unload();
}
private:
pybind_wrap_api* wrappers = nullptr;
void* pylib_handle = nullptr;
void* pybind_wrapper_handle = nullptr;
bool have_wrappers();
void load_libraries();
void populate_symbols();
void unload();
EmbeddedPythonLoader() {
if (!have_wrappers()) {
load_libraries();
populate_symbols();
}
}
};
pybind_wrap_api init_pybind_wrap_api() noexcept;
} // namespace pybind_wrappers
} // namespace nmodl