Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bindings] Remove static SofaInitializer, call unload into each respective module and clear cache #432

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ jobs:
echo "$WORKSPACE_ARTIFACT_PATH/lib" >> $GITHUB_PATH
echo "$WORKSPACE_ARTIFACT_PATH/bin" >> $GITHUB_PATH
elif [[ "$RUNNER_OS" == "macOS" ]]; then
echo "SOFA_PLUGIN_PATH=$WORKSPACE_ARTIFACT_PATH/lib" | tee -a $GITHUB_ENV
echo "DYLD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH" | tee -a $GITHUB_ENV
else # Linux
echo "SOFA_PLUGIN_PATH=$WORKSPACE_ARTIFACT_PATH/lib" | tee -a $GITHUB_ENV
echo "LD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH" | tee -a $GITHUB_ENV
fi
echo "LD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH" | tee -a $GITHUB_ENV
echo "PYTHONPATH=$WORKSPACE_ARTIFACT_PATH/lib/python3/site-packages" | tee -a $GITHUB_ENV
# Add execution right on the tests
chmod +x $WORKSPACE_ARTIFACT_PATH/bin/*.Tests${{ steps.sofa.outputs.exe }}
Expand Down
9 changes: 8 additions & 1 deletion Plugin/src/SofaPython3/DataHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,17 @@ py::slice toSlice(const py::object& o)

std::map<void*, std::pair<int, py::array>>& getObjectCache()
{
static std::map<void*, std::pair<int, py::array>> s_objectcache {} ;
static std::map<void*, std::pair<int, py::array>> s_objectcache{};
return s_objectcache;
}


void clearCache()
{
msg_info("SofaPython3") << "Clearing Sofa.Core cache...";
getObjectCache().clear();
}

void trimCache()
{
auto& memcache = getObjectCache();
Expand Down
1 change: 1 addition & 0 deletions Plugin/src/SofaPython3/DataHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ SOFAPYTHON3_API std::string getPathTo(Base* b);
SOFAPYTHON3_API const char* getFormat(const AbstractTypeInfo& nfo);

SOFAPYTHON3_API std::map<void*, std::pair<int, pybind11::array>>& getObjectCache();
SOFAPYTHON3_API void clearCache();
SOFAPYTHON3_API void trimCache();

SOFAPYTHON3_API bool hasArrayFor(BaseData* d);
Expand Down
11 changes: 10 additions & 1 deletion Plugin/src/SofaPython3/PythonEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ void PythonEnvironment::Release()
{
/// Finish the Python Interpreter
/// obviously can't use raii here
if( Py_IsInitialized() ) {
static bool isReleased = false;
if( Py_IsInitialized() && !isReleased) {
isReleased = true;

PyGILState_Ensure();
py::finalize_interpreter();
getStaticData()->reset();
Expand Down Expand Up @@ -480,6 +483,12 @@ void PythonEnvironment::addPluginManagerCallback()
}
}
);
PluginManager::getInstance().addOnPluginCleanupCallbacks(pluginLibraryPath,
[]()
{
PythonEnvironment::Release();
}
);
}

void PythonEnvironment::removePluginManagerCallback()
Expand Down
1 change: 1 addition & 0 deletions Plugin/src/SofaPython3/PythonEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>
#include <string>
#include <sofa/helper/logging/FileInfo.h>
#include <SofaPython3/DataHelper.h>

/// Fixes compile errors:
/// removing all slots macros is necessary if embedded in a Qt project
Expand Down
14 changes: 14 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ using sofa::helper::logging::Message;
#include <SofaPython3/Sofa/Core/Data/Binding_DataVectorString.h>
#include <SofaPython3/Sofa/Core/Data/Binding_DataContainer.h>

#include <sofa/core/init.h>

namespace sofapython3
{

Expand Down Expand Up @@ -130,6 +132,18 @@ PYBIND11_MODULE(Core, core)
moduleAddBaseMeshTopology(core);
moduleAddPointSetTopologyModifier(core);
moduleAddTaskScheduler(core);

// called when the module is unloaded
auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {

clearCache();

sofa::core::cleanup();

msg_info("SofaPython3.Core") << "Sofa.Core unload()";
}));

}

} ///namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Contact information: [email protected] *
******************************************************************************/

#include <sofa/core/init.h>
#include <sofa/helper/init.h>
#include <sofa/helper/logging/Messaging.h>
#include <SofaPython3/PythonEnvironment.h>
Expand Down Expand Up @@ -108,7 +107,6 @@ static void parse_emitter_message_then(py::args args, const Action& action) {
PYBIND11_MODULE(Helper, helper)
{
// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
sofa::core::init();
sofa::helper::init();

helper.doc() = R"doc(
Expand Down Expand Up @@ -155,6 +153,12 @@ PYBIND11_MODULE(Helper, helper)
moduleAddMessageHandler(helper);
moduleAddVector(helper);
moduleAddSystem(helper);

auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
sofa::helper::cleanup();
msg_info("SofaPython3.Helper") << "Sofa.Helper unload()";
}));
}

} ///namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using sofa::simulation::Simulation;

#include <sofa/core/init.h>
#include <sofa/simulation/init.h>
#include <sofa/simulation/common/init.h>
#include <sofa/simulation/graph/init.h>

namespace py = pybind11;
Expand Down Expand Up @@ -83,6 +84,17 @@ PYBIND11_MODULE(Simulation, simulation)
{
sofa::simulation::node::initTextures(n);
});

// called when the module is unloaded
auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {

sofa::simulation::core::cleanup();
sofa::simulation::common::cleanup();
sofa::simulation::graph::cleanup();

msg_info("SofaPython3.Simulation") << "Sofa.Simulation unload()";
}));
}

} /// namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,6 @@ using sofa::helper::logging::MainConsoleMessageHandler;
namespace sofapython3
{


class SofaInitializer
{
public:
// TODO, ces trucs sont fort laid. Normalement ce devrait être une joli plugin qui
// appelle le init.
SofaInitializer(){
sofa::simulation::common::init();
sofa::simulation::graph::init();
}

~SofaInitializer(){
sofa::simulation::common::cleanup();
sofa::simulation::graph::cleanup();
}
};

static std::vector<std::string> getCategories(const std::string& className)
{
std::vector<std::string> categories;
Expand All @@ -109,8 +92,6 @@ static std::vector<std::string> getCategories(const std::string& className)
return categories ;
}

static SofaInitializer s;

/// The first parameter must be named the same as the module file to load.
PYBIND11_MODULE(SofaRuntime, m) {

Expand All @@ -132,13 +113,6 @@ PYBIND11_MODULE(SofaRuntime, m) {

)doc";

// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
sofa::core::init();
sofa::helper::init();
sofa::simulation::core::init();
sofa::simulation::graph::init();
sofa::simulation::common::init();

// Add the plugin directory to PluginRepository
const std::string& pluginDir = Utils::getExecutableDirectory();
PluginRepository.addFirstPath(pluginDir);
Expand Down Expand Up @@ -198,6 +172,7 @@ PYBIND11_MODULE(SofaRuntime, m) {
m.def("getCategories", &getCategories);

addSubmoduleTimer(m);

}

} // namespace sofapython3
Loading