-
-
Notifications
You must be signed in to change notification settings - Fork 543
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
[Bug]: BaseSolver.solve() multiprocessing is broken on system's defaulting to spawn
context
#3974
Comments
spawn
contextspawn
context
One possible fix for this can be found by wrapping the top-level script in a import pybamm
import numpy as np
def main():
# create the model
model = pybamm.lithium_ion.DFN()
# set the default model parameters
param = model.default_parameter_values
# change the current function to be an input parameter
param["Current function [A]"] = "[input]"
simulation = pybamm.Simulation(model, parameter_values=param)
# solve the model at the given time points, passing the current as an input
t_eval = np.linspace(0, 600, 300)
current_dict = [{"Current function [A]": x} for x in range(1,3)]
sol = simulation.solve(t_eval, inputs=current_dict)
return sol
if __name__ == "__main__":
sol = main() |
If we are happy to move away from Adding this drop-in allows the above MWE to complete without error. from ray.util.multiprocessing import Pool
with Pool(processes=nproc) as p: @martinjrobins @jsbrittain @tinosulzer I'll start implementing this if it seems reasonable. Open to other suggestions! |
Hi, @BradyPlanden – thanks for investigating a solution and while I'm not sure whether it addresses everything at hand, I did want to highlight that |
Sounds good to me, this should definitely be an optional dependency |
Thanks for the replies @tinosulzer @agriyakhetarpal. Turns out |
Yeah I like sticking with multiprocessing over adding more dependencies |
PyBaMM Version
24.1
Python Version
3.11
Describe the bug
Multiprocessing inputs within BaseSolver.solve() is broken on operating systems that default to
spawn
(Windows, MacOS) instead offork
(Linux). A minimal working example is shown below; however, when run inside a Jupyter notebook, the code completes without problem, which seems to be how it didn't get caught in tests. I suspect this is due to the difference in call structure, i.e. jupyter might wrap the cells inmain()
. The code in question is:PyBaMM/pybamm/solvers/base_solver.py
Line 915 in c204bdd
Modifying this to
with mp.get_context("fork").Pool(processes=nproc) as p:
appears to solve the issue; however, as of python 3.14,fork
is being depreciated forspawn
orforkserver
.Linking pybop-team/PyBOP#283
Steps to Reproduce
MWE:
Relevant log output
The text was updated successfully, but these errors were encountered: