def run_parrallel_iterations(ps, landscape, config): iteration = 0 if config.num_iterations > 1: means = [np.zeros(landscape.shape) for _ in xrange(ps.max_time_steps)] #run_iteration(ps, landscape) #Perform the iterations using a process pool for concurrency pool = mp.Pool(num_processors) print "running...", num_iterations, "iterations for", ps.max_time_steps, "timesteps on", num_processors, "processors" results = [ pool.apply_async(run_iteration, args=[ps, landscape]) for _ in xrange(num_iterations) ] pool.close() #Process iterations as they complete while len(results) > 0: completed = [i for i, r in enumerate(results) if r.ready()] for i in reversed( completed ): #reversed so that indices aren't invalidated as we pop print "processing iteration " + str(iteration + 1) #Get the result from the list and save the iteration to file Ns = results.pop(i).get(None) config.ext = "/iteration " + str(iteration + 1) process_iteration(Ns, ps, landscape, config) iteration += 1 if config.num_iterations > 1: #Add the population for each time step in the iteration to the total for t, N in enumerate(Ns): means[t] += N time.sleep(15) pool.close() pool.join() #run_iteration(ps, landscape) if config.num_iterations > 1: for N in means: N /= config.num_iterations config.ext = "/means" io.process_iteration(means, ps, landscape, config)
def run_parrallel_iterations(ps, landscape, config): iteration = 0 if config.num_iterations > 1: means = [np.zeros(landscape.shape) for _ in xrange(ps.max_time_steps)] #run_iteration(ps, landscape) #Perform the iterations using a process pool for concurrency pool = mp.Pool(num_processors) print "running...", num_iterations, "iterations for", ps.max_time_steps,"timesteps on",num_processors,"processors" results = [pool.apply_async(run_iteration, args=[ps, landscape]) for _ in xrange(num_iterations)] pool.close() #Process iterations as they complete while len(results) > 0: completed = [i for i, r in enumerate(results) if r.ready()] for i in reversed(completed): #reversed so that indices aren't invalidated as we pop print "processing iteration " + str(iteration + 1) #Get the result from the list and save the iteration to file Ns = results.pop(i).get(None) config.ext = "/iteration " + str(iteration + 1) process_iteration(Ns, ps, landscape, config) iteration += 1 if config.num_iterations > 1: #Add the population for each time step in the iteration to the total for t, N in enumerate(Ns): means[t] += N time.sleep(15) pool.close() pool.join() #run_iteration(ps, landscape) if config.num_iterations > 1: for N in means: N /= config.num_iterations config.ext = "/means" io.process_iteration(means, ps, landscape, config)
def process_iteration(Ns, ps, landscape, config): io.process_iteration(Ns, ps, landscape, config)