File size: 8,341 Bytes
d844fc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Deployment-style validation of the production IncrementalBFS fallback.
//
// This harness deliberately does not reimplement the publication selector.
// A selector decision schedule is produced by publication_policy_bfs.cpp with
// fallback disabled, then replayed here against the production 0.35 fallback.
// This isolates the practical question: how much repair-then-full work does a
// pre-repair full choice avoid while preserving the same exact semantics?
#define main velographx_frozen_adaptive_policy_main
#include "adaptive_policy_bfs.cpp"
#undef main

#include <fstream>
#include <numeric>

namespace {

struct ScheduleEntry {
  bool explicit_full{false};
  double selector_us{0.0};
};

struct DeploymentResult {
  std::string name;
  std::vector<double> batch_us;
  std::vector<double> execution_us;
  std::vector<double> selector_us;
  std::vector<bool> explicit_full;
  std::vector<bool> internal_fallback;
  std::vector<std::size_t> affected_vertices;
  std::size_t explicit_full_batches{0};
  std::size_t internal_fallback_batches{0};
  bool exact{true};
};

std::vector<ScheduleEntry> read_schedule(const std::string& path) {
  std::ifstream in(path);
  if (!in) throw std::runtime_error("cannot open selector schedule: " + path);
  std::vector<ScheduleEntry> schedule;
  int full = 0;
  double selector_us = 0.0;
  while (in >> full >> selector_us) {
    if (full != 0 && full != 1) {
      throw std::runtime_error("selector schedule full flag must be 0 or 1");
    }
    if (selector_us < 0.0) {
      throw std::runtime_error("selector schedule cost must be non-negative");
    }
    schedule.push_back({full == 1, selector_us});
  }
  if (schedule.empty()) throw std::runtime_error("selector schedule is empty");
  return schedule;
}

DeploymentResult run_deployment_policy(
    const std::string& policy,
    const std::vector<Edge>& edges,
    std::size_t vertices,
    velographx::VertexId root,
    std::size_t imported_edges,
    std::size_t batch_size,
    double fallback_fraction,
    const std::vector<ScheduleEntry>& schedule) {
  std::vector<Edge> initial(edges.begin(), edges.begin() + imported_edges);
  velographx::DynamicGraph graph(vertices, true);
  graph.bulk_load_edges(initial);
  velographx::IncrementalBFS bfs(graph, root, fallback_fraction);

  DeploymentResult result;
  result.name = policy;
  std::size_t batch_index = 0;

  for (std::size_t begin = imported_edges; begin < edges.size(); begin += batch_size) {
    const auto end = std::min(begin + batch_size, edges.size());
    auto updates = make_batch(edges, imported_edges, begin, end);
    if (batch_index >= schedule.size()) {
      throw std::runtime_error("selector schedule shorter than workload");
    }

    const bool choose_full =
        policy == "always_full" ||
        (policy == "selector_plus_fallback" && schedule[batch_index].explicit_full);
    const double selector_us =
        policy == "selector_plus_fallback" ? schedule[batch_index].selector_us : 0.0;

    const auto execution_begin = Clock::now();
    bool fallback = false;
    std::size_t affected = 0;
    if (choose_full) {
      graph.apply(updates);
      bfs.recompute();
      ++result.explicit_full_batches;
    } else {
      bfs.apply(updates);
      fallback = bfs.last_used_full_recompute();
      affected = bfs.last_affected_vertices();
      if (fallback) ++result.internal_fallback_batches;
    }
    const auto execution_end = Clock::now();
    const double execution_us =
        std::chrono::duration<double, std::micro>(execution_end - execution_begin).count();

    result.execution_us.push_back(execution_us);
    result.selector_us.push_back(selector_us);
    result.batch_us.push_back(execution_us + selector_us);
    result.explicit_full.push_back(choose_full);
    result.internal_fallback.push_back(fallback);
    result.affected_vertices.push_back(affected);

    const auto reference = full_bfs(graph, root);
    if (reference != bfs.distances()) result.exact = false;
    ++batch_index;
  }

  if (batch_index != schedule.size()) {
    throw std::runtime_error("selector schedule longer than workload");
  }
  return result;
}

void print_bool_vector(const std::vector<bool>& values) {
  std::cout << '[';
  for (std::size_t i = 0; i < values.size(); ++i) {
    if (i) std::cout << ',';
    std::cout << (values[i] ? "true" : "false");
  }
  std::cout << ']';
}

void print_size_vector(const std::vector<std::size_t>& values) {
  std::cout << '[';
  for (std::size_t i = 0; i < values.size(); ++i) {
    if (i) std::cout << ',';
    std::cout << values[i];
  }
  std::cout << ']';
}

}  // namespace

