LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
cache_gamma.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_GAMMA_H
6#define LINE_API_CACHE_GAMMA_H
7
8/**
9 * @file
10 * @ingroup api_cache
11 * Access factors of a multi-list cache whose lists form a general access GRAPH.
12 *
13 * Templated port of jar/src/main/java/jline/api/cache/Cache_gamma.java. MATLAB
14 * has no counterpart: it carries only cache_gamma_lp, the specialization to a
15 * tree ("linear path"), which recovers the path by walking the unique parent
16 * relation and rejects a node with two parents.
17 *
18 * Here the structure is only required to be reachable. The path is the
19 * BREADTH-FIRST shortest path in the access graph of item i, so a node with
20 * several parents is admissible and the first shortest path found in node order
21 * is the one taken. Along that path,
22 *
23 * gamma(i,j) = (sum_v lambda(v,i,0)) prod_{edges (a,b)} sum_v lambda(v,i,a) R{v,i}(a,b)
24 *
25 * Column j walks to node j+1, node 0 being the miss list, as cache_gamma_lp
26 * does. Until 2026-11-14 all four codebases walked to node j, so column 0 was
27 * the trivial one-node path to the miss node and list h had no column at all;
28 * that off-by-one was corrected in every codebase together.
29 *
30 * TWO DIVERGENCES FROM cache_gamma_lp, both changing the number, so a caller
31 * must not treat the two as substitutes:
32 * - the leading factor is the aggregate miss-node request rate sum_v
33 * lambda(v,i,0), whereas the tree version starts the product at one;
34 * - each edge factor reads lambda(v,i,a) at the SOURCE node a alone, whereas
35 * the tree version sums lambda(v,i,t) over every t <= a.
36 * No solver in any codebase calls this routine. Use cache_gamma_lp for the
37 * access factors a cache solver consumes.
38 *
39 * An unreachable node gives gamma(i,j) = 0, which unlike the tree version is a
40 * REACHABLE branch here: the BFS genuinely returns no path.
41 *
42 * THE GRAPH IS READ FROM USER 0 ONLY. The JAR takes R.get(0).get(i) for the
43 * adjacency and then sums the per-user rates along that one path, so a model
44 * whose users route an item differently is analysed on the first user's graph.
45 * Reproduced rather than corrected, since changing it would silently move the
46 * answer for every such model.
47 *
48 * Arithmetic: EXACT-CAPABLE. Sums and products only; the BFS is pure integer
49 * bookkeeping and tests adjacency against zero, which is exact in any T.
50 */
51
52#include <cstddef>
53#include <deque>
54#include <vector>
55
56#include "line/num/number.h"
57#include "line/util/error.h"
58#include "line/util/matrix.h"
59
60namespace line {
61namespace cache {
62
63/** Return value of cache_gamma, mirroring the JAR's Ret.cacheGamma. */
64template <class T>
66 Matrix<T> gamma; ///< (n x h) access factors
67 std::size_t u; ///< number of user streams
68 std::size_t n; ///< number of items
69 std::size_t h; ///< number of lists
70};
71
72namespace detail {
73
74/**
75 * Breadth-first shortest path from source to destination over the nonzero
76 * entries of an adjacency matrix; empty when the destination is unreachable.
77 */
78template <class T>
79std::vector<std::size_t> cache_bfs_path(const Matrix<T>& adjacency, std::size_t source,
80 std::size_t destination) {
81 const std::size_t n = adjacency.rows();
82 if (source >= n || destination >= n) return std::vector<std::size_t>();
83 const T zero = num_traits<T>::from_int(0);
84 std::vector<bool> visited(n, false);
85 std::vector<int> parent(n, -1);
86 std::deque<std::size_t> queue;
87 queue.push_back(source);
88 visited[source] = true;
89 while (!queue.empty()) {
90 const std::size_t current = queue.front();
91 queue.pop_front();
92 if (current == destination) {
93 std::vector<std::size_t> path;
94 int node = static_cast<int>(destination);
95 while (node != -1) {
96 path.insert(path.begin(), static_cast<std::size_t>(node));
97 node = parent[node];
98 }
99 return path;
100 }
101 for (std::size_t next = 0; next < n; ++next)
102 if (!visited[next] && adjacency(current, next) > zero) {
103 visited[next] = true;
104 parent[next] = static_cast<int>(current);
105 queue.push_back(next);
106 }
107 }
108 return std::vector<std::size_t>();
109}
110
111} // namespace detail
112
113/**
114 * @brief Access factors of a multi-list cache whose lists form a general
115 * access GRAPH.
116 *
117 * @param lambda (u) matrices of size (n x (h+1)): lambda[v](i,t) is the rate at
118 * which user v requests item i while it sits at node t
119 * @param R (u x n) routing matrices of size ((h+1) x (h+1))
120 */
121template <class T>
123 const std::vector<std::vector<Matrix<T>>>& R) {
124 if (lambda.empty()) throw InputError("cache_gamma: no user streams");
125 const std::size_t u = lambda.size();
126 const std::size_t n = lambda[0].rows();
127 if (lambda[0].cols() == 0) throw InputError("cache_gamma: empty lambda");
128 const std::size_t h = lambda[0].cols() - 1;
129 if (R.size() != u) throw InputError("cache_gamma: R and lambda disagree on the user count");
130 for (std::size_t v = 0; v < u; ++v)
131 if (R[v].size() != n)
132 throw InputError("cache_gamma: R and lambda disagree on the item count");
133
134 const T zero = num_traits<T>::from_int(0);
136 res.u = u;
137 res.n = n;
138 res.h = h;
139 res.gamma = Matrix<T>(n, h, zero);
140
141 for (std::size_t i = 0; i < n; ++i) {
142 const Matrix<T>& graph = R[0][i];
143 for (std::size_t j = 0; j < h; ++j) {
144 const std::vector<std::size_t> Pj = detail::cache_bfs_path(graph, 0, j + 1);
145 if (Pj.empty()) continue; // unreachable list: no access factor
146 T g = zero;
147 for (std::size_t v = 0; v < u; ++v) g += lambda[v](i, 0);
148 for (std::size_t li = 1; li < Pj.size(); ++li) {
149 const std::size_t a = Pj[li - 1];
150 const std::size_t b = Pj[li];
151 T y = zero;
152 for (std::size_t v = 0; v < u; ++v) y += lambda[v](i, a) * R[v][i](a, b);
153 g *= y;
154 }
155 res.gamma(i, j) = g;
156 }
157 }
158 return res;
159}
160
161} // namespace cache
162} // namespace line
163
164#endif // LINE_API_CACHE_GAMMA_H
InputError(const std::string &what)
Definition error.h:105
std::size_t rows() const
Definition matrix.h:89
The exception types the port throws.
Dense matrix and non-owning view.
CacheGammaGraphResult< T > cache_gamma(const std::vector< Matrix< T > > &lambda, const std::vector< std::vector< Matrix< T > > > &R)
Access factors of a multi-list cache whose lists form a general access GRAPH.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Return value of cache_gamma, mirroring the JAR's Ret.cacheGamma.
Definition cache_gamma.h:65
std::size_t h
number of lists
Definition cache_gamma.h:69
Matrix< T > gamma
(n x h) access factors
Definition cache_gamma.h:66
std::size_t u
number of user streams
Definition cache_gamma.h:67
std::size_t n
number of items
Definition cache_gamma.h:68