LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
cache_prob_spm.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_PROB_SPM_H
6#define LINE_API_CACHE_PROB_SPM_H
7
8/**
9 * @file
10 * @ingroup api_cache
11 * Saddle-point approximation of the per-item cache hit probabilities.
12 *
13 * Templated port of matlab/src/api/cache/cache_prob_spm.m, cross-checked
14 * against jar/src/main/java/jline/api/cache/Cache_prob_rayint.java (the JAR's
15 * only implementation of this shape; see the note below).
16 *
17 * Same ratio-of-constants identity as cache_prob_erec,
18 *
19 * prob(i,1+j) = m(j) gamma(i,j) exp(lE_i - lE),
20 *
21 * with lE the log normalizing constant from cache_spm and lE_i the same
22 * quantity for the model with item i deleted and list j one slot smaller. The
23 * miss probability closes the row. Because both constants are approximations
24 * the rows only sum to one up to the saddle-point error, which is why the
25 * reference wraps the miss entry in abs().
26 *
27 * ARITHMETIC: transcendental, inherited from cache_spm.
28 */
29
30#include <cmath>
31#include <cstddef>
32#include <vector>
33
36#include "line/num/number.h"
37#include "line/util/error.h"
38#include "line/util/matrix.h"
39
40namespace line {
41namespace cache {
42
43/**
44 * @brief Saddle-point approximation of the per-item cache hit probabilities.
45 *
46 * @param gamma (n x h) access factors
47 * @param m (h) list capacities
48 * @return (n x (h+1)); column 0 miss, column 1+j hit in list j
49 */
50template <class T>
51Matrix<T> cache_prob_spm(const Matrix<T>& gamma, const std::vector<int>& m) {
53 "cache_prob_spm requires transcendental arithmetic");
54 using std::exp;
55 const std::size_t n = gamma.rows();
56 const std::size_t h = gamma.cols();
57 if (h != m.size()) throw InputError("cache_prob_spm: gamma and m disagree on the list count");
58
59 const T lE = cache_spm(gamma, m).lZ;
60 const T zero = num_traits<T>::from_int(0);
61 const T one = num_traits<T>::from_int(1);
62
63 Matrix<T> prob(n, h + 1, zero);
64 for (std::size_t i = 0; i < n; ++i) {
65 T hits = zero;
66 for (std::size_t j = 0; j < h; ++j) {
67 std::vector<int> mj = m;
68 mj[j] -= 1;
69 const T lEi = cache_spm(detail::gamma_without_row(gamma, i), mj).lZ;
70 const T p =
71 num_traits<T>::from_int(static_cast<long>(m[j])) * gamma(i, j) * exp(T(lEi - lE));
72 prob(i, j + 1) = p;
73 hits += p;
74 }
75 prob(i, 0) = num_abs(T(one - hits));
76 }
77 return prob;
78}
79
80} // namespace cache
81} // namespace line
82
83#endif // LINE_API_CACHE_PROB_SPM_H
Exact recursive normalizing constant of a multi-list cache model.
Saddle-point approximation of the cache normalizing constant.
InputError(const std::string &what)
Definition error.h:39
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
The exception types the port throws.
Dense matrix and non-owning view.
CacheSpmResult< T > cache_spm(const Matrix< T > &gamma_in, const std::vector< int > &m)
Saddle-point approximation of the cache normalizing constant.
Definition cache_spm.h:152
Matrix< T > cache_prob_spm(const Matrix< T > &gamma, const std::vector< int > &m)
Saddle-point approximation of the per-item cache hit probabilities.
T num_abs(const T &v)
Definition number.h:172
Number-type abstraction for the templated API port.