[SciPy-Dev] BSP interface in the SciPy.

Sturla Molden sturla@molden...
Tue Feb 14 10:49:26 CST 2012

On 14.02.2012 17:05, Sturla Molden wrote:

> If you need a barrier for BSP synchronization, this is the simplest
> implementation I can think of:

Moving the context manager to __call__ and adding a timeout to wait it 
becomes like this.

It's stange that Python does not have a barrier object in the standard 
lib. Considering usefulness to scientific computing it could be worth 
adding to numpy or scipy.


from multiprocessing import Event # or threading.Event

from math import ceil, log
from contextlib import contextmanager
from time import clock

class Barrier(object):

     def __init__(self, numproc):
         self._events = [Event() for n in range(numproc**2)]
         self._numproc = numproc

     def __call__(self, rank):
         self.wait(rank, None)
         self.wait(rank, None)

     def wait(self, rank, *timeout):
         t0 = clock()
         if timeout:
             timeout = timeout[0]
             if (timeout is not None) and (not isinstance(timeout, float)):
                 return ValueError, 'timeout must be None or a float'
         # loop log2(num_threads) times, rounding up
         for k in range(int(ceil(log(self._numproc)/log(2)))):
             # send event to process (rank + 2**k) % numproc
             receiver = (rank + 2**k) % self._numproc
             evt = self._events[rank * self._numproc + receiver]
             # wait for event from process (rank - 2**k) % numproc
             sender = (rank - 2**k) % self._numproc
             evt = self._events[sender * self._numproc + rank]
             if timeout:
                 t = clock()
                 if not evt.wait(max(0.0,timeout-(t-t0))):
                     return False
         return True

More information about the SciPy-Dev mailing list