File: seq.py

package info (click to toggle)
mpi4py 4.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,540 kB
  • sloc: python: 34,465; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (52 lines) | stat: -rw-r--r-- 1,303 bytes parent folder | download | duplicates (2)
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:
    """
    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()

    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)