5#ifndef LINE_API_CACHE_CACHE_MISS_RMF_H
6#define LINE_API_CACHE_CACHE_MISS_RMF_H
116 return i + k * n_items;
138namespace rmf_detail {
142T hit_rate(
const std::vector<T>& x,
const std::vector<T>& p, std::size_t level,
143 std::size_t n_items) {
145 for (std::size_t i = 0; i < n_items; ++i) hr += p[i] * x[
cache_miss_rmf_index(i, level, n_items)];
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) {
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) {
162 const T flow = p[i] * x[ik] - hr[k] * x[ik1] / m[k];
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) {
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) {
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];
234std::vector<T> hessian(
const std::vector<T>& p,
const std::vector<T>& m, std::size_t n_items,
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;
242 for (std::size_t i = 0; i < n_items; ++i) {
243 for (std::size_t k = 0; k + 1 <= h; ++k) {
246 for (std::size_t j = 0; j < n_items; ++j) {
247 if (j == i)
continue;
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];
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) {
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]);
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) {
304 opt.store_trajectory =
false;
305 const auto f = [&](
const T& t,
const std::vector<T>& x) {
307 return drift(x, p, m, n_items, h);
310 return ode_rosenbrock4(f, T(num_traits<T>::from_int(0)), tmax, x0, opt).final_state();
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,
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)));
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);
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];
379 std::size_t rank = 0;
380 std::vector<std::vector<T>> basis;
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);
388 Matrix<T> M(n, n, zero);
390 for (std::size_t i = 0; i < n; ++i)
391 for (std::size_t j = 0; j < n; ++j) {
394 if (a > scale) scale = a;
396 if (scale == zero) scale = num_traits<T>::from_int(1);
399 const T tol = scale * num_traits<T>::from_double(1e-8);
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;
405 for (std::size_t col = 0; col < n && row < n; ++col) {
408 for (std::size_t i = row + 1; i < n; ++i) {
409 const T a =
num_abs(M(i, col));
415 if (best <= tol)
continue;
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);
426 col_of_pivot.push_back(col);
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;
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);
445 for (std::size_t i = 0; i < ns.basis.size(); ++i) {
446 for (std::size_t k = 0; k < i; ++k) {
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];
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;
477inline NullSpace<double> left_null_space_svd(
const Matrix<double>& Fp) {
478#ifndef LINE_MP_HAVE_LAPACK
479 return left_null_space(Fp);
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;
493 lapack::dgesvd(
"A",
"N", &ni, &ni, a.data(), &ni, s.data(), u.data(), &ni, vt.data(), &one, &wopt,
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");
502 const double tol = 1e-8 * s[0];
503 NullSpace<double> ns;
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);
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) {
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];
533 for (std::size_t i = 0; i < n; ++i) nv += v[i] * v[i];
535 if (nv <= 1e-12)
continue;
536 for (std::size_t i = 0; i < n; ++i) v[i] /= nv;
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];
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);
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,
566 if (x.size() != dim)
throw InputError(
"cache_rmf_lna: the fixed point has the wrong length");
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));
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);
580 if (V.cols() == 0)
return Matrix<double>(dim, dim, 0.0);
583 "cache_rmf_lna: the centred basis does not span the state dimension; n, h and dim "
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);
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));
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))
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");
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);
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));
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) {
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);
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));
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;
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) {
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;
659 }
catch (
const Error&) {
666NullSpace<T> left_null_space_for(
const Matrix<T>& Fp) {
667 return left_null_space(Fp);
671inline NullSpace<double> left_null_space_for<double>(
const Matrix<double>& Fp) {
672 return left_null_space_svd(Fp);
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);
688 for (std::size_t a = 1; a + 1 <= h; ++a) g(a, a + 1) = one;
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);
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))
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);
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) {
739 for (std::size_t a = 0; a <= h; ++a) {
741 for (std::size_t b = 0; b <= h; ++b) srow += gk(a, b);
743 for (std::size_t b = 0; b <= h; ++b) gk(a, b) = gk(a, b) / srow;
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)
752 if (!is_linear) G = Gc;
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;
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) {
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);
785 std::vector<T> dX(model_dim, zero);
786 for (std::size_t k = 0; k < n; ++k) {
788 for (std::size_t i = 1; i <= h; ++i) {
790 T infl = p[k] * outk * G[k](0, i);
791 for (std::size_t s = 1; s + 1 <= i; ++s)
793 for (std::size_t b = i + 1; b <= h; ++b)
795 T outfl = p[k] * xki * T(one - G[k](i, i));
797 for (std::size_t s = 0; s + 1 <= i; ++s) disp += A(s, i);
798 outfl += disp * xki / m[i - 1];
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) {
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) {
823 return drift_graph(x, p, G, m, n, h);
826 T(num_traits<T>::from_int(20000)), x0, opt)
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");
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");
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");
873 std::vector<T> lam_i(n_items, 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);
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;
884 const std::size_t model_dim = n_items * (h + 1);
888 std::vector<T> x0(model_dim, zero);
890 for (std::size_t k = 1; k <= h; ++k)
891 for (
int jj = 0; jj < m_in[k - 1]; ++jj) {
898 const std::vector<Matrix<T> > G = rmf_detail::build_item_graphs(accost, lambda, n_items, h);
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) {
906 if (v < zero) v = zero;
907 if (v > one) v = one;
910 res.
MI.assign(n_items, zero);
912 for (std::size_t i = 0; i < n_items; ++i) {
913 res.
MI[i] = lam_i[i] * res.
pi0[i];
916 res.
MU.assign(u, zero);
917 for (std::size_t v = 0; v < u; ++v) {
919 for (std::size_t i = 0; i < n_items; ++i) s += lambda(v, i) * res.
pi0[i];
927 std::vector<T> xss = rmf_detail::fixed_point(x0, p, m, n_items, h, tmax);
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)
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);
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)
946 if (d != rk)
throw NumericError(
ErrorKind{
"Degenerate"},
"cache_miss_rmf: the reduction basis is too small");
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];
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);
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);
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);
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;
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) {
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;
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) {
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;
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) {
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;
1001 const Matrix<T> W_r = rmf_detail::lyapunov(Fp_r, Q_r);
1003 std::vector<T> C_r(rk, zero);
1004 for (std::size_t a = 0; a < rk; ++a) {
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);
1010 std::vector<T> rhs(rk, zero);
1011 for (std::size_t a = 0; a < rk; ++a)
1013 const std::vector<T> V_r =
line::solve(Fp_r, rhs);
1015 std::vector<T> xref(model_dim, zero);
1017 for (std::size_t i = 0; i < model_dim; ++i) {
1019 for (std::size_t a = 0; a < rk; ++a) s += Cinv(i, a) * V_r[a];
1038 rmf_detail::lna_covariance_into(res.
Wcov, xss, p, m, n_items, h, model_dim);
1041 res.
pi0.assign(n_items, zero);
1042 for (std::size_t i = 0; i < n_items; ++i) {
1044 if (v < zero) v = zero;
1045 if (v > one) v = one;
1048 res.
MI.assign(n_items, zero);
1050 for (std::size_t i = 0; i < n_items; ++i) {
1051 res.
MI[i] = lam_i[i] * res.
pi0[i];
1054 res.
MU.assign(u, zero);
1055 for (std::size_t v = 0; v < u; ++v) {
1057 for (std::size_t i = 0; i < n_items; ++i) s += lambda(v, i) * res.
pi0[i];
1066 const Matrix<T>& lambda,
const T& tmax) {
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");
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");
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]));
1107 std::vector<T> lam_i(n_items, 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);
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;
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) {
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);
1131 const std::size_t nt = s.
t.size();
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];
1137 for (std::size_t i = 0; i < n_items; ++i)
1138 for (std::size_t j = 0; j < nt; ++j) {
1140 if (v < zero) v = zero;
1141 if (v > one) v = one;
1142 res.
pi0_t(i, j) = v;
1145 for (std::size_t v = 0; v < u; ++v)
1146 for (std::size_t j = 0; j < nt; ++j) {
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;
1154 res.
sojourn.MU.assign(u, zero);
1155 for (std::size_t v = 0; v < u; ++v) {
1157 for (std::size_t i = 0; i < n_items; ++i) {
1159 if (pb < zero) pb = zero;
1160 if (pb > one) pb = one;
1161 acc += lambda(v, i) * pb;
1174 const Matrix<T>& lambda,
const T& t0,
const T& t1,
1175 const std::vector<T>& x0init,
1178 std::vector<std::vector<
Matrix<T> > >(), clk);
1187 std::vector<Matrix<T> >
W;
1220 const std::vector<int>& m_in,
const Matrix<T>& lambda,
const T& time, std::size_t n_points,
1222 if (!std::is_same<T, double>::value)
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");
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");
1233 throw InputError(
"cache_miss_rmf_expansion_transient: at least one cache list is required");
1235 throw InputError(
"cache_miss_rmf_expansion_transient: at least two output points are "
1236 "required to describe a trajectory");
1238 throw InputError(
"cache_miss_rmf_expansion_transient: the horizon must be positive");
1240 std::vector<T> m(h, zero);
1241 for (std::size_t k = 0; k < h; ++k) {
1243 throw InputError(
"cache_miss_rmf_expansion_transient: a list has non-positive capacity");
1247 std::vector<T> lam_i(n_items, 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);
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;
1259 const std::size_t d = n_items * (h + 1);
1262 std::vector<T> x0(d, zero);
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) {
1271 for (std::size_t i = obj; i < n_items; ++i)
1275 std::vector<double> grid(n_points, 0.0);
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);
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]);
1284 const std::vector<T> Fpp = order == 0 ? std::vector<T>() : rmf_detail::hessian(p, m, n_items, h);
1286 const auto rhs = [&](
double t,
const double* y,
double* dy) {
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) {
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;
1309 for (std::size_t a = 0; a < d; ++a)
1310 for (std::size_t b = 0; b < d; ++b) {
1312 for (std::size_t c = 0; c < d; ++c)
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");
1328 out.
t.assign(n_points, zero);
1331 out.W.assign(n_points,
Matrix<T>(d, d, zero));
1332 for (std::size_t j = 0; j < n_points; ++j) {
1334 for (std::size_t i = 0; i < d; ++i) {
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)
Grid-independent sojourn-weighted average of a cache mean-field transient.
The algorithm cannot produce a trustworthy answer on this instance: LINE:numerical:<kind>.
NumericError(const std::string &what)
UnsupportedError(const std::string &what)
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)
Conservation laws of a layered queueing network, enumerated from its structure.
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.
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,...
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
std::vector< std::complex< double > > eig_values(const Matrix< double > &A)
Eigenvalues of a general real square matrix, in LAPACK's order.
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.
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....
double atol
absolute tolerance, applied to every component
double rtol
relative tolerance, applied to every component
Result of an integration, mirroring OdeSolution in ode.h.
bool success
false when LSODA returned istate < 0
std::vector< std::vector< double > > y
y[i] is the state at t[i]
std::vector< double > t
output times, t[0] = t_eval[0]
Result of an integration.
std::vector< std::vector< T > > y
y[i] is the state at t[i]
std::vector< T > t
accepted time points, t[0] = t0
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).