-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
237 lines (184 loc) · 9.25 KB
/
draw.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
#!/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.
# Draw result of evaluation
import helpers
import config
import subprocess
import shlex
import logging
class Output():
"""Generate LaTeX virtualization of control message flow
Note: the unit in which we draw this is "\sku", a unit that has to
be defined outside of the tikzpicture environemnt. We really want
to define this as a multiple of "sp", the smallest unit TeX
understands. This guarantees exam display, as there will not be
any scaling errors.
We have to do this outside of the tikzpicture, as "sp" depends on
the fond, which is set to NULL inside of tikz [1]. Just use the
following code inside \begin{document}:
\newdimen\sku
\sku=2000sp
Note: "sp" is very small. 1sku in the final output will be equal
to 1 cycle of the machine simulated. For any useful output, "sku"
has hence to be a couple of thousand "sp"s.
[1] https://tex.stackexchange.com/questions/105615/why-does-pgf-not-understand-relative-dimensions-ex-em-sp
"""
cid = [0, 4, 16, 18]
color_map = [ "color%d" % i for i in cid ]
fill_map = [ "color%d" % (i+1) for i in cid ]
height_per_core = 500
scale_x = 5
"Wrapper to enable visualization output to file"
def __init__(self, name, model, topo):
self.name = name
self.model = model
self.topo = topo
self.obj_per_node = dict()
self.obj_per_core = dict()
self.f = open(name, 'w')
self.obj = []
self.last_node = None
self.cores = self.__core_label_to_y_index()
for c in self.model.get_graph().nodes():
# Label
node_name = 'core_%s_label' % c
self.__add_object(c, node_name)
self.f.write("\\node[minimum width=.6cm] (%s) at (0\\sku,%d\\sku) {%2d};\n" % \
(node_name, self._y_coord_for_core(c), c))
def __add_object(self, core, label):
"""
Keep track of objects for fit tikz class
Assumes that the object printes last is shown all the way to the right
"""
self.obj.append(label)
cidx = int(self.model.get_numa_id(core))
self.obj_per_node[cidx] = self.obj_per_node.get(cidx, []) + [label]
self.obj_per_core[core] = self.obj_per_core.get(core, []) + [label]
self.last_node = label
def _y_coord_for_core(self, core):
"Determines padding between cores"
if not core in self.cores:
import pdb
pdb.set_trace()
assert core in self.cores
return self.cores[core]*self.height_per_core
def _scale_time(self, time):
"Determines scale factor for time"
return (time + 50) * self.scale_x
def _scale_cost(self, cost):
return cost * self.scale_x
def _scale_height(self, size):
return size*self.height_per_core*.7
def send(self, core, to, time, cost):
"Visualize send operation"
name = 's_%s_%s' % (core, to)
self.f.write("\\node[draw,fill=color2!50,inner sep=0,minimum width=%d\\sku, minimum height=%d\\sku,anchor=west,font=\\tiny] "\
"(%s) at (%d\\sku,%d\\sku) {%d/%d};\n" % \
(self._scale_cost(cost), self._scale_height(1), name, \
self._scale_time(time), \
self._y_coord_for_core(core), time, cost) )
self.__add_object(core, name)
def receive(self, core, sender, time, cost):
"Visualize receive operation"
# Box indicating receive operation
name = 'r_%s_%s' % (sender, core)
self.f.write("\\node[draw,fill=color6!50,minimum width=%d\\sku, minimum height=%d\\sku,anchor=west,font=\\tiny] "\
"(%s) at (%d\\sku,%d\\sku) {%d/%d};\n" % \
(self._scale_cost(cost), self._scale_height(1), \
name, \
self._scale_time(time), \
self._y_coord_for_core(core),cost,time+cost))
self.__add_object(core, name)
# Iteratively add settings for drawing the connection
settings = [""]
if not self.model.on_same_numa_node(core, sender):
settings.append('semithick')
settings.append('color=color6')
# Arrow indicating flow
self.f.write("\\draw[->%s,decorate,decoration={snake,amplitude=.1mm,segment length=2mm}] ($(s_%s_%s.east)-(1\\sku,0\\sku)$) -- ($(r_%s_%s.west)+(1\\sku,0\\sku)$); \n" % \
(','.join(settings), sender, core, sender, core))
def finalize(self, final_time):
# header
self.f.write("\\begin{pgfonlayer}{background}\n")
# grey background box spanning all objects
n = [ '(%s)' % x for x in self.obj ]
# add empty node containing all objects for easier calculation
self.f.write("\\node [fit=%s] (allobjects) {};\n" % ' '.join(n))
self.f.write("\\node [fit=%s,scale=1.1] (bg) {};\n" % \
' '.join(n))
# Dummy object to extend NUMA nodes to the right
for c in self.model.get_graph().nodes():
numa_name = 'numa_axis_%s' % c
self.f.write("\\draw let \\p1 = (allobjects.east) in node[] (%s) at (\\x1,%d\\sku) {};\n" % \
(numa_name, self._y_coord_for_core(c)))
# Are cores "packed" on nodes?
packed = True
assert self.model.get_num_numa_nodes()>0 # at least one node
_cores_on_node = self.model.get_numa_node_by_id(0)
for i in range(len(_cores_on_node)-1):
packed = packed and _cores_on_node[i]+1 == _cores_on_node[i+1]
logging.info(('Visualization: packed disksplay %d' % packed))
# Individual core by core, no merging of neighboring cores in one box
if not packed:
print (self.obj_per_core)
for c in self.model.get_cores(True):
cidx = self.model.get_numa_id(c) % int(len(self.color_map))
color = self.color_map[cidx]
fill = self.fill_map[cidx]
self.obj_per_core[c] = self.obj_per_core.get(c, []) + ['numa_axis_%d.west' % (c)]
nn = [ '(%s)' % x for x in self.obj_per_core[c] ]
self.f.write("\\node [yscale=0.85,draw=%s,fill=%s,fit=%s,rounded corners] {};\n" \
% (color, fill, ' '.join(nn)))
# colored background box for each numa domain - merging nodes
else:
for i in range(self.model.get_num_numa_nodes()):
cidx = i % int(len(self.color_map))
color = self.color_map[cidx]
coreid = self.model.get_numa_node_by_id(i)[0]
self.obj_per_node[i].append('numa_axis_%d.west' % (coreid))
nn = [ '(%s)' % x for x in self.obj_per_node[i] ]
self.f.write("\\node [yscale=.95,draw=%s!50,fill=%s!10,fit=%s] (node%d) {};\n" \
% (color, color, ' '.join(nn), i))
self.f.write("\\node [rotate=90,left=of node%d,anchor=south,yshift=-.8cm] {NUMA %d};\n" \
% (i, i))
# X-axes
for c in self.model.get_graph().nodes():
self.f.write("\\draw[color=black!30] let \\p1 = (core_10_label.east), \\p2 = (allobjects.east) in (\\x1,%d\\sku) -- (\\x2,%d\\sku);\n" % \
(self._y_coord_for_core(c), self._y_coord_for_core(c)))
# self.f.write("\\node[draw=black,anchor=north,fill=black!20] at (bg.north) {Machine: %s, topology: %s};\n" % (
# self.model.get_name(),
# self.topo.get_name()
# ))
for xaxis_label in range(0, final_time, 500):
self.f.write("\\node at (%d\\sku,-%d\\sku) (cycles) { %d };\n" % \
(self._scale_time(xaxis_label), self.height_per_core,
xaxis_label))
self.f.write("\\node [right=of cycles] {cycles};\n")
# footer
self.f.write("\\end{pgfonlayer}\n")
self.f.close()
# Generate a PNG? Currently, this works only for atomic broadcasts
if config.args.visu and 'atomic_broadcast' in self.name:
self.generate_image()
def __core_label_to_y_index(self):
return helpers.core_index_dict(self.model.get_graph().nodes())
def generate_image(self):
with open('test.tex', 'w') as f:
_out = self.name.replace('.tex', '.png')
print ('Generating visualization .. ')
for line in open('template.tex', 'r'):
f.write(line.replace('{%file%}', self.name))
f.close()
try:
print (subprocess.check_output(shlex.split('rm -f test-figure0.pdf')))
print (subprocess.check_output(shlex.split('pdflatex -shell-escape -interaction nonstopmode test.tex')))
print (subprocess.check_output(shlex.split('convert -verbose -density 300 test-figure0.pdf %s' % _out)))
except Exception as e:
print ('Generating visualization failed, aborting')
raise e