File: render-topo.py

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (73 lines) | stat: -rw-r--r-- 2,490 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##
## Copyright (C) by Argonne National Laboratory
##     See COPYRIGHT in top-level directory
##

# This python script renders the file dumped by MPIR_CVAR_COORDINATES_DUMP.
# Input: Network coordinates file dumpped by MPIR_CVAR_COORDINATES_DUMP.
# Output: Graph of the coordinates. Output filename is defined by '--output'.
# Example: python3 ./render-topo.py coords --format png --output coords

import sys
import argparse

try:
    from graphviz import Digraph
except ImportError as e:
    sys.exit('Unable to import graphviz module: {}.\n'.format(e) +
             'Please install the graphviz module by running the following command:\n'
             '\n'
             '      python -m pip install graphviz\n')

def main():
    args = parse_args()
    dot = Digraph(args.output)

    edges = set()

    with open(args.filename) as coords_file:
        for line in coords_file:
            if (line[0] == '#'):
                continue
            rank_label, coord_node_labels = parse_line(line)
            dot.node(rank_label)
            [dot.node(label) for label in coord_node_labels]
            for index in range(len(coord_node_labels) - 1):
                edges.add((coord_node_labels[index], coord_node_labels[index+1]))
            edges.add((coord_node_labels[-1], rank_label))

    dot.edges(edges)
    dot.render(format=args.format, view=True)


def parse_args():
    description = 'Render a per-rank coordinates topology file as a graphical set of trees.'
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('filename', help='The file contains the coordinates.')
    parser.add_argument('--format', '-f', default='svg', help='Output format. Can be any format supported by graphviz. Default: svg.')
    parser.add_argument('--output', '-o', default='coords', help='Output file name. Default: coords.')
    return parser.parse_args()


def parse_line(line):
    rank_label, coord_set = line.split(':')
    # ignor the last coord, which is the port number
    coords = coord_set.split()[:-1]
    coord_node_labels = create_coord_node_labels(coords)
    return rank_label, coord_node_labels


def create_coord_node_labels(coords):
    result = []
    coord_combo = ''
    for prefix, coord in zip(['G', 'S'], coords):
        if 0 == len(coord_combo):
            coord_combo = coord
        else:
            coord_combo += "." + coord
        result.append(prefix + coord_combo)
    return result


if __name__ == '__main__':
    main()