Pathos KeyError with dill when using an instance method

179 Views Asked by At

I'm trying to run a simple ODE system with different values of a parameter, using Assimulo to call SUNDIAL's CVode. As a case study, I wrote these functions and solved the problem in parallel successfully:

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)

which yields

enter image description here

Then, I organized the functions above into a class:

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

Finally, I created an instance and passed the method of interest (solve_ode) to a new ParallelPool instance:

# ---------- Parallel solution with class
instance = ODETest(y_zero=5, t_final=20)
pool_2 = ParallelPool(4)
pool_2.map(instance.solve_ode, cts)

This time, the following error appears:

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'

I've seen some related errors regarding dill and that KeyError but I cannot see how to solve my problem.

0

There are 0 best solutions below