from xieweizhong.tk Using ipython for parallel computing Wed 25 December 2013 By Xiewei Zhong
In Python.
Problem
In my work, I meet one problem: the time consuming during computing. My Thought
At first, the GPU programming was occured in my mind. But, aftering trying, I found that the algorithm should be rewrite by myself using CUDA-c, and the helpful functions in scipy module can not be used. Therefore, I get rid of the mind, and begain to looking for the method: CPU-parallel programming. IPython
Before, I only know that the ipython is a wonderful python programming environment. Now, the other powerful function of ipython make me shocked: Using IPython for parallel computing.
Below, the detail solutions are illustrated.
from scipy.optimize import minimize from IPython.parallel import Client # The most important thing import numpy as np
def func(x): "My function for minimize"
def myminimize(x): """My function to wrap the minimize funtion""" ... ... # To determine the x0 res = minimize(func, x0, args=(x,), method='Nelder-Mead', options={'xtol': 1e-5, 'disp': False}) return res.x
if name == 'main': x = list() ... # initialize content of x rc = Client() dview = rc[:] # set the global informations dview['g_1'] = g_1 dview['np'] = np dview['minimize'] = minimize ... result = dview.map_sync(myminimize, x)
In the termial, typeipcluster start -n 16 at first, and then python yourcode.py