about summary refs log tree commit diff stats
path: root/archive/2024/winter/bsc_dichler/scripts/plot/malloc.py
blob: 50a21aa18bb5e796180bd370996f0975fc350217 (plain) (blame)
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
from pathlib import Path
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


def run(csv_file1, csv_file2, duration_type, output, format):
    df1 = pd.read_csv(csv_file1, sep=";")
    df2 = pd.read_csv(csv_file2, sep=";")

    def norm(df):
        df["duration_allocation"] = df["duration_allocation"] / df["allocation"] / 1000
        df["duration_deallocation"] = (
            df["duration_deallocation"] / df["allocation"] / 1000
        )

    norm(df1)
    norm(df2)

    grouped1 = (
        df1.groupby("size")[["duration_allocation", "duration_deallocation"]]
        .agg(["mean", "std"])
        .reset_index()
    )
    grouped2 = (
        df2.groupby("size")[["duration_allocation", "duration_deallocation"]]
        .agg(["mean", "std"])
        .reset_index()
    )

    _, ax1 = plt.subplots(figsize=(12, 6))

    library = [
        "16 Bytes",
        "128 Bytes",
        "256 Bytes",
        "1024 Bytes",
        "2048 Bytes",
        "4096 Bytes",
        "8192 Bytes",
    ]
    x = np.arange(len(library))
    bar_width = 0.35

    bars1 = ax1.bar(
        x - bar_width / 2,
        grouped1[duration_type]["mean"],
        yerr=grouped1[duration_type]["std"],
        width=bar_width,
        capsize=5,
        label="MTE disabled",
        color="#a6bddb",
        edgecolor="black",
        linewidth=2,
    )
    bars2 = ax1.bar(
        x + bar_width / 2,
        grouped2[duration_type]["mean"],
        yerr=grouped2[duration_type]["std"],
        width=bar_width,
        capsize=5,
        label="MTE enabled",
        color="#fc9272",
        edgecolor="black",
        linewidth=2,
    )

    for i in range(len(bars1)):
        height2 = bars2[i].get_height()
        height1 = bars1[i].get_height()

        ax1.annotate(
            "",
            xy=(x[i] - bar_width / 2, height2),
            xytext=(x[i] - bar_width / 2, height1),
            arrowprops=dict(arrowstyle="->", color="red", lw=2),
            color="red",
            ha="center",
        )

        difference = height2 / height1
        ax1.text(
            i - bar_width / 2 - 0.04,
            height1 + ((height2 - height1) / 2) - 0.08,
            f"{difference:.2f}×",
            color="red",
            fontweight="bold",
            bbox=dict(facecolor="white", alpha=1.0, edgecolor="none"),
            ha="center",
        )

    plt.ylabel("Time (µs)")
    ax1.set_xlabel("Batch size")
    ax1.legend(loc="upper left")
    plt.title("Lower is better ↓", color="navy")

    ax1.spines["top"].set_visible(False)
    ax1.spines["right"].set_visible(False)
    plt.tight_layout()

    plt.xticks(x, library)
    plt.tight_layout()
    plt.savefig(output, format=format)


def plot(output_root: Path, format):
    output_root = output_root / Path("malloc")

    run(
        output_root / Path("malloc.csv"),
        output_root / Path("malloc_mte.csv"),
        "duration_allocation",
        output_root / Path(f"result-alloc.{format}"),
        format,
    )
    run(
        output_root / Path("malloc.csv"),
        output_root / Path("malloc_mte.csv"),
        "duration_deallocation",
        output_root / Path(f"result-dealloc.{format}"),
        format,
    )