diff --git a/analysis/fig_milestones.png b/analysis/fig_milestones.png index eb03123..625b0c5 100644 Binary files a/analysis/fig_milestones.png and b/analysis/fig_milestones.png differ diff --git a/analysis/fig_tracks.png b/analysis/fig_tracks.png index 4b03f75..6641be5 100644 Binary files a/analysis/fig_tracks.png and b/analysis/fig_tracks.png differ diff --git a/analysis/plots.py b/analysis/plots.py index 9895710..e1ec119 100644 --- a/analysis/plots.py +++ b/analysis/plots.py @@ -1,6 +1,7 @@ import pandas as pd import numpy as np import matplotlib.pyplot as plt +import matplotlib.patheffects as pe #import seaborn as sns # tried it, the default look was worse df = pd.read_csv("analysis/results.csv") @@ -19,6 +20,61 @@ def getline(v): s = df[df.variant == v].sort_values("n") return s.n.values, s["flops_per_cycle"].values +def label_lines_above(ax, entries, fontsize=9, rotate=False, dy_frac=0.012, halo=0): + """Place each line's description on the line. + entries: (x, y, label, color) with two optional extra elements: + [4] xpos -> pin the label to that data-x (else labels are spread out) + [5] side -> "above" (default) or "below" to put the text under the line + Set halo>0 to draw a white outline behind the text. Call AFTER + set_xlim/set_ylim and tight_layout so any rotation angle is correct. + """ + logx = ax.get_xscale() == "log" + logy = ax.get_yscale() == "log" + fx = (lambda v: np.log10(v)) if logx else (lambda v: v) + fxi = (lambda v: 10**v) if logx else (lambda v: v) + fy = (lambda v: np.log10(v)) if logy else (lambda v: v) + fyi = (lambda v: 10**v) if logy else (lambda v: v) + + ent = [] + for e in entries: + x, y, lab, c = e[0], e[1], e[2], e[3] + if len(x) == 0: + continue + ent.append((np.asarray(x, float), np.asarray(y, float), lab, c, + e[4] if len(e) > 4 else None, + e[5] if len(e) > 5 else "above")) + + n = len(ent) + y0, y1 = ax.get_ylim() + dy = (fy(y1) - fy(y0)) * dy_frac + + for i, (x, y, lab, c, xpos, side) in enumerate(ent): + xlo, xhi = fx(x.min()), fx(x.max()) + if xpos is None: # spread across lines + frac = (i + 1) / (n + 1) + xt = fxi(xlo + frac * (xhi - xlo)) + else: + xt = xpos + yt = fyi(np.interp(fx(xt), fx(x), fy(y))) # y on the line at xt + + rot = 0 + if rotate: # match slope in pixels + dxt = fxi(fx(xt) + 0.01 * (xhi - xlo)) + ya = fyi(np.interp(fx(xt), fx(x), fy(y))) + yb = fyi(np.interp(fx(dxt), fx(x), fy(y))) + p1 = ax.transData.transform((xt, ya)) + p2 = ax.transData.transform((dxt, yb)) + rot = np.degrees(np.arctan2(p2[1] - p1[1], p2[0] - p1[0])) + + below = side == "below" + ypos = fyi(fy(yt) - dy) if below else fyi(fy(yt) + dy) + va = "top" if below else "bottom" + txt = ax.text(xt, ypos, lab, color=c, fontsize=fontsize, + ha="center", va=va, fontweight="bold", + rotation=rot, rotation_mode="anchor", clip_on=False, zorder=5) + if halo: + txt.set_path_effects([pe.withStroke(linewidth=halo, foreground="white")]) + base = get("variant_baseline") @@ -33,9 +89,11 @@ tracks = [ ] fig, ax = plt.subplots(figsize=(9,4.7)) +ends = [] for v,lab,c,ls in tracks: x,y = getline(v) - ax.plot(x, y, ls, color=c, marker="o", ms=4, lw= 2.4 if c==maroon else 1.8, label=lab) + ax.plot(x, y, ls, color=c, marker="o", ms=4, lw= 2.4 if c==maroon else 1.8) + ends.append((x, y, lab, c)) for yv,t in [(PEAK_D,"double peak (16)"), (PEAK_F,"float peak (32)")]: ax.axhline(yv, ls=":", color="#999", lw=1.2) @@ -50,8 +108,8 @@ ax.set_xlabel("number of pedestrians n") ax.set_ylabel("performance [flops/cycle]") ax.grid(color="#e9e9e9") ax.spines["top"].set_visible(False); ax.spines["right"].set_visible(False) -ax.legend(fontsize=9, loc="lower left", framealpha=.95) plt.tight_layout() +label_lines_above(ax, ends, fontsize=7.5, dy_frac=0.012) plt.savefig("analysis/fig_tracks.png", dpi=170, bbox_inches="tight") @@ -143,25 +201,29 @@ plt.savefig("analysis/fig_exp.png", dpi=170, bbox_inches="tight") # plot 5: the milestone ladder as lines (this is plot0 + line1, line2, ... for the doc). # one line per relevant optimization step across n, baseline at the bottom. # unlike fig_tracks this includes the scalar steps so you can see where each jump comes from. +# tuple: (variant, label, color, linestyle, xpos|None, side) where side is +# "above" or "below" -> alternate the crowded float labels above/below their line. milestones = [ - ("variant_baseline", "baseline", "#7a7a7a", "-"), - ("variant_cutoff_callsite", "+ cutoff (best scalar)", "#9e8f2f", "-"), - ("variant_inlining_7_SoA_AVX2", "AVX2 (double)", "#3b6fb0", "--"), - ("variant_inlining_7_SoA_AVX2_polyexp", "+ polyexp (double)", "#2f9e8f", "--"), - ("variant_inlining_8_SoA_AVX512_polyexp_moreavx", "AVX-512 (double)", "#2f5d9e", "-"), - ("variant_float_inlining_SoA_AVX2_polyexp", "float AVX2 + polyexp", "#e58aa6", "--"), - ("variant_float_inlining_SoA_AVX2_polyexp_rsqrt_loopopt", "float AVX2 + rsqrt", "#e08a1e", "--"), - ("variant_float_inlining_SoA_AVX512_polyexp_moreavx", "float AVX-512", "#c0436a", "-"), - ("variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt","float AVX-512 + rsqrt + ILP (best)", maroon, "-"), + ("variant_baseline", "baseline", "#7a7a7a", "-", None, "above"), + ("variant_cutoff_callsite", "baseline + cutoff (best scalar)", "#9e8f2f", "-", None, "below"), + ("variant_inlining_7_SoA_AVX2", "AVX2 (double)", "#3b6fb0", "--", None, "above"), + ("variant_inlining_7_SoA_AVX2_polyexp", "AVX2 + polyexp (double)", "#2f9e8f", "--", None, "below"), + ("variant_inlining_8_SoA_AVX512_polyexp_moreavx", "AVX-512 (double)", "#2f5d9e", "-", None, "below"), + ("variant_float_inlining_SoA_AVX2_polyexp", "float AVX2 + polyexp", "#e58aa6", "--", 600, "above"), + ("variant_float_inlining_SoA_AVX2_polyexp_rsqrt_loopopt", "float AVX2 + rsqrt", "#e08a1e", "--", 2200, "below"), + ("variant_float_inlining_SoA_AVX512_polyexp_moreavx", "float AVX-512", "#c0436a", "-", 1500, "above"), + ("variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt","float AVX-512 + rsqrt + ILP (best)", maroon, "-", 900, "above"), ] fig, ax = plt.subplots(figsize=(9.5,5.2)) -for v,lab,c,ls in milestones: +ends = [] +for v,lab,c,ls,xpos,side in milestones: x,y = getline(v) if len(x) == 0: # variant not in results.csv -> skip instead of crashing print("skip (not in csv):", v) continue - ax.plot(x, y, ls, color=c, marker="o", ms=4, lw=2.4 if c==maroon else 1.7, label=lab) + ax.plot(x, y, ls, color=c, marker="o", ms=4, lw=2.4 if c==maroon else 1.7) + ends.append((x, y, lab, c, xpos, side)) for yv,t in [(PEAK_D,"double peak (16)"), (PEAK_F,"float peak (32)")]: ax.axhline(yv, ls=":", color="#999", lw=1.2) @@ -176,8 +238,8 @@ ax.set_xlabel("number of pedestrians n") ax.set_ylabel("performance [flops/cycle]") ax.grid(color="#e9e9e9") ax.spines["top"].set_visible(False); ax.spines["right"].set_visible(False) -ax.legend(fontsize=8.5, loc="upper left", ncol=2, framealpha=.95) plt.tight_layout() +label_lines_above(ax, ends, fontsize=6.5, dy_frac=0.02, halo=0) plt.savefig("analysis/fig_milestones.png", dpi=170, bbox_inches="tight")