forked from StanfordLegion/legion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlegion_prof.py
executable file
·2834 lines (2465 loc) · 106 KB
/
legion_prof.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2017 Stanford University, NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import tempfile
import legion_spy
import argparse
import sys, os, shutil
import string, re, json, heapq, time, itertools
from collections import defaultdict
from math import sqrt, log
from cgi import escape
from operator import itemgetter
from os.path import dirname, exists, basename
from legion_serializer import LegionProfASCIIDeserializer, LegionProfBinaryDeserializer, GetFileTypeInfo
# Make sure this is up to date with lowlevel.h
processor_kinds = {
1 : 'GPU',
2 : 'CPU',
3 : 'Utility',
4 : 'IO',
5 : 'Proc Group',
6 : 'Proc Set',
7 : 'OpenMP',
}
# Make sure this is up to date with lowlevel.h
memory_kinds = {
0 : 'GASNet Global',
1 : 'System',
2 : 'Registered',
3 : 'Socket',
4 : 'Zero-Copy',
5 : 'Framebuffer',
6 : 'Disk',
7 : 'HDF5',
8 : 'File',
9 : 'L3 Cache',
10 : 'L2 Cache',
11 : 'L1 Cache',
}
# Micro-seconds per pixel
US_PER_PIXEL = 100
# Pixels per level of the picture
PIXELS_PER_LEVEL = 40
# Pixels per tick mark
PIXELS_PER_TICK = 200
# prof_uid counter
prof_uid_ctr = 0
def get_prof_uid():
global prof_uid_ctr
prof_uid_ctr += 1
return prof_uid_ctr
def data_tsv_str(level, start, end, color, opacity, title,
initiation, _in, out, children, parents, prof_uid):
# replace None with ''
def xstr(s):
return str(s or '')
return xstr(level) + "\t" + xstr(start) + "\t" + xstr(end) + "\t" + \
xstr(color) + "\t" + xstr(opacity) + "\t" + xstr(title) + "\t" + \
xstr(initiation) + "\t" + xstr(_in) + "\t" + xstr(out) + "\t" + \
xstr(children) + "\t" + xstr(parents) + "\t" + xstr(prof_uid) + "\n"
def slugify(filename):
# convert spaces to underscores
slugified = filename.replace(" ", "_")
# remove special characters
slugified = slugified.translate(None, "!@#$%^&*(),/?<>\"':;{}[]|/+=`~")
return slugified
# Helper function for computing nice colors
def color_helper(step, num_steps):
assert step <= num_steps
h = float(step)/float(num_steps)
i = ~~int(h * 6)
f = h * 6 - i
q = 1 - f
rem = i % 6
r = 0
g = 0
b = 0
if rem == 0:
r = 1
g = f
b = 0
elif rem == 1:
r = q
g = 1
b = 0
elif rem == 2:
r = 0
g = 1
b = f
elif rem == 3:
r = 0
g = q
b = 1
elif rem == 4:
r = f
g = 0
b = 1
elif rem == 5:
r = 1
g = 0
b = q
else:
assert False
r = (~~int(r*255))
g = (~~int(g*255))
b = (~~int(b*255))
r = "%02x" % r
g = "%02x" % g
b = "%02x" % b
return ("#"+r+g+b)
class PathRange(object):
def __init__(self, start, stop, path):
assert start <= stop
self.start = start
self.stop = stop
self.path = list(path)
def __cmp__(self, other):
self_elapsed = self.elapsed()
other_elapsed = other.elapsed()
if self_elapsed == other_elapsed:
return cmp(len(self.path), len(other.path))
else:
return cmp(self_elapsed, other_elapsed)
def clone(self):
return PathRange(self.start, self.stop, self.path)
def elapsed(self):
return self.stop - self.start
def __repr__(self):
return "(" + str(self.start) + "," + str(self.stop) + ")"
class HasDependencies(object):
def __init__(self):
self.deps = {"in": set(), "out": set(), "parents": set(), "children" : set()}
self.initiation_op = None
self.initiation = ''
# for critical path analysis
self.path = PathRange(0, 0, [])
self.visited = False
def add_initiation_dependencies(self, state, op_dependencies, transitive_map):
pass
def attach_dependencies(self, state, op_dependencies, transitive_map):
if self.op_id in op_dependencies:
self.deps["out"] |= op_dependencies[self.op_id]["out"]
self.deps["in"] |= op_dependencies[self.op_id]["in"]
self.deps["parents"] |= op_dependencies[self.op_id]["parents"]
self.deps["children"] |= op_dependencies[self.op_id]["children"]
def get_unique_tuple(self):
assert self.proc is not None #TODO: move to owner
cur_level = self.proc.max_levels - self.level
return (self.proc.node_id, self.proc.proc_in_node, self.prof_uid)
class HasInitiationDependencies(HasDependencies):
def __init__(self, initiation_op):
HasDependencies.__init__(self)
self.initiation_op = initiation_op
self.initiation = initiation_op.op_id
def add_initiation_dependencies(self, state, op_dependencies, transitive_map):
"""
Add the dependencies from the initiation to us
"""
unique_tuple = self.get_unique_tuple()
if self.initiation in state.operations:
op = state.find_op(self.initiation)
# this op exists
if op.proc is not None:
if self.initiation not in op_dependencies:
op_dependencies[self.initiation] = {
"in" : set(),
"out" : set(),
"parents" : set(),
"children" : set()
}
if op.stop < self.stop:
op_dependencies[self.initiation]["in"].add(unique_tuple)
else:
op_dependencies[self.initiation]["out"].add(unique_tuple)
def attach_dependencies(self, state, op_dependencies, transitive_map):
"""
Add the dependencies from the us to the initiation
"""
# add the out direction
if self.initiation in state.operations:
op = state.find_op(self.initiation)
if op.proc is not None: # this op exists
op_tuple = op.get_unique_tuple()
if op.stop < self.stop:
self.deps["out"].add(op_tuple)
else:
self.deps["in"].add(op_tuple)
def get_color(self):
return self.initiation_op.get_color()
class HasNoDependencies(HasDependencies):
def __init__(self):
HasDependencies.__init__(self)
def add_initiation_dependencies(self, state, op_dependencies, transitive_map):
pass
def attach_dependencies(self, state, op_dependencies, transitive_map):
pass
class TimeRange(object):
def __init__(self, create, ready, start, stop):
assert create <= ready
assert ready <= start
assert start <= stop
self.create = create
self.ready = ready
self.start = start
self.stop = stop
def __cmp__(self, other):
# The order chosen here is critical for sort_range. Ranges are
# sorted by start_event first, and then by *reversed*
# end_event, so that each range will precede any ranges they
# contain in the order.
if self.start < other.start:
return -1
if self.start > other.start:
return 1
if self.stop > other.stop:
return -1
if self.stop < other.stop:
return 1
return 0
def __repr__(self):
return "Start: %d us Stop: %d us Total: %d us" % (
self.start, self.end, self.total_time())
def total_time(self):
return self.stop - self.start
# The following must be overridden by subclasses that need them
def mapper_time(self):
pass
def active_time(self):
pass
def application_time(self):
pass
def meta_time(self):
pass
class Processor(object):
def __init__(self, proc_id, kind):
self.proc_id = proc_id
# PROCESSOR: tag:8 = 0x1d, owner_node:16, (unused):28, proc_idx: 12
# owner_node = proc_id[55:40]
# proc_idx = proc_id[11:0]
self.node_id = (proc_id >> 40) & ((1 << 16) - 1)
self.proc_in_node = (proc_id) & ((1 << 12) - 1)
self.kind = kind
self.app_ranges = list()
self.last_time = None
self.tasks = list()
self.max_levels = 0
self.time_points = list()
self.util_time_points = list()
def get_short_text(self):
return self.kind + " Proc " + str(self.proc_in_node)
def add_task(self, task):
task.proc = self
self.tasks.append(task)
def add_message(self, message):
# treating messages like any other task
message.proc = self
self.tasks.append(message)
def add_mapper_call(self, call):
# treating mapper calls like any other task
call.proc = self
self.tasks.append(call)
def add_runtime_call(self, call):
# treating runtime calls like any other task
call.proc = self
self.tasks.append(call)
def sort_time_range(self):
for task in self.tasks:
self.time_points.append(TimePoint(task.start, task, True))
self.time_points.append(TimePoint(task.stop, task, False))
self.util_time_points.append(TimePoint(task.start, task, True))
self.util_time_points.append(TimePoint(task.stop, task, False))
if isinstance(task, HasWaiters):
# wait intervals don't count for the util graph
for wait_interval in task.wait_intervals:
self.util_time_points.append(TimePoint(wait_interval.start,
task, False))
self.util_time_points.append(TimePoint(wait_interval.end,
task, True))
self.util_time_points.sort(key=lambda p: p.time_key)
self.time_points.sort(key=lambda p: p.time_key)
free_levels = set()
for point in self.time_points:
if point.first:
if free_levels:
point.thing.set_level(min(free_levels))
free_levels.remove(point.thing.level)
else:
self.max_levels += 1
point.thing.set_level(self.max_levels)
else:
free_levels.add(point.thing.level)
def add_initiation_dependencies(self, state, op_dependencies, transitive_map):
for point in self.time_points:
if point.first:
point.thing.add_initiation_dependencies(state, op_dependencies, transitive_map)
def attach_dependencies(self, state, op_dependencies, transitive_map):
for point in self.time_points:
if point.first:
point.thing.attach_dependencies(state, op_dependencies, transitive_map)
def emit_tsv(self, tsv_file, base_level):
# iterate over tasks in start time order
for point in self.time_points:
if point.first:
point.thing.emit_tsv(tsv_file, base_level,
self.max_levels + 1,
point.thing.level)
return base_level + max(self.max_levels, 1) + 1
def total_time(self):
total = 0
for task in self.tasks:
total += task.total_time()
return total
def active_time(self):
total = 0
for task in self.tasks:
total += task.active_time()
return total
def application_time(self):
total = 0
for task in self.tasks:
total += task.application_time()
return total
def meta_time(self):
total = 0
for task in self.tasks:
total += task.meta_time()
return total
def mapper_time(self):
total = 0
for task in self.tasks:
total += task.mapper_time()
return total
def print_stats(self, verbose):
total_time = self.total_time()
active_time = 0
application_time = 0
meta_time = 0
mapper_time = 0
active_ratio = 0.0
application_ratio = 0.0
meta_ratio = 0.0
mapper_ratio = 0.0
if total_time != 0:
active_time = self.active_time()
application_time = self.application_time()
meta_time = self.meta_time()
mapper_time = self.mapper_time()
active_ratio = 100.0*float(active_time)/float(total_time)
application_ratio = 100.0*float(application_time)/float(total_time)
meta_ratio = 100.0*float(meta_time)/float(total_time)
mapper_ratio = 100.0*float(mapper_time)/float(total_time)
if total_time != 0 or verbose:
print(self)
print(" Total time: %d us" % total_time)
print(" Active time: %d us (%.3f%%)" % (active_time, active_ratio))
print(" Application time: %d us (%.3f%%)" % (application_time, application_ratio))
print(" Meta time: %d us (%.3f%%)" % (meta_time, meta_ratio))
print(" Mapper time: %d us (%.3f%%)" % (mapper_time, mapper_ratio))
print()
def __repr__(self):
return '%s Processor %s' % (self.kind, hex(self.proc_id))
def __cmp__(a, b):
return cmp(a.proc_id, b.proc_id)
class TimePoint(object):
def __init__(self, time, thing, first):
assert time != None
self.time = time
self.thing = thing
self.first = first
self.time_key = 2*time + (0 if first is True else 1)
def __cmp__(a, b):
return cmp(a.time_key, b.time_key)
class Memory(object):
def __init__(self, mem_id, kind, capacity):
self.mem_id = mem_id
# MEMORY: tag:8 = 0x1e, owner_node:16, (unused):28, mem_idx: 12
# owner_node = mem_id[55:40]
self.node_id = (mem_id >> 40) & ((1 << 16) - 1)
self.mem_in_node = (mem_id) & ((1 << 12) - 1)
self.kind = kind
self.capacity = capacity
self.instances = set()
self.time_points = list()
self.max_live_instances = None
self.last_time = None
def get_short_text(self):
return self.kind + " Memory " + str(self.mem_in_node)
def add_instance(self, inst):
self.instances.add(inst)
inst.mem = self
def init_time_range(self, last_time):
# Fill in any of our instances that are not complete with the last time
for inst in self.instances:
if inst.stop is None:
inst.stop = last_time
self.last_time = last_time
def sort_time_range(self):
self.max_live_instances = 0
for inst in self.instances:
self.time_points.append(TimePoint(inst.start, inst, True))
self.time_points.append(TimePoint(inst.stop, inst, False))
# Keep track of which levels are free
self.time_points.sort(key=lambda p: p.time_key)
free_levels = set()
# Iterate over all the points in sorted order
for point in self.time_points:
if point.first:
# Find a level to assign this to
if len(free_levels) > 0:
point.thing.set_level(free_levels.pop())
else:
point.thing.set_level(self.max_live_instances + 1)
self.max_live_instances += 1
else:
# Finishing this instance so restore its point
free_levels.add(point.thing.level)
def emit_tsv(self, tsv_file, base_level):
max_levels = self.max_live_instances + 1
if max_levels > 1:
# iterate over tasks in start time order
max_levels = max(4, max_levels)
for point in self.time_points:
if point.first:
point.thing.emit_tsv(tsv_file, base_level,\
max_levels, point.thing.level)
return base_level + max_levels
# if max_levels > 1:
# for instance in self.instances:
# assert instance.level is not None
# assert instance.create is not None
# assert instance.destroy is not None
# inst_name = repr(instance)
# tsv_file.write("%d\t%ld\t%ld\t%s\t1.0\t%s\n" % \
# (base_level + (max_levels - instance.level),
# instance.create, instance.destroy,
# instance.get_color(), inst_name))
# return base_level + max_levels
def print_stats(self, verbose):
# Compute total and average utilization of memory
assert self.last_time is not None
average_usage = 0.0
max_usage = 0.0
current_size = 0
previous_time = 0
for point in sorted(self.time_points,key=lambda p: p.time_key):
# First do the math for the previous interval
usage = float(current_size)/float(self.capacity) if self.capacity <> 0 else 0
if usage > max_usage:
max_usage = usage
duration = point.time - previous_time
# Update the average usage
average_usage += usage * float(duration)
# Update the size
if point.first:
current_size += point.thing.size
else:
current_size -= point.thing.size
# Save the time for the next round through
previous_time = point.time
# Last interval is empty so don't worry about it
average_usage /= float(self.last_time)
if average_usage > 0.0 or verbose:
print(self)
print(" Total Instances: %d" % len(self.instances))
print(" Maximum Utilization: %.3f%%" % (100.0 * max_usage))
print(" Average Utilization: %.3f%%" % (100.0 * average_usage))
print()
def __repr__(self):
return '%s Memory %s' % (self.kind, hex(self.mem_id))
def __cmp__(a, b):
return cmp(a.mem_id, b.mem_id)
class Channel(object):
def __init__(self, src, dst):
self.src = src
self.dst = dst
self.copies = set()
self.time_points = list()
self.max_live_copies = None
self.last_time = None
def get_short_text(self):
if self.src is None:
return "Fill Channel"
else:
return "Mem to Mem Channel"
def add_copy(self, copy):
copy.chan = self
self.copies.add(copy)
def init_time_range(self, last_time):
self.last_time = last_time
def sort_time_range(self):
self.max_live_copies = 0
for copy in self.copies:
self.time_points.append(TimePoint(copy.start, copy, True))
self.time_points.append(TimePoint(copy.stop, copy, False))
# Keep track of which levels are free
self.time_points.sort(key=lambda p: p.time_key)
free_levels = set()
# Iterate over all the points in sorted order
for point in self.time_points:
if point.first:
if len(free_levels) > 0:
point.thing.level = free_levels.pop()
else:
point.thing.level = self.max_live_copies + 1
self.max_live_copies += 1
else:
# Finishing this instance so restore its point
free_levels.add(point.thing.level)
def emit_tsv(self, tsv_file, base_level):
max_levels = self.max_live_copies + 1
if max_levels > 1:
# iterate over tasks in start time order
max_levels = max(4, max_levels)
for point in self.time_points:
if point.first:
point.thing.emit_tsv(tsv_file, base_level,\
max_levels, point.thing.level)
return base_level + max_levels
# max_levels = self.max_live_copies + 1
# if max_levels > 1:
# max_levels = max(4, max_levels)
# for copy in self.copies:
# assert copy.level is not None
# assert copy.start is not None
# assert copy.stop is not None
# copy_name = repr(copy)
# tsv_file.write("%d\t%ld\t%ld\t%s\t1.0\t%s\n" % \
# (base_level + (max_levels - copy.level),
# copy.start, copy.stop,
# copy.get_color(), copy_name))
# return base_level + max_levels
def print_stats(self, verbose):
assert self.last_time is not None
total_usage_time = 0
max_transfers = 0
current_transfers = 0
previous_time = 0
for point in sorted(self.time_points,key=lambda p: p.time_key):
if point.first:
if current_transfers == 0:
previous_time = point.time
current_transfers += 1
if current_transfers > max_transfers:
max_transfers = current_transfers
else:
current_transfers -= 1
if current_transfers == 0:
total_usage_time += (point.time - previous_time)
average_usage = float(total_usage_time)/float(self.last_time)
if average_usage > 0.0 or verbose:
print(self)
print(" Total Transfers: %d" % len(self.copies))
print(" Maximum Executing Transfers: %d" % (max_transfers))
print(" Average Utilization: %.3f%%" % (100.0 * average_usage))
print()
def __repr__(self):
if self.src is None:
return 'Fill ' + self.dst.__repr__() + ' Channel'
else:
return self.src.__repr__() + ' to ' + self.dst.__repr__() + ' Channel'
def __cmp__(a, b):
return cmp(a.dst, b.dst)
class WaitInterval(object):
def __init__(self, start, ready, end):
self.start = start
self.ready = ready
self.end = end
class TaskKind(object):
def __init__(self, task_id, name):
self.task_id = task_id
self.name = name
def __repr__(self):
return self.name
class StatObject(object):
def __init__(self):
self.total_calls = defaultdict(int)
self.total_execution_time = defaultdict(int)
self.all_calls = defaultdict(list)
self.max_call = defaultdict(int)
self.min_call = defaultdict(lambda: sys.maxsize)
def get_total_execution_time(self):
total_execution_time = 0
for proc_exec_time in self.total_execution_time.itervalues():
total_execution_time += proc_exec_time
return total_execution_time
def get_total_calls(self):
total_calls = 0
for proc_calls in self.total_calls.itervalues():
total_calls += proc_calls
return total_calls
def increment_calls(self, exec_time, proc):
self.total_calls[proc] += 1
self.total_execution_time[proc] += exec_time
self.all_calls[proc].append(exec_time)
if exec_time > self.max_call[proc]:
self.max_call[proc] = exec_time
if exec_time < self.min_call[proc]:
self.min_call[proc] = exec_time
def print_task_stat(self, total_calls, total_execution_time,
max_call, max_dev, min_call, min_dev):
avg = float(total_execution_time) / float(total_calls) \
if total_calls > 0 else 0
print(' Total Invocations: %d' % total_calls)
print(' Total Time: %d us' % total_execution_time)
print(' Average Time: %.2f us' % avg)
print(' Maximum Time: %d us (%.3f sig)' % (max_call,max_dev))
print(' Minimum Time: %d us (%.3f sig)' % (min_call,min_dev))
def print_stats(self, verbose):
procs = sorted(self.total_calls.iterkeys())
total_execution_time = self.get_total_execution_time()
total_calls = self.get_total_calls()
avg = float(total_execution_time) / float(total_calls)
max_call = max(self.max_call.values())
min_call = min(self.min_call.values())
stddev = 0
for proc_calls in self.all_calls.values():
for call in proc_calls:
diff = float(call) - avg
stddev += sqrt(diff * diff)
stddev /= float(total_calls)
stddev = sqrt(stddev)
max_dev = (float(max_call) - avg) / stddev if stddev != 0.0 else 0.0
min_dev = (float(min_call) - avg) / stddev if stddev != 0.0 else 0.0
print(' '+repr(self))
self.print_task_stat(total_calls, total_execution_time,
max_call, max_dev, min_call, min_dev)
print()
if verbose and len(procs) > 1:
for proc in sorted(self.total_calls.iterkeys()):
avg = float(self.total_execution_time[proc]) / float(self.total_calls[proc]) \
if self.total_calls[proc] > 0 else 0
stddev = 0
for call in self.all_calls[proc]:
diff = float(call) - avg
stddev += sqrt(diff * diff)
stddev /= float(self.total_calls[proc])
stddev = sqrt(stddev)
max_dev = (float(self.max_call[proc]) - avg) / stddev if stddev != 0.0 else 0.0
min_dev = (float(self.min_call[proc]) - avg) / stddev if stddev != 0.0 else 0.0
print(' On ' + repr(proc))
self.print_task_stat(self.total_calls[proc],
self.total_execution_time[proc],
self.max_call[proc], max_dev,
self.min_call[proc], min_dev)
print()
class Variant(StatObject):
def __init__(self, variant_id, name):
StatObject.__init__(self)
self.variant_id = variant_id
self.name = name
self.op = dict()
self.task_kind = None
self.color = None
def __eq__(self, other):
return self.variant_id == other.variant_id
def set_task_kind(self, task_kind):
assert self.task_kind == None or self.task_kind == task_kind
self.task_kind = task_kind
def compute_color(self, step, num_steps):
assert self.color is None
self.color = color_helper(step, num_steps)
def assign_color(self, color):
assert self.color is None
self.color = color
def __repr__(self):
return self.name
class Base(object):
def __init__(self):
self.prof_uid = get_prof_uid()
self.level = None
def set_level(self, level):
self.level = level
def get_unique_tuple(self):
assert self.proc is not None
cur_level = self.proc.max_levels - self.level
return (self.proc.node_id, self.proc.proc_in_node, self.prof_uid)
def get_owner(self):
return self.proc
class Operation(Base):
def __init__(self, op_id):
Base.__init__(self)
self.op_id = op_id
self.kind_num = None
self.kind = None
self.is_task = False
self.is_meta = False
self.is_multi = False
self.is_proftask = False
self.name = 'Operation '+str(op_id)
self.variant = None
self.task_kind = None
self.color = None
self.owner = None
self.proc = None
def assign_color(self, color_map):
assert self.color is None
if self.kind is None:
self.color = '#000000' # Black
else:
assert self.kind_num in color_map
self.color = color_map[self.kind_num]
def get_color(self):
assert self.color is not None
return self.color
def get_op_id(self):
if self.is_proftask:
return ""
else:
return self.op_id
def get_info(self):
info = '<'+str(self.op_id)+">"
return info
def __repr__(self):
if self.is_task:
assert self.variant is not None
title = self.variant.task_kind.name if self.variant.task_kind is not None else 'unnamed'
if self.variant.name <> None and self.variant.name.find("unnamed") > 0:
title += ' ['+self.variant.name+']'
return title+' '+self.get_info()
elif self.is_multi:
assert self.task_kind is not None
if self.task_kind.name is not None:
return self.task_kind.name+' '+self.get_info()
else:
return 'Task '+str(self.task_kind.task_id)+' '+self.get_info()
elif self.is_proftask:
return 'ProfTask' + (' <{:d}>'.format(self.op_id) if self.op_id > 0 else '')
else:
if self.kind is None:
return 'Operation '+self.get_info()
else:
return self.kind+' Operation '+self.get_info()
class HasWaiters(object):
def __init__(self):
self.wait_intervals = list()
def add_wait_interval(self, start, ready, end):
self.wait_intervals.append(WaitInterval(start, ready, end))
def active_time(self):
active_time = 0
start = self.start
for wait_interval in self.wait_intervals:
active_time += (wait_interval.start - start)
start = max(start, wait_interval.end)
if start < self.stop:
active_time += (self.stop - start)
return active_time
def emit_tsv(self, tsv_file, base_level, max_levels, level):
title = repr(self)
initiation = str(self.initiation)
color = self.get_color()
_in = json.dumps(list(self.deps["in"])) if len(self.deps["in"]) > 0 else ""
out = json.dumps(list(self.deps["out"])) if len(self.deps["out"]) > 0 else ""
children = json.dumps(list(self.deps["children"])) if len(self.deps["children"]) > 0 else ""
parents = json.dumps(list(self.deps["parents"])) if len(self.deps["parents"]) > 0 else ""
if len(self.wait_intervals) > 0:
start = self.start
cur_level = base_level + (max_levels - level)
for wait_interval in self.wait_intervals:
init = data_tsv_str(level = cur_level,
start = start,
end = wait_interval.start,
color = color,
opacity = "1.0",
title = title,
initiation = initiation,
_in = _in,
out = out,
children = children,
parents = parents,
prof_uid = self.prof_uid)
# only write once
_in = ""
out = ""
children = ""
parents = ""
wait = data_tsv_str(level = cur_level,
start = wait_interval.start,
end = wait_interval.ready,
color = color,
opacity = "0.15",
title = title + " (waiting)",
initiation = initiation,
_in = "",
out = "",
children = "",
parents = "",
prof_uid = self.prof_uid)
ready = data_tsv_str(level = cur_level,
start = wait_interval.ready,
end = wait_interval.end,
color = color,
opacity = "0.45",
title = title + " (ready)",
initiation = initiation,
_in = "",
out = "",
children = "",
parents = "",
prof_uid = self.prof_uid)
tsv_file.write(init)
tsv_file.write(wait)
tsv_file.write(ready)
start = max(start, wait_interval.end)
if start < self.stop:
end = data_tsv_str(level = cur_level,
start = start,
end = self.stop,
color = color,
opacity = "1.0",
title = title,
initiation = initiation,
_in = "",
out = "",
children = "",
parents = "",
prof_uid = self.prof_uid)
tsv_file.write(end)
else:
line = data_tsv_str(level = base_level + (max_levels - level),
start = self.start,
end = self.stop,
color = color,
opacity = "1.0",
title = title,
initiation = initiation,
_in = _in,
out = out,
children = children,
parents = parents,
prof_uid = self.prof_uid)
tsv_file.write(line)
class Task(Operation, TimeRange, HasDependencies, HasWaiters):
def __init__(self, variant, op, create, ready, start, stop):
Operation.__init__(self, op.op_id)
HasDependencies.__init__(self)
HasWaiters.__init__(self)
TimeRange.__init__(self, create, ready, start, stop)
self.base_op = op
self.variant = variant
self.initiation = ''
self.is_task = True
def assign_color(self, color):
assert self.color is None
assert self.base_op.color is None
assert self.variant is not None
assert self.variant.color is not None
self.color = self.variant.color
self.base_op.color = self.color
def emit_tsv(self, tsv_file, base_level, max_levels, level):
return HasWaiters.emit_tsv(self, tsv_file, base_level, max_levels, level)
def get_color(self):
assert self.color is not None
return self.color
def get_op_id(self):
return self.op_id
def get_info(self):
info = '<'+str(self.op_id)+">"
return info
def active_time(self):
return HasWaiters.active_time(self)
def application_time(self):
return self.total_time()