LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
retrieval_mva.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_RETRIEVAL_RETRIEVAL_MVA_H
6#define LINE_API_RETRIEVAL_RETRIEVAL_MVA_H
7
8/**
9 * @file
10 * @ingroup api_retrieval
11 * Exact MVA-style recursion for delayed-hit (list-based) cache metrics.
12 *
13 * Templated port of matlab/src/api/retrieval/retrieval_mva.m. There is no JAR
14 * counterpart: jline/api/retrieval/ ships nc, metrics, fpi and fpi_latency
15 * only, so MATLAB is the sole reference for this function.
16 *
17 * This is the exact recursion that retrieval_fpi approximates. Writing
18 * phi^{(k)} for the delayed-hit probability in the system WITHOUT item k,
19 *
20 * theta_ij(m) = gamma_ij / (1 + lambda_i eta_{0,i}
21 * + sum_s lambda_i eta_{s,i}(1 + sum_{k!=i} phi^{(i)}_{s,k}(m-1_j)))
22 * xi_j(m) = m_j / sum_i theta_ij(m)(1 - pihit_i(m-1_j))
23 * pi_ij(m) = theta_ij(m) xi_j(m) (1 - pihit_i(m-1_j))
24 * pi_i0(m) = (1 - pihit_i(m)) / (1 + lambda_i eta_{0,i}
25 * + sum_s lambda_i eta_{s,i}(1 + sum_{k!=i} phi^{(i)}_{s,k}(m)))
26 * phi_{s,k}(m)= lambda_k pi_k0(m) eta_{s,k}(1 + sum_{i!=k} phi^{(k)}_{s,i}(m))
27 * phi_{0,k}(m)= lambda_k eta_{0,k} pi_k0(m)
28 *
29 * memoized over (item subset, capacity vector). The recursion bottoms out at
30 * the empty item set and at any capacity able to hold every remaining item,
31 * where the items are permanently cached (pihit = 1, no fetching at all).
32 * The recursion never assigns pi_ij at that boundary, so when sum(m) == n the
33 * hit ratios come from the gamma-only product form (saturated_phit below), and
34 * sum(m) > n is refused: every list is full in such a model, which admits no
35 * placement (retrieval_nc returns 0).
36 * Cost is O(2^n n^2 h r prod_j (1+m_j)) time and memory, so it is a small-case
37 * oracle: use retrieval_fpi beyond that.
38 *
39 * ARITHMETIC: additions, multiplications and divisions of the inputs only, so
40 * a finite field computation, exact in the exact instantiation. It then agrees
41 * with retrieval_metrics as an identity between rationals, which is the
42 * strongest available check on either implementation.
43 */
44
45#include <cstddef>
46#include <vector>
47
48#include "line/num/number.h"
49#include "line/util/error.h"
50#include "line/util/matrix.h"
51
52namespace line {
53namespace retrieval {
54
55/** Mirrors the [pmiss, phit, pdh] return list of the MATLAB function. */
56template <class T>
58 std::vector<T> pmiss; ///< (n) miss ratios pi_{i,0}
59 Matrix<T> phit; ///< (h x n) hit ratios pi_{i,j}
60 Matrix<T> pdh; ///< ((r+1) x n) delayed-hit probabilities phi_{s,i}, s = 0..r
61};
62
63namespace detail {
64
65/**
66 * Memo tables and the recursion itself. Held in an object rather than in
67 * statics so the function is reentrant and leaves no state behind.
68 */
69template <class T>
70class RetrievalMvaSolver {
71public:
72 RetrievalMvaSolver(const std::vector<int>& m, const std::vector<T>& lambda,
73 const Matrix<T>& eta, const Matrix<T>& gamma)
74 : m_(m), lambda_(lambda), eta_(eta), gamma_(gamma), n_(lambda.size()), h_(m.size()),
75 r_(eta.cols() - 1) {
76 radix_.resize(h_);
77 ncap_ = 1;
78 for (std::size_t j = 0; j < h_; ++j) {
79 radix_[j] = static_cast<std::size_t>(m_[j]) + 1;
80 ncap_ *= radix_[j];
81 }
82 nmask_ = static_cast<std::size_t>(1) << n_;
83 const std::size_t cells = nmask_ * ncap_;
84 done_.assign(cells, false);
85 const T zero = num_traits<T>::from_int(0);
86 pi0_.assign(cells * n_, zero);
87 pihit_.assign(cells * n_, zero);
88 pij_.assign(cells * n_ * h_, zero);
89 phi_.assign(cells * n_ * (r_ + 1), zero);
90 }
91
93 const std::size_t full = nmask_ - 1;
94 solve(full, m_);
95 const std::size_t ci = capidx(m_);
97 out.pmiss.assign(n_, num_traits<T>::from_int(0));
98 out.phit = Matrix<T>(h_, n_, num_traits<T>::from_int(0));
99 out.pdh = Matrix<T>(r_ + 1, n_, num_traits<T>::from_int(0));
100 long msum = 0;
101 for (int x : m_) msum += x;
102 const bool saturated = msum == static_cast<long>(n_);
103 for (std::size_t i = 0; i < n_; ++i) {
104 out.pmiss[i] = pi0_[(full * ncap_ + ci) * n_ + i];
105 if (!saturated)
106 for (std::size_t j = 0; j < h_; ++j)
107 out.phit(j, i) = pij_[((full * ncap_ + ci) * n_ + i) * h_ + j];
108 for (std::size_t s = 0; s <= r_; ++s)
109 out.pdh(s, i) = phi_[((full * ncap_ + ci) * n_ + i) * (r_ + 1) + s];
110 }
111 if (saturated) out.phit = saturated_phit();
112 return out;
113 }
114
115private:
116 /**
117 * Hit ratios pi_{i,j} when the lists hold exactly every item (sum(m) == n).
118 * No item is ever outside the cache, so the retrieval system is idle and
119 * the state is a placement of the n items with exactly m_j of them in list
120 * j. Its stationary law is the product form prod_i gamma(i, j_i), which is
121 * what retrieval_nc reduces to at sum(m) == n. With
122 * E(S, c) = sum_j c_j gamma(k, j) E(S minus k, c - 1_j) and E(empty) = 1,
123 * pi_{i,j} = m_j gamma(i, j) E(items minus i, m - 1_j) / E(items, m),
124 * exactly as retrieval_metrics; sum_j pi_{i,j} = 1 by expanding E(items, m)
125 * on item i.
126 */
127 Matrix<T> saturated_phit() const {
128 Matrix<T> phit(h_, n_, num_traits<T>::from_int(0));
129 std::vector<std::size_t> all(n_);
130 for (std::size_t i = 0; i < n_; ++i) all[i] = i;
131 const T E = saturated_const(all, m_);
132 for (std::size_t i = 0; i < n_; ++i) {
133 std::vector<std::size_t> others;
134 for (std::size_t k = 0; k < n_; ++k)
135 if (k != i) others.push_back(k);
136 for (std::size_t j = 0; j < h_; ++j) {
137 if (m_[j] == 0) continue;
138 std::vector<int> mp = m_;
139 mp[j] -= 1;
140 phit(j, i) = num_traits<T>::from_int(static_cast<long>(m_[j])) * gamma_(i, j) *
141 saturated_const(others, mp) / E;
142 }
143 }
144 return phit;
145 }
146
147 /** E(items, c) by a recursion over the item prefix, memoized on (k, c). */
148 T saturated_const(const std::vector<std::size_t>& items, const std::vector<int>& c) const {
149 const std::size_t K = items.size();
150 std::vector<T> memo((K + 1) * ncap_, num_traits<T>::from_int(0));
151 std::vector<bool> have((K + 1) * ncap_, false);
152 return saturated_rec(items, K, c, memo, have);
153 }
154
155 T saturated_rec(const std::vector<std::size_t>& items, std::size_t k, const std::vector<int>& cc,
156 std::vector<T>& memo, std::vector<bool>& have) const {
157 if (k == 0) return num_traits<T>::from_int(1);
158 const std::size_t key = k * ncap_ + capidx(cc);
159 if (have[key]) return memo[key];
160 const std::size_t it = items[k - 1];
161 T acc = num_traits<T>::from_int(0);
162 for (std::size_t j = 0; j < h_; ++j) {
163 if (cc[j] > 0) {
164 std::vector<int> nc = cc;
165 nc[j] -= 1;
166 acc += num_traits<T>::from_int(static_cast<long>(cc[j])) * gamma_(it, j) *
167 saturated_rec(items, k - 1, nc, memo, have);
168 }
169 }
170 memo[key] = acc;
171 have[key] = true;
172 return acc;
173 }
174
175 std::size_t capidx(const std::vector<int>& c) const {
176 std::size_t idx = 0, mul = 1;
177 for (std::size_t j = 0; j < h_; ++j) {
178 idx += static_cast<std::size_t>(c[j]) * mul;
179 mul *= radix_[j];
180 }
181 return idx;
182 }
183
184 T& PI0(std::size_t mask, std::size_t ci, std::size_t i) {
185 return pi0_[(mask * ncap_ + ci) * n_ + i];
186 }
187 T& PIHIT(std::size_t mask, std::size_t ci, std::size_t i) {
188 return pihit_[(mask * ncap_ + ci) * n_ + i];
189 }
190 T& PIJ(std::size_t mask, std::size_t ci, std::size_t i, std::size_t j) {
191 return pij_[((mask * ncap_ + ci) * n_ + i) * h_ + j];
192 }
193 T& PHI(std::size_t mask, std::size_t ci, std::size_t i, std::size_t s) {
194 return phi_[((mask * ncap_ + ci) * n_ + i) * (r_ + 1) + s];
195 }
196
197 void solve(std::size_t mask, const std::vector<int>& c) {
198 const std::size_t ci = capidx(c);
199 if (done_[mask * ncap_ + ci]) return;
200 if (mask == 0) {
201 done_[mask * ncap_ + ci] = true;
202 return;
203 }
204 std::vector<std::size_t> active;
205 for (std::size_t i = 0; i < n_; ++i)
206 if (mask & (static_cast<std::size_t>(1) << i)) active.push_back(i);
207
208 long csum = 0;
209 for (int x : c) csum += x;
210 if (csum >= static_cast<long>(active.size())) {
211 // The cache holds every active item, so all of them are cached
212 // permanently: no miss, no fetch, hit probability one. Only PIHIT
213 // and PHI are read here, never PIJ, so the split of that hit across
214 // the lists is left to saturated_phit, called by run() at sum(m) == n.
215 for (std::size_t i : active) PIHIT(mask, ci, i) = num_traits<T>::from_int(1);
216 done_[mask * ncap_ + ci] = true;
217 return;
218 }
219
220 const T one = num_traits<T>::from_int(1);
221
222 // dependencies at m - 1_j, with and without each active item
223 for (std::size_t j = 0; j < h_; ++j) {
224 if (c[j] > 0) {
225 std::vector<int> cj = c;
226 cj[j] -= 1;
227 solve(mask, cj);
228 for (std::size_t i : active) solve(mask & ~(static_cast<std::size_t>(1) << i), cj);
229 }
230 }
231 for (std::size_t k : active) solve(mask & ~(static_cast<std::size_t>(1) << k), c);
232
233 // theta, xi and pi_ij, all evaluated on the cache recursion m - 1_j
234 for (std::size_t j = 0; j < h_; ++j) {
235 if (c[j] == 0) continue;
236 std::vector<int> cj = c;
237 cj[j] -= 1;
238 const std::size_t cjx = capidx(cj);
239 std::vector<T> theta(n_, num_traits<T>::from_int(0));
240 for (std::size_t i : active) {
241 const std::size_t maski = mask & ~(static_cast<std::size_t>(1) << i);
242 T acc = num_traits<T>::from_int(0);
243 for (std::size_t s = 0; s < r_; ++s) {
244 T sphi = num_traits<T>::from_int(0);
245 for (std::size_t k : active)
246 if (k != i) sphi += PHI(maski, cjx, k, s + 1);
247 acc += lambda_[i] * eta_(i, s + 1) * (one + sphi);
248 }
249 theta[i] = gamma_(i, j) / (one + lambda_[i] * eta_(i, 0) + acc);
250 }
251 T sden = num_traits<T>::from_int(0);
252 for (std::size_t i : active) sden += theta[i] * (one - PIHIT(mask, cjx, i));
253 if (sden == num_traits<T>::from_int(0))
254 throw NumericError(ErrorKind{"Degenerate"}, "retrieval_mva: degenerate list occupancy (zero denominator)");
255 const T xi_j = num_traits<T>::from_int(static_cast<long>(c[j])) / sden;
256 for (std::size_t i : active)
257 PIJ(mask, ci, i, j) = theta[i] * xi_j * (one - PIHIT(mask, cjx, i));
258 }
259
260 for (std::size_t i : active) {
261 T ph = num_traits<T>::from_int(0);
262 for (std::size_t j = 0; j < h_; ++j) ph += PIJ(mask, ci, i, j);
263 PIHIT(mask, ci, i) = ph;
264 }
265
266 for (std::size_t i : active) {
267 const std::size_t maski = mask & ~(static_cast<std::size_t>(1) << i);
268 T acc = num_traits<T>::from_int(0);
269 for (std::size_t s = 0; s < r_; ++s) {
270 T sphi = num_traits<T>::from_int(0);
271 for (std::size_t k : active)
272 if (k != i) sphi += PHI(maski, ci, k, s + 1);
273 acc += lambda_[i] * eta_(i, s + 1) * (one + sphi);
274 }
275 PI0(mask, ci, i) =
276 (one - PIHIT(mask, ci, i)) / (one + lambda_[i] * eta_(i, 0) + acc);
277 }
278
279 for (std::size_t k : active) {
280 const std::size_t maskk = mask & ~(static_cast<std::size_t>(1) << k);
281 const T pi0k = PI0(mask, ci, k);
282 PHI(mask, ci, k, 0) = lambda_[k] * eta_(k, 0) * pi0k;
283 for (std::size_t s = 0; s < r_; ++s) {
284 T sphi = num_traits<T>::from_int(0);
285 for (std::size_t i : active)
286 if (i != k) sphi += PHI(maskk, ci, i, s + 1);
287 PHI(mask, ci, k, s + 1) = lambda_[k] * pi0k * eta_(k, s + 1) * (one + sphi);
288 }
289 }
290
291 done_[mask * ncap_ + ci] = true;
292 }
293
294 const std::vector<int>& m_;
295 const std::vector<T>& lambda_;
296 const Matrix<T>& eta_;
297 const Matrix<T>& gamma_;
298 std::size_t n_, h_, r_;
299 std::vector<std::size_t> radix_;
300 std::size_t ncap_ = 1, nmask_ = 1;
301 std::vector<bool> done_;
302 std::vector<T> pi0_, pihit_, pij_, phi_;
303};
304
305} // namespace detail
306
307/**
308 * @brief Exact MVA-style recursion for delayed-hit (list-based) cache
309 * metrics.
310 *
311 * @param m (h) cache list capacities
312 * @param lambda (n) per-item arrival rates
313 * @param eta (n x (r+1)) fetching demands, column 0 = IS station, columns 1..r = PS
314 * @param gamma (n x h) access factors
315 */
316template <class T>
317RetrievalMvaResult<T> retrieval_mva(const std::vector<int>& m, const std::vector<T>& lambda,
318 const Matrix<T>& eta, const Matrix<T>& gamma) {
319 const std::size_t n = lambda.size();
320 if (eta.rows() != n || gamma.rows() != n)
321 throw InputError("retrieval_mva: eta/gamma and lambda disagree on the item count");
322 if (gamma.cols() != m.size())
323 throw InputError("retrieval_mva: gamma and m disagree on the number of lists");
324 if (eta.cols() == 0) throw InputError("retrieval_mva: eta has no columns");
325 for (int x : m)
326 if (x < 0) throw InputError("retrieval_mva: negative list capacity");
327 long msum = 0;
328 for (int x : m) msum += x;
329 if (msum > static_cast<long>(n))
330 throw InputError("retrieval_mva: the lists hold more slots than there are items. Every list is "
331 "full in this model, so it admits no placement (retrieval_nc returns 0).");
332 if (n > 8 * sizeof(std::size_t) - 1)
333 throw UnsupportedError(ErrorKind{"StateSpace"}, "retrieval_mva: too many items for a bitmask subset enumeration");
334 detail::RetrievalMvaSolver<T> solver(m, lambda, eta, gamma);
335 return solver.run();
336}
337
338} // namespace retrieval
339} // namespace line
340
341#endif // LINE_API_RETRIEVAL_RETRIEVAL_MVA_H
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
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
The exception types the port throws.
Dense matrix and non-owning view.
RetrievalMvaResult< T > retrieval_mva(const std::vector< int > &m, const std::vector< T > &lambda, const Matrix< T > &eta, const Matrix< T > &gamma)
Exact MVA-style recursion for delayed-hit (list-based) cache metrics.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
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
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Mirrors the [pmiss, phit, pdh] return list of the MATLAB function.
Matrix< T > phit
(h x n) hit ratios pi_{i,j}
Matrix< T > pdh
((r+1) x n) delayed-hit probabilities phi_{s,i}, s = 0..r
std::vector< T > pmiss
(n) miss ratios pi_{i,0}