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 (per-level) hit probabilities are reported only from the exact
59% algorithm, where the miss column and the per-list columns of pij share one
60% consistent solution (so the per-list rows sum to the aggregate hit). The
61% approximate algorithms derive miss and per-list from different expansions, so
62% their per-list breakdown is left undefined (NaN).
63pijlist = []; % genuine per-list occupancy (n x h), set in the exact branch
64switch options.method
65 case 'exact'
66 % cache_prob_erec is exact only for the exchangeable (product-form)
67 % family (RR/FIFO/RANDOM-type); recency-based policies (LRU, h-LRU,
68 % q-LRU, CLIMB) have no product form and would silently return the
69 % exchangeable solution instead.
70 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
71 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
72 % supported
73 otherwise
74 line_error(mfilename,'NC does not support exact solution of the specified cache replacement policy; use the default (approximate) method or SolverCTMC.');
75 end
76 line_debug('Using exact method, calling cache_prob_erec');
77 [pij] = cache_prob_erec(gamma, m);
78 missRate = zeros(1,u);
79 for v=1:u
80 missRate(v) = lambda(v,:,1)*pij(:,1);
81 end
82 pijlist = pij(:, 2:end);
83 method='exact';
84 case 'sampling'
85 line_debug('Using sampling method, calling cache_miss_is');
86 [~,missRate,~,~,lE] = cache_miss_is(gamma, m, lambda, options.samples);
87 pij = cache_prob_is(gamma, m, options.samples);
88 method='sampling';
89 otherwise
90 line_debug('Default method: using SPM approximation method\n');
91 line_debug('Using SPM approximation method, calling cache_miss_spm');
92 [~,missRate,~,~,lE] = cache_miss_spm(gamma, m, lambda);
93 pij = cache_prob_spm(gamma, m, lE);
94 method='spm';
95end
96
97for r = 1:sn.nclasses
98 if length(ch.hitclass)>=r && ch.missclass(r)>0 && ch.hitclass(r)>0
99 XN(ch.missclass(r)) = XN(ch.missclass(r)) + missRate(r);
100 XN(ch.hitclass(r)) = XN(ch.hitclass(r)) + (sourceRate(r) - missRate(r));
101 end
102end
103
104% per-list (per-level) hit probabilities (access-weighted), only where the
105% exact algorithm produced a genuine per-list occupancy matrix.
106hitproblist = NaN(u, h);
107if ~isempty(pijlist)
108 for v=1:u
109 if any(~isnan(ch.pread{v}))
110 pread = ch.pread{v}(:).';
111 for l=1:h
112 hitproblist(v,l) = pread * pijlist(:,l);
113 end
114 end
115 end
116end
117% per-item occupancy [nitems x (lists+1)] (col 1 = miss, cols 2..end per-list).
118% Only the exact recursion (cache_prob_erec) yields a genuine per-list
119% distribution: the spm/is algorithms approximate each per-list column
120% independently and do not form a proper distribution for h>1. Since the cache
121% is product-form, derive the per-item table from the exact algorithm in the
122% approximate branches too (the approximate pij is still used above for the
123% aggregate miss rate). The exact recursion is only tractable for small item
124% sets, so it is skipped (NaN, with a warning) for caches with more than 10
125% items.
126if n > 10
127 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);
128 itemprob = NaN(n, h+1);
129elseif ~isempty(pijlist)
130 itemprob = pij; % exact branch: already [n x (h+1)] with col 1 = miss
131else
132 itemprob = cache_prob_erec(gamma, m); % [n x (h+1)] with col 1 = miss
133end
134runtime=toc(T0);
135end
Definition Station.m:245