Tuesday, 15 January 2013

python - Random Walk - Parallel processing -



python - Random Walk - Parallel processing -

i'm implementing monte-carlo method solve diffusion equation. solution can expressed mathematical expectation of phi(w) phi function (varies accordingly diffusion equation) , w symmetric random walk stopped @ boundary of domain. evaluate function @ point x, need start every walk in expectation x.

i want evaluate function @ big number of points. do:

i start simultaneously 1 walk every point i utilize same steps every point once each walk has reached boundary, stop i repeat these operations n times, in order approximate mathematical expectation frequency on n simulations.

my code (python) looks :

for k in range(n): #for each "walk" step = 0 while not(every walk has reach boundary): map(update_walk,points) #update walk starting each x in points incr(step)

the problem : extremely long since n can big , number of points also. looking solution help me optimize code.

i have thought of parallel processing (each walk independent) ipython did not succeed because within function (it returned error

"could not launch function 'f' because not found 'file.f' " 'f' define within file.big_f)

launching computation in separate process (which want in order avoid gil) create utilize of multiprocessing module. launching x parallel processes can done next code :

from multiprocessing import process queue import queue queue = queue(maxsize=max_nodes) # number of processes spawn procs = [process(target=f, args=(node, queue) node in nodes] # node node each process , queue mutual of processes [p.start() p in procs] # start them results = [] in range(len(nodes): results.append(queue.get())

all need modify func (target function) take relevant number of args plus queue, , @ end of computation phone call queue.put(result)

hope makes sense

python parallel-processing montecarlo

No comments:

Post a Comment