File: plot_performance.py

package info (click to toggle)
cp2k 2025.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 372,052 kB
  • sloc: fortran: 963,262; ansic: 64,495; f90: 21,676; python: 14,419; sh: 11,382; xml: 2,173; makefile: 953; pascal: 845; perl: 492; cpp: 345; lisp: 297; csh: 16
file content (66 lines) | stat: -rwxr-xr-x 2,206 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

# author: Ole Schuett

import re
import sys
from collections import OrderedDict


# ======================================================================================
def main():
    if len(sys.argv) < 4 or (len(sys.argv) - 1) % 3 != 0:
        print(
            "Usage: plot_performance.py <title_1> <plot_1> <file_1> ... "
            "<title_N> <plot_N> <file_N>"
        )
        sys.exit(1)

    titles, plots, timings = [], [], []
    for i in range((len(sys.argv) - 1) // 3):
        titles.append(sys.argv[3 * i + 1])
        plots.append(sys.argv[3 * i + 2])
        timings.append(parse_timings(sys.argv[3 * i + 3]))

    routines = list(set(r for t in timings for r in list(t.keys())[:6]))
    routines.remove("total")
    routines.sort(reverse=True, key=lambda r: timings[0].get(r, 0.0))

    for titel, plot, timing in zip(titles, plots, timings):
        timing["rest"] = timing["total"] - sum([timing.get(r, 0.0) for r in routines])

        full_title = f"Timings of {titel}"
        print(f'Plot: name="{plot}", title="{full_title}", ylabel="time [s]"')
        for r in ["rest"] + routines:
            t = timing.get(r, 0.0)
            print(f'PlotPoint: plot="{plot}", name="{r}", label="{r}", y={t}, yerr=0.0')
        print("")


# ======================================================================================
def parse_timings(out_fn):
    output = open(out_fn, encoding="utf8").read()

    pattern = r"\n( -+\n - +-\n - +T I M I N G +-\n([^\n]*\n){4}.*? -+)\n"
    match = re.search(pattern, output, re.DOTALL)
    report_lines = match.group(1).split("\n")[7:-1]
    print("\nFrom {}:\n{}\n".format(out_fn, match.group(0).strip()))

    # Extract average self time.
    timings = {}
    for line in report_lines:
        parts = line.split()
        timings[parts[0]] = float(parts[3])

    # Add total runtime.
    assert report_lines[0].split()[0] == "CP2K"
    timings["total"] = float(report_lines[0].split()[5])

    # Sort by time, longest first.
    return OrderedDict(sorted(timings.items(), reverse=True, key=lambda kv: kv[1]))


# ======================================================================================
main()

# EOF