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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With