Commit 588b5d6c authored by Markus Mirz's avatar Markus Mirz
Browse files

python: remove deprecated shmem interface



Use the new pybind interface instead if you would like to add a shmem interface.
Signed-off-by: Markus Mirz's avatarMarkus Mirz <mmirz@eonerc.rwth-aachen.de>
parent ae3a4ea6
/* Copyright 2017-2020 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
* DPsim
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/
// #############################################
// Do NOT include this header in any MPL2 files
// #############################################
#pragma once
#ifdef _DEBUG
......@@ -32,10 +20,6 @@
#include <cps/Definitions.h>
#include <dpsim/Interface.h>
#ifdef WITH_SHMEM
#include <dpsim-villas/InterfaceShmem.h>
#endif
namespace DPsim {
namespace Python {
......@@ -44,12 +28,6 @@ namespace Python {
struct Interface {
PyObject_HEAD
#ifdef WITH_SHMEM
DPsim::InterfaceShmem::Config conf;
DPsim::Interface::Ptr intf;
const char *wname, *rname;
PyObject *pyExports;
#endif
static void addExportDesc(Interface *self, int idx, const CPS::String &type, const CPS::String &name, const CPS::String &suffix = "");
static void dealloc(Interface *self);
......
......@@ -19,10 +19,6 @@ if(WIN32)
)
endif()
if(WITH_SHMEM)
target_sources(dpsim_python PUBLIC Interface.cpp)
endif()
target_link_libraries(dpsim_python dpsim cps ${LIBRARIES} ${WIN_LIBRARIES})
target_include_directories(dpsim_python PRIVATE ${INCLUDE_DIRS})
target_compile_options(dpsim_python PUBLIC ${DPSIM_CXX_FLAGS})
......
/* Copyright 2017-2020 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
* DPsim
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/
#include <dpsim/Config.h>
#ifdef WITH_SHMEM
#include <dpsim-villas/InterfaceShmem.h>
#include <cps/AttributeList.h>
#endif
#include <dpsim/Python/Interface.h>
#include <dpsim/Python/Component.h>
......@@ -30,7 +17,6 @@ using namespace DPsim;
void Python::Interface::addExportDesc(Python::Interface* self, int idx, const CPS::String &type, const CPS::String &name, const CPS::String &suffix)
{
#ifdef WITH_SHMEM
while (PyList_Size(self->pyExports) < idx + 1)
PyList_Append(self->pyExports, PyDict_New());
......@@ -39,7 +25,6 @@ void Python::Interface::addExportDesc(Python::Interface* self, int idx, const CP
PyDict_SetItemString(pyExport, "type", PyUnicode_FromString(type.c_str()));
PyList_SetItem(self->pyExports, idx, pyExport);
#endif
}
PyObject* Python::Interface::newfunc(PyTypeObject *type, PyObject *args, PyObject *kwds)
......@@ -48,13 +33,11 @@ PyObject* Python::Interface::newfunc(PyTypeObject *type, PyObject *args, PyObjec
self = (Python::Interface*) type->tp_alloc(type, 0);
if (self) {
#ifdef WITH_SHMEM
using SharedIntfPtr = std::shared_ptr<DPsim::Interface>;
new (&self->intf) SharedIntfPtr();
self->pyExports = PyList_New(0);
#endif
}
return (PyObject*) self;
......@@ -62,13 +45,11 @@ PyObject* Python::Interface::newfunc(PyTypeObject *type, PyObject *args, PyObjec
void Python::Interface::dealloc(Python::Interface* self)
{
#ifdef WITH_SHMEM
using SharedIntfPtr = std::shared_ptr<DPsim::Interface>;
self->intf.~SharedIntfPtr();
Py_DECREF(self->pyExports);
#endif
Py_TYPE(self)->tp_free((PyObject*) self);
}
......@@ -84,7 +65,6 @@ const char* Python::Interface::docAddImport =
":param imag_idx: Index of the imaginary part of the current or voltage.\n";
PyObject* Python::Interface::addImport(Interface* self, PyObject* args, PyObject *kwargs)
{
#ifdef WITH_SHMEM
PyObject *pyObj;
int idx;
const char *attrName;
......@@ -146,10 +126,6 @@ PyObject* Python::Interface::addImport(Interface* self, PyObject* args, PyObject
}
Py_RETURN_NONE;
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return nullptr;
#endif
}
const char* Python::Interface::docAddExport =
......@@ -162,7 +138,6 @@ const char* Python::Interface::docAddExport =
":param imag_idx: Index where the imaginary part of the voltage is written.\n";
PyObject* Python::Interface::addExport(Interface* self, PyObject* args, PyObject* kwargs)
{
#ifdef WITH_SHMEM
PyObject* pyObj;
int idx;
const char *attrName;
......@@ -299,15 +274,10 @@ PyObject* Python::Interface::addExport(Interface* self, PyObject* args, PyObject
}
Py_RETURN_NONE;
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return nullptr;
#endif
}
int Python::Interface::init(Python::Interface *self, PyObject *args, PyObject *kwds)
{
#ifdef WITH_SHMEM
static const char *kwlist[] = {"wname", "rname", "queuelen", "samplelen", "polling", nullptr};
/* Default values */
......@@ -320,24 +290,19 @@ int Python::Interface::init(Python::Interface *self, PyObject *args, PyObject *k
return -1;
}
self->intf = std::make_shared<DPsim::InterfaceShmem>(self->wname, self->rname, &self->conf);
// instantiate specific interface here
//self->intf = std::make_shared<DPsim::Interface>(self->wname, self->rname, &self->conf);
return 0;
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return -1;
#endif
}
PyMemberDef Python::Interface::members[] = {
#ifdef WITH_SHMEM
{(char *) "wname", T_STRING, offsetof(Python::Interface, wname), READONLY, nullptr},
{(char *) "rname", T_STRING, offsetof(Python::Interface, rname), READONLY, nullptr},
{(char *) "queuelen", T_INT, offsetof(Python::Interface, conf.queuelen), READONLY, nullptr},
{(char *) "samplelen", T_INT, offsetof(Python::Interface, conf.samplelen), READONLY, nullptr},
{(char *) "polling", T_INT, offsetof(Python::Interface, conf.polling), READONLY, nullptr},
{(char *) "exports", T_OBJECT, offsetof(Python::Interface, pyExports), READONLY, nullptr},
#endif
{nullptr}
};
......
/* Copyright 2017-2020 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
* DPsim
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/
#include <chrono>
......@@ -34,9 +26,6 @@
#include <dpsim/OpenMPLevelScheduler.h>
#endif
#ifdef WITH_SHMEM
#include <dpsim-villas/PthreadPoolScheduler.h>
#endif
using namespace DPsim;
using namespace CPS;
......@@ -55,16 +44,6 @@ void Python::Simulation::threadFunction(Python::Simulation *self)
Timer timer(Timer::Flags::fail_on_overrun);
#ifdef WITH_SHMEM
for (auto ifm : self->sim->interfaces())
ifm.interface->open(self->sim->mLog);
// optional start synchronization
if (self->startSync) {
self->sim->sync();
}
#endif
if (self->realTime) {
timer.setStartTime(self->startTime);
timer.setInterval(self->realTimeStep);
......@@ -124,11 +103,6 @@ void Python::Simulation::threadFunction(Python::Simulation *self)
self->sim->scheduler()->stop();
#ifdef WITH_SHMEM
for (auto ifm : self->sim->interfaces())
ifm.interface->close();
#endif
for (auto lg : self->sim->loggers())
lg->close();
......@@ -396,44 +370,6 @@ fail:
return nullptr;
}
const char* Python::Simulation::docAddInterface =
"add_interface(intf,sync_start=True)\n"
"Add an external interface to the simulation. "
"Before each timestep, values are read from this interface and results are written to this interface afterwards. "
"See the documentation of `Interface` for more details.\n"
"\n"
":param intf: The `Interface` to be added.\n"
":param sync_start: Whether to use the interface to synchronize at the start of the simulation.";
PyObject* Python::Simulation::addInterface(Simulation* self, PyObject* args, PyObject *kwargs)
{
#ifdef WITH_SHMEM
PyObject* pyObj;
Python::Interface* pyIntf;
int start_sync = 1;
const char *kwlist[] = {"intf", "sync_start", nullptr};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|p", (char **) kwlist, &pyObj, &start_sync))
return nullptr;
if (!PyObject_TypeCheck(pyObj, &Python::Interface::type)) {
PyErr_SetString(PyExc_TypeError, "Argument must be dpsim.Interface");
return nullptr;
}
pyIntf = (Python::Interface*) pyObj;
self->sim->addInterface(pyIntf->intf.get(), start_sync);
Py_INCREF(pyObj);
self->refs.push_back(pyObj);
Py_RETURN_NONE;
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return nullptr;
#endif
}
const char *Python::Simulation::docAddLogger =
"add_logger(logger)";
PyObject* Python::Simulation::addLogger(Simulation *self, PyObject *args, PyObject *kwargs)
......@@ -659,15 +595,6 @@ PyObject* Python::Simulation::setScheduler(Simulation *self, PyObject *args, PyO
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return nullptr;
#endif
} else if (!strcmp(schedName, "pthread_pool")) {
#ifdef WITH_SHMEM
if (threads <= 0)
threads = 1;
self->sim->setScheduler(std::make_shared<PthreadPoolScheduler>(threads));
#else
PyErr_SetString(PyExc_NotImplementedError, "not implemented on this platform");
return nullptr;
#endif
} else if (!strcmp(schedName, "thread_level")) {
// TODO sensible default (`nproc`?)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment