EventSetBuilder.kt 2.12 KB
Newer Older
1
package se.lth.cs.papicounters
2
3

import papi.EventSet
4
import se.lth.cs.util.*
5
6
7
8

/**
 * Creates an eventSet from a FeatureSet
 */
9
class EventSetBuilder(val features : FeatureSet) : FeatureVisitor {
10

11
    private var counterSet : LinkedHashSet<PAPICounter> = java.util.LinkedHashSet()
12

13
14
15
16
    init {
        features.accept(this)
    }

17
    override fun visitPAPICounter(c: PAPICounter) {
18
        // If the counter is de-activated we skip it
19
20
21
        if (!c.isSupported()) {
            val msg = "Creating eventSet for single counter $c failed: is it available on this machine?"
            throw RuntimeException(msg)
22
        }
23
24

        counterSet.add(c)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    }

    override fun visitConstant(constantFeature: ConstantFeature) {
        // Do nothing
    }

    override fun visitCost(costFeature: CostFeature) {
        counterSet.add(PAPICounter("PAPI_TOT_CYC"))
    }

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

40
    override fun visitMethodInvocations(methodInvocations: MethodInvocations) {
41
42
43
        // Do nothing
    }

44
    override fun visitTotalMethodInvocations(totalMethodInvocations: TotalMethodInvocations) {
45
46
47
48
        // Do nothing
    }

    override fun visitFeatureSet(featureSet: FeatureSet) {
49
        for (f in featureSet) {
50
51
52
53
54
55
56
57
            f.accept(this)
        }
    }

    fun getCounterSet() : Set<PAPICounter> {
        return counterSet.toSet()
    }

58
59
60
61
    companion object {
        val eventSetCache : MutableMap<Set<PAPICounter>, EventSet> = mutableMapOf()
    }

62
    fun getEventSet() : EventSet {
63
64
65
66
67
68
69
70
71
72
73
74
        if (counterSet.isEmpty()) {
            throw RuntimeException("Counter set is empty! Did you call accept() on the builder?")
        }
        return if (eventSetCache.containsKey(counterSet)) {
            val eventSet = eventSetCache.get(counterSet)!!
            eventSet.reset()
            eventSet
        } else {
            val counters = counterSet.map { it.toPAPIConstant()!! }.toIntArray()
            val eventSet = EventSet.create(*counters)
            eventSetCache.put(counterSet, eventSet!!)
            eventSet!!
75
76
77
        }
    }
}