LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ctmc_state_space_logsize.h
Go to the documentation of this file.
1#ifndef LINE_API_MC_CTMC_STATE_SPACE_LOGSIZE_H
2#define LINE_API_MC_CTMC_STATE_SPACE_LOGSIZE_H
3
4/**
5 * @file
6 * @ingroup api_mc
7 * Worst-case log-size of the CTMC state space induced by a NetworkStruct.
8 *
9 * Port of `matlab/src/api/mc/ctmc_state_space_logsize.m`. The estimate is the
10 * product of five factors, summed in log space:
11 *
12 * 1. job placements: stars-and-bars C(n_k+M-1, M-1) per class over the
13 * stations that keep no ordered buffer, with an open class holding AT MOST
14 * cutoff jobs over the non-Source stations;
15 * 2. buffer orderings: a station outside the share family keeps the CLASS
16 * SEQUENCE of the jobs it holds, so with K>1 classes one occupancy vector
17 * is as many states as its sequences;
18 * 3. service phases: the phase count raised to the number of jobs that can be
19 * in service concurrently at the station;
20 * 4. routing state: one pointer over the outgoing links per (node, class)
21 * routed RROBIN or WRROBIN;
22 * 5. cache content: the ordered items held by each Cache node, times its
23 * retrieval-system subsets; its in-transit job slot is summed with the
24 * placements, taking a closed job out of its chain.
25 *
26 * It is computed in LOG space throughout because the quantity it exists to
27 * detect overflows a double: the `intractableCTMC` fixture (8 PS stations,
28 * N=400, Erlang-5) sits at exp(200), and a linear-space estimator would report
29 * `inf` for everything above exp(709) and lose the ability to rank one
30 * intractable model against another.
31 *
32 * This is the quantity fed to `ctmc_memory_gate`. It is separate from the gate
33 * so a caller such as SolverAUTO can screen CTMC out of a ranking without
34 * building the chain.
35 */
36
37#include <algorithm>
38#include <cmath>
39#include <cstddef>
40#include <string>
41#include <limits>
42#include <utility>
43#include <vector>
44
48
49namespace line {
50namespace mc {
51
52
53
54/** Largest (m_1..m_K) box the exact ordered-buffer DP will walk. */
55inline constexpr double kOrderGridMax = 1.0e6;
56
57/**
58 * Log count of (placement, ordering) configurations over ALL order-preserving
59 * stations at once, POPULATION CONSERVED. `caps_per[a][k]` bounds buffered class
60 * k at ordered station a, `cap_tot[a]` bounds the buffer TOTAL there (a finite
61 * station capacity is a slot count, not a per-class bound).
62 *
63 * The DP state is the population still to place per POPULATION UNIT, not per
64 * class: `nj_u[u]` is unit u's population, `b_unit_of[k]` the unit of buffered
65 * class k, and unit u spreads what the ordered stations leave over `m_rem_u[u]`
66 * share (station, class) slots. A unit is one class, or a closed chain whose
67 * jobs switch class, whose N jobs may then take any class mix at a station.
68 *
69 * Cutoff truncates an OPEN class's population in the network exactly as the
70 * plain stars-and-bars term treats it, so open classes are conserved too.
71 */
72inline double log_ordered_joint(const std::vector<std::vector<int>>& caps_per,
73 const std::vector<double>& cap_tot,
74 const std::vector<int>& nj_u,
75 const std::vector<std::size_t>& b_unit_of,
76 const std::vector<double>& m_rem_u,
77 const std::vector<std::vector<double>>& log_ph_per = {},
78 const std::vector<std::vector<int>>& lev_per = {}) {
79 const std::size_t Kb = b_unit_of.size();
80 const std::size_t U = nj_u.size();
81 std::vector<std::size_t> dims(U);
82 std::size_t nstate = 1;
83 for (std::size_t u = 0; u < U; ++u) { dims[u] = static_cast<std::size_t>(nj_u[u]) + 1; nstate *= dims[u]; }
84 const double NEG = -std::numeric_limits<double>::infinity();
85 auto idx_of = [&](const std::vector<int>& v) {
86 std::size_t ix = 0, mult = 1;
87 for (std::size_t u = 0; u < U; ++u) { ix += static_cast<std::size_t>(v[u]) * mult; mult *= dims[u]; }
88 return ix;
89 };
90 auto sub_of = [&](std::size_t ix) {
91 std::vector<int> v(U);
92 for (std::size_t u = 0; u < U; ++u) { v[u] = static_cast<int>(ix % dims[u]); ix /= dims[u]; }
93 return v;
94 };
95 std::vector<double> L(nstate, NEG);
96 L[idx_of(nj_u)] = 0.0;
97 for (std::size_t a = 0; a < caps_per.size(); ++a) {
98 std::vector<double> Ln(nstate, NEG);
99 // lev_per[a][k] is the priority level of buffered class k at a station that orders jobs only WITHIN a level
100 // (LCFSPRIO, State priority-canonical buffers); an empty row marks a station that orders all its jobs.
101 const bool by_level = a < lev_per.size() && lev_per[a].size() == Kb;
102 std::vector<int> ulev;
103 if (by_level) {
104 ulev = lev_per[a];
105 std::sort(ulev.begin(), ulev.end());
106 ulev.erase(std::unique(ulev.begin(), ulev.end()), ulev.end());
107 }
108 for (std::size_t si = 0; si < nstate; ++si) {
109 if (!std::isfinite(L[si])) continue;
110 const std::vector<int> rem = sub_of(si);
111 std::vector<int> av(Kb);
112 for (std::size_t k = 0; k < Kb; ++k) av[k] = std::min(caps_per[a][k], rem[b_unit_of[k]]);
113 std::vector<int> m(Kb, 0);
114 for (;;) {
115 int t = 0;
116 for (std::size_t k = 0; k < Kb; ++k) t += m[k];
117 if (!(std::isfinite(cap_tot[a]) && t > cap_tot[a])) {
118 double v = L[si];
119 if (!by_level) {
120 v += std::lgamma(static_cast<double>(t) + 1.0);
121 for (std::size_t k = 0; k < Kb; ++k) v -= std::lgamma(static_cast<double>(m[k]) + 1.0);
122 } else if (t > 0) {
123 // One job of SOME present class c is in service and the other t-1 wait in the canonical
124 // layout, ordered only within a level: sum over c of prod_l (t_l(m-e_c))! / prod_k (m-e_c)_k!.
125 // Counting the waiting line alone (no in-service choice) gave 49 against 85 enumerated on a
126 // two-class open M/M/1 at cutoff 6 (MATLAB ctmc_logOrderedJoint).
127 std::vector<double> lc;
128 for (std::size_t c = 0; c < Kb; ++c) {
129 if (m[c] <= 0) continue;
130 std::vector<int> mc = m;
131 mc[c] -= 1;
132 double x = 0.0;
133 for (std::size_t k = 0; k < Kb; ++k) x -= std::lgamma(static_cast<double>(mc[k]) + 1.0);
134 for (int l : ulev) {
135 int tl = 0;
136 for (std::size_t k = 0; k < Kb; ++k)
137 if (lev_per[a][k] == l) tl += mc[k];
138 x += std::lgamma(static_cast<double>(tl) + 1.0);
139 }
140 lc.push_back(x);
141 }
142 double top = lc[0];
143 for (double x : lc) top = std::max(top, x);
144 double acc = 0.0;
145 for (double x : lc) acc += std::exp(x - top);
146 v += top + std::log(acc);
147 }
148 if (a < log_ph_per.size())
149 for (std::size_t k = 0; k < Kb && k < log_ph_per[a].size(); ++k)
150 v += m[k] * log_ph_per[a][k];
151 // a unit's classes draw on ONE remaining population, so a class mix may overdraw it
152 std::vector<int> nx = rem;
153 bool fits = true;
154 for (std::size_t k = 0; k < Kb; ++k) nx[b_unit_of[k]] -= m[k];
155 for (std::size_t u = 0; u < U; ++u) if (nx[u] < 0) fits = false;
156 if (fits) {
157 const std::size_t di = idx_of(nx);
158 if (!std::isfinite(Ln[di])) Ln[di] = v;
159 else { const double mx = std::max(Ln[di], v);
160 Ln[di] = mx + std::log(std::exp(Ln[di] - mx) + std::exp(v - mx)); }
161 }
162 }
163 std::size_t pos = 0;
164 while (pos < Kb && m[pos] == av[pos]) { m[pos] = 0; ++pos; }
165 if (pos == Kb) break;
166 ++m[pos];
167 }
168 }
169 L.swap(Ln);
170 }
171 std::vector<double> terms;
172 for (std::size_t si = 0; si < nstate; ++si) {
173 if (!std::isfinite(L[si])) continue;
174 const std::vector<int> rem = sub_of(si);
175 double v = L[si];
176 bool stranded = false;
177 for (std::size_t u = 0; u < U; ++u) {
178 const double r = rem[u];
179 if (m_rem_u[u] >= 1.0)
180 v += std::lgamma(r + m_rem_u[u]) - std::lgamma(r + 1.0) - std::lgamma(m_rem_u[u]);
181 else if (rem[u] > 0)
182 stranded = true; // leftovers with no share slot to go to
183 }
184 if (stranded) continue;
185 terms.push_back(v);
186 }
187 if (terms.empty()) return NEG;
188 double top = terms[0];
189 for (double v : terms) top = std::max(top, v);
190 double acc = 0.0;
191 for (double v : terms) acc += std::exp(v - top);
192 return top + std::log(acc);
193}
194
195/** Options the estimator reads; only the cutoff matters. */
197 double cutoff = -1.0; ///< < 0 or non-finite = not given, take the solver default
198};
199
200/**
201 * Worst-case log state-space size of `sn`.
202 *
203 * @param sn the struct, ideally after `sn_nonmarkov_toph`, since a
204 * non-Markovian service becomes phases the raw struct does not carry
205 * @return natural log of the worst-case state count
206 */
207template <class T>
212
213 const std::size_t M = sn.nstations;
214 const std::size_t K = sn.nclasses;
215 if (M == 0 || K == 0) return 0.0;
216
217 // The analyzer's own default for an open or mixed model, so the estimate is
218 // taken against the space that would actually be built.
219 double cutoff = opt.cutoff;
220 if (!(cutoff > 0.0) || !std::isfinite(cutoff))
221 cutoff = std::ceil(std::pow(6000.0, 1.0 / static_cast<double>(M * K)));
222
223 // ORDERED BUFFERS, computed EXACTLY -- see the python twin for the measured
224 // numbers. Omitting it under-priced gallery_mmap1_multiclass 311x; BOUNDING
225 // it instead of computing it double counts and refused a working model.
226 const auto is_share = [](SchedStrategy s) {
227 return s == SchedStrategy::INF || s == SchedStrategy::PS || s == SchedStrategy::DPS ||
228 s == SchedStrategy::GPS || s == SchedStrategy::PSPRIO ||
229 s == SchedStrategy::DPSPRIO || s == SchedStrategy::GPSPRIO ||
230 s == SchedStrategy::LPS;
231 };
232 std::vector<bool> is_buffered(K, true);
233 std::size_t Kb = 0;
234 for (std::size_t k = 0; k < K; ++k) {
235 if (k < sn.issignal.size() && sn.issignal[k]) is_buffered[k] = false;
236 if (is_buffered[k]) ++Kb;
237 }
238 // PREEMPT FAMILY. A preempt-family station stores the phase of EVERY job it
239 // holds, the buffered ones as [class, phase] pairs, so a class-k job there is
240 // phasessz(i,k) states on its own. Pricing only the jobs in service read a
241 // FCFSPRPRIO queue with an Erlang-2 class as 6862 states at cutoff 6 against
242 // 148863 enumerated. Such a station enters the ordered DP even with a single
243 // buffered class, carrying the per-job phase weight there.
244 const auto is_preempt = [](SchedStrategy s) {
245 return s == SchedStrategy::FCFSPR || s == SchedStrategy::FCFSPI ||
246 s == SchedStrategy::LCFSPR || s == SchedStrategy::LCFSPI ||
247 s == SchedStrategy::FCFSPRPRIO || s == SchedStrategy::FCFSPIPRIO ||
248 s == SchedStrategy::LCFSPRPRIO || s == SchedStrategy::LCFSPIPRIO ||
249 s == SchedStrategy::RR;
250 };
251 const auto is_ordered = [&](std::size_t i) {
252 const SchedStrategy s = sn.stations[i].sched;
253 return is_preempt(s) || (Kb > 1 && !(s == SchedStrategy::EXT || is_share(s)));
254 };
255 const auto log_phases = [&](std::size_t i, std::size_t k) {
256 const double p = static_cast<double>(sn.phasessz_of(i + 1, k + 1));
257 return (std::isfinite(p) && p > 1.0) ? std::log(p) : 0.0;
258 };
259 std::size_t n_ord = 0;
260 for (std::size_t i = 0; i < M; ++i)
261 if (is_ordered(i)) ++n_ord;
262
263 double log_nstates = 0.0;
264 std::vector<double> nk_eff(K, 0.0);
265 const std::vector<double>& njobs = sn.njobs();
266 for (std::size_t k = 0; k < K; ++k)
267 nk_eff[k] = (k < njobs.size() && std::isfinite(njobs[k])) ? njobs[k] : cutoff;
268
269 const auto place = [&](double nk, double ms) {
270 return std::lgamma(1.0 + nk + ms - 1.0) - std::lgamma(1.0 + ms - 1.0) -
271 std::lgamma(1.0 + nk);
272 };
273 // A ZERO per-class capacity means the class is DISABLED at that station, so it
274 // never occupies a slot and the placement term must spread it over the
275 // stations that admit it, not over all M. ld_whittle_bandwidth disables each
276 // of its three PS routes for the other two classes; counting all M=4 priced it
277 // at C(9,6)^3 = 592704 states, 7852 GB under the quadratic byte model, and the
278 // gate refused a model whose true space is 7^3 = 343 and solves at once.
279 const auto disabled_at = [&](std::size_t i, std::size_t k) {
280 return i < sn.classcap.size() && k < sn.classcap[i].size() && sn.classcap[i][k] == 0.0;
281 };
282 // ZERO VISITS IS THE SECOND WAY IN, and it is the state space generator's own
283 // rule: a (station, class) pair whose chain visit ratio is zero gets capacity
284 // 0 there, because a class the routing never delivers to a station cannot be
285 // there. Only an SPN PLACE escapes, keeping its declared initial marking, so a
286 // Place is left alone here too. Reading only classcap missed all of it, since
287 // that zeroes a pair whose SERVICE RATE is NaN -- the class is disabled at the
288 // station, not unrouted to it. A class that exists only as a switch target on
289 // some departure arc has a good service rate everywhere and a visit ratio of
290 // zero everywhere: a Source -> FCFS Queue -> Sink model with such a class was
291 // priced at 184755 states against the 11 the generator enumerates, predicted
292 // 17.62 GB, and had the gate refuse a model that solves in 0.05 s.
293 // Visit rows are STATEFUL-node indexed, not station indexed. A pair the chain
294 // never enters carries floating-point noise (~1e-17) rather than an exact 0,
295 // which is the tolerance below, shared with the generator.
296 const double kVisitZeroTol = 1.0e-12;
297 std::vector<std::vector<bool>> never(M, std::vector<bool>(K, false));
298 if (!sn.visits.empty() && !sn.chains.empty()) {
299 for (std::size_t i = 0; i < M; ++i) {
300 if (sn.stations[i].nodetype == lang::NodeType::Place) continue;
301 const std::size_t nd = sn.node_of_station(i + 1);
302 if (nd == 0) continue;
303 const std::size_t isf1 = sn.stateful_index(nd);
304 if (isf1 == 0) continue;
305 const std::size_t isf = isf1 - 1;
306 for (std::size_t k = 0; k < K; ++k) {
307 std::size_t c = sn.nchains;
308 for (std::size_t ci = 0; ci < sn.chains.size(); ++ci)
309 if (k < sn.chains[ci].size() && sn.chains[ci][k]) { c = ci; break; }
310 if (c >= sn.visits.size()) continue;
311 const auto& V = sn.visits[c];
312 if (isf >= V.rows() || k >= V.cols()) continue;
313 const double v = num_traits<T>::to_double(V(isf, k));
314 if (std::isfinite(v) && std::fabs(v) < kVisitZeroTol) never[i][k] = true;
315 }
316 }
317 }
318 const auto admits = [&](std::size_t i, std::size_t k) {
319 return !disabled_at(i, k) && !never[i][k];
320 };
321 const auto admitting_count = [&](std::size_t k) {
322 std::size_t n = 0;
323 for (std::size_t i = 0; i < M; ++i)
324 if (admits(i, k)) ++n;
325 return n;
326 };
327 const auto admitting_all = [&](std::size_t k) {
328 const std::size_t n = admitting_count(k);
329 return n < 1 ? std::size_t(1) : n;
330 };
331 const auto admitting_rem = [&](std::size_t k) {
332 std::size_t n = 0;
333 for (std::size_t i = 0; i < M; ++i) {
334 if (is_ordered(i)) continue;
335 if (admits(i, k)) ++n;
336 }
337 return n;
338 };
339 // A class no station admits holds no job anywhere, so its population is zero
340 // rather than the cutoff; leaving it at the cutoff made the class occupy
341 // ordered buffers it can never enter.
342 for (std::size_t k = 0; k < K; ++k)
343 if (nk_eff[k] > 0.0 && admitting_count(k) == 0) nk_eff[k] = 0.0;
344 // AN OPEN CLASS HOLDS AT MOST nk_eff JOBS, not exactly nk_eff: the generator enumerates 0..cutoff of them. The
345 // Source played that slack bin only for a class it emits; a class that exists only as a switch target downstream
346 // (a cache's hit and miss classes) has zero visits at the Source and was priced at ONE placement: an open LRU
347 // cache feeding a Delay and a PS queue read 24 states against 11616. So an open class takes C(n+M', M') over the
348 // M' non-Source stations admitting it, a closed one C(n+M-1, M-1) over all of them (MATLAB ctmc_placement_term).
349 const auto placement_term = [&](std::size_t k, double n) {
350 const bool is_open = !(k < njobs.size() && std::isfinite(njobs[k]));
351 if (!is_open) return place(n, static_cast<double>(admitting_all(k)));
352 std::size_t mopen = 0;
353 for (std::size_t i = 0; i < M; ++i)
354 if (sn.stations[i].sched != SchedStrategy::EXT && admits(i, k)) ++mopen;
355 const double mo = static_cast<double>(mopen);
356 return std::lgamma(1.0 + n + mo) - std::lgamma(1.0 + mo) - std::lgamma(1.0 + n);
357 };
358
359 // CLOSED CHAINS THAT SWITCH CLASS conserve their population per CHAIN, not per class: LINE gives the reference
360 // class all N jobs and the switch targets none, so per-class terms placed N jobs over the reference class's
361 // stations and NONE over the targets'. A closed cache loop (Delay -> Cache -> Delay, hit/miss classes back, 10
362 // items capacity 3) read 2880 states against 4320, a closed switching tandem 4 against 10. Such a chain is ONE
363 // population unit: its N jobs spread over every admitted (station, class) pair, C(N+P-1, P-1), and at an ordered
364 // station the DP sums over the chain's class mixes there. Each class of the chain may hold up to N jobs, which
365 // also bounds its phase and capacity terms (MATLAB ctmc_state_space_logsize.m, _kb/06).
366 std::vector<std::size_t> unit_of(K, 0);
367 std::vector<std::vector<std::size_t>> unit_cls;
368 for (std::size_t ci = 0; ci < sn.chains.size(); ++ci) {
369 std::vector<std::size_t> cls;
370 for (std::size_t k = 0; k < K && k < sn.chains[ci].size(); ++k)
371 if (sn.chains[ci][k] && k < njobs.size() && std::isfinite(njobs[k]) && admitting_count(k) > 0)
372 cls.push_back(k);
373 if (cls.size() > 1) {
374 double ntot = 0.0;
375 for (std::size_t k : cls) ntot += njobs[k];
376 unit_cls.push_back(cls);
377 for (std::size_t k : cls) { nk_eff[k] = ntot; unit_of[k] = unit_cls.size(); }
378 }
379 }
380 for (std::size_t k = 0; k < K; ++k)
381 if (unit_of[k] == 0) { unit_cls.push_back(std::vector<std::size_t>{k}); unit_of[k] = unit_cls.size(); }
382 // unit_of is 1-based from here on; unit_cls[unit_of[k] - 1] holds class k
383 const auto unit_closed = [&](std::size_t u) {
384 const std::size_t k = unit_cls[u][0];
385 return k < njobs.size() && std::isfinite(njobs[k]);
386 };
387
388 // Placements and buffer orderings with per-class populations nk.
389 const auto placement_block = [&](const std::vector<double>& nk) -> double {
390 double lp = 0.0;
391 if (n_ord == 0) {
392 for (std::size_t u = 0; u < unit_cls.size(); ++u) {
393 const std::vector<std::size_t>& cls = unit_cls[u];
394 if (cls.size() == 1) { lp += placement_term(cls[0], nk[cls[0]]); continue; }
395 const double n = nk[cls[0]];
396 double P = 0.0;
397 for (std::size_t k : cls) P += static_cast<double>(admitting_count(k));
398 P = std::max(P, 1.0);
399 lp += std::lgamma(n + P) - std::lgamma(P) - std::lgamma(n + 1.0);
400 }
401 return lp;
402 }
403 for (std::size_t k = 0; k < K; ++k)
404 if (!is_buffered[k]) lp += placement_term(k, nk[k]);
405 std::vector<int> njb;
406 std::vector<std::size_t> bunits; // 0-based unit ids, first-seen order over the buffered classes
407 std::vector<std::size_t> b_unit_of;
408 for (std::size_t k = 0; k < K; ++k) {
409 if (!is_buffered[k]) continue;
410 njb.push_back(static_cast<int>(std::floor(nk[k])));
411 const std::size_t u = unit_of[k] - 1;
412 std::size_t bu = 0;
413 while (bu < bunits.size() && bunits[bu] != u) ++bu;
414 if (bu == bunits.size()) bunits.push_back(u);
415 b_unit_of.push_back(bu);
416 }
417 std::vector<int> nj_u;
418 std::vector<double> m_rem_u;
419 double grid = 1.0;
420 for (std::size_t u : bunits) {
421 const int n = static_cast<int>(std::floor(nk[unit_cls[u][0]]));
422 nj_u.push_back(n);
423 grid *= static_cast<double>(n) + 1.0;
424 double mr = 0.0;
425 for (std::size_t k : unit_cls[u]) mr += static_cast<double>(admitting_rem(k));
426 m_rem_u.push_back(mr);
427 }
428 if (grid <= kOrderGridMax) {
429 std::vector<std::vector<int>> caps_per;
430 std::vector<std::vector<double>> log_ph_per;
431 std::vector<std::vector<int>> lev_per;
432 std::vector<double> cap_tot;
433 for (std::size_t i = 0; i < M; ++i) {
434 if (!is_ordered(i)) continue;
435 std::vector<double> lph;
436 for (std::size_t k = 0; k < K; ++k)
437 if (is_buffered[k]) lph.push_back(is_preempt(sn.stations[i].sched) ? log_phases(i, k) : 0.0);
438 log_ph_per.push_back(lph);
439 std::vector<int> lev;
440 if (sn.stations[i].sched == SchedStrategy::LCFSPRIO)
441 for (std::size_t k = 0; k < K; ++k)
442 if (is_buffered[k]) lev.push_back(k < sn.classes.size() ? sn.classes[k].prio : 0);
443 lev_per.push_back(lev);
444 std::vector<int> per;
445 std::size_t bi = 0;
446 for (std::size_t k = 0; k < K; ++k)
447 if (is_buffered[k]) {
448 int c = njb[bi];
449 if (i < sn.classcap.size() && k < sn.classcap[i].size() &&
450 std::isfinite(sn.classcap[i][k]))
451 c = std::min(c, static_cast<int>(std::floor(sn.classcap[i][k])));
452 if (!admits(i, k)) c = 0;
453 per.push_back(c);
454 ++bi;
455 }
456 caps_per.push_back(per);
457 const double c = sn.stations[i].cap;
458 cap_tot.push_back(std::isfinite(c) && c >= 0
459 ? std::floor(c) : std::numeric_limits<double>::infinity());
460 }
461 return lp + log_ordered_joint(caps_per, cap_tot, nj_u, b_unit_of, m_rem_u, log_ph_per, lev_per);
462 }
463 double total = 0.0;
464 for (int c : njb) total += c;
465 const std::size_t nb = njb.size();
466 if (nb > 1) { // one class has a single sequence; only a preempt station gets here with it
467 const double lkb = std::log(static_cast<double>(nb));
468 lp += static_cast<double>(n_ord) *
469 ((total + 1.0) * lkb - std::log(static_cast<double>(nb) - 1.0) +
470 std::log1p(-std::exp(-(total + 1.0) * lkb)));
471 }
472 for (std::size_t i = 0; i < M; ++i)
473 if (is_preempt(sn.stations[i].sched))
474 for (std::size_t k = 0; k < K; ++k)
475 if (is_buffered[k]) lp += nk[k] * log_phases(i, k);
476 for (std::size_t k = 0; k < K; ++k)
477 if (is_buffered[k]) {
478 const std::size_t mk = admitting_rem(k);
479 if (mk >= 1) lp += place(nk[k], static_cast<double>(mk));
480 }
481 return lp;
482 };
483
484 // A CACHE'S IN-TRANSIT SLOT holds no job or one job of a class visiting the cache. A closed job held there is
485 // NOT at a station, so it is taken out of its chain's population and the placements are SUMMED over the slot
486 // states; multiplying them by (1 + nin) counted that job twice and read the closed cache loop at 2x truth. An
487 // open job's truncated population is not reduced, as the generator enumerates it.
488 std::vector<std::vector<std::size_t>> transit; // per Cache node, the visiting classes
489 for (const auto& kv : sn.nodeparam) {
490 const std::size_t ind = kv.first;
491 if (ind == 0 || ind > sn.nodes.size() || sn.nodes[ind - 1].nodetype != lang::NodeType::Cache) continue;
492 if (kv.second.nitems == 0) continue;
493 std::vector<std::size_t> cls;
494 const std::size_t isf1 = sn.stateful_index(ind);
495 if (isf1 > 0 && !sn.visits.empty()) {
496 for (std::size_t k = 0; k < K; ++k) {
497 std::size_t c = sn.chains.size();
498 for (std::size_t ci = 0; ci < sn.chains.size(); ++ci)
499 if (k < sn.chains[ci].size() && sn.chains[ci][k]) { c = ci; break; }
500 if (c >= sn.visits.size()) continue;
501 const auto& V = sn.visits[c];
502 if (isf1 - 1 >= V.rows() || k >= V.cols()) continue;
503 if (num_traits<T>::to_double(V(isf1 - 1, k)) > kVisitZeroTol) cls.push_back(k);
504 }
505 }
506 transit.push_back(cls);
507 }
508 // per cache: (unit + 1, weight) options, unit 0 = empty slot of weight 1; weight = #visiting classes of the unit
509 std::vector<std::vector<std::pair<std::size_t, double>>> topts;
510 double combos = 1.0;
511 for (const auto& cls : transit) {
512 std::vector<std::pair<std::size_t, double>> o{{0, 1.0}};
513 for (std::size_t k : cls) {
514 const std::size_t u1 = unit_of[k];
515 bool found = false;
516 for (auto& e : o)
517 if (e.first == u1) { e.second += 1.0; found = true; }
518 if (!found) o.push_back({u1, 1.0});
519 }
520 combos *= static_cast<double>(o.size());
521 topts.push_back(o);
522 }
523 if (transit.empty() || combos > 4096.0) {
524 log_nstates += placement_block(nk_eff);
525 for (const auto& cls : transit) log_nstates += std::log(1.0 + static_cast<double>(cls.size()));
526 } else {
527 std::vector<double> vals;
528 std::vector<std::size_t> pick(topts.size(), 0);
529 for (;;) {
530 std::vector<double> nk = nk_eff;
531 double lw = 0.0;
532 bool ok = true;
533 for (std::size_t t = 0; t < topts.size(); ++t) {
534 const std::size_t u1 = topts[t][pick[t]].first;
535 if (u1 == 0) continue;
536 lw += std::log(topts[t][pick[t]].second);
537 const std::size_t u = u1 - 1;
538 if (unit_closed(u)) {
539 for (std::size_t k : unit_cls[u]) nk[k] -= 1.0;
540 if (nk[unit_cls[u][0]] < 0.0) ok = false;
541 }
542 }
543 if (ok) vals.push_back(lw + placement_block(nk));
544 std::size_t pos = 0;
545 while (pos < topts.size() && pick[pos] + 1 == topts[pos].size()) { pick[pos] = 0; ++pos; }
546 if (pos == topts.size()) break;
547 ++pick[pos];
548 }
549 double top = vals[0];
550 for (double v : vals) top = std::max(top, v);
551 double acc = 0.0;
552 for (double v : vals) acc += std::exp(v - top);
553 log_nstates += top + std::log(acc);
554 }
555
556 // A sharing discipline can hold every job in service at once; a queueing one
557 // holds at most its server count.
558 for (std::size_t i = 1; i <= M; ++i) {
559 const SchedStrategy sched = sn.stations[i - 1].sched;
560 const bool share = sched == SchedStrategy::INF || sched == SchedStrategy::PS ||
561 sched == SchedStrategy::DPS || sched == SchedStrategy::GPS ||
562 sched == SchedStrategy::PSPRIO || sched == SchedStrategy::DPSPRIO ||
563 sched == SchedStrategy::GPSPRIO || sched == SchedStrategy::LPS;
564 if (is_preempt(sched)) continue; // every job's phase is already in the ordered term
565 for (std::size_t r = 1; r <= K; ++r) {
566 const double p = static_cast<double>(sn.phasessz_of(i, r));
567 if (!std::isfinite(p) || p <= 1.0 || !admits(i - 1, r - 1)) continue; // no job, no phase to keep
568 double m;
569 if (sched == SchedStrategy::EXT)
570 m = 1.0;
571 else if (share)
572 m = nk_eff[r - 1];
573 else
574 m = std::min(nk_eff[r - 1], sn.stations[i - 1].nservers);
575 if (!std::isfinite(m)) m = nk_eff[r - 1];
576 log_nstates += std::lgamma(1.0 + m + p - 1.0) - std::lgamma(1.0 + p - 1.0) -
577 std::lgamma(1.0 + m);
578 }
579 }
580
581 // Round-robin routing is stateful: the pointer over the outgoing links is
582 // part of the state. The reference reads `sn.connmatrix`; this port has no
583 // such field and takes the out-degree from `rtnodes`, the same graph after
584 // the refresh resolved the routing strategies.
585 for (std::size_t ind = 1; ind <= sn.nodes.size(); ++ind) {
586 const std::vector<RoutingStrategy>& rt_i = sn.nodes[ind - 1].routing;
587 std::size_t nrr = 0;
588 for (std::size_t r = 0; r < K && r < rt_i.size(); ++r)
589 if (rt_i[r] == RoutingStrategy::RROBIN || rt_i[r] == RoutingStrategy::WRROBIN) ++nrr;
590 if (nrr == 0) continue;
591 const std::size_t nout = sn.downstream_stations(ind).size();
592 if (nout <= 1) continue;
593 log_nstates += static_cast<double>(nrr) * std::log(static_cast<double>(nout));
594 }
595
596 // 5. cache content: a Cache node keeps the ORDERED placement of C = sum(itemcap) distinct items out of n,
597 // n!/(n-C)! local states, times the subsets of at most retrieval_capacity of the other n-C items in its
598 // retrieval system. Leaving it out priced an open LRU cache (10 items, capacity 3) as if it had one local
599 // state and let a chain 720 times larger run.
600 for (const auto& kv : sn.nodeparam) {
601 const std::size_t ind = kv.first;
602 if (ind == 0 || ind > sn.nodes.size() || sn.nodes[ind - 1].nodetype != lang::NodeType::Cache) continue;
603 const qn::CacheParam<T>& cp = kv.second;
604 if (cp.nitems == 0) continue;
605 const double n = static_cast<double>(cp.nitems);
606 double csum = 0.0;
607 for (int c : cp.itemcap) csum += c;
608 const double C = std::min(csum, n);
609 log_nstates += std::lgamma(n + 1.0) - std::lgamma(n - C + 1.0);
610 // the job in transit is priced with the placements above, summed over the slot states
611 if (cp.retrieval_capacity > 0) {
612 const double rest = n - C;
613 const double smax = std::min(static_cast<double>(cp.retrieval_capacity), rest);
614 std::vector<double> lt;
615 for (double sv = 0.0; sv <= smax; sv += 1.0)
616 lt.push_back(std::lgamma(rest + 1.0) - std::lgamma(sv + 1.0) - std::lgamma(rest - sv + 1.0));
617 double top = lt[0];
618 for (double v : lt) top = std::max(top, v);
619 double acc = 0.0;
620 for (double v : lt) acc += std::exp(v - top);
621 log_nstates += top + std::log(acc);
622 }
623 }
624
625 // 6. TRUNCATED FORK-JOIN. An open chain through a Fork, or a quorum join, is
626 // solved over T = cutoff sibling tags (`qn::fj_tag`), and every tag can sit
627 // at every station of its branch: the reachable space carries the SET
628 // (sharing station) or the SEQUENCE (ordered station) of tags held there,
629 // which the placement count above never sees (python `_fj_tag_log_size`).
630 // A closed chain through a standard join is priced by the placements.
631 for (std::size_t p = 0; p < sn.fj.size(); ++p) {
632 const std::size_t f = sn.fj[p].first, j = sn.fj[p].second;
633 if (f == 0 || j == 0 || f > sn.nodes.size() || sn.rtnodes.rows() < sn.nodes.size() * K) continue;
634 for (std::size_t r = 0; r < K; ++r) {
635 std::vector<std::size_t> heads;
636 for (std::size_t c = 1; c <= sn.nodes.size(); ++c)
637 if (num_traits<T>::to_double(sn.rtnodes((f - 1) * K + r, (c - 1) * K + r)) > 0)
638 heads.push_back(c);
639 if (heads.size() < 2) continue;
640 const bool quorum = ::line::sn::sn_join_quorum(sn, j, heads.size()) < heads.size();
641 const bool closed = r < njobs.size() && std::isfinite(njobs[r]);
642 if (closed && !quorum) continue;
643 const double Tt = std::max(cutoff, closed ? njobs[r] : 0.0);
644 std::vector<bool> seen(sn.nodes.size() + 1, false);
645 std::vector<std::size_t> frontier = heads;
646 while (!frontier.empty()) {
647 const std::size_t nd = frontier.back();
648 frontier.pop_back();
649 if (nd == j || seen[nd]) continue;
650 seen[nd] = true;
651 for (std::size_t c = 1; c <= sn.nodes.size(); ++c)
652 if (num_traits<T>::to_double(sn.rtnodes((nd - 1) * K + r, (c - 1) * K + r)) > 0)
653 frontier.push_back(c);
654 }
655 for (std::size_t nd = 1; nd <= sn.nodes.size(); ++nd) {
656 if (!seen[nd] || sn.nodes[nd - 1].station == 0) continue;
657 if (is_share(sn.stations[sn.nodes[nd - 1].station - 1].sched)) {
658 log_nstates += Tt * std::log(2.0);
659 } else {
660 // sequences of distinct tags: sum_k T!/(T-k)!
661 std::vector<double> lt;
662 for (double k = 0.0; k <= Tt; k += 1.0)
663 lt.push_back(std::lgamma(Tt + 1.0) - std::lgamma(Tt - k + 1.0));
664 double top = lt[0];
665 for (double v : lt) top = std::max(top, v);
666 double acc = 0.0;
667 for (double v : lt) acc += std::exp(v - top);
668 log_nstates += top + std::log(acc);
669 }
670 }
671 }
672 }
673
674 return log_nstates;
675}
676
677} // namespace mc
678} // namespace line
679
680#endif // LINE_API_MC_CTMC_STATE_SPACE_LOGSIZE_H
A network plus its refreshed NetworkStruct.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
RoutingStrategy
Routing strategies, with the values of MATLAB RoutingStrategy.
Definition lang_types.h:430
double log_ordered_joint(const std::vector< std::vector< int > > &caps_per, const std::vector< double > &cap_tot, const std::vector< int > &nj_u, const std::vector< std::size_t > &b_unit_of, const std::vector< double > &m_rem_u, const std::vector< std::vector< double > > &log_ph_per={}, const std::vector< std::vector< int > > &lev_per={})
Log count of (placement, ordering) configurations over ALL order-preserving stations at once,...
double ctmc_state_space_logsize(const qn::NetworkStruct< T > &sn, const CtmcSizeOptions &opt=CtmcSizeOptions())
Worst-case log state-space size of sn.
constexpr double kOrderGridMax
Largest (m_1..m_K) box the exact ordered-buffer DP will walk.
std::size_t sn_join_quorum(const qn::NetworkStruct< T > &sn, std::size_t joinNode, std::size_t nbranches)
The number of siblings the Join node joinNode (1-based) fires on, out of nbranches forked.
Mat mult(const Mat &A, const Mat &B)
Definition spn_ispn.h:173
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Quorum joins: how many siblings a Join fires on, and what that drops.
Options the estimator reads; only the cutoff matters.
double cutoff
< 0 or non-finite = not given, take the solver default
The parameters of a Cache node, MATLAB's sn.nodeparam{ind} for a Cache.
int retrieval_capacity
Delayed-hit retrieval system (Cache.setRetrievalSystem).
std::vector< int > itemcap