analyse_tasks.py 14.4 KB
Newer Older
1
2
3
#!/usr/bin/env python
"""
Usage:
Peter W. Draper's avatar
Typo    
Peter W. Draper committed
4
    analyse_tasks.py [options] input.dat
5

6
7
8
where input.dat is a thread info file for a step (MPI or non-MPI). Use the
'-y interval' flag of the swift and swift_mpi commands to create these
(you will also need to configure with the --enable-task-debugging option).
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

The output is an analysis of the task timings, including deadtime per thread
and step, total amount of time spent for each task type, for the whole step
and per thread and the minimum and maximum times spent per task type.

This file is part of SWIFT.
Copyright (c) 2017 Peter W. Draper (p.w.draper@durham.ac.uk)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import matplotlib
32

33
34
35
36
37
38
39
40
41
42
43
matplotlib.use("Agg")
import matplotlib.collections as collections
import matplotlib.ticker as plticker
import pylab as pl
import sys
import argparse

#  Handle the command line.
parser = argparse.ArgumentParser(description="Analyse task dumps")

parser.add_argument("input", help="Thread data file (-y output)")
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
parser.add_argument(
    "-v",
    "--verbose",
    dest="verbose",
    help="Verbose output (default: False)",
    default=False,
    action="store_true",
)
parser.add_argument(
    "-r",
    "--rank",
    dest="rank",
    help="Rank to process (default: all)",
    default="all",
    action="store",
)
60
61
62
63
64

args = parser.parse_args()
infile = args.input

#  Tasks and subtypes. Indexed as in tasks.h.
65
66
67
68
69
70
71
72
73
74
75
76
77
78
TASKTYPES = [
    "none",
    "sort",
    "self",
    "pair",
    "sub_self",
    "sub_pair",
    "init_grav",
    "init_grav_out",
    "ghost_in",
    "ghost",
    "ghost_out",
    "extra_ghost",
    "drift_part",
79
    "drift_spart",
80
    "drift_gpart",
Matthieu Schaller's avatar
Matthieu Schaller committed
81
    "drift_gpart_out",
82
    "hydro_end_force",
83
84
85
    "kick1",
    "kick2",
    "timestep",
86
    "timestep_limiter",
87
88
89
90
91
92
93
    "send",
    "recv",
    "grav_long_range",
    "grav_mm",
    "grav_down_in",
    "grav_down",
    "grav_mesh",
94
    "grav_end_force",
95
96
    "cooling",
    "star_formation",
97
98
    "star_formation_in",
    "star_formation_out",
99
    "logger",
100
101
    "stars_in",
    "stars_out",
102
103
104
    "stars_ghost_in",
    "stars_ghost",
    "stars_ghost_out",
105
    "stars_sort",
106
107
108
109
110
111
112
113
    "count",
]

SUBTYPES = [
    "none",
    "density",
    "gradient",
    "force",
114
    "limiter",
115
116
    "grav",
    "external_grav",
117
118
119
    "tend_part",
    "tend_gpart",
    "tend_spart",
120
121
122
123
124
125
    "xv",
    "rho",
    "gpart",
    "multipole",
    "spart",
    "stars_density",
Alexei Borissov's avatar
Alexei Borissov committed
126
    "stars_feedback",
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    "count",
]

SIDS = [
    "(-1,-1,-1)",
    "(-1,-1, 0)",
    "(-1,-1, 1)",
    "(-1, 0,-1)",
    "(-1, 0, 0)",
    "(-1, 0, 1)",
    "(-1, 1,-1)",
    "(-1, 1, 0)",
    "(-1, 1, 1)",
    "( 0,-1,-1)",
    "( 0,-1, 0)",
    "( 0,-1, 1)",
    "( 0, 0,-1)",
]
145

146
#  Read input.
147
148
data = pl.loadtxt(infile)
full_step = data[0, :]
149

150
#  Do we have an MPI file?
151
full_step = data[0, :]
152
if full_step.size == 13:
153
    print("# MPI mode")
154
    mpimode = True
155
    nranks = int(max(data[:, 0])) + 1
156
    print("# Number of ranks:", nranks)
157
158
159
160
161
162
163
164
165
166
    rankcol = 0
    threadscol = 1
    taskcol = 2
    subtaskcol = 3
    ticcol = 5
    toccol = 6
    updates = int(full_step[7])
    g_updates = int(full_step[8])
    s_updates = int(full_step[9])
else:
167
    print("# non MPI mode")
168
169
170
171
172
173
174
175
176
177
178
179
180
    nranks = 1
    mpimode = False
    rankcol = -1
    threadscol = 0
    taskcol = 1
    subtaskcol = 2
    ticcol = 4
    toccol = 5
    updates = int(full_step[6])
    g_updates = int(full_step[7])
    s_updates = int(full_step[8])

#  Get the CPU clock to convert ticks into milliseconds.
181
182
CPU_CLOCK = float(full_step[-1]) / 1000.0
if args.verbose:
183
184
185
186
    print("# CPU frequency:", CPU_CLOCK * 1000.0)
print("#   updates:", updates)
print("# g_updates:", g_updates)
print("# s_updates:", s_updates)
187
188
189

if mpimode:
    if args.rank == "all":
190
        ranks = list(range(nranks))
191
192
193
    else:
        ranks = [int(args.rank)]
        if ranks[0] >= nranks:
194
            print("Error: maximum rank is " + str(nranks - 1))
195
196
197
198
            sys.exit(1)
else:
    ranks = [1]

199
maxthread = int(max(data[:, threadscol])) + 1
200
print("# Maximum thread id:", maxthread)
201

202
#  Avoid start and end times of zero.
203
204
sdata = data[data[:, ticcol] != 0]
sdata = data[data[:, toccol] != 0]
205
206
207
208

#  Now we process the required ranks.
for rank in ranks:
    if mpimode:
209
        print("# Rank", rank)
210
211
        data = sdata[sdata[:, rankcol] == rank]
        full_step = data[0, :]
212
213
214
215
216
217
    else:
        data = sdata

    #  Recover the start and end time
    tic_step = int(full_step[ticcol])
    toc_step = int(full_step[toccol])
218
    data = data[1:, :]
219
220

    #  Avoid start and end times of zero.
221
222
    data = data[data[:, ticcol] != 0]
    data = data[data[:, toccol] != 0]
223
224

    #  Calculate the time range.
225
    total_t = (toc_step - tic_step) / CPU_CLOCK
226
227
    print("# Data range: ", total_t, "ms")
    print()
228
229
230

    #  Correct times to relative values.
    start_t = float(tic_step)
231
232
    data[:, ticcol] -= start_t
    data[:, toccol] -= start_t
233
234
235
236
237
238
239
240
241
242
    end_t = (toc_step - start_t) / CPU_CLOCK

    tasks = {}
    tasks[-1] = []
    for i in range(maxthread):
        tasks[i] = []

    #  Gather into by thread data.
    num_lines = pl.shape(data)[0]
    for line in range(num_lines):
243
244
245
246
247
        thread = int(data[line, threadscol])
        tic = int(data[line, ticcol]) / CPU_CLOCK
        toc = int(data[line, toccol]) / CPU_CLOCK
        tasktype = int(data[line, taskcol])
        subtype = int(data[line, subtaskcol])
248
249
        sid = int(data[line, -1])

250
        tasks[thread].append([tic, toc, tasktype, subtype, sid])
251
252
253
254

    #  Sort by tic and gather used threads.
    threadids = []
    for i in range(maxthread):
255
256
        tasks[i] = sorted(tasks[i], key=lambda task: task[0])
        threadids.append(i)
257

258
    #  Times per task.
259
260
    print("# Task times:")
    print("# -----------")
261
262
263
264
265
    print(
        "# {0:<17s}: {1:>7s} {2:>9s} {3:>9s} {4:>9s} {5:>9s} {6:>9s}".format(
            "type/subtype", "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

    alltasktimes = {}
    sidtimes = {}
    for i in threadids:
        tasktimes = {}
        for task in tasks[i]:
            key = TASKTYPES[task[2]] + "/" + SUBTYPES[task[3]]
            dt = task[1] - task[0]
            if not key in tasktimes:
                tasktimes[key] = []
            tasktimes[key].append(dt)

            if not key in alltasktimes:
                alltasktimes[key] = []
            alltasktimes[key].append(dt)

            my_sid = task[4]
            if my_sid > -1:
                if not my_sid in sidtimes:
                    sidtimes[my_sid] = []
                sidtimes[my_sid].append(dt)

288
        print("# Thread : ", i)
289
290
291
292
        for key in sorted(tasktimes.keys()):
            taskmin = min(tasktimes[key])
            taskmax = max(tasktimes[key])
            tasksum = sum(tasktimes[key])
293
294
295
296
297
298
299
300
301
302
303
            print(
                "{0:19s}: {1:7d} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.4f} {6:9.2f}".format(
                    key,
                    len(tasktimes[key]),
                    taskmin,
                    taskmax,
                    tasksum,
                    tasksum / len(tasktimes[key]),
                    tasksum / total_t * 100.0,
                )
            )
304
        print()
305

306
    print("# All threads : ")
307
308
309
310
    for key in sorted(alltasktimes.keys()):
        taskmin = min(alltasktimes[key])
        taskmax = max(alltasktimes[key])
        tasksum = sum(alltasktimes[key])
311
312
313
314
315
316
317
318
319
320
321
        print(
            "{0:18s}: {1:7d} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.4f} {6:9.2f}".format(
                key,
                len(alltasktimes[key]),
                taskmin,
                taskmax,
                tasksum,
                tasksum / len(alltasktimes[key]),
                tasksum / (len(threadids) * total_t) * 100.0,
            )
        )
322
    print()
323

324
    # For pairs, show stuff sorted by SID
325
    print("# By SID (all threads): ")
326
327
328
329
330
    print(
        "# {0:<17s}: {1:>7s} {2:>9s} {3:>9s} {4:>9s} {5:>9s} {6:>9s}".format(
            "Pair/Sub-pair SID", "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
331

332
    for sid in range(0, 13):
333
334
335
336
337
338
339
        if sid in sidtimes:
            sidmin = min(sidtimes[sid])
            sidmax = max(sidtimes[sid])
            sidsum = sum(sidtimes[sid])
            sidcount = len(sidtimes[sid])
            sidmean = sidsum / sidcount
        else:
340
341
342
            sidmin = 0.0
            sidmax = 0.0
            sidsum = 0.0
343
            sidcount = 0
344
345
346
347
348
349
350
351
352
353
354
355
356
            sidmean = 0.0
        print(
            "{0:3d} {1:15s}: {2:7d} {3:9.4f} {4:9.4f} {5:9.4f} {6:9.4f} {7:9.2f}".format(
                sid,
                SIDS[sid],
                sidcount,
                sidmin,
                sidmax,
                sidsum,
                sidmean,
                sidsum / (len(threadids) * total_t) * 100.0,
            )
        )
357
    print()
358

359
    #  Dead times.
360
361
362
363
    print("# Times not in tasks (deadtimes)")
    print("# ------------------------------")
    print("# Time before first task:")
    print("# no.    : {0:>9s} {1:>9s}".format("value", "percent"))
364
365
366
367
    predeadtimes = []
    for i in threadids:
        if len(tasks[i]) > 0:
            predeadtime = tasks[i][0][0]
368
369
370
371
372
            print(
                "thread {0:2d}: {1:9.4f} {2:9.4f}".format(
                    i, predeadtime, predeadtime / total_t * 100.0
                )
            )
373
374
375
376
377
378
379
            predeadtimes.append(predeadtime)
        else:
            predeadtimes.append(0.0)

    predeadmin = min(predeadtimes)
    predeadmax = max(predeadtimes)
    predeadsum = sum(predeadtimes)
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
    print(
        "#        : {0:>9s} {1:>9s} {2:>9s} {3:>9s} {4:>9s} {5:>9s}".format(
            "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
    print(
        "all      : {0:9d} {1:9.4f} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.2f}".format(
            len(predeadtimes),
            predeadmin,
            predeadmax,
            predeadsum,
            predeadsum / len(predeadtimes),
            predeadsum / (len(threadids) * total_t) * 100.0,
        )
    )
395
    print()
396

397
398
    print("# Time after last task:")
    print("# no.    : {0:>9s} {1:>9s}".format("value", "percent"))
399
400
401
402
    postdeadtimes = []
    for i in threadids:
        if len(tasks[i]) > 0:
            postdeadtime = total_t - tasks[i][-1][1]
403
404
405
406
407
            print(
                "thread {0:2d}: {1:9.4f} {2:9.4f}".format(
                    i, postdeadtime, postdeadtime / total_t * 100.0
                )
            )
408
409
410
411
412
413
414
            postdeadtimes.append(postdeadtime)
        else:
            postdeadtimes.append(0.0)

    postdeadmin = min(postdeadtimes)
    postdeadmax = max(postdeadtimes)
    postdeadsum = sum(postdeadtimes)
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    print(
        "#        : {0:>9s} {1:>9s} {2:>9s} {3:>9s} {4:>9s} {5:>9s}".format(
            "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
    print(
        "all      : {0:9d} {1:9.4f} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.2f}".format(
            len(postdeadtimes),
            postdeadmin,
            postdeadmax,
            postdeadsum,
            postdeadsum / len(postdeadtimes),
            postdeadsum / (len(threadids) * total_t) * 100.0,
        )
    )
430
    print()
431
432

    #  Time in engine, i.e. from first to last tasks.
433
    print("# Time between tasks (engine deadtime):")
434
435
436
437
438
    print(
        "# no.    : {0:>9s} {1:>9s} {2:>9s} {3:>9s} {4:>9s} {5:>9s}".format(
            "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    enginedeadtimes = []
    for i in threadids:
        deadtimes = []
        if len(tasks[i]) > 0:
            last = tasks[i][0][0]
        else:
            last = 0.0
        for task in tasks[i]:
            dt = task[0] - last
            deadtimes.append(dt)
            last = task[1]

        #  Drop first value, last value already gone.
        if len(deadtimes) > 1:
            deadtimes = deadtimes[1:]
        else:
            #  Only one or fewer tasks, so no deadtime by definition.
            deadtimes = [0.0]

        deadmin = min(deadtimes)
        deadmax = max(deadtimes)
        deadsum = sum(deadtimes)
461
462
463
464
465
466
467
468
469
470
471
        print(
            "thread {0:2d}: {1:9d} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.4f} {6:9.2f}".format(
                i,
                len(deadtimes),
                deadmin,
                deadmax,
                deadsum,
                deadsum / len(deadtimes),
                deadsum / total_t * 100.0,
            )
        )
472
473
474
475
476
        enginedeadtimes.extend(deadtimes)

    deadmin = min(enginedeadtimes)
    deadmax = max(enginedeadtimes)
    deadsum = sum(enginedeadtimes)
477
478
479
480
481
482
483
484
485
486
    print(
        "all      : {0:9d} {1:9.4f} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.2f}".format(
            len(enginedeadtimes),
            deadmin,
            deadmax,
            deadsum,
            deadsum / len(enginedeadtimes),
            deadsum / (len(threadids) * total_t) * 100.0,
        )
    )
487
    print()
488
489

    #  All times in step.
490
    print("# All deadtimes:")
491
492
493
494
495
    print(
        "# no.    : {0:>9s} {1:>9s} {2:>9s} {3:>9s} {4:>9s} {5:>9s}".format(
            "count", "minimum", "maximum", "sum", "mean", "percent"
        )
    )
496
497
498
499
500
501
502
503
504
    alldeadtimes = []
    for i in threadids:
        deadtimes = []
        last = 0
        for task in tasks[i]:
            dt = task[0] - last
            deadtimes.append(dt)
            last = task[1]
        dt = total_t - last
505
        deadtimes.append(dt)
506
507
508
509

        deadmin = min(deadtimes)
        deadmax = max(deadtimes)
        deadsum = sum(deadtimes)
510
511
512
513
514
515
516
517
518
519
520
        print(
            "thread {0:2d}: {1:9d} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.4f} {6:9.2f}".format(
                i,
                len(deadtimes),
                deadmin,
                deadmax,
                deadsum,
                deadsum / len(deadtimes),
                deadsum / total_t * 100.0,
            )
        )
521
522
523
524
525
        alldeadtimes.extend(deadtimes)

    deadmin = min(alldeadtimes)
    deadmax = max(alldeadtimes)
    deadsum = sum(alldeadtimes)
526
527
528
529
530
531
532
533
534
535
    print(
        "all      : {0:9d} {1:9.4f} {2:9.4f} {3:9.4f} {4:9.4f} {5:9.2f}".format(
            len(alldeadtimes),
            deadmin,
            deadmax,
            deadsum,
            deadsum / len(alldeadtimes),
            deadsum / (len(threadids) * total_t) * 100.0,
        )
    )
536
    print()
537
538

sys.exit(0)