aboutsummaryrefslogtreecommitdiff
path: root/falsesharing.py
blob: 05a193e9862a3ce3c55894309b0927bdab20dbab (plain)
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
import csv
import pickle
import matplotlib.pyplot as plt
import multiprocessing
import numpy as np
import os
import re
import subprocess

from benchmark import Benchmark

time_re = re.compile("^Time elapsed = (?P<time>\d*\.\d*) seconds.$")

class Benchmark_Falsesharing( Benchmark ):
    def __init__(self):
        self.name = "falsesharing"
        self.descrition = """This benchmarks makes small allocations and writes
                            to them multiple times. If the allocated objects are
                            on the same cache line the writes will be expensive because
                            of cache thrashing."""

        self.cmd = "build/cache-{bench}{binary_suffix} {threads} 100 8 1000000"

        self.args = {
                        "bench" : ["thrash", "scratch"],
                        "threads" : range(1, multiprocessing.cpu_count() * 2 + 1)
                    }

        self.requirements = ["build/cache-thrash", "build/cache-scratch"]
        super().__init__()

    def process_stdout(self, result, stdout, verbose):
        result["time"] = time_re.match(stdout).group("time")

    def summary(self, sd=None):
        # Speedup thrash
        args = self.results["args"]
        nthreads = args["threads"]
        targets = self.results["targets"]

        sd = sd or ""

        for bench in self.results["args"]["bench"]:
            for target in targets:
                y_vals = []

                single_threaded_perm = self.Perm(bench=bench, threads=1)
                single_threaded = np.mean([float(m["time"])
                                                    for m in self.results[target][single_threaded_perm]])

                for perm in self.iterate_args_fixed({"bench" : bench}, args=args):

                    d = [float(m["time"]) for m in self.results[target][perm]]

                    y_vals.append(single_threaded / np.mean(d))

                plt.plot(nthreads, y_vals, marker='.', linestyle='-', label=target,
                            color=targets[target]["color"])

            plt.legend()
            plt.xlabel("threads")
            plt.ylabel("speedup")
            plt.title(bench + " speedup" )
            plt.savefig(os.path.join(sd, self.name + "." + bench + ".png"))
            plt.clf()

            for target in targets:
                y_vals = []

                for perm in self.iterate_args_fixed({"bench" : bench}, args=args):
                    l1_load_misses = []

                    for m in self.results[target][perm]:
                        misses = 0
                        loads = 0
                        for e in m:
                            if "L1-dcache-load-misses" in e:
                                misses = float(m[e])
                            elif "L1-dcache-loads" in e:
                                loads = float(m[e])
                        l1_load_misses.append(misses/loads)

                    y_vals.append(np.mean(l1_load_misses) * 100)

                plt.plot(nthreads, y_vals, marker='.', linestyle='-', label=target, color=targets[target]["color"])
            plt.legend()
            plt.xlabel("threads")
            plt.ylabel("l1-cache-misses in %")
            plt.title(bench + " cache-misses")
            plt.savefig(os.path.join(sd, self.name + "." + bench + ".l1misses.png"))
            plt.clf()

falsesharing = Benchmark_Falsesharing()