1 // Copyright (C) 2019 EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author: Anthony Geay, anthony.geay@edf.fr, EDF R&D
21 #include "AdaoExchangeLayer.hxx"
22 #include "AdaoExchangeLayerException.hxx"
23 #include "AdaoModelKeyVal.hxx"
24 #include "PyObjectRAII.hxx"
27 #include "py2cpp/py2cpp.hxx"
29 #include <semaphore.h>
38 struct DataExchangedBetweenThreads // data written by subthread and read by calling thread
41 DataExchangedBetweenThreads();
42 ~DataExchangedBetweenThreads();
45 sem_t _sem_result_is_here;
46 volatile bool _finished = false;
47 volatile PyObject *_data = nullptr;
50 /////////////////////////////////////////////
55 DataExchangedBetweenThreads *_data;
58 static PyObject *adaocallback_call(AdaoCallbackSt *self, PyObject *args, PyObject *kw)
60 if(!PyTuple_Check(args))
61 throw AdaoExchangeLayerException("Input args is not a tuple as expected !");
62 if(PyTuple_Size(args)!=1)
63 throw AdaoExchangeLayerException("Input args is not a tuple of size 1 as expected !");
64 PyObjectRAII zeobj(PyObjectRAII::FromBorrowed(PyTuple_GetItem(args,0)));
66 throw AdaoExchangeLayerException("Retrieve of elt #0 of input tuple has failed !");
67 volatile PyObject *ret(nullptr);
68 PyThreadState *tstate(PyEval_SaveThread());// GIL is acquired (see ExecuteAsync). Before entering into non python section. Release lock
70 self->_data->_finished = false;
71 self->_data->_data = zeobj;
72 sem_post(&self->_data->_sem);
73 sem_wait(&self->_data->_sem_result_is_here);
74 ret = self->_data->_data;
76 PyEval_RestoreThread(tstate);//End of parallel section. Reaquire the GIL and restore the thread state
77 return (PyObject *)ret;
80 static int adaocallback___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
82 static PyObject *adaocallback___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
84 return (PyObject *)( type->tp_alloc(type, 0) );
87 static void adaocallback_dealloc(PyObject *self)
89 Py_TYPE(self)->tp_free(self);
92 PyTypeObject AdaoCallbackType = {
93 PyVarObject_HEAD_INIT(&PyType_Type, 0)
95 sizeof(AdaoCallbackSt),
97 adaocallback_dealloc, /*tp_dealloc*/
104 0, /*tp_as_sequence*/
107 (ternaryfunc)adaocallback_call, /*tp_call*/
112 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
116 0, /*tp_richcompare*/
117 0, /*tp_weaklistoffset*/
128 adaocallback___init__, /*tp_init*/
129 PyType_GenericAlloc, /*tp_alloc*/
130 adaocallback___new__, /*tp_new*/
131 PyObject_GC_Del, /*tp_free*/
134 /////////////////////////////////////////////
136 DataExchangedBetweenThreads::DataExchangedBetweenThreads()
138 if(sem_init(&_sem,0,0)!=0)// put value to 0 to lock by default
139 throw AdaoExchangeLayerException("Internal constructor : Error on initialization of semaphore !");
140 if(sem_init(&_sem_result_is_here,0,0)!=0)// put value to 0 to lock by default
141 throw AdaoExchangeLayerException("Internal constructor : Error on initialization of semaphore !");
144 DataExchangedBetweenThreads::~DataExchangedBetweenThreads()
147 sem_destroy(&_sem_result_is_here);
150 class AdaoCallbackKeeper
153 void assign(AdaoCallbackSt *pt, DataExchangedBetweenThreads *data)
159 PyObject *getPyObject() const { return reinterpret_cast<PyObject*>(_pt); }
160 ~AdaoCallbackKeeper() { release(); }
162 void release() { if(_pt) { Py_XDECREF(_pt); } }
164 AdaoCallbackSt *_pt = nullptr;
167 class AdaoExchangeLayer::Internal
170 Internal():_context(PyObjectRAII::FromNew(PyDict_New()))
173 PyObject *mainmod(PyImport_AddModule("__main__"));
174 PyObject *globals(PyModule_GetDict(mainmod));
175 PyObject *bltins(PyEval_GetBuiltins());
176 PyDict_SetItemString(_context,"__builtins__",bltins);
179 PyObjectRAII _context;
180 PyObjectRAII _generate_case_func;
181 PyObjectRAII _decorator_func;
182 PyObjectRAII _adao_case;
183 PyObjectRAII _execute_func;
184 AdaoCallbackKeeper _py_call_back;
185 std::future< void > _fut;
186 PyThreadState *_tstate = nullptr;
187 DataExchangedBetweenThreads _data_btw_threads;
190 wchar_t **ConvertToWChar(int argc, const char *argv[])
192 wchar_t **ret(new wchar_t*[argc]);
193 for(int i=0;i<argc;++i)
195 std::size_t len(strlen(argv[i])+1);
196 wchar_t *elt(new wchar_t[len]);
198 std::mbstowcs(elt, argv[i], len);
203 void FreeWChar(int argc, wchar_t **tab)
205 for(int i=0;i<argc;++i)
210 AdaoExchangeLayer::AdaoExchangeLayer()
214 AdaoExchangeLayer::~AdaoExchangeLayer()
220 void AdaoExchangeLayer::init()
222 initPythonIfNeeded();
225 PyObject *AdaoExchangeLayer::getPythonContext() const
228 throw AdaoExchangeLayerException("getPythonContext : not initialized !");
229 return _internal->_context;
232 std::string AdaoExchangeLayer::printContext() const
235 PyObject *obj(this->getPythonContext());
236 if(!PyDict_Check(obj))
237 throw AdaoExchangeLayerException("printContext : not a dict !");
238 PyObject *key(nullptr), *value(nullptr);
240 std::ostringstream oss;
241 while( PyDict_Next(obj, &pos, &key, &value) )
243 if(!PyUnicode_Check(key))
244 throw AdaoExchangeLayerException("printContext : not a string as key !");
245 oss << PyUnicode_AsUTF8(key) << " = ";
246 PyObjectRAII reprOfValue(PyObjectRAII::FromNew(PyObject_Repr(value)));
247 oss << PyUnicode_AsUTF8(reprOfValue);
254 * AdaoExchangeLayer is based on multithreaded paradigm.
255 * Master thread (thread calling this method) and slave thread (thread calling ADAO algo)
256 * are calling both python interpretor. Consequence all python calls have to be surrounded with AGIL.
258 * User consequence : To avoid deadlocks this method release GIL. The downstream python calls must be with AGIL.
260 * This method initialize python interpretor if not already the case.
261 * At the end of this method the lock is released to be ready to perform RAII on GIL
264 void AdaoExchangeLayer::initPythonIfNeeded()
266 if (!Py_IsInitialized())
268 const char *TAB[]={"AdaoExchangeLayer"};
269 wchar_t **TABW(ConvertToWChar(1,TAB));
270 // Python is not initialized
271 Py_SetProgramName(const_cast<wchar_t *>(TABW[0]));
272 Py_Initialize(); // Initialize the interpreter
273 PySys_SetArgv(1,TABW);
275 PyEval_InitThreads();
277 _internal = new Internal;
278 _internal->_tstate=PyEval_SaveThread(); // release the lock acquired in AdaoExchangeLayer::initPythonIfNeeded by PyEval_InitThreads()
283 _internal = new Internal;
284 if( PyGILState_Check() )// is the GIL already acquired (typically by a PyEval_InitThreads) ?
285 _internal->_tstate=PyEval_SaveThread(); // release the lock acquired upstream
289 class Visitor1 : public AdaoModel::PythonLeafVisitor
292 Visitor1(PyObjectRAII func, PyObject *context):_func(func),_context(context)
296 void visit(AdaoModel::MainModel *godFather, AdaoModel::PyObjKeyVal *obj) override
298 if(obj->getKey()=="Matrix" || obj->getKey()=="DiagonalSparseMatrix")
300 std::ostringstream oss; oss << "__" << _cnt++;
301 std::string varname(oss.str());
302 obj->setVal(Py_None);
303 PyDict_SetItemString(_context,varname.c_str(),Py_None);
304 obj->setVarName(varname);
307 if(obj->getKey()=="OneFunction")
309 std::ostringstream oss; oss << "__" << _cnt++;
310 std::string varname(oss.str());
312 PyDict_SetItemString(_context,varname.c_str(),_func);
313 obj->setVarName(varname);
318 unsigned int _cnt = 0;
320 PyObject *_context = nullptr;
323 void AdaoExchangeLayer::setFunctionCallbackInModel(AdaoModel::MainModel *model)
326 const char DECORATOR_FUNC[]="def DecoratorAdao(cppFunc):\n"
327 " def evaluator( xserie ):\n"
328 " import numpy as np\n"
329 " yserie = [np.array(elt) for elt in cppFunc(xserie)]\n"
331 " return evaluator\n";
332 this->_internal->_py_call_back.assign(PyObject_GC_New(AdaoCallbackSt,&AdaoCallbackType),
333 &this->_internal->_data_btw_threads);
334 PyObject *callbackPyObj(this->_internal->_py_call_back.getPyObject());
337 PyObjectRAII res(PyObjectRAII::FromNew(PyRun_String(DECORATOR_FUNC,Py_file_input,this->_internal->_context,this->_internal->_context)));
338 PyObjectRAII decoratorGenerator( PyObjectRAII::FromBorrowed(PyDict_GetItemString(this->_internal->_context,"DecoratorAdao")) );
339 if(decoratorGenerator.isNull())
340 throw AdaoExchangeLayerException("Fail to locate DecoratorAdao function !");
341 PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1)));
342 { PyTuple_SetItem(args,0,callbackPyObj); Py_XINCREF(callbackPyObj); }
343 this->_internal->_decorator_func = PyObjectRAII::FromNew(PyObject_CallObject(decoratorGenerator,args));
344 if(this->_internal->_decorator_func.isNull())
345 throw AdaoExchangeLayerException("Fail to generate result of DecoratorAdao function !");
348 Visitor1 visitor(this->_internal->_decorator_func,this->_internal->_context);
349 model->visitPythonLeaves(&visitor);
352 void AdaoExchangeLayer::loadTemplate(AdaoModel::MainModel *model)
356 std::string sciptPyOfModelMaker(model->pyStr());
357 PyObjectRAII res(PyObjectRAII::FromNew(PyRun_String(sciptPyOfModelMaker.c_str(),Py_file_input,this->_internal->_context,this->_internal->_context)));
359 _internal->_adao_case = PyObjectRAII::FromNew(PyDict_GetItemString(this->_internal->_context,"case"));
361 if(_internal->_adao_case.isNull())
362 throw AdaoExchangeLayerException("Fail to generate ADAO case object !");
364 _internal->_execute_func=PyObjectRAII::FromNew(PyObject_GetAttrString(_internal->_adao_case,"execute"));
365 if(_internal->_execute_func.isNull())
366 throw AdaoExchangeLayerException("Fail to locate execute function of ADAO case object !");
369 void ExecuteAsync(PyObject *pyExecuteFunction, DataExchangedBetweenThreads *data)
372 AutoGIL gil; // launched in a separed thread -> protect python calls
373 PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(0)));
374 PyObjectRAII nullRes(PyObjectRAII::FromNew(PyObject_CallObject(pyExecuteFunction,args)));// go to adaocallback_call
377 data->_finished = true;
378 data->_data = nullptr;
379 sem_post(&data->_sem);
382 void AdaoExchangeLayer::execute()
384 _internal->_fut = std::async(std::launch::async,ExecuteAsync,_internal->_execute_func,&_internal->_data_btw_threads);
387 bool AdaoExchangeLayer::next(PyObject *& inputRequested)
389 sem_wait(&_internal->_data_btw_threads._sem);
390 if(_internal->_data_btw_threads._finished)
392 inputRequested = nullptr;
397 inputRequested = (PyObject *)_internal->_data_btw_threads._data;
402 void AdaoExchangeLayer::setResult(PyObject *outputAssociated)
404 _internal->_data_btw_threads._data = outputAssociated;
405 _internal->_data_btw_threads._finished = false;
406 sem_post(&_internal->_data_btw_threads._sem_result_is_here);
409 PyObject *AdaoExchangeLayer::getResult()
411 _internal->_fut.wait();
412 if(_internal->_tstate)
413 PyEval_RestoreThread(_internal->_tstate);
415 // now retrieve case.get("Analysis")[-1]
416 PyObjectRAII get_func_of_adao_case(PyObjectRAII::FromNew(PyObject_GetAttrString(_internal->_adao_case,"get")));
417 if(get_func_of_adao_case.isNull())
418 throw AdaoExchangeLayerException("Fail to locate \"get\" method from ADAO case !");
419 PyObjectRAII all_intermediate_results;
420 {// retrieve return data from case.get("Analysis")
421 PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1)));
422 PyTuple_SetItem(args,0,PyUnicode_FromString("Analysis"));
423 all_intermediate_results=PyObjectRAII::FromNew(PyObject_CallObject(get_func_of_adao_case,args));
424 if(all_intermediate_results.isNull())
425 throw AdaoExchangeLayerException("Fail to retrieve result of case.get(\"Analysis\") !");
427 PyObjectRAII optimum;
429 PyObjectRAII param(PyObjectRAII::FromNew(PyLong_FromLong(-1)));
430 optimum=PyObjectRAII::FromNew(PyObject_GetItem(all_intermediate_results,param));
432 throw AdaoExchangeLayerException("Fail to retrieve result of last element of case.get(\"Analysis\") !");
434 /*PyObjectRAII code(PyObjectRAII::FromNew(Py_CompileString("case.get(\"Analysis\")[-1]","retrieve result",Py_file_input)));
436 throw AdaoExchangeLayerException("Fail to compile code to retrieve result after ADAO computation !");
437 PyObjectRAII res(PyObjectRAII::FromNew(PyEval_EvalCode(code,_internal->_context,_internal->_context)));*/
438 return optimum.retn();