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; MeshingParameters mp;
if(pars) mp = *pars; if(pars) mp = *pars;
{ CreateMPfromKwargs(mp, kwargs);
py::gil_scoped_acquire aq; py::gil_scoped_release gil_rel;
CreateMPfromKwargs(mp, kwargs);
}
auto mesh = make_shared<Mesh>(); auto mesh = make_shared<Mesh>();
SetGlobalMesh (mesh); SetGlobalMesh (mesh);
mesh->SetGeometry(geo); 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!"); throw Exception("Meshing failed!");
return mesh; return mesh;
}, py::arg("mp") = nullptr, }, py::arg("mp") = nullptr,
meshingparameter_description.c_str(), meshingparameter_description.c_str())
py::call_guard<py::gil_scoped_release>())
; ;
m.def("Save", FunctionPointer m.def("Save", FunctionPointer

View File

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

View File

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