LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
retrieval_fpi_latency.m
1%{ @file retrieval_fpi_latency.m
2 % @brief FPI-based approximation of delayed-hit count and expected latency
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Approximates the mean delayed-hit count d_i and expected latency Z
9 %
10 % @details
11 % Implements the latency-approximation algorithm of the paper (Appendix
12 % "sec:fpi-di", Algorithm "Approximation procedure of the latency Z"), based on
13 % Theorem "thm:di":
14 %
15 % d_i = phi_i * lambda_i * E0[F_i^2] / (2 E0[F_i])
16 %
17 % where F_i is the fetch-period duration of item i and E0[.] is the Palm
18 % expectation conditioned on a miss. The fetch moments are obtained from a
19 % reduced absorbing CTMC of item i's visits to the retrieval stations:
20 %
21 % E0[F_i^k] = k! * pi_e^{(i)} (-D0^{(i)})^{-k} e (eq. moments)
22 %
23 % Steps (per the algorithm):
24 % 1. FPI on the full system -> phi_i, pi_{i,0} (retrieval_fpi)
25 % 2. For each i: FPI without item i -> phi^{(i)}_{s,k};
26 % PS-station occupancy phi~_s^{(i)} = sum_{k!=i} phi^{(i)}_{s,k}
27 % 3. Reduced CTMC for item i: one block of PH phases per station; shared
28 % stations (PS/SIRO/FCFS/LCFSPR) have their rates slowed by 1/(1+phi~_s^{(i)})
29 % while IS stations stay independent; routing per R; absorption = return to
30 % cache. D0^{(i)} = transient subgenerator, pi_e^{(i)} = entry distribution
31 % (R(outside->s) * PH-initial).
32 % 4. Moments via eq. moments -> d_i via Theorem thm:di.
33 % 5. Z = sum_i(phi_i + d_i) / sum_i lambda_i(phi_i + pi_{i,0}) (eq. latency tot)
34 %
35 % Routing convention: R is (S+1) x (S+1) x n; index 1 = outside (entry on miss /
36 % return to cache on completion), indices 2..S+1 = retrieval stations 1..S;
37 % R(a,b,i) is the routing probability from a to b for item i (rows sum to 1).
38 %
39 % @par Syntax:
40 % @code
41 % [Z, d, phi, pi0] = retrieval_fpi_latency(m, lambda, gamma, alpha, T, R, station_type)
42 % @endcode
43 %
44 % @par Parameters:
45 % <table>
46 % <tr><th>Name<th>Description
47 % <tr><td>m<td>Cache list capacities (1 x h)
48 % <tr><td>lambda<td>Per-item arrival rates (1 x n)
49 % <tr><td>gamma<td>Access factors gamma(i,j)=gamma_{i,j}, (n x h)
50 % <tr><td>alpha<td>Cell(1,S); alpha{s}(1,:,i) PH entry vector of item i at station s
51 % <tr><td>T<td>Cell(1,S); T{s}(:,:,i) PH subgenerator of item i at station s
52 % <tr><td>R<td>(S+1) x (S+1) x n routing matrices (index 1 = outside, 2..S+1 = stations)
53 % <tr><td>station_type<td>(1 x S) string/cellstr per station, one of "IS", "PS", "SIRO", "FCFS", "LCFSPR". PS and LCFSPR (symmetric/insensitive BCMP disciplines) admit general phase-type service and class-dependent rates. SIRO and FCFS require exponential (single-phase) service with identical per-class rates; otherwise an error is raised.
54 % </table>
55 %
56 % @par Returns:
57 % <table>
58 % <tr><th>Name<th>Description
59 % <tr><td>Z<td>Expected latency of the delayed-hit system
60 % <tr><td>d<td>Mean number of delayed hits awaiting fetch, per item (1 x n)
61 % <tr><td>phi<td>Delayed-hit ratio phi_i per item (1 x n)
62 % <tr><td>pi0<td>Miss ratio pi_{i,0} per item (1 x n)
63 % </table>
64%}
65function [Z,d,phi,pi0]=retrieval_fpi_latency(m,lambda,gamma,alpha,T,R,station_type)
66n = numel(lambda);
67m = m(:).';
68S = numel(alpha);
69fsz = zeros(1,S);
70for s = 1:S
71 fsz(s) = size(T{s},1);
72end
73station_type = string(station_type);
74% see _kb/09-ldes-and-cache.md (delayed-hit latency: station-type equivalences)
75unsupported = ~((station_type == "IS") | (station_type == "PS") | (station_type == "SIRO") | (station_type == "FCFS") | (station_type == "LCFSPR"));
76if any(unsupported)
77 bad = strjoin(cellstr(unique(station_type(unsupported))), ', ');
78 line_error(mfilename, sprintf(['retrieval_fpi_latency supports only IS, PS, SIRO, FCFS and ' ...
79 'LCFSPR retrieval stations; unsupported station type(s): %s'], bad));
80end
81for s = find(station_type == "SIRO" | station_type == "FCFS")
82 if fsz(s) > 1
83 line_error(mfilename, sprintf(['retrieval_fpi_latency supports SIRO/FCFS retrieval stations ' ...
84 'only with exponential (single-phase) service; station %d has phase-type service.'], s));
85 end
86end
87% SIRO and FCFS reduce to the PS single-exponential sojorn only with
88% class-independent service rates: every item must share the same mean service
89% time at such a station. (LCFSPR is exempt -- it is insensitive.)
90for s = find(station_type == "FCFS" | station_type == "SIRO")
91 taus = zeros(1, n);
92 for i = 1:n
93 taus(i) = -alpha{s}(:,:,i) / T{s}(:,:,i) * ones(fsz(s), 1);
94 end
95 if max(taus) - min(taus) > 1e-9 * max(taus)
96 line_error(mfilename, sprintf(['retrieval_fpi_latency requires class-independent (identical) ' ...
97 'mean service rates at SIRO/FCFS station %d.'], s));
98 end
99end
100isIdx = find(station_type == "IS");
101psIdx = find(station_type == "PS" | station_type == "SIRO" | station_type == "FCFS" | station_type == "LCFSPR");
102r = numel(psIdx);
103
104% --- per-item fetching demands eta_{s,i} = visits_{s,i} * mean PH service time ---
105% eta_fpi(i, 1) = sum over IS stations; eta_fpi(i, 1+p) = PS station psIdx(p)
106eta_fpi = zeros(n, r+1);
107for i = 1:n
108 Ri = R(:,:,i);
109 a = Ri(1, 2:S+1); % outside -> station entry probs (1 x S)
110 P = Ri(2:S+1, 2:S+1); % station -> station
111 visits = a / (eye(S) - P); % expected visits per fetch (1 x S)
112 tau = zeros(1,S);
113 for s = 1:S
114 al = alpha{s}(:,:,i);
115 Tm = T{s}(:,:,i);
116 tau(s) = -al / Tm * ones(fsz(s),1); % mean PH service time
117 end
118 eta_s = visits .* tau; % eta_{s,i} per station
119 eta_fpi(i,1) = sum(eta_s(isIdx));
120 for p = 1:r
121 eta_fpi(i,1+p) = eta_s(psIdx(p));
122 end
123end
124
125% --- step 1: FPI on the full system ---
126[pi0, ~, pdh] = retrieval_fpi(m, lambda, eta_fpi, gamma);
127phi = sum(pdh, 1); % phi_i = sum_s phi_{s,i}
128
129d = zeros(1,n);
130for i = 1:n
131 % --- step 2: FPI without item i -> PS-station occupancy phi~_s^{(i)} ---
132 keep = [1:i-1, i+1:n];
133 [~, ~, pdh_i] = retrieval_fpi(m, lambda(keep), eta_fpi(keep,:), gamma(keep,:));
134 phitilde = zeros(1,S); % occupancy per actual station
135 for p = 1:r
136 phitilde(psIdx(p)) = sum(pdh_i(1+p, :));
137 end
138
139 % --- step 3: reduced absorbing CTMC for item i's fetch ---
140 Phi = sum(fsz);
141 off = [0, cumsum(fsz(1:end-1))];
142 D0 = zeros(Phi, Phi);
143 pe = zeros(1, Phi);
144 Ri = R(:,:,i);
145 for s = 1:S
146 rows = off(s)+(1:fsz(s));
147 if station_type(s) == "PS" || station_type(s) == "SIRO" || station_type(s) == "FCFS" || station_type(s) == "LCFSPR"
148 scale = 1/(1 + phitilde(s)); % PS/SIRO/FCFS/LCFSPR mean-field sharing slowdown
149 else
150 scale = 1; % IS: independent
151 end
152 blk = scale * T{s}(:,:,i);
153 D0(rows, rows) = D0(rows, rows) + blk;
154 compl = -blk * ones(fsz(s),1); % completion-rate vector at station s
155 for sp = 1:S
156 cols = off(sp)+(1:fsz(sp));
157 D0(rows, cols) = D0(rows, cols) + compl * Ri(s+1, sp+1) * alpha{sp}(:,:,i);
158 end
159 pe(rows) = Ri(1, s+1) * alpha{s}(:,:,i); % entry distribution
160 end
161
162 % --- step 4: moments E0[F_i], E0[F_i^2] (eq. moments) and d_i (thm:di) ---
163 e = ones(Phi,1);
164 A = -D0;
165 M1 = pe * (A \ e);
166 M2 = 2 * pe * (A \ (A \ e));
167 d(i) = phi(i) * lambda(i) * M2 / (2*M1);
168end
169
170% --- step 5: expected latency Z (eq. latency tot) ---
171Z = sum(phi + d) / sum(lambda(:).' .* (phi + pi0));
172end