PapiRunner.kt 6.36 KB
Newer Older
Noric Couderc's avatar
Noric Couderc committed
1
2
package se.lth.cs

3
import org.openjdk.jmh.infra.Blackhole
Noric Couderc's avatar
Noric Couderc committed
4
5
import papi.EventSet
import papi.Papi
6
import se.lth.cs.bcgen.BCBenchmarkPackage
Noric Couderc's avatar
Noric Couderc committed
7
import se.lth.cs.papicounters.EventSetBuilder
8
import se.lth.cs.papicounters.PAPICounter
9
import se.lth.cs.papicounters.PapiBenchmarkAnalyzer
10
import se.lth.cs.timing.OperationType
Noric Couderc's avatar
Noric Couderc committed
11
import se.lth.cs.util.FeatureSet
12
import java.io.FileWriter
Noric Couderc's avatar
Noric Couderc committed
13

14
open class PapiRunner() : PapiBenchmarkAnalyzer {
15

16
17
18
    init {
        Papi.init()
    }
19

20
21
    protected var blackhole = Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous.")

Noric Couderc's avatar
Noric Couderc committed
22
23
24
25
    /**
     * Empty benchmark:
     * Test to see if the results are stable.
     */
Noric Couderc's avatar
Noric Couderc committed
26
    fun emptyBenchmark(counters : FeatureSet): Map<PAPICounter, List<Long>> {
Noric Couderc's avatar
Noric Couderc committed
27
28
        // For each counter,
        // we store the values for each run (10 runs)
29
        var data: MutableMap<PAPICounter, List<Long>> = mutableMapOf()
30
        val eb = EventSetBuilder(counters)
Noric Couderc's avatar
Noric Couderc committed
31

Noric Couderc's avatar
Noric Couderc committed
32
33
        for (counter in eb.getCounterSet()) {
            val evset = eb.getEventSet()
Noric Couderc's avatar
Noric Couderc committed
34

Noric Couderc's avatar
Noric Couderc committed
35
            val current: MutableList<Long> = mutableListOf()
Noric Couderc's avatar
Noric Couderc committed
36

Noric Couderc's avatar
Noric Couderc committed
37
            for (warmup in 0..100) {
Noric Couderc's avatar
Noric Couderc committed
38
39
40
41
42
43
44
45
                val a = (0..warmup).toList().toTypedArray()
                val b = IntArray(warmup)

                evset.start()
                // Synthetic piece of code to see if counters run as expected
                var acc = 0
                for (i in 0 until warmup) {
                    acc += a[i]
Noric Couderc's avatar
Noric Couderc committed
46
                    if (acc % 2 == 1) {
Noric Couderc's avatar
Noric Couderc committed
47
48
49
50
51
52
53
54
                        b[i] = acc
                    }
                }
                evset.stop()
                // Get data
                val currentData = evset.counters
                current.addAll(currentData.toList())
            }
55
            data.set(counter, current)
Noric Couderc's avatar
Noric Couderc committed
56
        }
57
        return data.toMap()
Noric Couderc's avatar
Noric Couderc committed
58
59
    }

60
61
62
    /**
     * Creates a list of specifications of what counter to get from what benchmarks
     */
Noric Couderc's avatar
Noric Couderc committed
63
    fun createRunSpecs(numRuns: Int, features : FeatureSet,
64
                       syntheticBenchmarks: List<BCBenchmarkPackage<*>>): List<PapiBenchmarkAnalyzer.RunSpec> {
65
66
        // Let's say we want to minimize the counter change
        // We put it in the outer loop
67
        val specification = mutableListOf<PapiBenchmarkAnalyzer.RunSpec>()
68
        for (b in syntheticBenchmarks) {
Noric Couderc's avatar
Noric Couderc committed
69
            specification.add(PapiBenchmarkAnalyzer.RunSpec(numRuns, features, b))
70
71
72
73
        }
        return specification.toList()
    }

74
    override fun runSpec(spec : PapiBenchmarkAnalyzer.RunSpec): List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
75
        // println("Running benchmark: " + spec.syntheticBenchmark.benchmarkIdentifier)
76
        val writer = FileWriter("/dev/null")
77
78
        val samples = mutableListOf<PapiBenchmarkAnalyzer.BenchmarkRunData>()

79
        for (i in 0 until spec.numberRuns) {
80
            val app = spec.syntheticBenchmark
Noric Couderc's avatar
Noric Couderc committed
81
            val evset = spec.eventSet()
82
            // We do the measurements
83
            app.reset(app.getDatastructureSize())
84
85
            var accumulator = 0
            evset.start()
86
            app.runBenchmark(blackhole)
87
            evset.stop()
88
            val resultDataStructure = app.getDatastructure()
89
90
91
92
93

            // Write the result somewhere to prevent dead code elimination
            writer.write(accumulator)
            writer.write(resultDataStructure.toString())
            // We record the data
94
            val runSamples = spec.counters().zip(evset.counters.toList()).map {
95
96
                PapiBenchmarkAnalyzer.BenchmarkRunSample(it.first, it.second.toDouble())
            }
97
98
99
100
101
102
            val runData = PapiBenchmarkAnalyzer.BenchmarkRunData(
                spec.syntheticBenchmark,
                i,
                runSamples,
                mapOf()
            )
103
            samples.add(runData)
104
        }
Noric Couderc's avatar
Noric Couderc committed
105
        writer.close()
106
107
        return samples
    }
