-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
412 lines (295 loc) · 12 KB
/
model.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
#
# Copyright (c) 2013-2016, ETH Zurich.
# All rights reserved.
#
# This file is distributed under the terms in the attached LICENSE file.
# If you do not find this file, copies can be found by writing to:
# ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
import sys
import logging
import config
import helpers
import re
sys.path.append(config.MACHINE_DATABASE_SCRIPTS)
import topology_parser
import itertools
import gzip
from multimessage import MultiMessage
# --------------------------------------------------
class Model(object):
def __init__(self, graph=None):
"""
We initialize models with the graph
"""
print ('Initializing Model(%s)' % self.get_name())
self.send_cost = {}
self.recv_cost = {}
self.send_history = {}
self.mm = None # MultiMessage benchmark - see NetosMachine
self.enable_mm = False
assert graph == None
# Topology parser
# --------------------------------------------------
try:
self.machine_topology = topology_parser.parse_machine_db(self.get_name(), 'machinedb/')
print ('Parsing machine topology was successful, machine is %s' % \
(topology_parser.generate_short_name(self.machine_topology)))
except:
helpers.warn('Warning: topology parser did not find machine data')
raise
exit (0)
# Pairwise
# --------------------------------------------------
# Parse pairwise send and receive costs. We need this to
# build the graph with the pairwise measurements.
self._parse_receive_result_file()
self._parse_send_result_file()
# Multimessage
# --------------------------------------------------
mm_fname = '%s/%s/multimessage.gz' % \
(config.MACHINE_DATABASE_DATA, self.get_name())
self.mm = None
print ('Reading multimessage data: ', mm_fname)
try:
f = gzip.open(mm_fname, 'r')
self.mm = MultiMessage(f, self)
f.close()
except IOError:
print ('No multimessage data for this machine')
except Exception as e:
helpers.warn('Unable to read multimessage data' + str(e))
# Build graph and reset
# --------------------------------------------------
self.graph = self._build_graph()
self.reset()
# --------------------------------------------------
# Characteritics of model
def get_name(self):
return None
def get_num_numa_nodes(self):
"""Get the number of NUMA nodes
"""
return len(self.machine_topology['NUMA'].get())
def get_num_cores(self, only_mc=False):
"""Get number of cores.
param only_mc: only return the cores that are active in the
multicast.
"""
if only_mc:
return len(self.get_cores(True))
else:
return self.machine_topology['numcpus']
def get_cores_per_node(self):
"""Assumptions: All NUMA nodes have the same number of cores.
"""
return len(self.machine_topology['NUMA'].get()[0])
def get_send_history_cost(self, sender, cores, corrected=False):
"""Return the send cost for the given history of cores.
@param sender int The core ID of the sender.
@param cores list(int) The cores in the send history.
@param corrected If set to True, enables correction of the
n-send costs based on multimessage data.
"""
cost = 0
for c in cores:
cost += self.get_send_cost(sender, c, False, False)
# Correct if requested
if self.enable_mm and corrected and self.mm:
cost /= self.mm.get_factor(sender, cores)
return cost
def get_send_cost(self, sender, receiver, corrected=False, add_history=False):
"""Determine the send cost for a single message on the link <sender>
to <receiver> given the sender's previous history.
@param corrected If corrected is True, correct the send cost
based on the previous history
"""
if (sender==receiver):
return 0
cost = -1
if corrected:
cost = self.get_send_cost_for_history(sender, receiver,
self.send_history.get(sender, []))
else:
if not (sender, receiver) in self.send_cost:
cost = sys.maxsize
else:
cost = self.send_cost[(sender, receiver)]
if add_history:
self.add_send_history(sender, receiver)
assert cost>0
return cost
def get_send_cost_for_history(self, sender, receiver, cores):
"""Determine the cost of sending one individual message from <sender>
to <receiver> given the previous send history <cores>
Read from pairwise n-receive and scale by the factor given in
multimessage.
"""
cost = self.get_send_cost(sender, receiver, False, False)
assert not receiver in cores # otherwise, we would repeatedly
# send to the same core (chances
# are the receiving core has been
# added to the send history
# before actually sending to it)
if self.enable_mm:
cost /= self.mm.get_factor(sender, cores + [receiver])
return cost
def get_numa_information(self):
"""
Return information on NUMA nodes. This is a a list of
list. Every element of the outer list represents a NUMA node
and the inner list the cores in that NUMA node.
"""
return self.machine_topology['NUMA'].get()
# --------------------------------------------------
# Helpers
def get_cores(self, only_mc=False):
"""Return a list of cores
@param only_mc If set to true, only return nodes that
participate in multicast.
"""
c = list(itertools.chain.from_iterable(self.machine_topology['NUMA'].get()))
c = self.filter_active_cores(c, only_mc)
return c
def filter_active_cores(self, n, only_active):
if config.args.multicast and only_active:
n = [ n_ for n_ in n if n_ in map(int, config.get_mc_group()) ]
return n
def output_graph(self):
""" Write the graph out to disk as <machine_name>_graph
"""
helpers.output_graph(self.graph, '%s_graph' % self.get_name())
# --------------------------------------------------
# Methods used for building overlay + scheduling
def get_graph(self):
return self.graph
def on_same_numa_node(self, core1, core2):
"""
Return whether two nodes are in the same NUMA region
"""
for node in self.get_numa_information():
if core1 in node:
return core2 in node
return None
def get_numa_node_by_id(self, nidx):
"""Get all cores on the given NUMA node
"""
return self.machine_topology['NUMA'].get()[nidx]
def get_numa_node(self, core1):
"""Return all cores that are on the same NUMA node then the given core.
This works as long as get_numa_information is implemented
correctly for a given machine.
"""
numa_node = []
for node in self.graph.nodes():
if self.on_same_numa_node(core1, node):
numa_node.append(node)
return numa_node
def get_numa_id(self, c):
"""Determine ID of the NUMA node <core1> resides on.
"""
nodes = self.machine_topology['NUMA'].get()
for (n, i) in zip(nodes, range(len(nodes))):
if c in n:
return i
def get_root_node(self):
return None
def _parse_receive_result_file(self):
"""
Parse pairwise receive cost results measure with the UMP receive
benchmark in the Barrelfish tree.
We then use these measurements for the receive cost in the simulator
"""
fname = '%s/%s/pairwise-nsend_receive' % \
(config.MACHINE_DATABASE_DATA, self.get_name())
print ('Reading receive costs from %s' % fname)
f = open(fname)
for l in f.readlines():
l = l.rstrip()
m = re.match('(\d+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)', l)
if m:
(src, dest, cost) = (int(m.group(1)),
int(m.group(2)),
float(m.group(3)))
assert (src, dest) not in self.recv_cost
self.recv_cost[(src, dest)] = cost
assert len(self.recv_cost.items())>0
def _parse_send_result_file(self):
"""Parse pairwise send cost results.
For each pair of cores, the input values indicate the cost of
sending a message between that pair of cores.
"""
fname = '%s/%s/pairwise-nsend_send' % \
(config.MACHINE_DATABASE_DATA, self.get_name())
print ('Reading send costs from %s' % fname)
f = open(fname)
for l in f.readlines():
l = l.rstrip()
m = re.match('(\d+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)', l)
if m:
(src, dest, cost) = (int(m.group(1)),
int(m.group(2)),
float(m.group(3)))
assert (src, dest) not in self.send_cost
self.send_cost[(src, dest)] = cost
assert len(self.send_cost.items())>0
def get_receive_cost(self, src, dest):
"""
Return the receive cost for a pair (src, dest) of cores
This is the cost on dest to receive a message from src.
"""
if (src==dest):
return 0
if not (src, dest) in self.recv_cost:
return sys.maxsize
return self.recv_cost[(src, dest)]
def get_receive_send_ratio(self):
"""Get ratio of receive and send costs
"""
import tools
cmax = self.get_num_cores()
sends = []
recvs = []
for s in range(cmax):
for r in range(cmax):
sends.append(self.query_send_cost(s, r))
recvs.append(self.get_receive_cost(s, r))
(s_avg, _, _, s_min, s_max) = tools.statistics(sends)
(r_avg, _, _, r_min, r_max) = tools.statistics(recvs)
return s_avg/r_avg, s_max/r_max, s_min/r_min
def update_edge(self, src, dest, topology):
"""Modifies the topolgy by adding a new edge.
Adds edge src -> dest and removing any old eges n ->
dest. Also updates the send history.
@param topolgy The topology to update
"""
num = 0
prev_parent = None
for c in self.get_cores():
# Delete existing edge
if topology.has_edge((c, dest)):
prev_parent = c
topology.del_edge((c, dest))
num += 1
assert num == 1 # Make sure we only deleted one edge
# Add new one
topology.add_edge((src, dest), \
self.graph.edge_weight((src, dest)))
# Update the send history
assert dest in self.send_history[prev_parent]
self.send_history[prev_parent].remove(dest)
self.add_send_history(src, dest)
def reset(self):
## XXX Also need to reset send history on an individual node
## for barriers etc, when reverting from reduction to ab
if self.send_history:
logging.info(('Resetting send history' + str(self.send_history)))
self.send_history = {}
def add_send_history(self, src, dest):
"""Add the given <src> to <dest> communication to the send
history. This is triggered when a message as actually going to
be sent.
@param src Source core
@param dest Destination core
"""
self.send_history[src] = self.send_history.get(src, []) + [dest]