Fix GIL issues in GenerateMesh() functions

Functions with a python typed argument (kwargs in this case) cannot use
  py::call_guard<py::gil_scoped_release>()
because it means, the GIL is not held when the function returns (and
cleans up arguments/temporary variables).

Thus, remove the global call guard and create a local variable
  py::gil_scoped_release gil_release;
after arguments are processed and before meshing starts.

This local variable is destroyed before the function returns
(acquiring the GIL again).
This commit is contained in:
Matthias Hochsteger 2024-06-21 11:33:21 +02:00
parent c2f42f2f16
commit 163135981e
3 changed files with 16 additions and 27 deletions

View File

@ -756,10 +756,8 @@ However, when r = 0, the top part becomes a point(tip) and meshing fails!
{
MeshingParameters mp;
if(pars) mp = *pars;
{
py::gil_scoped_acquire aq;
CreateMPfromKwargs(mp, kwargs);
}
py::gil_scoped_release gil_rel;
auto mesh = make_shared<Mesh>();
SetGlobalMesh (mesh);
mesh->SetGeometry(geo);
@ -770,8 +768,7 @@ However, when r = 0, the top part becomes a point(tip) and meshing fails!
throw Exception("Meshing failed!");
return mesh;
}, py::arg("mp") = nullptr,
meshingparameter_description.c_str(),
py::call_guard<py::gil_scoped_release>())
meshingparameter_description.c_str())
;
m.def("Save", FunctionPointer

View File

@ -401,10 +401,8 @@ NGCORE_API_EXPORT void ExportGeom2d(py::module &m)
{
MeshingParameters mp;
if(pars) mp = *pars;
{
py::gil_scoped_acquire aq;
CreateMPfromKwargs(mp, kwargs);
}
py::gil_scoped_release gil_release;
auto mesh = make_shared<Mesh>();
mesh->SetGeometry(self);
SetGlobalMesh (mesh);
@ -414,7 +412,6 @@ NGCORE_API_EXPORT void ExportGeom2d(py::module &m)
throw Exception("Meshing failed!");
return mesh;
}, py::arg("mp") = nullopt,
py::call_guard<py::gil_scoped_release>(),
meshingparameter_description.c_str())
.def("_SetDomainTensorMeshing", &SplineGeometry2d::SetDomainTensorMeshing)
;
@ -466,10 +463,8 @@ NGCORE_API_EXPORT void ExportGeom2d(py::module &m)
{
MeshingParameters mp;
if(pars) mp = *pars;
{
py::gil_scoped_acquire aq;
CreateMPfromKwargs(mp, kwargs);
}
py::gil_scoped_release gil_release;
auto mesh = make_shared<Mesh>();
auto geo = self.GenerateSplineGeometry();
mesh->SetGeometry(geo);
@ -480,7 +475,6 @@ NGCORE_API_EXPORT void ExportGeom2d(py::module &m)
throw Exception("Meshing failed!");
return mesh;
}, py::arg("mp") = nullopt,
py::call_guard<py::gil_scoped_release>(),
meshingparameter_description.c_str())
;

View File

@ -243,8 +243,6 @@ DLL_HEADER void ExportNgOCC(py::module &m)
{
MeshingParameters mp;
OCCParameters occparam;
{
py::gil_scoped_acquire aq;
if(pars)
{
auto mp_kwargs = CreateDictFromFlags(pars->geometrySpecificParameters);
@ -253,7 +251,7 @@ DLL_HEADER void ExportNgOCC(py::module &m)
}
CreateOCCParametersFromKwargs(occparam, kwargs);
CreateMPfromKwargs(mp, kwargs);
}
py::gil_scoped_release gil_release;
geo->SetOCCParameters(occparam);
if(!mesh)
mesh = make_shared<Mesh>();
@ -279,7 +277,7 @@ DLL_HEADER void ExportNgOCC(py::module &m)
}
return mesh;
}, py::arg("mp") = nullptr, py::arg("comm")=NgMPI_Comm{},
py::arg("mesh")=nullptr, py::call_guard<py::gil_scoped_release>(),
py::arg("mesh")=nullptr,
(meshingparameter_description + occparameter_description).c_str())
.def_property_readonly("shape", [](const OCCGeometry & self) { return self.GetShape(); })
;