PapiRunner.kt 8.2 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
7
import se.lth.cs.papicounters.PapiBenchmarkAnalyzer
8
import se.lth.cs.timing.OperationType
9
import java.io.FileWriter
Noric Couderc's avatar
Noric Couderc committed
10

11
open class PapiRunner(val numRuns: Int, counters: CounterSpecification) : PapiBenchmarkAnalyzer {
12

13
14
15
    init {
        Papi.init()
    }
16
17
18

    val counterSpec = counters

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

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

30
        for (kvp in counterSpec.currentSpec) {
Noric Couderc's avatar
Noric Couderc committed
31
32
            val evset = EventSet.create(kvp.value)

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

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

58
59
60
    /**
     * Creates a list of specifications of what counter to get from what benchmarks
     */
61
    fun createRunSpecs(syntheticBenchmarks: List<BCBenchmarkPackage<*>>): List<PapiBenchmarkAnalyzer.RunSpec> {
62
63
        // Let's say we want to minimize the counter change
        // We put it in the outer loop
64
        val specification = mutableListOf<PapiBenchmarkAnalyzer.RunSpec>()
65
66
67
68
69
        val counterNames = counterSpec.getCounterNames()
        val counterValues = counterSpec.getCounterValues()
        val eventSet = EventSet.create(*counterValues.toIntArray())
        for (b in syntheticBenchmarks) {
            specification.add(PapiBenchmarkAnalyzer.RunSpec(numRuns, counterNames, eventSet, 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.numRuns) {
80
81
82
            val app = spec.syntheticBenchmark
            val evset = spec.eventSet
            // 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

Noric Couderc's avatar
Noric Couderc committed
109
110
111
    /**
     * A function for running one benchmark, gathering all the hardware counters
     * Present in the counter specification we have.
112
     */
Noric Couderc's avatar
Noric Couderc committed
113
    @Deprecated("Used only in tests??")
114
    fun runApplication(app: BCBenchmarkPackage<*>) : Map<String, List<Long>> {
Noric Couderc's avatar
Noric Couderc committed
115
116
117
118
119
120
121
        // We will write the aggregators to nowhere
        val writer = FileWriter("/dev/null")
        // We get integers for all the counters.
        val counters = counterSpec.currentSpec

        var result = mutableMapOf<String, List<Long>>()
        for (counter in counters.keys) {
122
123
            val counterId = counterSpec.getCounter(counter)
            val evset = EventSet.create(counterId!!)
Noric Couderc's avatar
Noric Couderc committed
124
125
126
            val samples = mutableListOf<Long>()
            for (run in 0 until numRuns) {
                // We do the measurements
127
                app.reset(app.getDatastructureSize())
Noric Couderc's avatar
Noric Couderc committed
128
129
                var accumulator = 0
                evset.start()
130
                app.runBenchmark(blackhole)
Noric Couderc's avatar
Noric Couderc committed
131
                evset.stop()
132
133
134
135
136
137
138
                System.out.println("missed trace methods count = " + app.getMissedMethods())
                if (app.getMissedMethods() > 0) {
                    System.out.println("[XWARN:SIMMISS]: missed "
                            + Integer.toString(app.getMissedMethods())
                            + " out of "
                            + Integer.toString(app.getTrace().size) + " trace methods")
                }
139
                val resultDataStructure = app.getDatastructure()
140

Noric Couderc's avatar
Noric Couderc committed
141
142
                // We reset the benchmark
                app.reset(0)
143

Noric Couderc's avatar
Noric Couderc committed
144
145
146
147
148
                // Write the result somewhere to prevent dead code elimination
                writer.write(accumulator)
                writer.write(resultDataStructure.toString())
                // We record the data
                samples.addAll(evset.counters.toList())
149
            }
Noric Couderc's avatar
Noric Couderc committed
150
151
            result[counter] = ArrayList(samples)
            samples.clear()
Noric Couderc's avatar
Noric Couderc committed
152
        }
Noric Couderc's avatar
Noric Couderc committed
153
        app.clearAll();
154
155
156

        writer.close()

Noric Couderc's avatar
Noric Couderc committed
157
        return result.toMap()
Noric Couderc's avatar
Noric Couderc committed
158
159
    }

Noric Couderc's avatar
Noric Couderc committed
160
    override fun runApplications(syntheticBenchmarks: List<BCBenchmarkPackage<*>>): List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
161
        // return syntheticBenchmarks.map { b -> runApplication(iterations, b) }
162
        val specs = createRunSpecs(syntheticBenchmarks)
163
164
        val numberBenchmarks = specs.size
        var i = 0
165
        val samples = specs.flatMap {
166
167
            print("Running spec: $i / $numberBenchmarks\r")
            i++
Noric Couderc's avatar
Noric Couderc committed
168
            runSpec(it) }
169

170
        println("Finished running specs.")
171

172
        return samples
173
174
    }

Noric Couderc's avatar
Noric Couderc committed
175
    fun runApplicationsNormalized(syntheticBenchmarks: List<BCBenchmarkPackage<*>>) : List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
176
        val benchmarkRuns = runApplications(syntheticBenchmarks)
177

178
        return benchmarkRuns.map {
179
            it.normalized()
180
181
182
183
184
185
186
187
188
189
190
191
192
        }
    }

    /**
     * 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
        }
193
    }
Noric Couderc's avatar
Noric Couderc committed
194
}
195
196
197
198
199

/**
 * A mockup class which returns deterministic results when you "run" a spec.
 */
class MockupPapiRunner(numRuns : Int, counters : CounterSpecification) : PapiRunner(numRuns, counters) {
200
    override fun runSpec(spec : PapiBenchmarkAnalyzer.RunSpec) : List<PapiBenchmarkAnalyzer.BenchmarkRunData> {
201
202
203
204
205
206
207
208
        // 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]!!
209
210
        val iterations = 0 until spec.numRuns
        return iterations.map {
211
212
            val samples = spec.counters.map {
                PapiBenchmarkAnalyzer.BenchmarkRunSample(it, 10000.0)
213
            }
214
215
216
217
218
219
220
221
222

            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 )
223
        }
224
225
    }
}