Skip to content
Snippets Groups Projects
Commit c53bb9b9 authored by Timo Koch's avatar Timo Koch
Browse files

[python] Make sure we use the serial multithreading backend

The current implementatino of the Pythons where C++ calls Python
code without making sure to have the GIL is not thread-safe.
parent ce5be865
No related branches found
No related tags found
1 merge request!2909Feature/multithreaded assembly using a coloring scheme
# pylint: skip-file
# until that decorator bit can be removed again
"""Classes and function related to the assembly of linear systems"""
from dune.generator.generator import SimpleGenerator
......@@ -5,6 +8,25 @@ from dune.common.hashit import hashIt
from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias
def decoratePre(pre):
def wrappedPre(*args, **kwargs):
preamble = pre(*args, **kwargs)
newPreamble = ""
for line in preamble.split("\n"):
newPreamble += line + "\n"
if line.startswith("#include <config.h>"):
newPreamble += "#undef DUMUX_MULTITHREADING_BACKEND\n"
newPreamble += "#define DUMUX_MULTITHREADING_BACKEND Serial\n"
return newPreamble
return wrappedPre
myAttributes = vars(SimpleGenerator).copy()
myAttributes["pre"] = decoratePre(myAttributes["pre"])
MySimpleGenerator = type("MySimpleGenerator", (object,), myAttributes)
@cppWrapperCreator
def _createFVAssembler(*, problem, gridVariables, model, diffMethod="numeric", isImplicit=True):
"""
......@@ -40,13 +62,20 @@ def _createFVAssembler(*, problem, gridVariables, model, diffMethod="numeric", i
includes += ["dumux/python/assembly/fvassembler.hh"]
moduleName = "fvassembler_" + hashIt(assemblerType)
generator = SimpleGenerator("FVAssembler", "Dumux::Python")
# remark: use SimpleGenerator again starting with dune 2.9
generator = MySimpleGenerator("FVAssembler", "Dumux::Python")
module = generator.load(
includes,
assemblerType,
moduleName,
holder="std::shared_ptr",
preamble=model.cppHeader,
# make sure the assembler is compiled with the Serial backend
# as currently the assembly in combination with Python is not thread-safe
# the following is nicer but only works with dune > 2.8
# extraCMake=[
# "target_compile_definitions(TARGET PUBLIC DUMUX_MULTITHREADING_BACKEND=Serial)"
# ],
)
return module.FVAssembler(problem, problem.gridGeometry(), gridVariables)
......
......@@ -2,7 +2,14 @@ add_python_targets(common
__init__
properties
)
dune_add_pybind11_module(NAME _common)
# currently the Python bindings only work with
# serial dumux code
dune_add_pybind11_module(
NAME _common
COMPILE_DEFINITIONS DUMUX_MULTITHREADING_BACKEND=Serial
)
set_property(TARGET _common PROPERTY LINK_LIBRARIES dunecommon dunegrid APPEND)
if(SKBUILD)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment