开发者

Parallelism with SciPy.optimize

I'm working on some research code which uses scipy.optimize.leastsq to optimize a function. It does this about 18 times per iteration, so I would like call leastsq in parallel to reduce running time. This shouldn't be a problem because the optimizations are almost completely separate so very little synchronization is required. I recently found out about multiprocessing.pool.ThreadPool which would allow me to do this without having to explicitly set up shared memory (a pain since most of my data are in NumPy arrays). So I made a slight rewrite of my code, hoping it would work, but it throws a strange error: SystemError: null argument to internal routine.

The following is a simplification of my code:

def optfunc(id):
    def errfunc(x):
        return somedata[id] - somefunc(x)

    lock.acquire()
    x0 = numpy.copy(currentx[id])
    lock.release()

    result = scipy.optimize.leastsq(errfunc, x0)

    lock.acquire()
    currentx[id] = result
    lock.release()

ThreadPool(processes=8).map(optfunc, range(idcount))

This should work fine, unless scipy.optimize.leastsq isn't threadsafe. So I tried putting a lock arou开发者_JS百科nd scipy.optimize.leastsq; lo and behold it works. However, processor utilization is stuck at 100%, so this is useless to me.

My question is then what can I do about this? I think my options are:

  1. Find a thread-safe implementation of LM (levmar, maybe?)
  2. Try using processes rather than threads (I don't think this would make a difference)

Any help or suggestions would be greatly appreciated.


Using processes instead of threads will make a difference -- it will work whether the program is threadsafe or not. Of course, whether it is faster depends if the time taken in solving the problem is larger than the overhead.

Using processes may require some additional hassle with setting up all necessary data. The multiprocessing module however takes care of most of the work, so that shouldn't be too difficult to do.


How many CPUs/cores? There's nothing to be gained if what you have is running at 100% without threading. If leastq() is not thread safe, and you're not using a 100% of the computer you have, then you can run one instance of the program per core and have the instances synchronize through the file system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