From 36a711320cc76921ea5ce303c6da41e4bf203b02 Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Fri, 1 Nov 2024 11:07:02 +0100 Subject: [PATCH] Release GIL in optimize and compute_marginals Relates to #839 --- python/core/py_sparse_optimizer.cpp | 50 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/python/core/py_sparse_optimizer.cpp b/python/core/py_sparse_optimizer.cpp index 10849ca8a..c2b611fca 100644 --- a/python/core/py_sparse_optimizer.cpp +++ b/python/core/py_sparse_optimizer.cpp @@ -44,28 +44,34 @@ void declareSparseOptimizer(py::module& m) { &CLS::computeInitialGuess), "propagator"_a) // virtual - .def("optimize", &CLS::optimize, "iterations"_a, - "online"_a = false) // -> int - .def("compute_marginals", - [](CLS& optimizer, - const std::vector >& block_indices) { - auto* spinv = new SparseBlockMatrix(); - bool result = optimizer.computeMarginals(*spinv, block_indices); - return std::make_pair(spinv, result); - }) - .def("compute_marginals", - [](CLS& optimizer, const OptimizableGraph::Vertex* vertex) { - auto* spinv = new SparseBlockMatrix(); - bool result = optimizer.computeMarginals(*spinv, vertex); - return std::make_pair(spinv, result); - }) - .def("compute_marginals", - [](CLS& optimizer, - const OptimizableGraph::VertexContainer& vertices) { - auto* spinv = new SparseBlockMatrix(); - bool result = optimizer.computeMarginals(*spinv, vertices); - return std::make_pair(spinv, result); - }) + .def("optimize", &CLS::optimize, "iterations"_a, "online"_a = false, + py::call_guard()) // -> int + .def( + "compute_marginals", + [](CLS& optimizer, + const std::vector >& block_indices) { + auto* spinv = new SparseBlockMatrix(); + bool result = optimizer.computeMarginals(*spinv, block_indices); + return std::make_pair(spinv, result); + }, + py::call_guard()) + .def( + "compute_marginals", + [](CLS& optimizer, const OptimizableGraph::Vertex* vertex) { + auto* spinv = new SparseBlockMatrix(); + bool result = optimizer.computeMarginals(*spinv, vertex); + return std::make_pair(spinv, result); + }, + py::call_guard()) + .def( + "compute_marginals", + [](CLS& optimizer, + const OptimizableGraph::VertexContainer& vertices) { + auto* spinv = new SparseBlockMatrix(); + bool result = optimizer.computeMarginals(*spinv, vertices); + return std::make_pair(spinv, result); + }, + py::call_guard()) // The gauge should be fixed() and then the optimization can work (if no // additional dof are in the system. The default implementation returns a