OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PyParser.cpp
Go to the documentation of this file.
1#include <Python.h>
2#include <structmember.h>
3
4#include <boost/algorithm/string.hpp>
5#include <vector>
6#include <string>
7#include <sstream>
8#include <algorithm>
9#include <iterator>
10
11#include "Main.cpp"
12//#include "PyOpal/Globals.h"
13#include "mpi.h"
14#include "Parser/Parser.h" // Classic
16
18std::string("Initialise from opal file\n")+
19std::string("If you are getting an error message from openMPI, try\n")+
20std::string("rebuilding the MPI library with --disable-dlopen switch\n");
21
22extern "C" {
23PyObject* initialise_from_opal_file(PyObject *self, PyObject *args, PyObject *kwds) {
24 static char *kwlist[] = {const_cast<char*>("file_name"),
25 NULL};
26 char* value;
27 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist,
28 &value)) {
29 return NULL;
30 }
31 const char* exe = "parser";
32 char* argvr[3];
33 // argv must be NULL terminated array (a week of my life figuring that one)
34 argvr[0] = static_cast<char*>(malloc(sizeof('c')*7));
35 argvr[1] = value;
36 argvr[2] = NULL;
37 strcpy(argvr[0], exe);
38 //strcpy(argvr[1], value);
39 try {
40 opalMain(2, argvr);
41 } catch (...) {
42 std::string err = "Failed to initialise OPAL from file";
43 PyErr_SetString(PyExc_ValueError, err.c_str());
44 }
45 Py_RETURN_NONE;
46}
47}
48
49std::string list_objects_docstring = "List objects";
50
51PyObject* list_objects(PyObject *self, PyObject *args, PyObject *kwds) {
52 std::vector<std::string> names = OpalData::getInstance()->getAllNames();
53 for (size_t i = 0; i < names.size(); ++i) {
54 std::cout << " " << names[i] << std::endl;
55 }
56 Py_RETURN_NONE;
57}
58
59const char* module_docstring = "parser module parses the input";
60
61static PyMethodDef _module_methods[] = {
62{"initialise_from_opal_file", (PyCFunction)initialise_from_opal_file,
63 METH_VARARGS|METH_KEYWORDS, initialise_from_opal_file_docstring.c_str()},
64{"list_objects", (PyCFunction)list_objects,
65 METH_VARARGS|METH_KEYWORDS, list_objects_docstring.c_str()},
66{NULL}
67};
68
69static struct PyModuleDef parserdef = {
70 PyModuleDef_HEAD_INIT,
71 "parser", /* m_name */
72 module_docstring, /* m_doc */
73 -1, /* m_size */
74 _module_methods, /* m_methods */
75 NULL, /* m_reload */
76 NULL, /* m_traverse */
77 NULL, /* m_clear */
78 NULL, /* m_free */
79};
80
81PyMODINIT_FUNC PyInit_parser(void) {
82 //PyOpal::Globals::Initialise();
83 PyObject* module = PyModule_Create(&parserdef);
84 return module;
85}
int opalMain(int argc, char *argv[])
Definition: Main.cpp:133
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::vector< std::string > getAllNames() const
Get a list of all objects.
Definition: OpalData.cpp:610
static OpalData * getInstance()
Definition: OpalData.cpp:196
PyObject * initialise_from_opal_file(PyObject *self, PyObject *args, PyObject *kwds)
Definition: PyParser.cpp:23
const char * module_docstring
Definition: PyParser.cpp:59
std::string list_objects_docstring
Definition: PyParser.cpp:49
PyObject * list_objects(PyObject *self, PyObject *args, PyObject *kwds)
Definition: PyParser.cpp:51
std::string initialise_from_opal_file_docstring
Definition: PyParser.cpp:17
PyMODINIT_FUNC PyInit_parser(void)
Definition: PyParser.cpp:81