gen_traffic_pattern_config.py 6.67 KB
Newer Older
1
2
3
4
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Utilities for traffic pattern config."""
5
6
7
import re
from pathlib import Path

8
from superbench.common.utils import logger
9
from superbench.common.utils import gen_topo_aware_config
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


def gen_all_nodes_config(n):
    """Generate all nodes config.

    Args:
        n (int): the number of participants.

    Returns:
        config (list): the generated config list, each item in the list is a str like "0,1,2,3".
    """
    config = []
    if n <= 0:
        logger.warning('n is not positive')
        return config
    config = [','.join(map(str, range(n)))]
    return config


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
def gen_pair_wise_config(n):
    """Generate pair-wised VM pairs config.

    One-to-one means that each participant plays every other participant once.
    The algorithm refers circle method of Round-robin tournament in
    https://en.wikipedia.org/wiki/Round-robin_tournament.
    if n is even, there are a total of n-1 rounds, with n/2 pair of 2 unique participants in each round.
    If n is odd, there will be n rounds, each with n-1/2 pairs, and one participant rotating empty in that round.
    In each round, pair up two by two from the beginning to the middle as (begin, end),(begin+1,end-1)...
    Then, all the participants except the beginning shift left one position, and repeat the previous step.

    Args:
        n (int): the number of participants.

    Returns:
        config (list): the generated config list, each item in the list is a str like "0,1;2,3".
    """
    config = []
    if n <= 0:
        logger.warning('n is not positive')
        return config
    candidates = list(range(n))
    # Add a fake participant if n is odd
    if n % 2 == 1:
        candidates.append(-1)
    count = len(candidates)
    non_moving = [candidates[0]]
    for _ in range(count - 1):
        pairs = [
            '{},{}'.format(candidates[i], candidates[count - i - 1]) for i in range(0, count // 2)
            if candidates[i] != -1 and candidates[count - i - 1] != -1
        ]
        row = ';'.join(pairs)
        config.append(row)
        robin = candidates[2:] + candidates[1:2]
        candidates = non_moving + robin
    return config


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def gen_k_batch_config(n, batch):
    """Generate VM groups config with specified batch scale.

    Args:
        n (int): the number of participants.
        batch (int): the scale of batch.

    Returns:
        config (list): the generated config list, each item in the list is a str like "0,1;2,3".
    """
    config = []
    if batch is None:
        logger.warning('scale is not specified')
        return config
    if batch <= 0 or n <= 0:
        logger.warning('scale or n is not positive')
        return config
    if batch > n:
        logger.warning('scale large than n')
        return config

    group = []
    rem = n % batch
    for i in range(0, n - rem, batch):
        group.append(','.join(map(str, list(range(i, i + batch)))))
    config = [';'.join(group)]
    return config


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def __convert_config_to_host_group(config, host_list):
    """Convert config format to host node.

    Args:
        host_list (list): the list of hostnames read from hostfile.
        config (list): the traffic pattern config.

    Returns:
        host_groups (list): the host groups converted from traffic pattern config.
    """
    host_groups = []
    for item in config:
        groups = item.strip().strip(';').split(';')
        host_group = []
        for group in groups:
            hosts = []
            for index in group.split(','):
                hosts.append(host_list[int(index)])
            host_group.append(hosts)
        host_groups.append(host_group)
    return host_groups


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def gen_ibstat(ansible_config, ibstat_path):    # pragma: no cover
    """Generate the ibstat file in specified path.

    Args:
        ansible_config (DictConfig): Ansible config object.
        ibstat_path (str): the expected path of ibstat file.

    Returns:
        ibstat_path (str): the generated path of ibstat file.
    """
    from superbench.runner import AnsibleClient
    ibstat_list = []
    stdout_regex = re.compile(r'\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))')
    ansible_client = AnsibleClient(ansible_config)
    cmd = 'cat /sys/class/infiniband/*/sys_image_guid | tr -d :'

    # callback function to collect and parse ibstat
    def _ibstat_parser(artifact_dir):
        stdout_path = Path(artifact_dir) / 'stdout'
        with stdout_path.open(mode='r') as raw_outputs:
            for raw_output in raw_outputs:
                output = stdout_regex.sub('', raw_output).strip()
                if ' | CHANGED | rc=0 >>' in output:
                    output = 'VM_hostname ' + output.replace(' | CHANGED | rc=0 >>', '')
                ibstat_list.append(output)

    config = ansible_client.get_shell_config(cmd)
    config['artifacts_handler'] = _ibstat_parser
    rc = ansible_client.run(config)
    if rc != 0:
        logger.error('Failed to gather ibstat with config: {}'.format(config))
    with Path(ibstat_path).open(mode='w') as f:
        for ibstat in ibstat_list:
            f.write(ibstat + '\n')
    return ibstat_path


157
158
def gen_traffic_pattern_host_groups(host_list, pattern, mpi_pattern_path, benchmark_name):
    """Generate host group from specified traffic pattern and write in specified path.
159
160
161
162

    Args:
        host_list (list): the list of hostnames read from hostfile.
        pattern (DictConfig): the mpi pattern dict.
163
164
        mpi_pattern_path (str): the path of mpi pattern config file.
        benchmark_name (str): the name of benchmark.
165
166

    Returns:
167
        host_groups (list): the host groups generated from traffic pattern.
168
169
170
    """
    config = []
    n = len(host_list)
171
    if pattern.type == 'all-nodes':
172
        config = gen_all_nodes_config(n)
173
    elif pattern.type == 'pair-wise':
174
        config = gen_pair_wise_config(n)
175
176
177
178
179
180
    elif pattern.type == 'k-batch':
        config = gen_k_batch_config(n, pattern.batch)
    elif pattern.type == 'topo-aware':
        config = gen_topo_aware_config(
            host_list, pattern.ibstat, pattern.ibnetdiscover, pattern.min_dist, pattern.max_dist
        )
181
    else:
182
        logger.error('Unsupported traffic pattern: {}'.format(pattern.type))
183
184
    host_groups = __convert_config_to_host_group(config, host_list)
    # write traffic pattern host groups to specified path
185
186
187
188
189
190
191
192
193
194
    with open(mpi_pattern_path, 'a') as f:
        f.write('benchmark_name: {} pattern_type: {}'.format(benchmark_name, pattern.type) + '\n')
        for host_group in host_groups:
            row = []
            for host_list in host_group:
                group = ','.join(host_list)
                row.append(group)
            group = ';'.join(row)
            f.write(group + '\n')
        f.write('\n')
195
    return host_groups