From 8f465500836ee2faf2c48c5aa49d6cce648c8ac4 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Fri, 1 Mar 2019 16:30:05 +0100 Subject: [PATCH] Add GIL management. GIL management for JobParametersProxy and PyStudyFunction. Some minor code formating changes. --- src/cpp/JobParametersProxy.cxx | 60 ++++++++++++++++++++++++++++++++++ src/cpp/JobParametersProxy.hxx | 5 +++ src/cpp/PyStudyFunction.cxx | 43 +++++++++++++++++++++++- src/cpp/PyStudyFunction.hxx | 2 ++ src/gui/ParamsConfig.cxx | 3 ++ src/gui/ParamsConfig.hxx | 4 ++- src/gui/PathsConfig.cxx | 3 ++ src/gui/PathsConfig.hxx | 4 ++- src/gui/QuickConfig.cxx | 3 ++ src/gui/QuickConfig.hxx | 4 ++- src/gui/ResourceWidget.cxx | 3 ++ src/gui/ResourceWidget.hxx | 4 ++- src/gui/ydefxgui.cxx | 2 +- src/pydefx/configuration.py | 2 +- src/pydefx/parameters.py | 8 ++++- 15 files changed, 142 insertions(+), 8 deletions(-) diff --git a/src/cpp/JobParametersProxy.cxx b/src/cpp/JobParametersProxy.cxx index 662b856..c0ce84a 100644 --- a/src/cpp/JobParametersProxy.cxx +++ b/src/cpp/JobParametersProxy.cxx @@ -33,16 +33,52 @@ namespace ydefx JobParametersProxy::JobParametersProxy() : _pyParameters(nullptr) { + py2cpp::AutoGIL gil; py2cpp::PyFunction objConstructor; objConstructor.loadExp("pydefx", "Parameters"); _pyParameters = objConstructor(); } +JobParametersProxy::JobParametersProxy(const JobParametersProxy& copy) +: _pyParameters(nullptr) +{ + if(copy._pyParameters) + { + py2cpp::AutoGIL gil; + py2cpp::PyFunction deepCopyFn; + deepCopyFn.loadExp("copy", "deepcopy"); + _pyParameters = deepCopyFn(copy._pyParameters); + } +} + +JobParametersProxy::~JobParametersProxy() +{ + py2cpp::AutoGIL gil; + _pyParameters.reset(nullptr); +} + +JobParametersProxy& JobParametersProxy::operator=(const JobParametersProxy& copy) +{ + if(this != ©) + { + py2cpp::AutoGIL gil; + if(copy._pyParameters) + { + py2cpp::PyFunction deepCopyFn; + deepCopyFn.loadExp("copy", "deepcopy"); + _pyParameters = deepCopyFn(copy._pyParameters); + } + else + _pyParameters.reset(nullptr); + } + return *this; +} std::string JobParametersProxy::getAttrString(const std::string& attributeName)const { std::string result; + py2cpp::AutoGIL gil; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr(attributeName); return result; @@ -52,6 +88,7 @@ void JobParametersProxy::setAttr(const std::string& attributeName, const std::string& value) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .setAttr(attributeName, py2cpp::toPyPtr(value)); } @@ -59,6 +96,7 @@ JobParametersProxy::setAttr(const std::string& attributeName, int JobParametersProxy::getResAttr(const std::string& attributeName)const { int result; + py2cpp::AutoGIL gil; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr("resource_required") .getAttr(attributeName); @@ -67,6 +105,7 @@ int JobParametersProxy::getResAttr(const std::string& attributeName)const void JobParametersProxy::setResAttr(const std::string& attributeName, int value) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .getAttr("resource_required") .setAttr(attributeName, py2cpp::toPyPtr(value)); @@ -124,6 +163,7 @@ void JobParametersProxy::env_file(const std::string& v) std::list JobParametersProxy::in_files()const { + py2cpp::AutoGIL gil; std::list result; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr("in_files"); @@ -146,6 +186,7 @@ void JobParametersProxy::remove_in_file(const std::string& path) void JobParametersProxy::in_files(const std::list& pathList) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .setAttr("in_files", py2cpp::toPyPtr(pathList)); } @@ -193,6 +234,7 @@ void JobParametersProxy::maximum_duration(const std::string& v) // ResourceParameters std::string JobParametersProxy::resource_name()const { + py2cpp::AutoGIL gil; std::string result; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr("resource_required") @@ -202,6 +244,7 @@ std::string JobParametersProxy::resource_name()const void JobParametersProxy::resource_name(const std::string& name) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .getAttr("resource_required") .setAttr("name", py2cpp::toPyPtr(name)); @@ -269,6 +312,7 @@ void JobParametersProxy::partition(const std::string& v) bool JobParametersProxy::exclusive()const { + py2cpp::AutoGIL gil; bool result; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr("exclusive"); @@ -277,12 +321,14 @@ bool JobParametersProxy::exclusive()const void JobParametersProxy::exclusive(bool v) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .setAttr("exclusive", py2cpp::toPyPtr(v)); } unsigned int JobParametersProxy::mem_per_cpu()const { + py2cpp::AutoGIL gil; int result; py2cpp::pyResult(result) = _pyParameters.getAttr("salome_parameters") .getAttr("mem_per_cpu"); @@ -293,6 +339,7 @@ unsigned int JobParametersProxy::mem_per_cpu()const void JobParametersProxy::mem_per_cpu(unsigned int v) { + py2cpp::AutoGIL gil; _pyParameters.getAttr("salome_parameters") .setAttr("mem_per_cpu", py2cpp::toPyPtr(v)); } @@ -320,6 +367,7 @@ void JobParametersProxy::extra_params(const std::string& v) unsigned int JobParametersProxy::nb_branches()const { + py2cpp::AutoGIL gil; unsigned int result; py2cpp::pyResult(result) = _pyParameters.getAttr("nb_branches"); return result; @@ -327,11 +375,13 @@ unsigned int JobParametersProxy::nb_branches()const void JobParametersProxy::nb_branches(unsigned int v) { + py2cpp::AutoGIL gil; _pyParameters.setAttr("nb_branches", py2cpp::toPyPtr(v)); } void JobParametersProxy::configureResource(const std::string& resourceName) { + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyParameters, "configureResource"); pyFn(resourceName); @@ -339,13 +389,23 @@ void JobParametersProxy::configureResource(const std::string& resourceName) void JobParametersProxy::createResultDirectory(const std::string& basePath) { + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyParameters, "createResultDirectory"); pyFn(basePath); } +void JobParametersProxy::createTmpResultDirectory() +{ + py2cpp::AutoGIL gil; + py2cpp::PyFunction pyFn; + pyFn.loadExp(_pyParameters, "createTmpResultDirectory"); + pyFn(); +} + std::list JobParametersProxy::AvailableResources() { + py2cpp::AutoGIL gil; std::list result; py2cpp::PyFunction pyFn; pyFn.loadExp("pydefx.configuration", "availableResources"); diff --git a/src/cpp/JobParametersProxy.hxx b/src/cpp/JobParametersProxy.hxx index 285bfd0..769bc47 100644 --- a/src/cpp/JobParametersProxy.hxx +++ b/src/cpp/JobParametersProxy.hxx @@ -29,6 +29,9 @@ class JobParametersProxy { public: JobParametersProxy(); + JobParametersProxy(const JobParametersProxy& copy); + JobParametersProxy& operator=(const JobParametersProxy& copy); + ~JobParametersProxy(); std::string job_name()const; void job_name(const std::string& v); @@ -106,6 +109,8 @@ public: void configureResource(const std::string& resourceName); //! create a new result directory as a subdirectory of the given path. void createResultDirectory(const std::string& basePath); + //! create a new result directory in the system tmp + void createTmpResultDirectory(); friend PyObject * py2cpp::toPy(const JobParametersProxy& jp); static std::list AvailableResources(); diff --git a/src/cpp/PyStudyFunction.cxx b/src/cpp/PyStudyFunction.cxx index 96dedc2..2ee90bc 100644 --- a/src/cpp/PyStudyFunction.cxx +++ b/src/cpp/PyStudyFunction.cxx @@ -33,15 +33,50 @@ namespace ydefx PyStudyFunction::PyStudyFunction() : _pyObject(nullptr) { + py2cpp::AutoGIL gil; py2cpp::PyFunction objConstructor; objConstructor.loadExp("pydefx", "PyScript"); _pyObject = objConstructor(); } -PyStudyFunction::~PyStudyFunction(){} +PyStudyFunction::PyStudyFunction(const PyStudyFunction& copy) +: _pyObject(nullptr) +{ + if(copy._pyObject) + { + py2cpp::AutoGIL gil; + py2cpp::PyFunction deepCopyFn; + deepCopyFn.loadExp("copy", "deepcopy"); + _pyObject = deepCopyFn(copy._pyObject); + } +} + +PyStudyFunction::~PyStudyFunction() +{ + py2cpp::AutoGIL gil; + _pyObject.reset(nullptr); +} + +PyStudyFunction& PyStudyFunction::operator=(const PyStudyFunction& copy) +{ + if(this != ©) + { + py2cpp::AutoGIL gil; + if(copy._pyObject) + { + py2cpp::PyFunction deepCopyFn; + deepCopyFn.loadExp("copy", "deepcopy"); + _pyObject = deepCopyFn(copy._pyObject); + } + else + _pyObject.reset(nullptr); + } + return *this; +} void PyStudyFunction::loadFile(const std::string& path) { + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "loadFile"); pyFn(path); @@ -49,6 +84,7 @@ void PyStudyFunction::loadFile(const std::string& path) void PyStudyFunction::loadString(const std::string& value) { + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "loadString"); pyFn(value); @@ -56,6 +92,7 @@ void PyStudyFunction::loadString(const std::string& value) void PyStudyFunction::save(const std::string& path) { + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "saveFile"); pyFn(path); @@ -64,6 +101,7 @@ void PyStudyFunction::save(const std::string& path) std::string PyStudyFunction::content()const { std::string result; + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "content"); py2cpp::pyResult(result) = pyFn(); @@ -73,6 +111,7 @@ std::string PyStudyFunction::content()const std::list PyStudyFunction::inputNames()const { std::list result; + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "getInputNames"); py2cpp::pyResult(result) = pyFn(); @@ -82,6 +121,7 @@ std::list PyStudyFunction::inputNames()const std::list PyStudyFunction::outputNames()const { std::list result; + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "getOutputNames"); py2cpp::pyResult(result) = pyFn(); @@ -91,6 +131,7 @@ std::list PyStudyFunction::outputNames()const std::string PyStudyFunction::errors()const { std::string result; + py2cpp::AutoGIL gil; py2cpp::PyFunction pyFn; pyFn.loadExp(_pyObject, "getErrors"); py2cpp::pyResult(result) = pyFn(); diff --git a/src/cpp/PyStudyFunction.hxx b/src/cpp/PyStudyFunction.hxx index df6dcdd..e3e02a5 100644 --- a/src/cpp/PyStudyFunction.hxx +++ b/src/cpp/PyStudyFunction.hxx @@ -27,6 +27,8 @@ class PyStudyFunction : StudyFunction { public: PyStudyFunction(); + PyStudyFunction(const PyStudyFunction& copy); + PyStudyFunction& operator=(const PyStudyFunction& copy); virtual ~PyStudyFunction(); virtual void loadFile(const std::string& path); virtual void loadString(const std::string&); diff --git a/src/gui/ParamsConfig.cxx b/src/gui/ParamsConfig.cxx index 3dbaa32..bb52bf0 100644 --- a/src/gui/ParamsConfig.cxx +++ b/src/gui/ParamsConfig.cxx @@ -18,6 +18,8 @@ // #include "ParamsConfig.hxx" +namespace ydefx +{ ParamsConfigWidget::ParamsConfigWidget(ydefx::JobParametersProxy& model, QWidget* parent) : QScrollArea(parent) @@ -196,3 +198,4 @@ void ParamsConfigWidget::updateExtraParams() _model.extra_params(_extraEdit->toPlainText().toStdString()); } +} diff --git a/src/gui/ParamsConfig.hxx b/src/gui/ParamsConfig.hxx index 22b7831..d461b3a 100644 --- a/src/gui/ParamsConfig.hxx +++ b/src/gui/ParamsConfig.hxx @@ -21,6 +21,8 @@ #include "JobParametersProxy.hxx" #include +namespace ydefx +{ class ParamsConfigWidget: public QScrollArea { Q_OBJECT @@ -48,5 +50,5 @@ private: ydefx::JobParametersProxy& _model; QTextEdit * _extraEdit; }; - +} #endif //IDEFX_ParamsConfigWidget_HXX diff --git a/src/gui/PathsConfig.cxx b/src/gui/PathsConfig.cxx index 951fc94..1ac57d8 100644 --- a/src/gui/PathsConfig.cxx +++ b/src/gui/PathsConfig.cxx @@ -18,6 +18,8 @@ // #include "PathsConfig.hxx" +namespace ydefx +{ PathsConfigWidget::PathsConfigWidget(ydefx::JobParametersProxy& model, QWidget* parent) : QScrollArea(parent) @@ -184,3 +186,4 @@ void PathsConfigWidget::removeInputFiles() delete item; } } +} diff --git a/src/gui/PathsConfig.hxx b/src/gui/PathsConfig.hxx index bcc9722..f27ab4e 100644 --- a/src/gui/PathsConfig.hxx +++ b/src/gui/PathsConfig.hxx @@ -21,6 +21,8 @@ #include "JobParametersProxy.hxx" #include +namespace ydefx +{ class PathsConfigWidget: public QScrollArea { Q_OBJECT @@ -46,5 +48,5 @@ private: QListWidget * _inputFilesList; QPushButton *_removeInputFilesButton; }; - +} #endif //IDEFX_PathsConfigWidget_HXX diff --git a/src/gui/QuickConfig.cxx b/src/gui/QuickConfig.cxx index 40900b9..0282afb 100644 --- a/src/gui/QuickConfig.cxx +++ b/src/gui/QuickConfig.cxx @@ -18,6 +18,8 @@ // #include "QuickConfig.hxx" +namespace ydefx +{ QuickConfigWidget::QuickConfigWidget(ydefx::JobParametersProxy& model, QWidget* parent) : QScrollArea(parent) @@ -77,3 +79,4 @@ void QuickConfigWidget::updateNbBranches(int value) _model.nb_branches(value); emit defaultNbBranches(value); } +} diff --git a/src/gui/QuickConfig.hxx b/src/gui/QuickConfig.hxx index 8f5398b..29bfb21 100644 --- a/src/gui/QuickConfig.hxx +++ b/src/gui/QuickConfig.hxx @@ -21,6 +21,8 @@ #include "JobParametersProxy.hxx" #include +namespace ydefx +{ class QuickConfigWidget: public QScrollArea { Q_OBJECT @@ -39,5 +41,5 @@ signals: private: ydefx::JobParametersProxy& _model; }; - +} #endif //IDEFX_QuickConfigWidget_HXX diff --git a/src/gui/ResourceWidget.cxx b/src/gui/ResourceWidget.cxx index 967a833..2e5c5a1 100644 --- a/src/gui/ResourceWidget.cxx +++ b/src/gui/ResourceWidget.cxx @@ -22,6 +22,8 @@ #include "PathsConfig.hxx" #include "ParamsConfig.hxx" +namespace ydefx +{ ResourceWidget::ResourceWidget(ydefx::JobParametersProxy& model, QWidget* parent) : QTabWidget(parent) @@ -44,3 +46,4 @@ ResourceWidget::ResourceWidget(ydefx::JobParametersProxy& model, ResourceWidget::~ResourceWidget() { } +} diff --git a/src/gui/ResourceWidget.hxx b/src/gui/ResourceWidget.hxx index a89f908..cf8db53 100644 --- a/src/gui/ResourceWidget.hxx +++ b/src/gui/ResourceWidget.hxx @@ -20,6 +20,8 @@ #define IDEFX_RESOURCEWIDGET_HXX #include "JobParametersProxy.hxx" #include +namespace ydefx +{ class ResourceWidget: public QTabWidget { @@ -30,5 +32,5 @@ public: private: ydefx::JobParametersProxy& _model; }; - +} #endif //IDEFX_RESOURCEWIDGET_HXX diff --git a/src/gui/ydefxgui.cxx b/src/gui/ydefxgui.cxx index 96e4988..9f05f1c 100644 --- a/src/gui/ydefxgui.cxx +++ b/src/gui/ydefxgui.cxx @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) { ydefx::JobParametersProxy jpp; jpp.createResultDirectory("/tmp"); - ResourceWidget mygui(jpp); + ydefx::ResourceWidget mygui(jpp); mygui.show(); ret = app.exec(); std::cout << "job_name:" << jpp.job_name() << std::endl; diff --git a/src/pydefx/configuration.py b/src/pydefx/configuration.py index 53ba752..570cc8b 100644 --- a/src/pydefx/configuration.py +++ b/src/pydefx/configuration.py @@ -46,7 +46,7 @@ def newResultDirectory(basedir=None): def defaultWckey(resource="localhost"): result = "" if resource != "localhost": - result = "P11U5:CARBONES" + result = "P11N0:SALOME" return result def availableResources(): diff --git a/src/pydefx/parameters.py b/src/pydefx/parameters.py index 4167973..52a1ade 100644 --- a/src/pydefx/parameters.py +++ b/src/pydefx/parameters.py @@ -19,6 +19,7 @@ # import salome from . import configuration +import tempfile class Parameters: def __init__(self, resource="localhost", @@ -52,4 +53,9 @@ class Parameters: self.salome_parameters.wckey = configuration.defaultWckey(resource) def createResultDirectory(self, result_base_dir): - self.salome_parameters.result_directory = configuration.newResultDirectory(result_base_dir) + self.salome_parameters.result_directory = configuration.newResultDirectory( + result_base_dir) + + def createTmpResultDirectory(self): + self.salome_parameters.result_directory = configuration.newResultDirectory( + tempfile.gettempdir()) -- 2.39.2