LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_nc_cache_analyzer.m
1function [QN,UN,RN,TN,CN,XN,lG,pij,runtime,method,hitproblist,itemprob] = solver_nc_cache_analyzer(sn, options)
2% [Q,U,R,T,C,X,LG,PIJ,RUNTIME,METHOD,HITPROBLIST] = SOLVER_NC_CACHE_ANALYZER(QN, OPTIONS)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7T0=tic;
8QN = []; UN = [];
9RN = []; TN = [];
10CN = [];
11XN = zeros(1,sn.nclasses);
12lG = NaN;
13iter = NaN;
14
15line_debug('NC cache analyzer starting: method=%s, nclasses=%d', options.method, sn.nclasses);
16
17source_ist = sn.nodeToStation(sn.nodetype == NodeType.Source);
18sourceRate = sn.rates(source_ist,:);
19sourceRate(isnan(sourceRate)) = 0;
20TN(source_ist,:) = sourceRate;
21
22ch = sn.nodeparam{sn.nodetype == NodeType.Cache};
23
24m = ch.itemcap;
25n = ch.nitems;
26
27if n<m+2
28 line_error(mfilename,'NC requires the number of items to exceed the cache capacity at least by 2.');
29end
30
31h = length(m);
32u = sn.nclasses;
33lambda = zeros(u,n,h);
34
35for v=1:u
36 for k=1:n
37 for l=1:(h+1)
38 if ~isnan(ch.pread{v})
39 lambda(v,k,l) = sourceRate(v) * ch.pread{v}(k);
40 end
41 end
42 end
43end
44
45R = ch.accost;
46if isempty(R)
47 % Default linear cache routing: items flow from list l to list l+1
48 R = cell(u, n);
49 for v = 1:u
50 for k = 1:n
51 Rmat = diag(ones(1, h), 1);
52 Rmat(h+1, h+1) = 1;
53 R{v, k} = Rmat;
54 end
55 end
56end
57gamma = cache_gamma_lp(lambda,R);
58% per-list hit probabilities are genuine only on the exact branch; see
59% _kb/09-ldes-and-cache.md on cache-analyzer per-list/per-item reporting
60pijlist = []; % genuine per-list occupancy (n x h), set in the exact branch
61switch options.method
62 case 'exact'
63 % cache_prob_erec is exact only for the exchangeable (RR/FIFO/RANDOM)
64 % family; see _kb/09-ldes-and-cache.md on cache-analyzer reporting
65 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
66 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
67 % supported
68 otherwise
69 line_error(mfilename,'NC does not support exact solution of the specified cache replacement policy; use the default (approximate) method or SolverCTMC.');
70 end
71 line_debug('Using exact method, calling cache_prob_erec');
72 [pij] = cache_prob_erec(gamma, m);
73 missRate = zeros(1,u);
74 for v=1:u
75 missRate(v) = lambda(v,:,1)*pij(:,1);
76 end
77 pijlist = pij(:, 2:end);
78 method='exact';
79 case 'sampling'
80 line_debug('Using sampling method, calling cache_miss_is');
81 [~,missRate,~,~,lE] = cache_miss_is(gamma, m, lambda, options.samples);
82 pij = cache_prob_is(gamma, m, options.samples);
83 method='sampling';
84 otherwise
85 line_debug('Default method: using SPM approximation method\n');
86 line_debug('Using SPM approximation method, calling cache_miss_spm');
87 [~,missRate,~,~,lE] = cache_miss_spm(gamma, m, lambda);
88 pij = cache_prob_spm(gamma, m, lE);
89 method='spm';
90end
91
92for r = 1:sn.nclasses
93 if length(ch.hitclass)>=r && ch.missclass(r)>0 && ch.hitclass(r)>0
94 XN(ch.missclass(r)) = XN(ch.missclass(r)) + missRate(r);
95 XN(ch.hitclass(r)) = XN(ch.hitclass(r)) + (sourceRate(r) - missRate(r));
96 end
97end
98
99% per-list (per-level) hit probabilities (access-weighted), only where the
100% exact algorithm produced a genuine per-list occupancy matrix.
101hitproblist = NaN(u, h);
102if ~isempty(pijlist)
103 for v=1:u
104 if any(~isnan(ch.pread{v}))
105 pread = ch.pread{v}(:).';
106 for l=1:h
107 hitproblist(v,l) = pread * pijlist(:,l);
108 end
109 end
110 end
111end
112% per-item occupancy [nitems x (lists+1)] (col 1 = miss) from the exact
113% cache_prob_erec recursion (skipped, NaN, for >10 items); see
114% _kb/09-ldes-and-cache.md on cache-analyzer per-list/per-item reporting
115if n > 10
116 line_warning(mfilename, 'Per-item cache occupancy (getAvgItemTable) requires the exact algorithm and is skipped for caches with more than 10 items (%d items); reporting NaN.', n);
117 itemprob = NaN(n, h+1);
118elseif ~isempty(pijlist)
119 itemprob = pij; % exact branch: already [n x (h+1)] with col 1 = miss
120else
121 itemprob = cache_prob_erec(gamma, m); % [n x (h+1)] with col 1 = miss
122end
123runtime=toc(T0);
124end