LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
cache_miss_rmf.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_CACHE_CACHE_MISS_RMF_H
6#define LINE_API_CACHE_CACHE_MISS_RMF_H
7
8/**
9 * @file
10 * @ingroup api_cache
11 * Refined mean field (RMF) miss rates of a multi-list RANDOM(m) cache.
12 *
13 * Templated port of matlab/src/api/cache/cache_miss_rmf.m, including its
14 * nested rmf_drift, rmf_jacobian, rmf_hessian, rmf_noise_matrix,
15 * rmf_fixed_point, rmf_dimension_reduction and rmf_expansion_steady_state.
16 *
17 * The model is a density dependent population process (DDPP) whose state
18 * x(i,k) is the probability that item i sits in list k, k = 0 meaning "not
19 * cached". A request for item i in list k promotes it to list k+1 and demotes
20 * a uniformly chosen occupant of list k+1, so the drift is
21 *
22 * flow(i,k) = p(i) x(i,k) - hit(k) x(i,k+1) / m(k+1),
23 * dx(i,k)/dt -= flow(i,k), dx(i,k+1)/dt += flow(i,k),
24 * hit(k) = sum_j p(j) x(j,k),
25 *
26 * for k = 0..h-1. The mean-field fixed point pi is the t -> infinity limit of
27 * that drift, and Gast's refinement adds the 1/N correction
28 *
29 * E[X] = pi + V/N + O(1/N^2), V = -(F')^-1 (1/2) sum_{b,c} F''_{bc} W_bc,
30 *
31 * with W the solution of the Lyapunov equation F' W + W F'^T + Q = 0 and Q the
32 * noise intensity of the DDPP. Reference: N. Gast, "Expected Values Estimated
33 * via Mean-Field Approximation are 1/N-Accurate", POMACS 2017.
34 *
35 * WHAT UNBLOCKED THIS. The fixed point is reached by integrating the drift to
36 * t = 1e4, which MATLAB does with ode15s. The drift is stiff: the per-item
37 * request rates p(i) of a Zipf-like popularity profile span several orders of
38 * magnitude, so the fast items equilibrate in O(1/p_max) while the slow ones
39 * need O(1/p_min), and an explicit integrator would be pinned to the fast
40 * scale for the whole horizon. The port now has line/util/ode.h, an adaptive
41 * Rosenbrock-4 with an embedded lower-order estimate, and this file uses it.
42 * The Jacobian is supplied analytically (rmf_jacobian is needed for the
43 * refinement anyway), so the integrator never forms a numeric one here.
44 *
45 * ALIGNMENT WITH THE REFERENCE. The fixed point is the single ode15s-style
46 * integration at RelTol 1e-8 / AbsTol 1e-10 over [0, tmax] that
47 * rmf_fixed_point performs (an earlier edition of this port polished it with a
48 * second pass at 1e-13 / 1e-16, which is no longer done), and the rank of the
49 * Jacobian is decided by a threshold of 1e-8 relative to sigma_1, the rule
50 * MATLAB, the JAR and python also use since BUG-102 (2026-10-06): rank()'s
51 * max(size) * eps(sigma_1) flipped on ULP-level changes of lambda, because the
52 * 1e-10 fixed point lifts one exact null direction to a singular value of that
53 * order, while the nonzero ones are O(0.1). Both choices are what makes
54 * the port return the reference's answer, including on the cases where the
55 * reference's refinement comes out non-finite and cache_miss_rmf.m silently
56 * keeps the plain mean-field fixed point: the port now takes the same branch
57 * there, which the result's `refined` flag reports. The one remaining
58 * difference is in the basis of the null space: LAPACK's SVD at T = double
59 * (the same quantity MATLAB reads out of svd(Fp)) and a pivoted elimination
60 * plus Gram-Schmidt at any other T. Only the SUBSPACE affects the result:
61 * writing C = [C1; C2], the block C1 Fp D1 that the reduction keeps depends on
62 * C2 only through ker(C2), and the expansion V = D1 V_r likewise, so any basis
63 * of the same subspace gives the same V.
64 * The LNA stationary covariance of the occupancy (rmf_lna_covariance) is
65 * returned as `Wcov` at T = double, empty when it is not computable or not
66 * finite, exactly as the reference's try/catch leaves it.
67 * MEASURED AGREEMENT (MATLAB R2025a, T = double), global miss rate M, as
68 * recorded for the earlier edition of this port:
69 * lambda = [0.5 0.3 0.15 0.05; 0.1 0.2 0.3 0.4] (2 users, 4 items)
70 * m = [2] MATLAB 0.992377021789716
71 * m = [1 2] MATLAB 0.490863272756674
72 * lambda = [49 49 49 49 7 1 1]/205 (1 user, 7 items)
73 * m = [1 1 3] MATLAB 0.024017720168154
74 * m = [3] MATLAB 0.348362022568998 (refinement rejected as non-finite)
75 * When the reduced Jacobian is singular -- a small or non-hyperbolic fixed
76 * point -- MATLAB's linear solves emit a warning and return non-finite
77 * entries, which cache_miss_rmf.m detects and discards, keeping the plain
78 * mean-field fixed point. The port raises NumericError from the same solves
79 * and catches it in the same place, with the same outcome; the result carries
80 * a `refined` flag saying which of the two was used, which the reference does
81 * not expose.
82 *
83 * ARITHMETIC. Gated: the fixed point is reached by a tolerance-driven
84 * integration, so it is an approximation in any arithmetic.
85 *
86 * COST. The Hessian is a dense model_dim^3 tensor and the Lyapunov solve is a
87 * dense rk^2 by rk^2 system, exactly as in the reference. With n items and h
88 * lists model_dim = n(h+1), so the refinement is practical for tens of items
89 * and quickly stops being so; the plain mean-field path has no such limit.
90 */
91
92#include <algorithm>
93#include <cmath>
94#include <complex>
95#include <cstddef>
96#include <limits>
97#include <type_traits>
98#include <vector>
99
100#include "line/num/number.h"
101#include "line/util/error.h"
102#include "line/util/eig.h"
103#include "line/util/linalg.h"
104#include "line/util/lu.h"
105#include "line/util/matrix.h"
106#include "line/util/lsoda.h"
107#include "line/util/ode.h"
108#include "line/util/sylvester.h"
110
111namespace line {
112namespace cache {
113
114/** Flat index of (item i, list k), k = 0 meaning "not cached" (rmf_index.m). */
115inline std::size_t cache_miss_rmf_index(std::size_t i, std::size_t k, std::size_t n_items) {
116 return i + k * n_items;
117}
118
119/** Return value of cache_miss_rmf, mirroring [M,MU,MI,pi0,tout,pi0_t,MU_t,xtraj]. */
120template <class T>
122 T M; ///< global miss rate
123 std::vector<T> MU; ///< (u) per-user miss rate
124 std::vector<T> MI; ///< (n_items) per-item miss rate
125 std::vector<T> pi0; ///< (n_items) per-item miss probability, clipped to [0,1]
126 std::vector<T> xss; ///< the occupancy the metrics were read from
127 bool refined = false; ///< true when the 1/N correction was accepted
128 /** LNA stationary covariance of the occupancy (model_dim x model_dim); empty when unavailable or not finite, and at T != double. */
130 std::vector<T> tout; ///< transient time grid, empty unless tspan was given
131 Matrix<T> pi0_t; ///< (n_items x nt) transient miss probability
132 Matrix<T> MU_t; ///< (u x nt) transient per-user miss rate
133 Matrix<T> xtraj; ///< (model_dim x nt) transient occupancy
134 /** Grid-independent average of the transient against a clock; empty unless one was given. */
136};
137
138namespace rmf_detail {
139
140/** hit rate of list `level`: sum_i p(i) x(i,level) (rmf_hit_rate.m). */
141template <class T>
142T hit_rate(const std::vector<T>& x, const std::vector<T>& p, std::size_t level,
143 std::size_t n_items) {
144 T hr = num_traits<T>::from_int(0);
145 for (std::size_t i = 0; i < n_items; ++i) hr += p[i] * x[cache_miss_rmf_index(i, level, n_items)];
146 return hr;
147}
148
149/** Mean-field drift F(x) (rmf_drift.m). */
150template <class T>
151std::vector<T> drift(const std::vector<T>& x, const std::vector<T>& p, const std::vector<T>& m,
152 std::size_t n_items, std::size_t h) {
153 const T zero = num_traits<T>::from_int(0);
154 const std::size_t model_dim = n_items * (h + 1);
155 std::vector<T> hr(h + 1, zero);
156 for (std::size_t k = 0; k <= h; ++k) hr[k] = hit_rate(x, p, k, n_items);
157 std::vector<T> dX(model_dim, zero);
158 for (std::size_t i = 0; i < n_items; ++i) {
159 for (std::size_t k = 0; k + 1 <= h; ++k) {
160 const std::size_t ik = cache_miss_rmf_index(i, k, n_items);
161 const std::size_t ik1 = cache_miss_rmf_index(i, k + 1, n_items);
162 const T flow = p[i] * x[ik] - hr[k] * x[ik1] / m[k];
163 dX[ik] -= flow;
164 dX[ik1] += flow;
165 }
166 }
167 return dX;
168}
169
170/**
171 * dF/dx at x (rmf_jacobian.m).
172 *
173 * NOT A DEFECT, although it differs from a central difference of drift().
174 * This transcribes rmf_jacobian in cache_miss_rmf.m, which is the exact
175 * derivative of the PAIRWISE (per-transition) drift of the density-dependent
176 * population process, in which item i at list k swaps with an explicitly
177 * chosen item j of list k+1 at rate p(i) x(i,k) x(j,k+1) / m(k+1). drift()
178 * implements the AGGREGATED form
179 *
180 * flow(i,k) = p(i) x(i,k) - hit(k) x(i,k+1) / m(k+1), hit(k) = sum_j p(j) x(j,k)
181 *
182 * obtained by summing over j with sum_j x(j,k+1) = m(k+1). The two drifts agree
183 * on that conserved manifold, so they share every trajectory and fixed point,
184 * but their derivatives differ in the directions that leave it: the pairwise
185 * form contributes a further -p(i) x(i,k)/m(k+1) to d flow(i,k) / d x(j,k+1)
186 * for EVERY j, which is what the test pins against central differences of
187 * drift() (about 10 percent at those entries).
188 *
189 * The refined mean-field correction (Gast and Van Houdt) is built from the
190 * first and second derivatives of the transition-level drift, so the pairwise
191 * derivatives are the right ones here, and hessian() is the matching second
192 * derivative. Checked numerically (2026-11-14): jacobian() matches central
193 * differences of the pairwise drift to 5.7e-12 at the fixed point, and
194 * replacing it and hessian() by derivatives of the aggregated drift moves the
195 * refined miss ratio AWAY from the exact RANDOM(m) product form. It is not used
196 * as an integration Jacobian: the fixed point is integrated with a numeric
197 * Jacobian, as the reference's own ode15s call does.
198 */
199template <class T>
200Matrix<T> jacobian(const std::vector<T>& x, const std::vector<T>& p, const std::vector<T>& m,
201 std::size_t n_items, std::size_t h) {
202 const T zero = num_traits<T>::from_int(0);
203 const std::size_t model_dim = n_items * (h + 1);
204 std::vector<T> hr(h + 1, zero);
205 for (std::size_t k = 0; k <= h; ++k) hr[k] = hit_rate(x, p, k, n_items);
206 Matrix<T> Fp(model_dim, model_dim, zero);
207 for (std::size_t i = 0; i < n_items; ++i) {
208 for (std::size_t k = 0; k + 1 <= h; ++k) {
209 const std::size_t ik = cache_miss_rmf_index(i, k, n_items);
210 const std::size_t ik1 = cache_miss_rmf_index(i, k + 1, n_items);
211 Fp(ik, ik) -= p[i];
212 Fp(ik1, ik) += p[i];
213 Fp(ik, ik1) += hr[k] / m[k];
214 Fp(ik1, ik1) -= hr[k] / m[k];
215 for (std::size_t j = 0; j < n_items; ++j) {
216 const std::size_t jk = cache_miss_rmf_index(j, k, n_items);
217 const std::size_t jk1 = cache_miss_rmf_index(j, k + 1, n_items);
218 Fp(ik, jk1) -= p[i] * x[ik] / m[k];
219 Fp(ik1, jk1) += p[i] * x[ik] / m[k];
220 Fp(ik, jk) += p[j] * x[ik1] / m[k];
221 Fp(ik1, jk) -= p[j] * x[ik1] / m[k];
222 }
223 }
224 }
225 return Fp;
226}
227
228/**
229 * d^2F/dx^2 (rmf_hessian.m), stored flat: H[(a*model_dim + b)*model_dim + c] is
230 * d^2 F_a / (dx_b dx_c). The drift is quadratic, so the Hessian does not
231 * depend on x, exactly as in the reference.
232 */
233template <class T>
234std::vector<T> hessian(const std::vector<T>& p, const std::vector<T>& m, std::size_t n_items,
235 std::size_t h) {
236 const T zero = num_traits<T>::from_int(0);
237 const std::size_t md = n_items * (h + 1);
238 std::vector<T> H(md * md * md, zero);
239 const auto at = [md](std::size_t a, std::size_t b, std::size_t c) {
240 return (a * md + b) * md + c;
241 };
242 for (std::size_t i = 0; i < n_items; ++i) {
243 for (std::size_t k = 0; k + 1 <= h; ++k) {
244 const std::size_t ik = cache_miss_rmf_index(i, k, n_items);
245 const std::size_t ik1 = cache_miss_rmf_index(i, k + 1, n_items);
246 for (std::size_t j = 0; j < n_items; ++j) {
247 if (j == i) continue;
248 const std::size_t jk = cache_miss_rmf_index(j, k, n_items);
249 const std::size_t jk1 = cache_miss_rmf_index(j, k + 1, n_items);
250 H[at(ik, jk, ik1)] += p[j] / m[k];
251 H[at(ik, ik1, jk)] += p[j] / m[k];
252 H[at(ik, jk1, ik)] -= p[i] / m[k];
253 H[at(ik, ik, jk1)] -= p[i] / m[k];
254 H[at(ik1, jk, ik1)] -= p[j] / m[k];
255 H[at(ik1, ik1, jk)] -= p[j] / m[k];
256 H[at(ik1, jk1, ik)] += p[i] / m[k];
257 H[at(ik1, ik, jk1)] += p[i] / m[k];
258 }
259 }
260 }
261 return H;
262}
263
264/** Noise intensity Q(x) of the DDPP (rmf_noise_matrix.m). */
265template <class T>
266Matrix<T> noise_matrix(const std::vector<T>& x, const std::vector<T>& p, const std::vector<T>& m,
267 std::size_t n_items, std::size_t h) {
268 const T zero = num_traits<T>::from_int(0);
269 const std::size_t md = n_items * (h + 1);
270 Matrix<T> Q(md, md, zero);
271 const int signs[4] = {-1, 1, 1, -1};
272 for (std::size_t i = 0; i < n_items; ++i) {
273 for (std::size_t k = 0; k + 1 <= h; ++k) {
274 for (std::size_t j = 0; j < n_items; ++j) {
275 const T rate = p[i] * x[cache_miss_rmf_index(i, k, n_items)] *
276 x[cache_miss_rmf_index(j, k + 1, n_items)] / m[k];
277 const std::size_t idx[4] = {cache_miss_rmf_index(i, k, n_items),
278 cache_miss_rmf_index(j, k, n_items),
279 cache_miss_rmf_index(i, k + 1, n_items),
280 cache_miss_rmf_index(j, k + 1, n_items)};
281 for (int ia = 0; ia < 4; ++ia)
282 for (int ib = 0; ib < 4; ++ib)
283 Q(idx[ia], idx[ib]) +=
284 rate * num_traits<T>::from_int(signs[ia] * signs[ib]);
285 }
286 }
287 }
288 return Q;
289}
290
291/**
292 * Mean-field fixed point by integrating the drift to tmax (rmf_fixed_point.m).
293 * The reference uses ode15s with RelTol 1e-8 and AbsTol 1e-10 over [0,10000];
294 * the port uses the same horizon and the same tolerances with ode_rosenbrock4
295 * and the analytic Jacobian.
296 */
297template <class T>
298std::vector<T> fixed_point(const std::vector<T>& x0, const std::vector<T>& p,
299 const std::vector<T>& m, std::size_t n_items, std::size_t h,
300 const T& tmax, const T& rtol, const T& atol) {
301 OdeOptions<T> opt;
302 opt.rtol = rtol;
303 opt.atol = atol;
304 opt.store_trajectory = false;
305 const auto f = [&](const T& t, const std::vector<T>& x) {
306 (void)t;
307 return drift(x, p, m, n_items, h);
308 };
309 // numeric-Jacobian rationale: see _kb/09-ldes-and-cache.md (cpp port notes)
310 return ode_rosenbrock4(f, T(num_traits<T>::from_int(0)), tmax, x0, opt).final_state();
311}
312
313/** fixed_point with the reference tolerances, RelTol 1e-8 and AbsTol 1e-10. */
314template <class T>
315std::vector<T> fixed_point(const std::vector<T>& x0, const std::vector<T>& p,
316 const std::vector<T>& m, std::size_t n_items, std::size_t h,
317 const T& tmax) {
318 return fixed_point(x0, p, m, n_items, h, tmax, T(num_traits<T>::from_double(1e-8)),
319 T(num_traits<T>::from_double(1e-10)));
320}
321
322/**
323 * Solve the Lyapunov equation F W + W F^T + Q = 0 by vectorization.
324 *
325 * The vectorized operator is (I (x) F + F (x) I) acting on vec(W); with W
326 * stored row-major, row (a,b) of the system reads
327 * sum_c F(a,c) W(c,b) + sum_c F(b,c) W(a,c) = -Q(a,b).
328 * The system is r^2 by r^2 and is solved with the port's LU. MATLAB calls
329 * lyap(), which uses a Bartels-Stewart Schur factorization; the two compute
330 * the same W, and the Schur route is the faster one, not a different answer.
331 * The vectorized route is used here because it needs nothing beyond the LU
332 * that the port already has, and because it fails loudly (a singular matrix
333 * throws) when F has a zero or a symmetric pair of eigenvalues, which is the
334 * case the caller must detect and reject.
335 */
336template <class T>
337Matrix<T> lyapunov(const Matrix<T>& F, const Matrix<T>& Q) {
338 const std::size_t r = F.rows();
339 const T zero = num_traits<T>::from_int(0);
340 Matrix<T> A(r * r, r * r, zero);
341 std::vector<T> rhs(r * r, zero);
342 for (std::size_t a = 0; a < r; ++a)
343 for (std::size_t b = 0; b < r; ++b) {
344 const std::size_t row = a * r + b;
345 for (std::size_t c = 0; c < r; ++c) {
346 A(row, c * r + b) += F(a, c);
347 A(row, a * r + c) += F(b, c);
348 }
349 rhs[row] = -Q(a, b);
350 }
351 const std::vector<T> w = line::solve(A, rhs);
352 Matrix<T> W(r, r, zero);
353 for (std::size_t a = 0; a < r; ++a)
354 for (std::size_t b = 0; b < r; ++b) W(a, b) = w[a * r + b];
355 return W;
356}
357
358/**
359 * Numeric rank and an orthonormal basis of the LEFT null space of Fp, by
360 * Gaussian elimination with full pivoting on Fp^T followed by Gram-Schmidt.
361 *
362 * MATLAB gets both from svd(Fp): rk = rank(Fp) and the trailing left singular
363 * vectors. The port cannot, because the port's SVD entry point is LAPACK-backed
364 * and double-only while this header is templated; and it does not need to,
365 * because only the SUBSPACE matters, not the basis of it (see the note at the
366 * top of this file). What does matter is the rank DECISION, which is a
367 * tolerance comparison in both codes: MATLAB thresholds the singular values at
368 * 1e-8*sigma_max, this thresholds the elimination pivots at 1e-8*max|Fp|. The
369 * null singular values sit near the 1e-10 accuracy of the fixed point and the
370 * nonzero ones are O(0.1), so both land in the same gap.
371 *
372 * The left null space is where the mean-field conservation laws live: each
373 * item's occupancies sum to one, so the n item-indicator vectors are always in
374 * it, and a symmetric popularity profile (items with equal request rates) adds
375 * more. That is why the rank cannot simply be taken as model_dim - n_items.
376 */
377template <class T>
378struct NullSpace {
379 std::size_t rank = 0;
380 std::vector<std::vector<T>> basis; ///< orthonormal rows spanning {w : w^T Fp = 0}
381};
382
383template <class T>
384NullSpace<T> left_null_space(const Matrix<T>& Fp) {
385 const std::size_t n = Fp.rows();
386 const T zero = num_traits<T>::from_int(0);
387 // Work on M = Fp^T so that its null vectors are the left null vectors of Fp.
388 Matrix<T> M(n, n, zero);
389 T scale = zero;
390 for (std::size_t i = 0; i < n; ++i)
391 for (std::size_t j = 0; j < n; ++j) {
392 M(i, j) = Fp(j, i);
393 const T a = num_abs(Fp(j, i));
394 if (a > scale) scale = a;
395 }
396 if (scale == zero) scale = num_traits<T>::from_int(1);
397 // Rank threshold: 1e-8 relative to sigma_max, as in the MATLAB reference
398 // (BUG-102), applied to the pivots with max|Fp| standing in for sigma_max.
399 const T tol = scale * num_traits<T>::from_double(1e-8);
400
401 std::vector<std::size_t> col_of_pivot;
402 std::vector<std::size_t> perm(n);
403 for (std::size_t i = 0; i < n; ++i) perm[i] = i;
404 std::size_t row = 0;
405 for (std::size_t col = 0; col < n && row < n; ++col) {
406 std::size_t p = row;
407 T best = num_abs(M(row, col));
408 for (std::size_t i = row + 1; i < n; ++i) {
409 const T a = num_abs(M(i, col));
410 if (a > best) {
411 best = a;
412 p = i;
413 }
414 }
415 if (best <= tol) continue; // no pivot in this column: it is free
416 if (p != row)
417 for (std::size_t j = 0; j < n; ++j) std::swap(M(row, j), M(p, j));
418 const T d = M(row, col);
419 for (std::size_t j = 0; j < n; ++j) M(row, j) = M(row, j) / d;
420 for (std::size_t i = 0; i < n; ++i) {
421 if (i == row) continue;
422 const T f = M(i, col);
423 if (f == zero) continue;
424 for (std::size_t j = 0; j < n; ++j) M(i, j) -= f * M(row, j);
425 }
426 col_of_pivot.push_back(col);
427 ++row;
428 }
429
430 NullSpace<T> ns;
431 ns.rank = col_of_pivot.size();
432 std::vector<bool> is_pivot(n, false);
433 for (std::size_t c : col_of_pivot) is_pivot[c] = true;
434 // One basis vector per free column, from the reduced row echelon form.
435 for (std::size_t free_col = 0; free_col < n; ++free_col) {
436 if (is_pivot[free_col]) continue;
437 std::vector<T> v(n, zero);
438 v[free_col] = num_traits<T>::from_int(1);
439 for (std::size_t r = 0; r < col_of_pivot.size(); ++r)
440 v[col_of_pivot[r]] = -M(r, free_col);
441 ns.basis.push_back(v);
442 }
443 // Gram-Schmidt, so that the basis is orthonormal like MATLAB's.
444 using std::sqrt;
445 for (std::size_t i = 0; i < ns.basis.size(); ++i) {
446 for (std::size_t k = 0; k < i; ++k) {
447 T dot = zero;
448 for (std::size_t j = 0; j < n; ++j) dot += ns.basis[i][j] * ns.basis[k][j];
449 for (std::size_t j = 0; j < n; ++j) ns.basis[i][j] -= dot * ns.basis[k][j];
450 }
451 T nrm2 = zero;
452 for (std::size_t j = 0; j < n; ++j) nrm2 += ns.basis[i][j] * ns.basis[i][j];
453 const T nrm = sqrt(nrm2);
454 if (nrm <= tol) throw NumericError(ErrorKind{"Degenerate"}, "cache_miss_rmf: the null-space basis degenerated");
455 for (std::size_t j = 0; j < n; ++j) ns.basis[i][j] = ns.basis[i][j] / nrm;
456 }
457 return ns;
458}
459
460/**
461 * The same thing from the SVD, which is what the reference uses.
462 *
463 * This matters more than a change of basis normally would. The Jacobian at the
464 * mean-field fixed point is very ill conditioned: on the four-item two-list
465 * case below its singular values are 7.5e-1 ... 1.7e-1, then 5.4e-10, then
466 * five at 1e-17. The 5.4e-10 one is a null direction that the finite accuracy
467 * of the fixed point has lifted off zero, so it counts as a nonzero singular
468 * value under both MATLAB's rank tolerance and any other, and the reduced
469 * Jacobian inherits it. Everything downstream then divides by it, and the 1/N
470 * correction depends on which basis of the (numerically ambiguous) null space
471 * was chosen. Reproducing the reference's numbers therefore requires
472 * reproducing its basis, not merely its subspace. This routine is used when
473 * LAPACK is available and T is double; the templated elimination above is the
474 * fallback, and the two agree whenever the fixed point is hyperbolic and the
475 * rank decision is unambiguous.
476 */
477inline NullSpace<double> left_null_space_svd(const Matrix<double>& Fp) {
478#ifndef LINE_MP_HAVE_LAPACK
479 return left_null_space(Fp);
480#else
481 // A binary that loads LAPACK at run time (lapack.h) may find none; it then
482 // takes the same fallback as a build configured without it.
483 if (!lapack::available()) return left_null_space(Fp);
484 const std::size_t n = Fp.rows();
485 std::vector<double> a(n * n);
486 for (std::size_t i = 0; i < n; ++i)
487 for (std::size_t j = 0; j < n; ++j) a[j * n + i] = Fp(i, j);
488 const int ni = static_cast<int>(n);
489 std::vector<double> s(n), u(n * n), vt(1);
490 int info = 0, lwork = -1;
491 double wopt = 0.0;
492 const int one = 1;
493 lapack::dgesvd("A", "N", &ni, &ni, a.data(), &ni, s.data(), u.data(), &ni, vt.data(), &one, &wopt,
494 &lwork, &info);
495 if (info != 0) throw NumericError(ErrorKind{"Lapack"}, "cache_miss_rmf: LAPACK workspace query failed");
496 lwork = static_cast<int>(wopt);
497 std::vector<double> work(static_cast<std::size_t>(lwork));
498 lapack::dgesvd("A", "N", &ni, &ni, a.data(), &ni, s.data(), u.data(), &ni, vt.data(), &one,
499 work.data(), &lwork, &info);
500 if (info != 0) throw NumericError(ErrorKind{"NonConvergence"}, "cache_miss_rmf: LAPACK dgesvd failed to converge");
501 // 1e-8 relative to sigma_1, as in the MATLAB reference (BUG-102).
502 const double tol = 1e-8 * s[0];
503 NullSpace<double> ns;
504 ns.rank = 0;
505 for (double v : s)
506 if (v > tol) ++ns.rank;
507 for (std::size_t c = ns.rank; c < n; ++c) {
508 std::vector<double> row(n, 0.0);
509 for (std::size_t i = 0; i < n; ++i) row[i] = u[c * n + i];
510 ns.basis.push_back(row);
511 }
512 return ns;
513#endif
514}
515
516/**
517 * Orthonormal basis of `{u in R^n : sum(u) = 0}`, as an n-by-(n-1) matrix
518 * (local_centered_basis in the reference, QR of I - ones/n).
519 */
520inline Matrix<double> lna_centered_basis(std::size_t n) {
521 if (n <= 1) return Matrix<double>(n, 0, 0.0);
522 std::vector<std::vector<double>> cols;
523 for (std::size_t c = 0; c < n && cols.size() + 1 < n; ++c) {
524 std::vector<double> v(n, 0.0);
525 for (std::size_t i = 0; i < n; ++i)
526 v[i] = (i == c ? 1.0 : 0.0) - 1.0 / static_cast<double>(n);
527 for (std::size_t b = 0; b < cols.size(); ++b) {
528 double d = 0.0;
529 for (std::size_t i = 0; i < n; ++i) d += v[i] * cols[b][i];
530 for (std::size_t i = 0; i < n; ++i) v[i] -= d * cols[b][i];
531 }
532 double nv = 0.0;
533 for (std::size_t i = 0; i < n; ++i) nv += v[i] * v[i];
534 nv = std::sqrt(nv);
535 if (nv <= 1e-12) continue;
536 for (std::size_t i = 0; i < n; ++i) v[i] /= nv;
537 cols.push_back(v);
538 }
539 Matrix<double> U(n, cols.size(), 0.0);
540 for (std::size_t c = 0; c < cols.size(); ++c)
541 for (std::size_t i = 0; i < n; ++i) U(i, c) = cols[c][i];
542 return U;
543}
544
545/** `kron(A, B)` in MATLAB's ordering. */
546inline Matrix<double> lna_kron(const Matrix<double>& A, const Matrix<double>& B) {
547 Matrix<double> K(A.rows() * B.rows(), A.cols() * B.cols(), 0.0);
548 for (std::size_t i = 0; i < A.rows(); ++i)
549 for (std::size_t j = 0; j < A.cols(); ++j)
550 for (std::size_t k = 0; k < B.rows(); ++k)
551 for (std::size_t l = 0; l < B.cols(); ++l)
552 K(i * B.rows() + k, j * B.cols() + l) = A(i, j) * B(k, l);
553 return K;
554}
555
556/**
557 * Stationary covariance of the occupancy under the LNA (rmf_lna_covariance):
558 * F' W + W F'^T + Q = 0 restricted to the double-centred subspace, the tensor
559 * product of the zero-sum item space with the zero-sum list space. Refuses a
560 * fixed point that is not exponentially stable on that subspace. Double only:
561 * the eigenvalue test and the Bartels-Stewart solve are LAPACK-backed.
562 */
563inline Matrix<double> lna_covariance(const std::vector<double>& x, const std::vector<double>& p,
564 const std::vector<double>& m, std::size_t n, std::size_t h,
565 std::size_t dim) {
566 if (x.size() != dim) throw InputError("cache_rmf_lna: the fixed point has the wrong length");
567
568 const Matrix<double> Fp = jacobian<double>(x, p, m, n, h);
569 Matrix<double> Q = noise_matrix<double>(x, p, m, n, h);
570 for (std::size_t i = 0; i < Q.rows(); ++i)
571 for (std::size_t j = 0; j < i; ++j) {
572 const double v = 0.5 * (Q(i, j) + Q(j, i));
573 Q(i, j) = v;
574 Q(j, i) = v;
575 }
576
577 const Matrix<double> Ui = lna_centered_basis(n);
578 const Matrix<double> Ul = lna_centered_basis(h + 1);
579 const Matrix<double> V = lna_kron(Ul, Ui); // item-major, i + k*n
580 if (V.cols() == 0) return Matrix<double>(dim, dim, 0.0);
581 if (V.rows() != dim)
582 throw InputError(
583 "cache_rmf_lna: the centred basis does not span the state dimension; n, h and dim "
584 "disagree");
585
586 const std::size_t d = V.cols();
587 Matrix<double> Ar(d, d, 0.0), Qr(d, d, 0.0);
588 for (std::size_t a = 0; a < d; ++a)
589 for (std::size_t b = 0; b < d; ++b) {
590 double sa = 0.0, sq = 0.0;
591 for (std::size_t i = 0; i < dim; ++i)
592 for (std::size_t j = 0; j < dim; ++j) {
593 sa += V(i, a) * Fp(i, j) * V(j, b);
594 sq += V(i, a) * Q(i, j) * V(j, b);
595 }
596 Ar(a, b) = sa;
597 Qr(a, b) = sq;
598 }
599 for (std::size_t i = 0; i < d; ++i)
600 for (std::size_t j = 0; j < i; ++j) {
601 const double v = 0.5 * (Qr(i, j) + Qr(j, i));
602 Qr(i, j) = v;
603 Qr(j, i) = v;
604 }
605
606 const std::vector<std::complex<double>> ev = eig_values(Ar);
607 double worst = -std::numeric_limits<double>::infinity();
608 for (std::size_t i = 0; i < ev.size(); ++i) worst = std::max(worst, ev[i].real());
609 if (worst >= -std::sqrt(2.220446049250313e-16))
610 throw InputError(
611 "cache_rmf_lna: the cache fluid fixed point is not exponentially stable on the "
612 "reachable subspace, so the occupancy process has no stationary covariance");
613
614 Matrix<double> ArT(d, d, 0.0);
615 for (std::size_t i = 0; i < d; ++i)
616 for (std::size_t j = 0; j < d; ++j) ArT(i, j) = Ar(j, i);
617 Matrix<double> Wr = lyap_schur(Ar, ArT, Qr);
618 for (std::size_t i = 0; i < d; ++i)
619 for (std::size_t j = 0; j < i; ++j) {
620 const double v = 0.5 * (Wr(i, j) + Wr(j, i));
621 Wr(i, j) = v;
622 Wr(j, i) = v;
623 }
624
625 Matrix<double> W(dim, dim, 0.0);
626 for (std::size_t i = 0; i < dim; ++i)
627 for (std::size_t j = 0; j < dim; ++j) {
628 double s = 0.0;
629 for (std::size_t a = 0; a < d; ++a)
630 for (std::size_t b = 0; b < d; ++b) s += V(i, a) * Wr(a, b) * V(j, b);
631 W(i, j) = s;
632 }
633 for (std::size_t i = 0; i < dim; ++i)
634 for (std::size_t j = 0; j < i; ++j) {
635 const double v = 0.5 * (W(i, j) + W(j, i));
636 W(i, j) = v;
637 W(j, i) = v;
638 }
639 return W;
640}
641
642/** Fills `W` with the LNA covariance when it is computable and finite; T != double leaves it empty. */
643template <class T>
644void lna_covariance_into(Matrix<T>& W, const std::vector<T>& x, const std::vector<T>& p,
645 const std::vector<T>& m, std::size_t n, std::size_t h, std::size_t dim) {
646 (void)W; (void)x; (void)p; (void)m; (void)n; (void)h; (void)dim;
647}
648
649template <>
650inline void lna_covariance_into<double>(Matrix<double>& W, const std::vector<double>& x,
651 const std::vector<double>& p, const std::vector<double>& m,
652 std::size_t n, std::size_t h, std::size_t dim) {
653 try {
654 Matrix<double> C = lna_covariance(x, p, m, n, h, dim);
655 for (std::size_t i = 0; i < C.rows(); ++i)
656 for (std::size_t j = 0; j < C.cols(); ++j)
657 if (!std::isfinite(C(i, j))) return;
658 W = C;
659 } catch (const Error&) {
660 // the reference's try/catch: Wcov stays empty
661 }
662}
663
664/** Dispatch: the SVD basis at double, the elimination basis otherwise. */
665template <class T>
666NullSpace<T> left_null_space_for(const Matrix<T>& Fp) {
667 return left_null_space(Fp);
668}
669
670template <>
671inline NullSpace<double> left_null_space_for<double>(const Matrix<double>& Fp) {
672 return left_null_space_svd(Fp);
673}
674
675// ---------------------------------------------------------------------------
676// The general access graph (`accost`).
677// ---------------------------------------------------------------------------
678/**
679 * `rmf_linear_graph`: the standard chain. Row 0 is miss admission (column 0 is
680 * reject, column 1+l admit to list l), row 1+i is a hit in list i, and the top
681 * list self-loops because a hit there moves nothing.
682 */
683template <class T>
684Matrix<T> linear_graph(std::size_t h) {
685 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
686 Matrix<T> g(h + 1, h + 1, zero);
687 g(0, 1) = one;
688 for (std::size_t a = 1; a + 1 <= h; ++a) g(a, a + 1) = one;
689 g(h, h) = one;
690 return g;
691}
692
693/**
694 * `rmf_build_item_graphs`: one (h+1)x(h+1) graph per item, the per-user graphs
695 * averaged by request rate and row-normalized.
696 *
697 * RETURNS EMPTY WHEN THE RESULT IS THE LINEAR CHAIN, and that is not an
698 * optimization. The linear chain is the only case for which the 1/N REFINEMENT
699 * is defined here -- its Jacobian, Hessian and noise matrix are all written for
700 * the chain drift -- so the caller must be able to tell "no graph was declared,
701 * take the refined path" from "a graph was declared, take the plain mean-field
702 * fixed point of the general drift". An empty return is that signal.
703 */
704template <class T>
705std::vector<Matrix<T> > build_item_graphs(const std::vector<std::vector<Matrix<T> > >& accost,
706 const Matrix<T>& lambda, std::size_t n, std::size_t h) {
707 std::vector<Matrix<T> > G;
708 if (accost.empty()) return G;
709 const T zero = num_traits<T>::from_int(0);
710 const Matrix<T> lin = linear_graph<T>(h);
711 const std::size_t u = accost.size();
712 std::vector<Matrix<T> > Gc(n, lin);
713 bool is_linear = true;
714 for (std::size_t k = 0; k < n; ++k) {
715 Matrix<T> num(h + 1, h + 1, zero);
716 T den = zero;
717 for (std::size_t v = 0; v < u; ++v) {
718 if (k >= accost[v].size()) continue;
719 const Matrix<T>& gvk = accost[v][k];
720 if (gvk.rows() == 0) continue;
721 if (gvk.rows() != h + 1 || gvk.cols() != h + 1)
722 throw InputError("cache_miss_rmf: an access graph is not (h+1)x(h+1)");
723 double w = (v < lambda.rows() && k < lambda.cols())
724 ? num_traits<T>::to_double(lambda(v, k))
725 : 0.0;
726 if (!std::isfinite(w)) w = 0.0;
727 const T wv = num_traits<T>::from_double(w);
728 for (std::size_t a = 0; a <= h; ++a)
729 for (std::size_t b = 0; b <= h; ++b) num(a, b) += wv * gvk(a, b);
730 den += wv;
731 }
732 Matrix<T> gk = lin;
733 if (den > zero) {
734 for (std::size_t a = 0; a <= h; ++a)
735 for (std::size_t b = 0; b <= h; ++b) gk(a, b) = num(a, b) / den;
736 } else if (!accost.empty() && k < accost[0].size() && accost[0][k].rows() == h + 1) {
737 gk = accost[0][k];
738 }
739 for (std::size_t a = 0; a <= h; ++a) {
740 T srow = zero;
741 for (std::size_t b = 0; b <= h; ++b) srow += gk(a, b);
742 if (srow > zero)
743 for (std::size_t b = 0; b <= h; ++b) gk(a, b) = gk(a, b) / srow;
744 }
745 Gc[k] = gk;
746 for (std::size_t a = 0; a <= h && is_linear; ++a)
747 for (std::size_t b = 0; b <= h && is_linear; ++b)
748 if (std::fabs(num_traits<T>::to_double(gk(a, b)) -
749 num_traits<T>::to_double(lin(a, b))) >= 1e-9)
750 is_linear = false;
751 }
752 if (!is_linear) G = Gc;
753 return G;
754}
755
756/**
757 * `rmf_drift_graph`: the general RANDOM(m) drift under a per-item access graph.
758 *
759 * A miss is admitted to list i with probability G[k](0,1+i) and a hit in list s
760 * promotes to list i with probability G[k](1+s,1+i); the occupant it displaces
761 * is drawn UNIFORMLY from the target list, which is the RR sample path
762 * (`State.afterEventCache`) and is why every displacement term carries the
763 * 1/m(i) factor. It reduces to the chain drift above when G is the linear
764 * graph, which is what makes `build_item_graphs`'s emptiness test sound.
765 */
766template <class T>
767std::vector<T> drift_graph(const std::vector<T>& x_in, const std::vector<T>& p,
768 const std::vector<Matrix<T> >& G, const std::vector<T>& m,
769 std::size_t n, std::size_t h) {
770 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
771 const std::size_t model_dim = n * (h + 1);
772 std::vector<T> x = x_in;
773 for (std::size_t a = 0; a < x.size(); ++a) {
774 if (x[a] < zero) x[a] = zero;
775 if (x[a] > one) x[a] = one;
776 }
777 // A(s,i): total rate of insertion or promotion into list i out of list s.
778 Matrix<T> A(h + 1, h + 1, zero);
779 for (std::size_t s = 0; s <= h; ++s)
780 for (std::size_t j = 0; j < n; ++j) {
781 const T xjs = x[cache_miss_rmf_index(j, s, n)];
782 if (xjs == zero) continue;
783 for (std::size_t i = 1; i <= h; ++i) A(s, i) += p[j] * xjs * G[j](s, i);
784 }
785 std::vector<T> dX(model_dim, zero);
786 for (std::size_t k = 0; k < n; ++k) {
787 const T outk = x[cache_miss_rmf_index(k, 0, n)];
788 for (std::size_t i = 1; i <= h; ++i) {
789 const T xki = x[cache_miss_rmf_index(k, i, n)];
790 T infl = p[k] * outk * G[k](0, i);
791 for (std::size_t s = 1; s + 1 <= i; ++s)
792 infl += p[k] * x[cache_miss_rmf_index(k, s, n)] * G[k](s, i);
793 for (std::size_t b = i + 1; b <= h; ++b)
794 infl += A(i, b) * x[cache_miss_rmf_index(k, b, n)] / m[b - 1];
795 T outfl = p[k] * xki * T(one - G[k](i, i));
796 T disp = zero;
797 for (std::size_t s = 0; s + 1 <= i; ++s) disp += A(s, i);
798 outfl += disp * xki / m[i - 1];
799 dX[cache_miss_rmf_index(k, i, n)] += infl - outfl;
800 }
801 T acc = zero;
802 for (std::size_t i = 1; i <= h; ++i) acc += dX[cache_miss_rmf_index(k, i, n)];
803 dX[cache_miss_rmf_index(k, 0, n)] = -acc;
804 }
805 return dX;
806}
807
808/**
809 * `rmf_fixed_point_graph`: the plain mean-field fixed point of the general
810 * drift, at the reference's own horizon of 20000 (twice the chain's, because
811 * the general drift has no refinement to fall back on if it stops short).
812 */
813template <class T>
814std::vector<T> fixed_point_graph(const std::vector<T>& x0, const std::vector<T>& p,
815 const std::vector<Matrix<T> >& G, const std::vector<T>& m,
816 std::size_t n, std::size_t h) {
817 OdeOptions<T> opt;
818 opt.rtol = num_traits<T>::from_double(1e-8);
819 opt.atol = num_traits<T>::from_double(1e-10);
820 opt.store_trajectory = false;
821 const auto f = [&](const T& t, const std::vector<T>& x) {
822 (void)t;
823 return drift_graph(x, p, G, m, n, h);
824 };
825 return ode_rosenbrock4(f, T(num_traits<T>::from_int(0)),
826 T(num_traits<T>::from_int(20000)), x0, opt)
827 .final_state();
828}
829
830} // namespace rmf_detail
831
832/**
833 * Refined mean-field miss rates of a RANDOM(m) multi-list cache.
834 *
835 * @param gamma item access factors. Present for signature compatibility with
836 * cache_miss_rmf.m, which marks it unused and reads
837 * only its size; nothing here depends on it either.
838 * @param m_in (h) list capacities
839 * @param lambda (u x n_items) per-user per-item request rates. The MATLAB
840 * argument is a three-dimensional array and the function reads
841 * only its first page, lambda(v,:,1); this is that page.
842 * @param tmax integration horizon for the fixed point (reference: 1e4)
843 * @param accost per-(user,item) access graph, each an (h+1)x(h+1) matrix; empty
844 * is the linear chain. A NON-LINEAR graph switches the solve to
845 * the general drift AND drops the 1/N refinement, exactly as the
846 * reference does: the refinement's Jacobian, Hessian and noise
847 * matrix are written for the chain drift, so applying it to
848 * another drift would correct the wrong system.
849 */
850template <class T>
851CacheMissRmfResult<T> cache_miss_rmf(const std::vector<T>& gamma, const std::vector<int>& m_in,
852 const Matrix<T>& lambda, const T& tmax,
853 const std::vector<std::vector<Matrix<T> > >& accost) {
855 "cache_miss_rmf requires transcendental arithmetic: its fixed point is reached "
856 "by a tolerance-driven integration of the mean-field drift");
857 (void)gamma;
858 const T zero = num_traits<T>::from_int(0);
859 const T one = num_traits<T>::from_int(1);
860 const std::size_t u = lambda.rows();
861 const std::size_t n_items = lambda.cols();
862 const std::size_t h = m_in.size();
863 if (u == 0 || n_items == 0) throw InputError("cache_miss_rmf: empty request-rate matrix");
864 if (h == 0) throw InputError("cache_miss_rmf: at least one cache list is required");
865
866 std::vector<T> m(h, zero);
867 for (std::size_t k = 0; k < h; ++k) {
868 if (m_in[k] <= 0) throw InputError("cache_miss_rmf: a list has non-positive capacity");
869 m[k] = num_traits<T>::from_int(static_cast<long>(m_in[k]));
870 }
871
872 // non-finite rate rejection rationale: see _kb/09-ldes-and-cache.md (cpp port notes)
873 std::vector<T> lam_i(n_items, zero);
874 T lam_tot = zero;
875 for (std::size_t v = 0; v < u; ++v)
876 for (std::size_t i = 0; i < n_items; ++i) {
877 lam_i[i] += lambda(v, i);
878 lam_tot += lambda(v, i);
879 }
880 if (lam_tot == zero) throw InputError("cache_miss_rmf: all request rates are zero");
881 std::vector<T> p(n_items, zero);
882 for (std::size_t i = 0; i < n_items; ++i) p[i] = lam_i[i] / lam_tot;
883
884 const std::size_t model_dim = n_items * (h + 1);
885
886 // Initial occupancy: the first m(1) items in list 1, the next m(2) in list
887 // 2, and so on; every remaining item outside the cache.
888 std::vector<T> x0(model_dim, zero);
889 std::size_t obj = 0;
890 for (std::size_t k = 1; k <= h; ++k)
891 for (int jj = 0; jj < m_in[k - 1]; ++jj) {
892 ++obj;
893 if (obj <= n_items) x0[cache_miss_rmf_index(obj - 1, k, n_items)] = one;
894 }
895 for (std::size_t i = obj; i < n_items; ++i) x0[cache_miss_rmf_index(i, 0, n_items)] = one;
896
898 const std::vector<Matrix<T> > G = rmf_detail::build_item_graphs(accost, lambda, n_items, h);
899 if (!G.empty()) {
900 // A declared access graph takes the general drift and the plain fixed
901 // point; `refined` stays false, which is the honest report.
902 res.xss = rmf_detail::fixed_point_graph(x0, p, G, m, n_items, h);
903 res.pi0.assign(n_items, zero);
904 for (std::size_t i = 0; i < n_items; ++i) {
905 T v = res.xss[cache_miss_rmf_index(i, 0, n_items)];
906 if (v < zero) v = zero;
907 if (v > one) v = one;
908 res.pi0[i] = v;
909 }
910 res.MI.assign(n_items, zero);
911 res.M = zero;
912 for (std::size_t i = 0; i < n_items; ++i) {
913 res.MI[i] = lam_i[i] * res.pi0[i];
914 res.M += res.MI[i];
915 }
916 res.MU.assign(u, zero);
917 for (std::size_t v = 0; v < u; ++v) {
918 T s = zero;
919 for (std::size_t i = 0; i < n_items; ++i) s += lambda(v, i) * res.pi0[i];
920 res.MU[v] = s;
921 }
922 return res;
923 }
924 // One integration pass at the reference's tolerances (RelTol 1e-8,
925 // AbsTol 1e-10), as rmf_fixed_point does; no second polishing pass, so the
926 // rank decision below sees the same fixed point the reference sees.
927 std::vector<T> xss = rmf_detail::fixed_point(x0, p, m, n_items, h, tmax);
928
929 // 1/N refinement failure rationale: see _kb/09-ldes-and-cache.md (cpp port notes)
930 try {
931 const Matrix<T> Fp = rmf_detail::jacobian(xss, p, m, n_items, h);
932 const rmf_detail::NullSpace<T> ns = rmf_detail::left_null_space_for(Fp);
933 const std::size_t rk = ns.rank;
934 if (rk == 0 || rk >= model_dim)
935 throw NumericError(ErrorKind{"Degenerate"}, "cache_miss_rmf: the reduction is degenerate");
936 const std::vector<T> Fpp = rmf_detail::hessian(p, m, n_items, h);
937 const Matrix<T> Q = rmf_detail::noise_matrix(xss, p, m, n_items, h);
938
939 // change-of-basis rationale: see _kb/09-ldes-and-cache.md (cpp port notes)
940 Matrix<T> C(model_dim, model_dim, zero);
941 {
942 std::size_t d = 0;
943 for (std::size_t l = 0; l <= h && d < rk; ++l)
944 for (std::size_t i = 0; i + 1 < n_items && d < rk; ++i, ++d)
945 C(d, cache_miss_rmf_index(i, l, n_items)) = one;
946 if (d != rk) throw NumericError(ErrorKind{"Degenerate"}, "cache_miss_rmf: the reduction basis is too small");
947 }
948 if (ns.basis.size() != model_dim - rk)
949 throw NumericError(ErrorKind{"Degenerate"}, "cache_miss_rmf: the null-space basis has the wrong size");
950 for (std::size_t i = 0; i < ns.basis.size(); ++i)
951 for (std::size_t j = 0; j < model_dim; ++j) C(rk + i, j) = ns.basis[i][j];
952 const Matrix<T> Cinv = inverse(C);
953
954 Matrix<T> Fp_r(rk, rk, zero);
955 {
956 const Matrix<T> tmp = matmul(C, matmul(Fp, Cinv));
957 for (std::size_t a = 0; a < rk; ++a)
958 for (std::size_t b = 0; b < rk; ++b) Fp_r(a, b) = tmp(a, b);
959 }
960 Matrix<T> Q_r(rk, rk, zero);
961 {
962 Matrix<T> Ct(model_dim, model_dim, zero);
963 for (std::size_t a = 0; a < model_dim; ++a)
964 for (std::size_t b = 0; b < model_dim; ++b) Ct(a, b) = C(b, a);
965 const Matrix<T> tmp = matmul(C, matmul(Q, Ct));
966 for (std::size_t a = 0; a < rk; ++a)
967 for (std::size_t b = 0; b < rk; ++b) Q_r(a, b) = tmp(a, b);
968 }
969
970 // Reduced Hessian, by the same three contractions the reference does.
971 const auto Hat = [model_dim](std::size_t a, std::size_t b, std::size_t c) {
972 return (a * model_dim + b) * model_dim + c;
973 };
974 std::vector<T> tmp1(rk * model_dim * model_dim, zero);
975 for (std::size_t a = 0; a < rk; ++a)
976 for (std::size_t j = 0; j < model_dim; ++j)
977 for (std::size_t k = 0; k < model_dim; ++k) {
978 T s = zero;
979 for (std::size_t i = 0; i < model_dim; ++i) s += C(a, i) * Fpp[Hat(i, j, k)];
980 tmp1[(a * model_dim + j) * model_dim + k] = s;
981 }
982 std::vector<T> tmp2(rk * rk * model_dim, zero);
983 for (std::size_t a = 0; a < rk; ++a)
984 for (std::size_t b = 0; b < rk; ++b)
985 for (std::size_t k = 0; k < model_dim; ++k) {
986 T s = zero;
987 for (std::size_t j = 0; j < model_dim; ++j)
988 s += tmp1[(a * model_dim + j) * model_dim + k] * Cinv(j, b);
989 tmp2[(a * rk + b) * model_dim + k] = s;
990 }
991 std::vector<T> Fpp_r(rk * rk * rk, zero);
992 for (std::size_t a = 0; a < rk; ++a)
993 for (std::size_t b = 0; b < rk; ++b)
994 for (std::size_t c = 0; c < rk; ++c) {
995 T s = zero;
996 for (std::size_t k = 0; k < model_dim; ++k)
997 s += tmp2[(a * rk + b) * model_dim + k] * Cinv(k, c);
998 Fpp_r[(a * rk + b) * rk + c] = s;
999 }
1000
1001 const Matrix<T> W_r = rmf_detail::lyapunov(Fp_r, Q_r);
1002
1003 std::vector<T> C_r(rk, zero);
1004 for (std::size_t a = 0; a < rk; ++a) {
1005 T s = zero;
1006 for (std::size_t b = 0; b < rk; ++b)
1007 for (std::size_t c = 0; c < rk; ++c) s += Fpp_r[(a * rk + b) * rk + c] * W_r(b, c);
1008 C_r[a] = s;
1009 }
1010 std::vector<T> rhs(rk, zero);
1011 for (std::size_t a = 0; a < rk; ++a)
1012 rhs[a] = -C_r[a] / num_traits<T>::from_int(2);
1013 const std::vector<T> V_r = line::solve(Fp_r, rhs);
1014
1015 std::vector<T> xref(model_dim, zero);
1016 bool finite = true;
1017 for (std::size_t i = 0; i < model_dim; ++i) {
1018 T s = zero;
1019 for (std::size_t a = 0; a < rk; ++a) s += Cinv(i, a) * V_r[a];
1020 xref[i] = xss[i] + s / num_traits<T>::from_int(static_cast<long>(n_items));
1021 if (!std::isfinite(num_traits<T>::to_double(xref[i]))) finite = false;
1022 }
1023 // the reference accepts the refinement only when all(isfinite(xref))
1024 if (finite) {
1025 xss = xref;
1026 res.refined = true;
1027 }
1028 } catch (const NumericError&) {
1029 // keep the plain mean-field fixed point, as the reference does
1030 } catch (const InputError&) {
1031 // ditto: a degenerate reduction is not an error of the caller's making
1032 }
1033
1034 // Wcov: the LNA stationary covariance at the occupancy the metrics are
1035 // read from (rmf_lna_covariance), kept only when finite, as the reference
1036 // does inside its try/catch. The eigenvalue test and the Lyapunov solve
1037 // run at double, so the field is filled at T = double only.
1038 rmf_detail::lna_covariance_into(res.Wcov, xss, p, m, n_items, h, model_dim);
1039
1040 res.xss = xss;
1041 res.pi0.assign(n_items, zero);
1042 for (std::size_t i = 0; i < n_items; ++i) {
1043 T v = xss[cache_miss_rmf_index(i, 0, n_items)];
1044 if (v < zero) v = zero;
1045 if (v > one) v = one;
1046 res.pi0[i] = v;
1047 }
1048 res.MI.assign(n_items, zero);
1049 res.M = zero;
1050 for (std::size_t i = 0; i < n_items; ++i) {
1051 res.MI[i] = lam_i[i] * res.pi0[i];
1052 res.M += res.MI[i];
1053 }
1054 res.MU.assign(u, zero);
1055 for (std::size_t v = 0; v < u; ++v) {
1056 T s = zero;
1057 for (std::size_t i = 0; i < n_items; ++i) s += lambda(v, i) * res.pi0[i];
1058 res.MU[v] = s;
1059 }
1060 return res;
1061}
1062
1063/** cache_miss_rmf on the linear chain, i.e. with no declared access graph. */
1064template <class T>
1065CacheMissRmfResult<T> cache_miss_rmf(const std::vector<T>& gamma, const std::vector<int>& m_in,
1066 const Matrix<T>& lambda, const T& tmax) {
1067 return cache_miss_rmf(gamma, m_in, lambda, tmax, std::vector<std::vector<Matrix<T> > >());
1068}
1069
1070/** cache_miss_rmf with the reference horizon tmax = 1e4. */
1071template <class T>
1072CacheMissRmfResult<T> cache_miss_rmf(const std::vector<T>& gamma, const std::vector<int>& m,
1073 const Matrix<T>& lambda) {
1074 return cache_miss_rmf(gamma, m, lambda, T(num_traits<T>::from_int(10000)));
1075}
1076
1077/**
1078 * Transient mean-field trajectory over [t0,t1] from a given initial occupancy,
1079 * the optional TSPAN/X0INIT path of cache_miss_rmf.m. Fills tout, xtraj, pi0_t
1080 * and MU_t of the result; the steady-state fields are left at their defaults
1081 * because the reference computes them independently of the transient.
1082 *
1083 * A non-empty `clk` also fills `sojourn`: the clock-weighted average of the
1084 * transient, integrated with the drift (`cache_sojourn_ode`), and its per-user
1085 * miss rate, which is the weighted average of MU_t because MU_t is affine in x.
1086 */
1087template <class T>
1089 const Matrix<T>& lambda, const T& t0, const T& t1,
1090 const std::vector<T>& x0init,
1091 const std::vector<std::vector<Matrix<T> > >& accost,
1094 "cache_miss_rmf_transient requires transcendental arithmetic");
1095 const T zero = num_traits<T>::from_int(0);
1096 const T one = num_traits<T>::from_int(1);
1097 const std::size_t u = lambda.rows();
1098 const std::size_t n_items = lambda.cols();
1099 const std::size_t h = m_in.size();
1100 const std::size_t model_dim = n_items * (h + 1);
1101 if (x0init.size() != model_dim)
1102 throw InputError("cache_miss_rmf_transient: initial occupancy has the wrong length");
1103
1104 std::vector<T> m(h, zero);
1105 for (std::size_t k = 0; k < h; ++k) m[k] = num_traits<T>::from_int(static_cast<long>(m_in[k]));
1106
1107 std::vector<T> lam_i(n_items, zero);
1108 T lam_tot = zero;
1109 for (std::size_t v = 0; v < u; ++v)
1110 for (std::size_t i = 0; i < n_items; ++i) {
1111 lam_i[i] += lambda(v, i);
1112 lam_tot += lambda(v, i);
1113 }
1114 std::vector<T> p(n_items, zero);
1115 for (std::size_t i = 0; i < n_items; ++i) p[i] = lam_i[i] / lam_tot;
1116
1118 opt.rtol = num_traits<T>::from_double(1e-8);
1119 opt.atol = num_traits<T>::from_double(1e-10);
1120 // The transient follows the SAME drift the fixed point is taken on: the
1121 // general one under a declared access graph, the chain drift otherwise.
1122 const std::vector<Matrix<T> > G = rmf_detail::build_item_graphs(accost, lambda, n_items, h);
1123 const auto f = [&](const T& t, const std::vector<T>& x) {
1124 (void)t;
1125 if (!G.empty()) return rmf_detail::drift_graph(x, p, G, m, n_items, h);
1126 return rmf_detail::drift(x, p, m, n_items, h);
1127 };
1128 const OdeSolution<T> s = ode_rosenbrock4(f, t0, t1, x0init, opt);
1129
1131 const std::size_t nt = s.t.size();
1132 res.tout = s.t;
1133 res.xtraj = Matrix<T>(model_dim, nt, zero);
1134 for (std::size_t j = 0; j < nt; ++j)
1135 for (std::size_t i = 0; i < model_dim; ++i) res.xtraj(i, j) = s.y[j][i];
1136 res.pi0_t = Matrix<T>(n_items, nt, zero);
1137 for (std::size_t i = 0; i < n_items; ++i)
1138 for (std::size_t j = 0; j < nt; ++j) {
1139 T v = s.y[j][cache_miss_rmf_index(i, 0, n_items)];
1140 if (v < zero) v = zero;
1141 if (v > one) v = one;
1142 res.pi0_t(i, j) = v;
1143 }
1144 res.MU_t = Matrix<T>(u, nt, zero);
1145 for (std::size_t v = 0; v < u; ++v)
1146 for (std::size_t j = 0; j < nt; ++j) {
1147 T acc = zero;
1148 for (std::size_t i = 0; i < n_items; ++i) acc += lambda(v, i) * res.pi0_t(i, j);
1149 res.MU_t(v, j) = acc;
1150 }
1151 if (!clk.empty()) {
1152 res.sojourn = cache_sojourn_ode(f, t0, t1, x0init, clk);
1153 if (!res.sojourn.empty()) {
1154 res.sojourn.MU.assign(u, zero);
1155 for (std::size_t v = 0; v < u; ++v) {
1156 T acc = zero;
1157 for (std::size_t i = 0; i < n_items; ++i) {
1158 T pb = res.sojourn.xbar[cache_miss_rmf_index(i, 0, n_items)];
1159 if (pb < zero) pb = zero;
1160 if (pb > one) pb = one;
1161 acc += lambda(v, i) * pb;
1162 }
1163 res.sojourn.MU[v] = acc;
1164 }
1165 }
1166 }
1167 res.M = zero;
1168 return res;
1169}
1170
1171/** cache_miss_rmf_transient on the linear chain, i.e. with no declared access graph. */
1172template <class T>
1174 const Matrix<T>& lambda, const T& t0, const T& t1,
1175 const std::vector<T>& x0init,
1177 return cache_miss_rmf_transient(m_in, lambda, t0, t1, x0init,
1178 std::vector<std::vector<Matrix<T> > >(), clk);
1179}
1180
1181/** Result of `cache_miss_rmf_expansion_transient`. */
1182template <class T>
1184 std::vector<T> t; ///< (n_points) output instants, t[0] = 0
1185 Matrix<T> X; ///< (n_points x model_dim) mean-field trajectory
1186 Matrix<T> V; ///< (n_points x model_dim) 1/N correction trajectory
1187 std::vector<Matrix<T> > W; ///< (n_points) covariance, each model_dim x model_dim
1188};
1189
1190/**
1191 * Refined mean-field TRANSIENT, `CacheRMF.meanFieldExpansionTransient`.
1192 *
1193 * The steady-state refinement solves F' V = -(1/2) sum F''_bc W_bc at the fixed
1194 * point; the transient one carries the same three objects along the trajectory,
1195 * as one coupled system in y = [X (d), V (d), W (d x d)]:
1196 *
1197 * dX/dt = F(X),
1198 * dV/dt = F'(X) V + (1/2) sum_{b,c} F''_{a,b,c} W_{b,c},
1199 * dW/dt = F'(X) W + W F'(X)^T + Q(X),
1200 *
1201 * started from V(0) = 0, W(0) = 0 at the same initial occupancy the fixed point
1202 * uses: the first m(1) items in list 1, the next m(2) in list 2, the rest
1203 * outside. The reported trajectory is X(t) + V(t)/N.
1204 *
1205 * `order = 0` integrates the drift alone and returns V and W identically zero,
1206 * which is the reference's own escape rather than a degenerate case of the
1207 * coupled system.
1208 *
1209 * DOUBLE ONLY, and for the reason the fluid solvers are: the coupled system is
1210 * integrated by LSODA at the reference's own `ode15s` tolerances (RelTol 1e-6,
1211 * AbsTol 1e-10) on the reference's own output grid `linspace(0, time,
1212 * n_points)`, and LSODA's coefficients assume double precision.
1213 *
1214 * THE HESSIAN IS HOISTED OUT OF THE RIGHT-HAND SIDE. The drift is quadratic, so
1215 * F'' does not depend on x -- the reference recomputes it per step, which is
1216 * d^3 work per evaluation for a value that never changes.
1217 */
1218template <class T>
1220 const std::vector<int>& m_in, const Matrix<T>& lambda, const T& time, std::size_t n_points,
1221 int order) {
1222 if (!std::is_same<T, double>::value)
1223 throw UnsupportedError(ErrorKind{"Arithmetic"},
1224 "cache_miss_rmf_expansion_transient: the coupled (X,V,W) system is integrated with "
1225 "LSODA, whose coefficients assume double precision; rerun with --arith double");
1226 const T zero = num_traits<T>::from_int(0);
1227 const std::size_t u = lambda.rows();
1228 const std::size_t n_items = lambda.cols();
1229 const std::size_t h = m_in.size();
1230 if (u == 0 || n_items == 0)
1231 throw InputError("cache_miss_rmf_expansion_transient: empty request-rate matrix");
1232 if (h == 0)
1233 throw InputError("cache_miss_rmf_expansion_transient: at least one cache list is required");
1234 if (n_points < 2)
1235 throw InputError("cache_miss_rmf_expansion_transient: at least two output points are "
1236 "required to describe a trajectory");
1237 if (!(num_traits<T>::to_double(time) > 0.0))
1238 throw InputError("cache_miss_rmf_expansion_transient: the horizon must be positive");
1239
1240 std::vector<T> m(h, zero);
1241 for (std::size_t k = 0; k < h; ++k) {
1242 if (m_in[k] <= 0)
1243 throw InputError("cache_miss_rmf_expansion_transient: a list has non-positive capacity");
1244 m[k] = num_traits<T>::from_int(static_cast<long>(m_in[k]));
1245 }
1246
1247 std::vector<T> lam_i(n_items, zero);
1248 T lam_tot = zero;
1249 for (std::size_t v = 0; v < u; ++v)
1250 for (std::size_t i = 0; i < n_items; ++i) {
1251 lam_i[i] += lambda(v, i);
1252 lam_tot += lambda(v, i);
1253 }
1254 if (lam_tot == zero)
1255 throw InputError("cache_miss_rmf_expansion_transient: all request rates are zero");
1256 std::vector<T> p(n_items, zero);
1257 for (std::size_t i = 0; i < n_items; ++i) p[i] = lam_i[i] / lam_tot;
1258
1259 const std::size_t d = n_items * (h + 1);
1260
1261 // The same initial occupancy `cache_miss_rmf` starts its fixed point from.
1262 std::vector<T> x0(d, zero);
1263 {
1264 std::size_t obj = 0;
1265 for (std::size_t k = 1; k <= h; ++k)
1266 for (int jj = 0; jj < m_in[k - 1]; ++jj) {
1267 ++obj;
1268 if (obj <= n_items)
1269 x0[cache_miss_rmf_index(obj - 1, k, n_items)] = num_traits<T>::from_int(1);
1270 }
1271 for (std::size_t i = obj; i < n_items; ++i)
1272 x0[cache_miss_rmf_index(i, 0, n_items)] = num_traits<T>::from_int(1);
1273 }
1274
1275 std::vector<double> grid(n_points, 0.0);
1276 const double tend = num_traits<T>::to_double(time);
1277 for (std::size_t j = 0; j < n_points; ++j)
1278 grid[j] = tend * static_cast<double>(j) / static_cast<double>(n_points - 1);
1279
1280 const std::size_t total = order == 0 ? d : d + d + d * d;
1281 std::vector<double> y0(total, 0.0);
1282 for (std::size_t i = 0; i < d; ++i) y0[i] = num_traits<T>::to_double(x0[i]);
1283
1284 const std::vector<T> Fpp = order == 0 ? std::vector<T>() : rmf_detail::hessian(p, m, n_items, h);
1285
1286 const auto rhs = [&](double t, const double* y, double* dy) {
1287 (void)t;
1288 std::vector<T> x(d, zero);
1289 for (std::size_t i = 0; i < d; ++i) x[i] = num_traits<T>::from_double(y[i]);
1290 const std::vector<T> F = rmf_detail::drift(x, p, m, n_items, h);
1291 for (std::size_t i = 0; i < d; ++i) dy[i] = num_traits<T>::to_double(F[i]);
1292 if (order == 0) return;
1293 const Matrix<T> Fp = rmf_detail::jacobian(x, p, m, n_items, h);
1294 const Matrix<T> Q = rmf_detail::noise_matrix(x, p, m, n_items, h);
1295 for (std::size_t a = 0; a < d; ++a) {
1296 T acc = zero;
1297 for (std::size_t b = 0; b < d; ++b) acc += Fp(a, b) * num_traits<T>::from_double(y[d + b]);
1298 // 0.5 * sum_{b,c} F''_{a,b,c} W_{b,c}
1299 T hcontr = zero;
1300 for (std::size_t b = 0; b < d; ++b)
1301 for (std::size_t c = 0; c < d; ++c) {
1302 const T hv = Fpp[(a * d + b) * d + c];
1303 if (hv == zero) continue;
1304 hcontr += hv * num_traits<T>::from_double(y[2 * d + b * d + c]);
1305 }
1306 dy[d + a] = num_traits<T>::to_double(acc) +
1307 0.5 * num_traits<T>::to_double(hcontr);
1308 }
1309 for (std::size_t a = 0; a < d; ++a)
1310 for (std::size_t b = 0; b < d; ++b) {
1311 T acc = Q(a, b);
1312 for (std::size_t c = 0; c < d; ++c)
1313 acc += Fp(a, c) * num_traits<T>::from_double(y[2 * d + c * d + b]) +
1314 num_traits<T>::from_double(y[2 * d + a * d + c]) * Fp(b, c);
1315 dy[2 * d + a * d + b] = num_traits<T>::to_double(acc);
1316 }
1317 };
1318
1319 LsodaOptions lopt;
1320 lopt.rtol = 1e-6;
1321 lopt.atol = 1e-10;
1322 const LsodaSolution s = lsoda_integrate(rhs, y0, grid, lopt);
1323 if (!s.success || s.y.size() != n_points)
1324 throw NumericError(ErrorKind{"NonConvergence"}, "cache_miss_rmf_expansion_transient: the coupled (X,V,W) system could "
1325 "not be integrated over the requested horizon");
1326
1328 out.t.assign(n_points, zero);
1329 out.X = Matrix<T>(n_points, d, zero);
1330 out.V = Matrix<T>(n_points, d, zero);
1331 out.W.assign(n_points, Matrix<T>(d, d, zero));
1332 for (std::size_t j = 0; j < n_points; ++j) {
1333 out.t[j] = num_traits<T>::from_double(s.t[j]);
1334 for (std::size_t i = 0; i < d; ++i) {
1335 out.X(j, i) = num_traits<T>::from_double(s.y[j][i]);
1336 if (order != 0) out.V(j, i) = num_traits<T>::from_double(s.y[j][d + i]);
1337 }
1338 if (order == 0) continue;
1339 for (std::size_t a = 0; a < d; ++a)
1340 for (std::size_t b = 0; b < d; ++b)
1341 out.W[j](a, b) = num_traits<T>::from_double(s.y[j][2 * d + a * d + b]);
1342 }
1343 return out;
1344}
1345
1346} // namespace cache
1347} // namespace line
1348
1349#endif // LINE_API_CACHE_CACHE_MISS_RMF_H
Grid-independent sojourn-weighted average of a cache mean-field transient.
Malformed or inconsistent input (dimensions, negative populations, ...): LINE:input:<kind>.
Definition error.h:103
InputError(const std::string &what)
Definition error.h:105
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
The algorithm cannot produce a trustworthy answer on this instance: LINE:numerical:<kind>.
Definition error.h:110
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
Eigenvalues and singular values, backed by LAPACK.
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LSODA: the LINE-facing wrapper over the vendored solver in third_party/lsoda.hpp.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
CacheMissRmfResult< T > cache_miss_rmf(const std::vector< T > &gamma, const std::vector< int > &m_in, const Matrix< T > &lambda, const T &tmax, const std::vector< std::vector< Matrix< T > > > &accost)
Refined mean-field miss rates of a RANDOM(m) multi-list cache.
CacheRmfExpansionTransient< T > cache_miss_rmf_expansion_transient(const std::vector< int > &m_in, const Matrix< T > &lambda, const T &time, std::size_t n_points, int order)
Refined mean-field TRANSIENT, CacheRMF.meanFieldExpansionTransient.
CacheSojournResult< T > cache_sojourn_ode(const F &f, const T &t0, const T &t1, const std::vector< T > &x0, const CacheSojournClock< T > &clk)
Integrates dx/dt = f(t, x) over [t0, t1] from x0 together with clk; see the file comment.
CacheMissRmfResult< T > cache_miss_rmf_transient(const std::vector< int > &m_in, const Matrix< T > &lambda, const T &t0, const T &t1, const std::vector< T > &x0init, const std::vector< std::vector< Matrix< T > > > &accost, const CacheSojournClock< T > &clk=CacheSojournClock< T >())
Transient mean-field trajectory over [t0,t1] from a given initial occupancy, the optional TSPAN/X0INI...
std::size_t cache_miss_rmf_index(std::size_t i, std::size_t k, std::size_t n_items)
Flat index of (item i, list k), k = 0 meaning "not cached" (rmf_index.m).
double dot(const Vec &a, const Vec &b)
Definition spn_ispn.h:201
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_abs(const T &v)
Definition number.h:184
OdeSolution< T > ode_rosenbrock4(const F &f, const J &jac, const T &t0, const T &t1, const std::vector< T > &y0, const OdeOptions< T > &opt)
Integrate y' = f(t,y) from t0 to t1 with an analytic Jacobian.
Definition ode.h:303
LsodaSolution lsoda_integrate(const LsodaRhs &f, const std::vector< double > &y0, const std::vector< double > &t_eval, const LsodaOptions &opt=LsodaOptions())
Integrate dy/dt = f(t, y) from t_eval.front() through every later entry of t_eval,...
Definition lsoda.h:178
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Definition linalg.h:72
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Definition linalg.h:36
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Definition lu.h:158
std::vector< std::complex< double > > eig_values(const Matrix< double > &A)
Eigenvalues of a general real square matrix, in LAPACK's order.
Definition eig.h:46
Matrix< double > lyap_schur(const Matrix< double > &A, const Matrix< double > &B, const Matrix< double > &C)
MATLAB lyap(A,B,C) at double via Bartels-Stewart: A X + X B + C = 0.
Definition sylvester.h:257
Number-type abstraction for the templated API port.
Adaptive stiff ODE integrator: a four-stage Rosenbrock method of order four with an embedded order-th...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Integration controls.
Definition lsoda.h:64
double atol
absolute tolerance, applied to every component
Definition lsoda.h:66
double rtol
relative tolerance, applied to every component
Definition lsoda.h:65
Result of an integration, mirroring OdeSolution in ode.h.
Definition lsoda.h:126
bool success
false when LSODA returned istate < 0
Definition lsoda.h:132
std::vector< std::vector< double > > y
y[i] is the state at t[i]
Definition lsoda.h:128
std::vector< double > t
output times, t[0] = t_eval[0]
Definition lsoda.h:127
Integration controls.
Definition ode.h:117
Result of an integration.
Definition ode.h:142
std::vector< std::vector< T > > y
y[i] is the state at t[i]
Definition ode.h:144
std::vector< T > t
accepted time points, t[0] = t0
Definition ode.h:143
Return value of cache_miss_rmf, mirroring [M,MU,MI,pi0,tout,pi0_t,MU_t,xtraj].
std::vector< T > pi0
(n_items) per-item miss probability, clipped to [0,1]
Matrix< T > pi0_t
(n_items x nt) transient miss probability
std::vector< T > tout
transient time grid, empty unless tspan was given
Matrix< T > xtraj
(model_dim x nt) transient occupancy
bool refined
true when the 1/N correction was accepted
Matrix< T > MU_t
(u x nt) transient per-user miss rate
Matrix< T > Wcov
LNA stationary covariance of the occupancy (model_dim x model_dim); empty when unavailable or not fin...
std::vector< T > MI
(n_items) per-item miss rate
CacheSojournResult< T > sojourn
Grid-independent average of the transient against a clock; empty unless one was given.
std::vector< T > xss
the occupancy the metrics were read from
std::vector< T > MU
(u) per-user miss rate
Result of cache_miss_rmf_expansion_transient.
std::vector< Matrix< T > > W
(n_points) covariance, each model_dim x model_dim
Matrix< T > X
(n_points x model_dim) mean-field trajectory
Matrix< T > V
(n_points x model_dim) 1/N correction trajectory
std::vector< T > t
(n_points) output instants, t[0] = 0
A phase-type clock: dphi/dt = phi A, phi(t0) = phi0, density phi c.
A sojourn average: xbar (empty when wtot is not positive), clock mass, per-user miss rate.
The Sylvester equation A X + X B = C, and MATLAB's lyap(A,B,C).