56 PyObject *convert(
const T &t)
58 PyErr_SetString(PyExc_NotImplementedError,
"failed to convert C++ object to PyObject");
62 PyObject *convert<std::string>(
const std::string &
s);
67 T convert(PyObject *,
bool &ok)
73 template <
typename T1,
typename T2,
typename T3>
74 PyObject *convert(
const std::tuple<T1, T2, T3> &a234)
76 PyObject *list = PyList_New(0);
77 PyObject *o = convert(std::get<0>(a234));
83 PyList_Append(list, o);
85 o = convert(std::get<1>(a234));
91 PyList_Append(list, o);
93 o = convert(std::get<2>(a234));
99 PyList_Append(list, o);
104 template <
typename T>
105 PyObject *convert(
const std::set<T> &set)
107 PyObject *list = PyList_New(0);
108 for (
auto iter = set.begin(); iter != set.end(); iter++)
110 PyObject *e = convert(*iter);
118 if (PyList_Append(list, convert(*iter)) != 0)
129 template <
typename T>
130 PyObject *convert(
const std::list<T> &set)
132 PyObject *list = PyList_New(0);
133 for (
auto iter = set.begin(); iter != set.end(); iter++)
135 PyObject *e = convert(*iter);
143 if (PyList_Append(list, convert(*iter)) != 0)
154 template <
typename K,
typename V>
155 PyObject *convert(
const std::map<K, V> &set)
157 PyObject *dict = PyDict_New();
158 for (
auto iter = set.begin(); iter != set.end(); iter++)
160 PyObject *k = convert(iter->first);
168 PyObject *
v = convert(iter->second);
177 if (PyDict_SetItem(dict, k,
v) != 0)
201 PyObject *convert<std::string>(
const std::string &
s)
203 return PyString_FromString(
s.c_str());
208 return PyInt_FromSsize_t(
s);
212 std::string convert<std::string>(PyObject *o,
bool &ok)
214 char *
str = PyString_AsString(o);
217 return std::string(
str);
231 template <std::set<std::string> (*list)()>
232 static PyObject *pyetiss_listX(PyObject *
self, PyObject *
args)
234 return convert(list());
236 template <std::list<std::string> (*list)()>
237 static PyObject *pyetiss_listX(PyObject *
self, PyObject *
args)
239 return convert(list());
242 static PyObject *pyetiss_loadLibrary(PyObject *
self, PyObject *
args)
247 if (!PyArg_ParseTuple(
args,
"ss", &name, &path))
265 static PyObject *pyetiss_cfg_get(PyObject *
self, PyObject *
args)
270 if (!PyArg_ParseTuple(
args,
"ss", &name, &def))
272 return convert(
etiss::cfg().get<std::string>(std::string(name), std::string(def)));
274 static PyObject *pyetiss_cfg_set(PyObject *
self, PyObject *
args)
279 if (!PyArg_ParseTuple(
args,
"ss", &name, &def))
281 return convert(
etiss::cfg().get<std::string>(std::string(name), std::string(def)));
283 static PyObject *pyetiss_cfg_all(PyObject *
self, PyObject *
args)
287 static PyObject *pyetiss_cfg_help(PyObject *
self, PyObject *
args)
292 static PyObject *pyetiss_returnCodes(PyObject *
self, PyObject *
args)
294 std::map<etiss::int32, const char *> m = etiss::RETURNCODE::getErrorMessages();
295 std::map<etiss::int32, std::string> ret;
296 for (
auto iter = m.begin(); iter != m.end(); iter++)
298 ret[iter->first] = std::string(etiss::RETURNCODE::getErrorNames()[iter->first]) +
": " + iter->second;
307 static PyMethodDef ETISSMethods[] = {
309 {
"listCPUArchs", pyetiss_listX<etiss::listCPUArchs>, METH_VARARGS,
"List available cpu architectures." },
310 {
"listJITs", pyetiss_listX<etiss::listJITs>, METH_VARARGS,
"List compilers." },
311 {
"listPlugins", pyetiss_listX<etiss::listPlugins>, METH_VARARGS,
"List plugins." },
312 {
"listCPUs", pyetiss_listX<etiss::CPUCore::list>, METH_VARARGS,
"List plugins." },
313 {
"listLibraries", pyetiss_listX<etiss::listLibraries>, METH_VARARGS,
"List loaded libraries." },
314 {
"loadLibrary", pyetiss_loadLibrary, METH_VARARGS,
"Try to open a shared library." },
315 {
"cfgGet", pyetiss_cfg_get, METH_VARARGS,
"Get the string value of an option" },
316 {
"cfgSet", pyetiss_cfg_set, METH_VARARGS,
"Set the string value of an option" },
317 {
"cfg", pyetiss_cfg_all, METH_VARARGS,
"Get all options in a list" },
318 {
"cfgHelp", pyetiss_cfg_help, METH_VARARGS,
"Get all announced option info in a dictionary" },
319 {
"RETURNCODES", pyetiss_returnCodes, METH_VARARGS,
"Get global return codes" },
335 static bool done =
false;
339 Py_InitModule3(
"etiss", ETISSMethods,
"ETISS python bindings");
346 PyThreadState *etiss_main_pyThreadState = 0;
358 void run(std::function<
void(
void)> func)
360 PyGILState_STATE gstate = PyGILState_Ensure();
362 PyGILState_Release(gstate);
365 void runEXT(std::function<
void(
void)> func)
367 Py_BEGIN_ALLOW_THREADS func();
373 run([&
s]() { PyRun_SimpleString(
s); });
379 runString(
"print('ETISS functions are provided the the module \\'etiss\\'.')");
380 runString(
"import code;\ncode.interact(\"ETISS Python Console\")");
386 if (
etiss::cfg().get<bool>(
"pyinitialize",
true))
389 if (
etiss::cfg().get<bool>(
"pyinittheads",
true))
391 if (PyEval_ThreadsInitialized() == 0)
393 PyEval_InitThreads();
394 PyEval_ReleaseLock();
402 PyEval_AcquireLock();
404 PyRun_SimpleString(
"print('ETISS: INFO: ETISS has been build with python support.')\n");
406 PyEval_ReleaseLock();
412 if (
etiss::cfg().get<bool>(
"pyfinalize",
true))
419 std::string
evalForString(
const char *stmt,
bool *ok =
nullptr)
423 PyObject *result = 0;
424 run([stmt, &result]() {
425 PyObject *
main = PyImport_AddModule(
"__main__");
430 PyObject *globalDictionary = PyModule_GetDict(
main);
431 if (globalDictionary == 0)
435 PyObject *localDictionary = PyDict_New();
436 if (localDictionary == 0)
440 result = PyRun_String(stmt, Py_file_input, globalDictionary, localDictionary);
445 std::string ret = convert<std::string>(result, ok ? *ok : lok);
474 void run(std::function<
void(
void)> func) {}
Header file of the ETISS library.
__device__ __2f16 float bool s
contains defines to configure ETISS.
bool loadLibrary(std::string path, std::string name)
Load a library.
int main(int argc, const char *argv[])
std::string evalForString(const char *stmt, bool *ok=nullptr)
void runString(std::string s)
void run(std::function< void(void)> func)
void runEXT(std::function< void(void)> func)
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Configuration & cfg(const std::string &cfgName)
Get reference of the global ETISS configuration object.
Verbosity & verbosity()
Get log level reference.
void log(Verbosity level, std::string msg)
write log message at the given level.