Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
194 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,134 @@ | |||
import sys | |||
import time | |||
import threading | |||
from itertools import izip, count | |||
|
|||
def foreach(f,l,ARk, X, A, threads=4,return_=False): | |||
""" | |||
Apply f to each element of l, in parallel | |||
""" | |||
|
|||
if threads>1: | |||
iteratorlock = threading.Lock() | |||
exceptions = [] | |||
if return_: | |||
n = 0 | |||
d = {} | |||
i = izip(count(),l.__iter__()) | |||
else: | |||
i = l.__iter__() | |||
|
|||
|
|||
def runall(): | |||
while True: | |||
iteratorlock.acquire() | |||
try: | |||
try: | |||
if exceptions: | |||
return | |||
v = i.next() | |||
finally: | |||
iteratorlock.release() | |||
except StopIteration: | |||
return | |||
try: | |||
if return_: | |||
n,x = v | |||
d[n] = f(x, ARk, X, A) | |||
else: | |||
f(v, ARk, X[i], A) | |||
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 | |||
if return_: | |||
r = d.items() | |||
r.sort() | |||
return [v for (n,v) in r] | |||
else: | |||
if return_: | |||
return [f(v, ARk, X, A) for v in l] | |||
else: | |||
for v in l: | |||
f(v, ARk, X, A) | |||
return | |||
|
|||
def parallel_map(f,l,ARk, X, A,threads=4): | |||
return foreach(f,l,ARk, X, A, threads=threads,return_=True) | |||
|
|||
def parallel_map2(f,l,ARki, A,threads=4): | |||
return foreach2(f,l,ARki, A, threads=threads,return_=True) | |||
|
|||
def foreach2(f,l,ARki, A, threads=4,return_=False): | |||
""" | |||
Apply f to each element of l, in parallel | |||
""" | |||
|
|||
if threads>1: | |||
iteratorlock = threading.Lock() | |||
exceptions = [] | |||
if return_: | |||
n = 0 | |||
d = {} | |||
i = izip(count(),l.__iter__()) | |||
else: | |||
i = l.__iter__() | |||
|
|||
|
|||
def runall(): | |||
while True: | |||
iteratorlock.acquire() | |||
try: | |||
try: | |||
if exceptions: | |||
return | |||
v = i.next() | |||
finally: | |||
iteratorlock.release() | |||
except StopIteration: | |||
return | |||
try: | |||
if return_: | |||
n,x = v | |||
d[n] = f(x, ARki, A) | |||
else: | |||
f(v, ARki, A) | |||
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 | |||
if return_: | |||
r = d.items() | |||
r.sort() | |||
return [v for (n,v) in r] | |||
else: | |||
if return_: | |||
return [f(v, ARki, A) for v in l] | |||
else: | |||
for v in l: | |||
f(v, ARki, A) | |||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters