File size: 7,486 Bytes
5c1df24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Compare the actual SuperIntervals C++ library under the same workload,
// oracle, compiler and measurement harness as Guardshift's native controls.
#define GUARDSHIFT_BENCH_LIBRARY
#include "bench.cpp"
#undef GUARDSHIFT_BENCH_LIBRARY
#include "superintervals.hpp"

namespace {

template <class Map>
class SuperIntervalsAdapter {
    struct Field {
        Map intervals;
        std::vector<std::size_t> previous;
        std::vector<std::size_t> next;
        Value value{};
    };
    std::vector<Field> fields;
    Counts counts;

public:
    SuperIntervalsAdapter(const std::vector<Value>& initial, const Guards& guards)
        : fields(initial.size()), counts(initial, guards) {
        for (Id id = 0; id < guards.size(); ++id) {
            for (const auto& term : guards[id]) {
                fields[term.signal].intervals.add(term.low, term.high, id);
            }
        }
        for (std::size_t s = 0; s < fields.size(); ++s) {
            Field& field = fields[s];
            field.value = initial[s];
            field.intervals.build();
            if (!field.intervals.starts.empty() && field.value >= field.intervals.starts.front())
                field.intervals.search_idxs(field.value, field.value, field.previous);
        }
    }

    bool active(Id guard) const { return counts.active(guard); }

    std::vector<Change> apply(const std::vector<Update>& patch) {
        for (const Update& u : patch) {
            Field& field = fields[u.signal];
            if (u.value == field.value) continue;
            // SuperIntervals returns indices in descending order. Cache the last
            // answer and merge both lists, avoiding a second point query or sort.
            field.next.clear();
            if (!field.intervals.starts.empty() && u.value >= field.intervals.starts.front())
                field.intervals.search_idxs(u.value, u.value, field.next);
            std::size_t a = 0, b = 0;
            const auto& ids = field.intervals.data;
            while (a < field.previous.size() && b < field.next.size()) {
                const auto old_hit = field.previous[a];
                const auto new_hit = field.next[b];
                if (old_hit == new_hit) {
                    ++a;
                    ++b;
                } else if (old_hit > new_hit) {
                    counts.adjust(ids[old_hit], 1);
                    ++a;
                } else {
                    counts.adjust(ids[new_hit], -1);
                    ++b;
                }
            }
            while (a < field.previous.size()) counts.adjust(ids[field.previous[a++]], 1);
            while (b < field.next.size()) counts.adjust(ids[field.next[b++]], -1);
            field.previous.swap(field.next);
            field.value = u.value;
        }
        return counts.finish();
    }
};

// An alternative adapter indexes entry and exit events as singleton points.
// A single range search then returns all crossed endpoints on either leg.
template <class Map>
class SuperIntervalsEdges {
    std::vector<Map> fields;
    std::vector<Value> values;
    std::vector<Id> hits;
    Counts counts;

public:
    SuperIntervalsEdges(const std::vector<Value>& initial, const Guards& guards)
        : fields(initial.size()), values(initial), counts(initial, guards) {
        if (guards.size() > std::numeric_limits<Id>::max() / 2) {
            throw std::length_error("benchmark crossing ID out of range");
        }
        struct Entry { Value at; Id event; };
        std::vector<std::vector<Entry>> entries(fields.size());
        for (Id id = 0; id < guards.size(); ++id) {
            for (const auto& term : guards[id]) {
                auto& field = entries[term.signal];
                if (term.low != std::numeric_limits<Value>::min()) {
                    field.push_back({term.low, (id << 1) | 1u});
                }
                if (term.high != std::numeric_limits<Value>::max()) {
                    const auto end = term.high + 1;
                    field.push_back({end, id << 1});
                }
            }
        }
        for (std::size_t i = 0; i < fields.size(); ++i) {
            auto& input = entries[i];
            std::sort(input.begin(), input.end(),
                      [](const Entry& a, const Entry& b) { return a.at < b.at; });
            fields[i].reserve(input.size());
            for (const auto& entry : input) fields[i].add(entry.at, entry.at, entry.event);
            std::vector<Entry>().swap(input);
            fields[i].build();
        }
    }

    bool active(Id guard) const { return counts.active(guard); }

    std::vector<Change> apply(const std::vector<Update>& patch) {
        for (const Update& update : patch) {
            auto& previous = values[update.signal];
            if (previous == update.value) continue;
            const bool up = update.value > previous;
            const auto low = std::min(previous, update.value);
            const auto high = std::max(previous, update.value);
            hits.clear();
            const auto& map = fields[update.signal];
            if (!map.starts.empty() && high >= map.starts.front())
                map.search_values(low + 1, high, hits);
            for (const auto& hit : hits) {
                counts.adjust(hit >> 1, bool(hit & 1u) == up ? -1 : 1);
            }
            previous = update.value;
        }
        return counts.finish();
    }
};

void compare(const Workload& w, unsigned seed, unsigned repeats) {
    const auto oracle = make_oracle(w);
    for (unsigned rep = 0; rep < repeats; ++rep) {
        for (unsigned k = 0; k < 5; ++k) {
            switch ((k + rep) % 5) {
                case 0:
                    measure<guardshift::Index>(w, oracle, "guardshift", seed, rep);
                    break;
                case 1:
                    measure<SuperIntervalsAdapter<si::IntervalMap<Value, Id>>>(
                        w, oracle, "superintervals", seed, rep);
                    break;
                case 2:
                    measure<SuperIntervalsAdapter<si::IntervalMapEytz<Value, Id>>>(
                        w, oracle, "superintervals_eytz", seed, rep);
                    break;
                case 3:
                    measure<SuperIntervalsEdges<si::IntervalMap<Value, Id>>>(
                        w, oracle, "superintervals_edges", seed, rep);
                    break;
                case 4:
                    measure<SuperIntervalsEdges<si::IntervalMapEytz<Value, Id>>>(
                        w, oracle, "superintervals_edges_eytz", seed, rep);
                    break;
            }
        }
    }
}

}  // namespace

int main(int argc, char** argv) {
    try {
        if (argc != 2) throw std::invalid_argument("usage: external replay/azure-code.csv");
        for (unsigned seed : {17u, 42u, 217u}) {
            compare(make_work("atomic", 8192, 1200, seed), seed, 3);
            compare(make_work("atomic", 32768, 1200, seed), seed, 3);
            compare(make_azure(argv[1], 8192, 1200, seed), seed, 3);
            compare(make_mixed(seed), seed, 3);
            compare(make_work("teleport", 100000, 512, seed), seed, 3);
            compare(make_work("jump", 32768, 512, seed), seed, 3);
            compare(make_work("sparse", 1000000, 192, seed), seed, 3);
            compare(make_mixed(seed, 1000000, 192), seed, 3);
        }
    } catch (const std::exception& ex) {
        std::cerr << ex.what() << '\n';
        return 1;
    }
}