From 51a7f368eee9cf6c3d0100bc37b5fbb2ef142930 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 12 Jul 2019 16:14:09 +0200 Subject: [PATCH] Ready to use --- AdaoExchangeLayer4Quintet.cxx | 5 -- TestAdaoExchange.cxx | 121 ++++++++++++++++++---------------- TestAdaoHelper.cxx | 55 +--------------- 3 files changed, 68 insertions(+), 113 deletions(-) diff --git a/AdaoExchangeLayer4Quintet.cxx b/AdaoExchangeLayer4Quintet.cxx index ea267bc..4b82af4 100644 --- a/AdaoExchangeLayer4Quintet.cxx +++ b/AdaoExchangeLayer4Quintet.cxx @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -308,10 +307,6 @@ void AdaoExchangeLayer4Quintet::loadTemplate(AdaoModel::MainModel *model) // { std::string sciptPyOfModelMaker(model->pyStr()); - { - std::ofstream ofs("/tmp/H87074/jj"); - ofs << sciptPyOfModelMaker; - } PyObjectRAII res(PyObjectRAII::FromNew(PyRun_String(sciptPyOfModelMaker.c_str(),Py_file_input,this->_internal->_context,this->_internal->_context))); PyErr_Print(); _internal->_adao_case = PyObjectRAII::FromNew(PyDict_GetItemString(this->_internal->_context,"case")); diff --git a/TestAdaoExchange.cxx b/TestAdaoExchange.cxx index a690fe6..803cbc1 100644 --- a/TestAdaoExchange.cxx +++ b/TestAdaoExchange.cxx @@ -32,58 +32,66 @@ #include "TestAdaoHelper.cxx" -PyObject *multiFunc(PyObject *inp) +// Functor a remplacer par un appel a un evaluateur parallele +class NonParallelFunctor { - PyGILState_STATE gstate(PyGILState_Ensure()); - PyObjectRAII iterator(PyObjectRAII::FromNew(PyObject_GetIter(inp))); - if(iterator.isNull()) - throw AdaoExchangeLayerException("Input object is not iterable !"); - PyObject *item(nullptr); - PyObjectRAII numpyModule(PyObjectRAII::FromNew(PyImport_ImportModule("numpy"))); - if(numpyModule.isNull()) - throw AdaoExchangeLayerException("Failed to load numpy"); - PyObjectRAII ravelFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(numpyModule,"ravel"))); - std::vector< PyObjectRAII > pyrets; - while( item = PyIter_Next(iterator) ) - { - PyObjectRAII item2(PyObjectRAII::FromNew(item)); +public: + NonParallelFunctor(std::function< std::vector(const std::vector&) > cppFunction):_cpp_function(cppFunction) { } + PyObject *operator()(PyObject *inp) const + { + PyGILState_STATE gstate(PyGILState_Ensure()); + PyObjectRAII iterator(PyObjectRAII::FromNew(PyObject_GetIter(inp))); + if(iterator.isNull()) + throw AdaoExchangeLayerException("Input object is not iterable !"); + PyObject *item(nullptr); + PyObjectRAII numpyModule(PyObjectRAII::FromNew(PyImport_ImportModule("numpy"))); + if(numpyModule.isNull()) + throw AdaoExchangeLayerException("Failed to load numpy"); + PyObjectRAII ravelFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(numpyModule,"ravel"))); + std::vector< PyObjectRAII > pyrets; + while( item = PyIter_Next(iterator) ) { - PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1))); - PyTuple_SetItem(args,0,item2.retn()); - PyObjectRAII npyArray(PyObjectRAII::FromNew(PyObject_CallObject(ravelFunc,args))); - // Waiting management of npy arrays into py2cpp - PyObjectRAII lolistFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(npyArray,"tolist"))); - PyObjectRAII listPy; + PyObjectRAII item2(PyObjectRAII::FromNew(item)); { - PyObjectRAII args2(PyObjectRAII::FromNew(PyTuple_New(0))); - listPy=PyObjectRAII::FromNew(PyObject_CallObject(lolistFunc,args2)); + PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1))); + PyTuple_SetItem(args,0,item2.retn()); + PyObjectRAII npyArray(PyObjectRAII::FromNew(PyObject_CallObject(ravelFunc,args))); + // Waiting management of npy arrays into py2cpp + PyObjectRAII lolistFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(npyArray,"tolist"))); + PyObjectRAII listPy; + { + PyObjectRAII args2(PyObjectRAII::FromNew(PyTuple_New(0))); + listPy=PyObjectRAII::FromNew(PyObject_CallObject(lolistFunc,args2)); } - std::vector vect; - { - py2cpp::PyPtr listPy2(listPy.retn()); - py2cpp::fromPyPtr(listPy2,vect); + std::vector vect; + { + py2cpp::PyPtr listPy2(listPy.retn()); + py2cpp::fromPyPtr(listPy2,vect); + } + // + PyGILState_Release(gstate); + std::vector res(_cpp_function(vect));// L'appel effectif est ici + gstate=PyGILState_Ensure(); + // + py2cpp::PyPtr resPy(py2cpp::toPyPtr(res)); + PyObjectRAII resPy2(PyObjectRAII::FromBorrowed(resPy.get())); + pyrets.push_back(resPy2); } - // - PyGILState_Release(gstate); - std::vector res(funcBase(vect)); - gstate=PyGILState_Ensure(); - // - py2cpp::PyPtr resPy(py2cpp::toPyPtr(res)); - PyObjectRAII resPy2(PyObjectRAII::FromBorrowed(resPy.get())); - pyrets.push_back(resPy2); } - } - std::size_t len(pyrets.size()); - PyObjectRAII ret(PyObjectRAII::FromNew(PyList_New(len))); - for(std::size_t i=0;i(const std::vector&) > _cpp_function; +}; PyObjectRAII NumpyToListWaitingForPy2CppManagement(PyObject *npObj) { @@ -111,6 +119,7 @@ using namespace AdaoModel; void AdaoExchangeTest::test3DVar() { + NonParallelFunctor functor(funcBase); MainModel mm; AdaoExchangeLayer4Quintet adao; adao.init(); @@ -122,13 +131,13 @@ void AdaoExchangeTest::test3DVar() // { std::string sciptPyOfModelMaker(mm.pyStr()); - std::cerr << sciptPyOfModelMaker << std::endl; + //std::cerr << sciptPyOfModelMaker << std::endl; } adao.execute(); PyObject *listOfElts( nullptr ); while( adao.next(listOfElts) ) { - PyObject *resultOfChunk(multiFunc(listOfElts)); + PyObject *resultOfChunk(functor(listOfElts)); adao.setResult(resultOfChunk); } PyObject *res(adao.getResult()); @@ -160,6 +169,7 @@ void AdaoExchangeTest::testBlue() void exitSubDir(DictKeyVal *subdir) { } }; + NonParallelFunctor functor(funcBase); MainModel mm; // TestBlueVisitor vis; @@ -175,13 +185,13 @@ void AdaoExchangeTest::testBlue() // { std::string sciptPyOfModelMaker(mm.pyStr()); - std::cerr << sciptPyOfModelMaker << std::endl; + //std::cerr << sciptPyOfModelMaker << std::endl; } adao.execute(); PyObject *listOfElts( nullptr ); while( adao.next(listOfElts) ) { - PyObject *resultOfChunk(multiFunc(listOfElts)); + PyObject *resultOfChunk(functor(listOfElts)); adao.setResult(resultOfChunk); } PyObject *res(adao.getResult()); @@ -212,7 +222,7 @@ void AdaoExchangeTest::testNonLinearLeastSquares() void enterSubDir(DictKeyVal *subdir) { } void exitSubDir(DictKeyVal *subdir) { } }; - + NonParallelFunctor functor(funcBase); MainModel mm; // TestNonLinearLeastSquaresVisitor vis; @@ -228,13 +238,13 @@ void AdaoExchangeTest::testNonLinearLeastSquares() // { std::string sciptPyOfModelMaker(mm.pyStr()); - std::cerr << sciptPyOfModelMaker << std::endl; + //std::cerr << sciptPyOfModelMaker << std::endl; } adao.execute(); PyObject *listOfElts( nullptr ); while( adao.next(listOfElts) ) { - PyObject *resultOfChunk(multiFunc(listOfElts)); + PyObject *resultOfChunk(functor(listOfElts)); adao.setResult(resultOfChunk); } PyObject *res(adao.getResult()); @@ -253,6 +263,7 @@ void AdaoExchangeTest::testNonLinearLeastSquares() void AdaoExchangeTest::testCasCrue() { + NonParallelFunctor functor(funcCrue); MainModel mm; AdaoExchangeLayer4Quintet adao; adao.init(); @@ -264,13 +275,13 @@ void AdaoExchangeTest::testCasCrue() // { std::string sciptPyOfModelMaker(mm.pyStr()); - std::cerr << sciptPyOfModelMaker << std::endl; + //std::cerr << sciptPyOfModelMaker << std::endl; } adao.execute(); PyObject *listOfElts( nullptr ); while( adao.next(listOfElts) ) { - PyObject *resultOfChunk(multiFuncCrue(listOfElts)); + PyObject *resultOfChunk(functor(listOfElts)); adao.setResult(resultOfChunk); } PyObject *res(adao.getResult()); diff --git a/TestAdaoHelper.cxx b/TestAdaoHelper.cxx index 09d8929..553be51 100644 --- a/TestAdaoHelper.cxx +++ b/TestAdaoHelper.cxx @@ -34,59 +34,7 @@ std::vector funcCrue(const std::vector& vec) return ret; } -PyObject *multiFuncCrue(PyObject *inp) -{ - PyGILState_STATE gstate(PyGILState_Ensure()); - PyObjectRAII iterator(PyObjectRAII::FromNew(PyObject_GetIter(inp))); - if(iterator.isNull()) - throw AdaoExchangeLayerException("Input object is not iterable !"); - PyObject *item(nullptr); - PyObjectRAII numpyModule(PyObjectRAII::FromNew(PyImport_ImportModule("numpy"))); - if(numpyModule.isNull()) - throw AdaoExchangeLayerException("Failed to load numpy"); - PyObjectRAII ravelFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(numpyModule,"ravel"))); - std::vector< PyObjectRAII > pyrets; - while( item = PyIter_Next(iterator) ) - { - PyObjectRAII item2(PyObjectRAII::FromNew(item)); - { - PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1))); - PyTuple_SetItem(args,0,item2.retn()); - PyObjectRAII npyArray(PyObjectRAII::FromNew(PyObject_CallObject(ravelFunc,args))); - // Waiting management of npy arrays into py2cpp - PyObjectRAII lolistFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(npyArray,"tolist"))); - PyObjectRAII listPy; - { - PyObjectRAII args2(PyObjectRAII::FromNew(PyTuple_New(0))); - listPy=PyObjectRAII::FromNew(PyObject_CallObject(lolistFunc,args2)); - } - std::vector vect; - { - py2cpp::PyPtr listPy2(listPy.retn()); - py2cpp::fromPyPtr(listPy2,vect); - } - // - PyGILState_Release(gstate); - std::vector res(funcCrue(vect)); - gstate=PyGILState_Ensure(); - // - py2cpp::PyPtr resPy(py2cpp::toPyPtr(res)); - PyObjectRAII resPy2(PyObjectRAII::FromBorrowed(resPy.get())); - pyrets.push_back(resPy2); - } - } - std::size_t len(pyrets.size()); - PyObjectRAII ret(PyObjectRAII::FromNew(PyList_New(len))); - for(std::size_t i=0;i