On 17/04/07, Lou Pecora <[EMAIL PROTECTED]> wrote:
I get what you are saying, but I'm not even at the
Stupidly Easy Parallel level, yet.  Eventually.

Well, it's hardly wonderful, but I wrote a little package to make idioms like:

d = {}
def work(f):
   d[f] = sum(exp(2.j*pi*f*times))
foreach(work,freqs,threads=3)

work fine.

Of course you need to make sure your threads don't accidentally
trample all over each other, but otherwise it's an easy way to get a
factor-of-two speedup.

Anne
import sys
import time
import threading

# FIXME: propagate exceptions
def foreach(f,l,threads=3):
    """
    Apply f to each element of l, in parallel
    """

    if threads>1:
	iteratorlock = threading.Lock()
	i = l.__iter__()
	exceptions = []

	def runall():
	    while True:
		iteratorlock.acquire()
		try:
		    try:
			if exceptions:
			    return
			v = i.next()
		    finally:
			iteratorlock.release()
		except StopIteration:
		    return
		try:
		    f(v)
		except:
		    e = sys.exc_info()
		    iteratorlock.acquire()
		    try:
			exceptions.append(e)
		    finally:
			iteratorlock.release()
	
	threadlist = [threading.Thread(target=runall) for j in xrange(threads)]
	for t in threadlist:
	    t.start()
	for t in threadlist:
	    t.join()
	if exceptions:
	    a, b, c = exceptions[0]
	    raise a, b, c
    else:
	for v in l:
	    f(v)

if __name__=='__main__':
    def f(x):
	print x
	time.sleep(0.5)
    foreach(f,range(10))
    def g(x):
	time.sleep(0.5)
	print x
	raise ValueError, x
	time.sleep(0.5)
    foreach(g,range(10))
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to