PapiRunner.kt 6.38 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()
Noric Couderc's avatar
Noric Couderc committed
30
31
        val eb = EventSetBuilder()
        counters.accept(eb)
Noric Couderc's avatar
Noric Couderc committed
32

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

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

Noric Couderc's avatar
Noric Couderc committed
38
            for (warmup in 0..100) {
Noric Couderc's avatar
Noric Couderc committed
39
40
41
42
43
44
45
46
                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
47
                    if (acc % 2 == 1) {
Noric Couderc's avatar
Noric Couderc committed
48
49
50
51
52
53
54
55
                        b[i] = acc
                    }
                }
                evset.stop()
                // Get data
                val currentData = evset.counters
                current.addAll(currentData.toList())
            }
56
            data.set(counter, current)
Noric Couderc's avatar
Noric Couderc committed
57
        }
58
        return data.toMap()
Noric Couderc's avatar
Noric Couderc committed
59
60
    }

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

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

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

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

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

122
        println("Finished running specs.")
123

124
        return samples
125
126
    }

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

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

    /**
     * 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
        }
146
    }
Noric Couderc's avatar
Noric Couderc committed
147
}
148
149
150
151

/**
 * A mockup class which returns deterministic results when you "run" a spec.
 */
152
class MockupPapiRunner() : PapiRunner() {
153
    override fun runSpec(spec : PapiBenchmarkAnalyzer.RunSpec) : List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
154
155
156
157
158
159
160
161
        // 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]!!
162
        val iterations = 0 until spec.numberRuns
163
        return iterations.map {
164
            val samples = spec.counters().map {
165
                PapiBenchmarkAnalyzer.BenchmarkRunSample(it, 10000.0)
166
            }
167
168
169
170
171
172
173
174
175

            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 )
176
        }
177
178
    }
}