int main(int argc, char** argv) {
  if (argc != 7) {
    std::cerr << "usage: " << argv[0]
              << " EDGE_LIST ROOT IMPORT_RATE BATCH_SIZE FALLBACK_FRACTION SCHEDULE\n";
    return 2;
  }

  try {
    const std::string path = argv[1];
    const auto root64 = std::stoull(argv[2]);
    const double imported_rate = std::stod(argv[3]);
    const auto batch_size = static_cast<std::size_t>(std::stoull(argv[4]));
    const double fallback_fraction = std::stod(argv[5]);
    const std::string schedule_path = argv[6];
    if (root64 > std::numeric_limits<velographx::VertexId>::max() ||
        imported_rate <= 0.0 || imported_rate >= 1.0 ||
        batch_size == 0 || fallback_fraction <= 0.0 || fallback_fraction > 1.0) {
      return 2;
    }
    const auto root = static_cast<velographx::VertexId>(root64);

    std::size_t vertices = 0;
    const auto edges = read_edges(path, vertices);
    const auto imported_edges = static_cast<std::size_t>(edges.size() * imported_rate);
    if (edges.empty() || imported_edges == 0 || imported_edges >= edges.size()) return 2;
    const auto schedule = read_schedule(schedule_path);

    const std::vector<std::string> names = {
        "fallback_only", "selector_plus_fallback", "always_full"};
    std::vector<DeploymentResult> results;
    for (const auto& name : names) {
      results.push_back(run_deployment_policy(
          name, edges, vertices, root, imported_edges, batch_size,
          fallback_fraction, schedule));
    }

    const auto batches = results.front().batch_us.size();
    bool all_exact = true;
    std::cout << "{\"schema_version\":1"
              << ",\"artifact_type\":\"velographx-production-fallback-replay\""
              << ",\"selector_source\":\"publication-preflight-v1 decision replay\""
              << ",\"fallback_fraction\":" << fallback_fraction
              << ",\"root\":" << root64
              << ",\"vertices\":" << vertices
              << ",\"batch_size\":" << batch_size
              << ",\"batches\":" << batches
              << ",\"policies\":[";

    for (std::size_t p = 0; p < results.size(); ++p) {
      if (p) std::cout << ',';
      const auto& result = results[p];
      all_exact = all_exact && result.exact;
      const double total_us =
          std::accumulate(result.batch_us.begin(), result.batch_us.end(), 0.0);
      std::cout << "{\"name\":\"" << result.name << "\""
                << ",\"exact\":" << (result.exact ? "true" : "false")
                << ",\"total_us\":" << total_us
                << ",\"mean_batch_us\":"
                << total_us / std::max<std::size_t>(1, batches)
                << ",\"explicit_full_batches\":" << result.explicit_full_batches
                << ",\"internal_fallback_batches\":" << result.internal_fallback_batches
                << ",\"batch_us\":";
      print_array(result.batch_us);
      std::cout << ",\"execution_us\":";
      print_array(result.execution_us);
      std::cout << ",\"selector_us\":";
      print_array(result.selector_us);
      std::cout << ",\"explicit_full\":";
      print_bool_vector(result.explicit_full);
      std::cout << ",\"internal_full_fallback\":";
      print_bool_vector(result.internal_fallback);
      std::cout << ",\"affected_vertices\":";
      print_size_vector(result.affected_vertices);
      std::cout << '}';
    }

    std::cout << "],\"verification_excluded_from_timing\":true"
              << ",\"selector_cost_included_for_selector_plus_fallback\":true"
              << ",\"all_policies_exact\":" << (all_exact ? "true" : "false")
              << "}\n";
    return all_exact ? 0 : 1;
  } catch (const std::exception& exc) {
    std::cerr << "error: " << exc.what() << '\n';
    return 2;
  }
}