Experiment.kt 4.15 KB
Newer Older
1
2
package se.lth.cs.util

Noric Couderc's avatar
Noric Couderc committed
3
4
5
6
7
import se.lth.cs.bcgen.BCBenchmarkPackage
import se.lth.cs.papicounters.EventSetBuilder
import se.lth.cs.papicounters.PAPICounter
import se.lth.cs.papicounters.PapiBenchmarkAnalyzer
import se.lth.cs.papicounters.PapiTracerRunner
8
import se.lth.cs.smartmodules.tracer.Tracer
Noric Couderc's avatar
Noric Couderc committed
9
import se.lth.cs.timing.OperationTypeTable
10
import java.util.stream.Stream
Noric Couderc's avatar
Noric Couderc committed
11
12
13

class Experiment(val numberIterations : Int,
                 val benchmark : BCBenchmarkPackage<*>,
14
15
                 val features : FeatureSet,
                 val papiRunner : PapiTracerRunner) : FeatureVisitor {
Noric Couderc's avatar
Noric Couderc committed
16

17
18
19
20
21
22
23
24
25
26
27
28
29
    inner class Result(val iterationNumber : Int,
                 val benchmark : BCBenchmarkPackage<*>,
                 val values : Map<Feature, Double>) {
        /**
         * Returns only the features we specified in the feature set
         * If feature F1 requires F2, but F2 is not explicitly requested
         * we omit it.
         */
        fun strict() : Result {
            return Result(iterationNumber, benchmark,
                values.filterKeys { it in features } )
        }
    }
Noric Couderc's avatar
Noric Couderc committed
30

31
    fun getIterations(): Stream<List<PapiTracerRunner.TraceRecord>> {
Noric Couderc's avatar
Noric Couderc committed
32
        val iterations = papiRunner.runIterations(
Noric Couderc's avatar
Noric Couderc committed
33
            PapiBenchmarkAnalyzer.RunSpec(numberIterations, features, benchmark)
Noric Couderc's avatar
Noric Couderc committed
34
35
        )

36
        return iterations.stream()
37
38
    }

39
40
41
42
43
44
45
46
47
48
49
50
51
52
    private fun runIteration(i : List<PapiTracerRunner.TraceRecord>,
                     iterationNumber: Int): Result {
        currentIteration = i
        currentIterationResults = mutableMapOf()
        // We aggregate all our features for that
        // specific iteration!
        features.accept(this)
        val values = currentIterationResults.toMap()

        return Result(iterationNumber, benchmark, values)
    }

    fun run() : Stream<Result> {
        if (features.isEmpty()) { return Stream.empty() }
53
54
55

        val iterations = getIterations()

Noric Couderc's avatar
Noric Couderc committed
56
        var iterationNumber = 0
57

58
59
        return iterations.map {
            val r = runIteration(it, iterationNumber)
Noric Couderc's avatar
Noric Couderc committed
60
            iterationNumber++
61
            r
Noric Couderc's avatar
Noric Couderc committed
62
63
64
65
66
67
68
69
        }
    }

    var currentIteration : List<PapiTracerRunner.TraceRecord> = listOf()

    var currentIterationResults : MutableMap<Feature, Double> = mutableMapOf()

    override fun visitPAPICounter(c: PAPICounter) {
Noric Couderc's avatar
Noric Couderc committed
70
        if (currentIterationResults.containsKey(c)) {
71
            return
Noric Couderc's avatar
Noric Couderc committed
72
73
        }

Noric Couderc's avatar
Noric Couderc committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        val values = currentIteration.filter { it.featureName == c.toHexa() }
            .map { it.featureValue }

        currentIterationResults[c] = values.sum().toDouble()
    }

    override fun visitConstant(constantFeature: ConstantFeature) {
        currentIterationResults[constantFeature] = constantFeature.value
    }

    override fun visitCost(costFeature: CostFeature) {
        costFeature.counter.accept(this)

        val sum = currentIteration.filter {
            val methodType = OperationTypeTable.getType(it.method)

            val rightMethodType = methodType.map { opType ->
                opType == costFeature.opType
            }.orElse(false)

            val rightCounter = it.featureName == costFeature.counter.toHexa()
            rightMethodType && rightCounter
        }.sumBy { it.featureValue }

        currentIterationResults[costFeature] = sum.toDouble()
    }

    override fun visitRatio(featureRatio: FeatureRatio) {
        featureRatio.f1.accept(this)
        featureRatio.f2.accept(this)

105
106
        val f1Value = currentIterationResults[featureRatio.f1]
        val f2Value = currentIterationResults[featureRatio.f2]
Noric Couderc's avatar
Noric Couderc committed
107

108
109
110
        if (f1Value != null && f2Value != null) {
            currentIterationResults[featureRatio] = f1Value / f2Value
        }
Noric Couderc's avatar
Noric Couderc committed
111
112
113
    }

    override fun visitFeatureSet(featureSet: FeatureSet) {
114
        for (f in featureSet) {
Noric Couderc's avatar
Noric Couderc committed
115
116
117
118
119
120
121
122
123
124
125
126
            f.accept(this)
        }
    }

    override fun visitMethodInvocations(methodInvocations: MethodInvocations) {
        val invokes = benchmark.methodHistogram()[methodInvocations.method]!!
        currentIterationResults[methodInvocations] = invokes.toDouble()
    }

    override fun visitTotalMethodInvocations(totalMethodInvocations: TotalMethodInvocations) {
        currentIterationResults[totalMethodInvocations] = benchmark.traceSize.toDouble()
    }
127
}