Commit 78ac7429 authored by Noric Couderc's avatar Noric Couderc
Browse files

Refactoring: Removing usuless nested maps

Switched from exporting benchmark results as nested maps to exporting
lists of triplets with benchmark, counter name, and values.
parent 138c3d96
......@@ -198,36 +198,37 @@ open class PapiRunner(numRuns : Int, counters: CounterSpecification) {
return medianLong(data)
}
fun runApplications(syntheticBenchmarks: List<SyntheticBenchmark<*>>): List<Map<String, List<Long>>> {
fun runApplications(syntheticBenchmarks: List<SyntheticBenchmark<*>>): List<Triple<SyntheticBenchmark<*>, String, List<Long>>> {
// return syntheticBenchmarks.map { b -> runApplication(iterations, b) }
val specs = createRunSpecs(syntheticBenchmarks)
val map = specs.map {
val spectToCounters = specs.map {
val samples = runSpec(it)
Pair(it, samples)
}
// TODO: Return a Map<Pair<CounterString, Benchmark>, List<Long>> instead of this mess.
val map1 = map.groupBy { s -> s.first.syntheticBenchmark }
val mapsOfSamples = map1.mapValues {
it.value.map {
Pair(it.first.counter, it.second)
}.toMap()
}.map {
it.value
return spectToCounters.map {
Triple(it.first.syntheticBenchmark,
it.first.counter,
it.second)
}
return mapsOfSamples
}
fun runApplicationsMedian(syntheticBenchmarks: List<SyntheticBenchmark<*>>) : List<Map<String, Double>> {
val mapsOfSamples = runApplications(syntheticBenchmarks)
return mapsOfSamples.map {
medianFeatures(it)
fun runApplicationsMedian(syntheticBenchmarks: List<SyntheticBenchmark<*>>) : List<Triple<SyntheticBenchmark<*>, String, Double>> {
val samples = runApplications(syntheticBenchmarks)
return samples.map {
Triple(it.first, it.second, medianLong(it.third))
}
}
fun runApplicationsNormalized(syntheticBenchmarks: List<SyntheticBenchmark<*>>) : List<Map<String, Double>> {
val mapsOfMedianFeatures = runApplicationsMedian(syntheticBenchmarks)
return mapsOfMedianFeatures.map {
normalizeFeatures(it)
fun runApplicationsNormalized(syntheticBenchmarks: List<SyntheticBenchmark<*>>) : List<Triple<SyntheticBenchmark<*>, String, Double>> {
val countersMedians = runApplicationsMedian(syntheticBenchmarks)
val instructionsForBenchmark = countersMedians.filter { it.second == "PAPI_TOT_INS" }
.map { Pair(it.first, it.third) }
.toMap()
return countersMedians.map {
Triple(it.first, it.second, it.third / instructionsForBenchmark[it.first]!!)
}
}
......
......@@ -27,7 +27,7 @@ abstract class SyntheticBenchmarkDataPrinter(out : Writer,
}
}
open fun printToCSV(syntheticBenchmarks : Sequence<SyntheticBenchmark<*>>) {
open fun printToCSV(syntheticBenchmarks: Sequence<SyntheticBenchmark<*>>) {
printHeader();
for (syntheticBenchmark in syntheticBenchmarks) {
......
......@@ -10,11 +10,11 @@ class SyntheticBenchmarkFeaturePrinter(out: Writer,
: SyntheticBenchmarkDataPrinter(out, methodPrintFormat) {
private val papiRunner: PapiRunner = papiRunner
private var benchmarkToCounters = HashMap<SyntheticBenchmark<*>,
Map<String, List<Long>>>()
private val normalizeFeatures = normalizeFeatures
private var benchmarkResults : Map<String, List<Triple<SyntheticBenchmark<*>, String, Double>>> = mapOf()
/**
* Prints the header for benchmark features
* (They are printed in long form)
......@@ -46,13 +46,9 @@ class SyntheticBenchmarkFeaturePrinter(out: Writer,
printer.printRecord(identifier, getMethodName(kvp.key), kvp.value)
}
val hardwareCounters = benchmarkToCounters[syntheticBenchmark]!!
val hardwareFeatures = when (normalizeFeatures) {
true -> papiRunner.normalizeFeatures(papiRunner.medianFeatures(hardwareCounters))
false -> papiRunner.medianFeatures(hardwareCounters)
}
for (kvp in hardwareFeatures) {
printer.printRecord(identifier, kvp.key, kvp.value)
val hardwareFeatures = benchmarkResults[syntheticBenchmark.benchmarkIdentifier]!!
for (triple in hardwareFeatures) {
printer.printRecord(identifier, triple.second, triple.third)
}
}
......@@ -62,9 +58,14 @@ class SyntheticBenchmarkFeaturePrinter(out: Writer,
}
private fun runBenchmarks(syntheticBenchmarks: List<SyntheticBenchmark<*>>) {
benchmarkToCounters.clear()
val hardwareCounters = papiRunner.runApplications(syntheticBenchmarks)
syntheticBenchmarks.zip(hardwareCounters).toMap(benchmarkToCounters)
benchmarkResults = when (normalizeFeatures) {
true -> papiRunner.runApplicationsNormalized(syntheticBenchmarks).groupBy {
it.first.benchmarkIdentifier
}
false -> papiRunner.runApplicationsMedian(syntheticBenchmarks).groupBy {
it.first.benchmarkIdentifier
}
}
}
}
import kotlin.Triple;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -73,15 +74,14 @@ public class PapiRunnerTest {
3,
100
);
List<Map<String, Double>> data = runner.runApplicationsMedian(syntheticBenchmarks);
List<Triple<SyntheticBenchmark<?>, String, Double>> data = runner.runApplicationsMedian(syntheticBenchmarks);
// We check all known Papi counters are in the map
Assert.assertFalse(data.isEmpty());
for (Map<String, Double> counters : data) {
Assert.assertEquals(
specification.getCurrentSpec().keySet(),
counters.keySet());
Assert.assertFalse(counters.isEmpty());
for (Triple<SyntheticBenchmark<?>, String, Double> counters : data) {
Assert.assertTrue(
specification.getCurrentSpec().containsKey(counters.component2())
);
}
}
......@@ -106,9 +106,9 @@ public class PapiRunnerTest {
benchmarks.add(bench);
}
List<Map<String, Double>> results = runner.runApplicationsMedian(benchmarks);
for (Map<String, Double> r : results) {
Assert.assertFalse(r.isEmpty());
List<Triple<SyntheticBenchmark<?>, String, List<Long>>> results = runner.runApplications(benchmarks);
for (Triple<SyntheticBenchmark<?>, String, List<Long>> r : results) {
Assert.assertFalse(r.component3().isEmpty());
}
}
......
......@@ -53,4 +53,30 @@ class SyntheticBenchmarkFeaturePrinterTest {
expectedCounters.all { it -> writer.toString().contains(it) }
)
}
@Test
fun testNormalizedBenchmark() {
val plan = listOf("runAdd", "runAdd", "runGet", "runGet")
val benchmark = ListSyntheticBenchmark("Test benchmark", plan, ArrayList())
val writer = StringWriter()
val csvPrinter = SyntheticBenchmarkFeaturePrinter(writer,
runner,
"SYNTHETIC-RUN-FORMAT",
true)
val expectedBeginning =
"benchmark_id,feature,value\n" +
"Trace:Test benchmark:0:0:List:0:ArrayList,collection,ArrayList\n" +
"Trace:Test benchmark:0:0:List:0:ArrayList,runGet,0.5\n" +
"Trace:Test benchmark:0:0:List:0:ArrayList,runAdd,0.5"
csvPrinter.printToCSV(sequenceOf(benchmark))
Assert.assertEquals(
expectedBeginning.lines(),
writer.toString().lines().take(4)
)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment