LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
cache_rrm_meanfield.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_RRM_MEANFIELD_H
6#define LINE_API_CACHE_CACHE_RRM_MEANFIELD_H
7
8/**
9 * @file
10 * @ingroup api_cache
11 * Steady state of the RANDOM(m) multi-list mean field.
12 *
13 * Port of matlab/src/api/cache/cache_rrm_meanfield.m, which is a SCRIPT rather
14 * than a function: it fixes n = 7 items, m = [1 1 3], a fixed popularity
15 * vector and a fixed initial condition, integrates the drift of
16 * cache_rrm_meanfield_ode.m with ode23s over [0,10000] and prints the terminal
17 * occupancy, the miss rate lambda*x(:,1) and the miss ratio. This header is
18 * that computation with the data as arguments.
19 *
20 * The right-hand side was already ported (cache_rrm_meanfield_ode.h) and noted
21 * there that the steady state needed a stiff integrator that the port did not
22 * have. It now does: line/util/ode.h. The Jacobian is not written out here --
23 * the drift is bilinear, so a numeric central-difference Jacobian is accurate
24 * to about eps^(2/3) and the integrator forms it itself; that is the deliberate
25 * choice, since unlike cache_miss_rmf nothing downstream needs an exact
26 * Jacobian and an analytic one would be a second expression of the same
27 * formula to keep in step with the first.
28 *
29 * The initial condition is the reference's: every item outside the cache,
30 * x(k,0) = 1. Integrating from there to t = 1e4 is exactly what
31 * cache_rrm_meanfield.m does.
32 *
33 * ARITHMETIC. Gated: the answer is the limit of a tolerance-driven
34 * integration.
35 */
36
37#include <cstddef>
38#include <vector>
39
41#include "line/num/number.h"
42#include "line/util/error.h"
43#include "line/util/ode.h"
44
45namespace line {
46namespace cache {
47
48/** Return value of cache_rrm_meanfield. */
49template <class T>
51 std::vector<T> x; ///< (n*(h+1)) terminal occupancy, x[k + s*n] = x(item k, level s)
52 T missrate; ///< lambda . x(:,0)
53 T missratio; ///< missrate / sum(lambda)
54};
55
56/**
57 * @brief Steady state of the RANDOM(m) multi-list mean field.
58 *
59 * @param lambda (n) per-item request rates
60 * @param m (h) list capacities
61 * @param tmax integration horizon (the reference uses 1e4)
62 */
63template <class T>
65 const std::vector<int>& m, const T& tmax) {
67 "cache_rrm_meanfield requires transcendental arithmetic: the steady state is "
68 "the limit of a tolerance-driven integration of the mean-field drift");
69 const std::size_t n = lambda.size();
70 const std::size_t h = m.size();
71 if (n == 0) throw InputError("cache_rrm_meanfield: no items");
72 if (h == 0) throw InputError("cache_rrm_meanfield: no cache lists");
73
74 const T zero = num_traits<T>::from_int(0);
75 std::vector<T> x0(n * (h + 1), zero);
76 for (std::size_t k = 0; k < n; ++k) x0[k] = num_traits<T>::from_int(1);
77
78 const auto f = [&](const T& t, const std::vector<T>& x) {
79 (void)t;
80 return cache_rrm_meanfield_ode(x, lambda, m);
81 };
84 opt.atol = num_traits<T>::from_double(1e-10);
85 opt.store_trajectory = false;
86
88 res.x = ode_rosenbrock4(f, T(num_traits<T>::from_int(0)), tmax, x0, opt).final_state();
89 res.missrate = zero;
90 T tot = zero;
91 for (std::size_t k = 0; k < n; ++k) {
92 res.missrate += lambda[k] * res.x[k];
93 tot += lambda[k];
94 }
95 res.missratio = res.missrate / tot;
96 return res;
97}
98
99/** cache_rrm_meanfield with the reference horizon tmax = 1e4. */
100template <class T>
102 const std::vector<int>& m) {
103 return cache_rrm_meanfield(lambda, m, T(num_traits<T>::from_int(10000)));
104}
105
106} // namespace cache
107} // namespace line
108
109#endif // LINE_API_CACHE_CACHE_RRM_MEANFIELD_H
Mean-field drift of the RANDOM(m) multi-list cache.
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
std::vector< T > cache_rrm_meanfield_ode(const std::vector< T > &x, const std::vector< T > &lambda, const std::vector< int > &m)
Mean-field drift of the RANDOM(m) multi-list cache.
CacheRrmMeanfieldResult< T > cache_rrm_meanfield(const std::vector< T > &lambda, const std::vector< int > &m, const T &tmax)
Steady state of the RANDOM(m) multi-list mean field.
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:304
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...
Integration controls.
Definition ode.h:118
Return value of cache_rrm_meanfield.
std::vector< T > x
(n*(h+1)) terminal occupancy, x[k + s*n] = x(item k, level s)