6

I am numerically solving a differential equation that depends on parameters. I am not really interested on the solutions but on their behaviour depending on the value of the parameters. Since I want a very precise description I must use a very fine array of parameters' values resulting in a lot of ODE solving processes. So I want to know if it would be possible to "parallelize" such a program. The idea is that maybe each processor of my computer can solve the ODE for a distinct pair of parameters. A kind of example is the following:

import matplotlib.pyplot as plt
from scipy.integrate import ode
import numpy as np

# - ODE - #
def sys(t,x,p1,p2): #p1 and p2 are the parameters
    dx=np.zeros(2)
    dx[0] = x[1]
    dx[1] = (p1+p2*cos(t))*x[0]
    return dx

t0=0; tEnd=10; dt=0.01
r = ode(sys).set_integrator('dopri5', nsteps=10,max_step=dt)
Y=[];S=[];T=[]
ic=[.1,0] 
# - parameters range - # 
P1=np.linspace(0,1,100)
    P2=np.linspace(0,1,100)
# -------------------- #
for p1 in P1:
    for p2 in P2:
        r.set_initial_value(ic, t0).set_f_params(p1,p2)
        flag='No'
        while r.successful() and r.t +dt < tEnd:
            r.integrate(r.t+dt)
            Y.append(r.y)
            T.append(r.t)
                #-This is what we want to know.
            if r.y[0]>2*ic[0]:
                flag='Yes'
                break
        if flag=='Yes':     
            plt.scatter(p1,p2,s=1, c='k', marker='.')
# ------------------------------------ #
plt.show()

Note that each for loop is independent so: Is it possible to make those for loops in a parallel way? So I would imagine that it is possible that each of my 8 processors do one double for loop at a time and then probably make the computations roughly 8 times faster? Or at least faster?

PepeToro
  • 559
  • 4
  • 14
  • Take a look at the [queue](http://docs.python.org/3.3/library/queue.html) module – kalhartt Oct 04 '13 at 14:43
  • 1
    Thanks, I checked it, but I found also [dispy](http://dispy.sourceforge.net) which solves my problem just as I wanted, I thought on answering my own question but It seems it is not possible, at least for me. – PepeToro Nov 01 '13 at 18:49
  • @user58533 I think you have enough reputation points now to answer your own question. I think it'd be of interest seeing any more detail to your solution :) – Steve Koch Jan 13 '14 at 22:29
  • You could also use [mpi4py](http://mpi4py.scipy.org) if you are familiar with MPI itself. Your problem can be easily parallelized using MPI and the mpi4py interface is quite straightforward. – deepak Sep 04 '15 at 00:05

1 Answers1

4

I think it is easiest to use multiprocessing, just implement inner loops as a stand-alone function and run result = Pool(8).map(solver, P1). To scale on multiple computers I'd recommend Apache Spark.

Edit: Note that you cannot call plotting methods within the method itself, you should return raw numbers to the caller and do plotting after the .map calls has finished.

NikoNyrh
  • 3,578
  • 2
  • 18
  • 32