1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
class Seq(object):
"""
Sequential execution
"""
def __init__(self, comm, ng=1, tag=0):
ng = int(ng)
tag = int(tag)
assert ng >= 1
assert ng <= comm.Get_size()
self.comm = comm
self.ng = ng
self.tag = tag
def __enter__(self):
self.begin()
return self
def __exit__(self, *exc):
self.end()
return None
def begin(self):
"""
Begin a sequential execution of a section of code
"""
comm = self.comm
size = comm.Get_size()
if size == 1: return
rank = comm.Get_rank()
ng = self.ng
tag = self.tag
if rank != 0:
comm.Recv([None, 'B'], rank - 1, tag)
if rank != (size - 1) and (rank % ng) < (ng - 1):
comm.Send([None, 'B'], rank + 1, tag)
def end(self):
"""
End a sequential execution of a section of code
"""
comm = self.comm
size = comm.Get_size()
if size == 1: return
rank = comm.Get_rank()
ng = self.ng
tag = self.tag
if rank == (size - 1) or (rank % ng) == (ng - 1):
comm.Send([None, 'B'], (rank + 1) % size, tag)
if rank == 0:
comm.Recv([None, 'B'], size - 1, tag)
|