我正在尝试运行一个具有不同参数值的简单 ODE 系统,Assimulo
用于调用 SUNDIAL 的 CVode。作为案例研究,我编写了这些函数并成功地并行解决了问题:
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
import matplotlib.pyplot as plt
from pathos.pools import ParallelPool
def linear_ode(time, y, param):
dy_dt = -param * y
return dy_dt
def solve_ode(param, t_final=20):
y_zero = 5
problem = Explicit_Problem(lambda time, x: linear_ode(time, x, param),
y_zero, 0)
solver = CVode(problem)
t_vals, y_vals = solver.simulate(t_final)
return t_vals, y_vals
if __name__ == '__main__':
# ---------- Parallel solution
cts = (0.1, 0.2, 0.3)
pool = ParallelPool(4)
results = pool.map(solve_ode, cts)
# Plot
fig_par, ax_par = plt.subplots()
for result in results:
t_prof, y_prof = result
ax_par.plot(t_prof, y_prof)
产生
然后,我将上面的函数组织成一个类:
class ODETest:
def __init__(self, y_zero, t_final):
self.y_zero = y_zero
self.t_final = t_final
def linear_ode(self, time, y, param):
dy_dt = -param * y
return dy_dt
def solve_ode(self, param):
problem = Explicit_Problem(lambda time, x: self.linear_ode(time, x, param),
self.y_zero, 0)
solver = CVode(problem)
t_vals, y_vals = solver.simulate(self.t_final)
return t_vals, y_vals
最后,我创建了一个实例并将感兴趣的方法 ( solve_ode
) 传递给一个新ParallelPool
实例:
# ---------- Parallel solution with class
instance = ODETest(y_zero=5, t_final=20)
pool_2 = ParallelPool(4)
pool_2.map(instance.solve_ode, cts)
这次出现如下错误:
A fatal error has occured during the function execution
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\ppft\__main__.py", line 107, in run
__args = pickle.loads(ppc.b_(__sargs))
File "C:\ProgramData\Anaconda3\lib\site-packages\dill\_dill.py", line 275, in loads
return load(file, ignore, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\dill\_dill.py", line 270, in load
return Unpickler(file, ignore=ignore, **kwds).load()
File "C:\ProgramData\Anaconda3\lib\site-packages\dill\_dill.py", line 472, in load
obj = StockUnpickler.load(self)
File "C:\ProgramData\Anaconda3\lib\site-packages\dill\_dill.py", line 577, in _load_type
return _reverse_typemap[name]
KeyError: 'ClassType'
我已经看到了一些与 KeyError 相关的错误dill
,但我看不到如何解决我的问题。