108

109
    override fun runApplications(numberRuns: Int,
Noric Couderc's avatar
Noric Couderc committed
110
                                 features : FeatureSet,
111
                                 syntheticBenchmarks: List<BCBenchmarkPackage<*>>): List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
112
        // return syntheticBenchmarks.map { b -> runApplication(iterations, b) }
Noric Couderc's avatar
Noric Couderc committed
113
        val specs = createRunSpecs(numberRuns, features, syntheticBenchmarks)
114
115
        val numberBenchmarks = specs.size
        var i = 0
116
        val samples = specs.flatMap {
117
118
            print("Running spec: $i / $numberBenchmarks\r")
            i++
Noric Couderc's avatar
Noric Couderc committed
119
            runSpec(it) }
120

121
        println("Finished running specs.")
122

123
        return samples
124
125
    }

Noric Couderc's avatar
Noric Couderc committed
126
    fun runApplicationsNormalized(numberRuns : Int, features : FeatureSet,
127
                                  syntheticBenchmarks: List<BCBenchmarkPackage<*>>) : List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
Noric Couderc's avatar
Noric Couderc committed
128
        val benchmarkRuns = runApplications(numberRuns, features, syntheticBenchmarks)
129

130
        return benchmarkRuns.map {
131
            it.normalized()
132
133
134
135
136
137
138
139
140
141
142
143
144
        }
    }

    /**
     * Normalize a map of counters by the PAPI_TOT_INS (Total number of instructions)
     * @param counters: A map from PAPI counter names to their median value
     */
    fun normalizeFeatures(counters : Map<String, Double>) : Map<String, Double> {
        val total = counters["PAPI_TOT_INS"] ?: error("'PAPI_TOT_INS' not present in counters")

        return counters.mapValues {
            it.value / total
        }
145
    }
Noric Couderc's avatar
Noric Couderc committed
146
}
147
148
149
150

/**
 * A mockup class which returns deterministic results when you "run" a spec.
 */
151
class MockupPapiRunner() : PapiRunner() {
152
    override fun runSpec(spec : PapiBenchmarkAnalyzer.RunSpec) : List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
153
154
155
156
157
158
159
160
        // We map the counters to integers
        // val counterToInt = mapOf<String, Long>(
        //     Pair("PAPI_CA_ITV", 0),
        //     Pair("PAPI_L1_DCM", 1),
        //     Pair("PAPI_TOT_CYC", 2),
        //     Pair("PAPI_TOT_INS", 3)
        // )
        // val n = seed + counterToInt[spec.counter]!!
161
        val iterations = 0 until spec.numberRuns
162
        return iterations.map {
163
            val samples = spec.counters().map {
164
                PapiBenchmarkAnalyzer.BenchmarkRunSample(it, 10000.0)
165
            }
166
167
168
169
170
171
172
173
174

            val cyclesPerOpType = mapOf(
                Pair(OperationType.DELETE, 1000.0),
                Pair(OperationType.INSERT, 1000.0),
                Pair(OperationType.ITERATE, 1000.0),
                Pair(OperationType.SEARCH, 1000.0)
            )

            PapiBenchmarkAnalyzer.BenchmarkRunData(spec.syntheticBenchmark, it, samples, cyclesPerOpType )
175
        }
176
177
    }
}