LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_ctmc_analyzer.m
1function [QN,UN,RN,TN,CN,XN,InfGen,StateSpace,StateSpaceAggr,EventFiltration,runtime,fname,sncopy] = solver_ctmc_analyzer(sn, options)
2% [QN,UN,RN,TN,CN,XN,INFGEN,STATESPACE,STATESPACEAGGR,EVENTFILTRATION,RUNTIME,FNAME,sn] = SOLVER_CTMC_ANALYZER(sn, OPTIONS)
3%
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7%if options.remote
8% sn.rtfun = {};
9% sn.lst = {};
10% qn_json = jsonencode(sn);
11% sn = NetworkStruct.fromJSON(qn_json)
12%return
13%end
14
15M = sn.nstations; %number of stations
16K = sn.nclasses; %number of classes
17S = sn.nservers;
18NK = sn.njobs'; % initial population per class
19sched = sn.sched;
20
21Tstart = tic;
22PH = sn.proc;
23
24line_debug('CTMC analyzer starting: nstations=%d, nclasses=%d, njobs=%s', M, K, mat2str(NK));
25
26% Note: hide_immediate now selectively preserves Cache immediate transitions
27% in solver_ctmc.m, so we no longer need to disable it entirely for Cache nodes
28
29line_debug('Building state space and infinitesimal generator via solver_ctmc');
30[InfGen,StateSpace,StateSpaceAggr,EventFiltration,arvRates,depRates,sn] = solver_ctmc(sn, options); % sn is updated with the state space
31
32% if the initial state does not reflect the final size of the state
33% vectors, attempt to correct it
34for isf=1:sn.nstateful
35 if size(sn.state{isf},2) < size(sn.space{isf},2)
36 sn.state{isf} = [zeros(1,size(sn.space{isf},2)-size(sn.state{isf},2)),sn.state{isf}];
37 end
38end
39sncopy = sn;
40
41if options.keep
42 line_debug('Saving CTMC data to file (options.keep=true)');
43 fname = lineTempName;
44 save([fname,'.mat'],'InfGen','StateSpace','StateSpaceAggr','EventFiltration')
45 line_printf('CTMC infinitesimal generator and state space saved in: ');
46 line_printf(strrep(sprintf('%s.mat\n',fname),'\','\\'))
47else
48 fname = '';
49end
50
51wset = 1:length(InfGen);
52
53line_debug('State space built: %d states, solving CTMC', length(InfGen));
54
55use_ctmc_solve_stable = true;
56if use_ctmc_solve_stable
57 % stable version
58 % Note: solver_ctmc now selectively preserves Cache immediate transitions
59 % to enable hit/miss rate computation while hiding other immediate transitions
60 [probSysState, ~, nConnComp, connComp] = ctmc_solve(InfGen, options);
61
62 if nConnComp > 1
63 line_debug('CTMC is reducible: %d connected components', nConnComp);
64 % the matrix was reducible
65 initState = matchrow(StateSpace, cell2mat(sn.state'));
66 if initState <= 0
67 % Initial state may have been removed by stochcomp (e.g., SPN with
68 % immediate ENABLE states). Use the largest connected component.
69 compSizes = accumarray(connComp(:), 1);
70 [~, largestComp] = max(compSizes);
71 wset = find(connComp == largestComp);
72 else
73 % determine the weakly connected component associated to the initial state
74 wset = find(connComp == connComp(initState));
75 end
76 if initState > 0
77 line_debug('Using component %d with %d states (from initial state)', connComp(initState), length(wset));
78 else
79 line_debug('Using largest component with %d states (initial state removed by stochcomp)', length(wset));
80 end
81 probSysState = ctmc_solve(InfGen(wset, wset), options);
82 InfGen = InfGen(wset, wset);
83 % reduce all per-state arrays to the retained component and remap wset
84 % to local indices so matrix-form and loop-form estimators stay aligned
85 StateSpace = StateSpace(wset,:);
86 StateSpaceAggr = StateSpaceAggr(wset,:);
87 arvRates = arvRates(wset,:,:);
88 depRates = depRates(wset,:,:);
89 wset = 1:numel(wset);
90 else
91 line_debug('CTMC is irreducible, using full state space');
92 end
93else
94 % development version
95
96 % we now find the initial state and then solver the CTMC allowing for the
97 % case where it is reducible
98 initState = matchrow(StateSpace, cell2mat(sn.state'));
99 pi0 = zeros(1,length(InfGen)); pi0(initState) = 1.0;
100 [pi,pis,~,scc,~] = ctmc_solve_reducible(InfGen, pi0, options);
101
102 if size(pis,1)==1
103 probSysState = pi;
104 else
105 wset = scc == scc(initState);
106 InfGen = InfGen(wset, wset);
107 StateSpace = StateSpace(wset,:);
108 probSysState = pis(scc(initState),scc == scc(initState));
109 end
110end
111probSysState(probSysState<GlobalConstants.Zero)=0;
112probSysState = probSysState/sum(probSysState);
113
114XN = NaN*zeros(1,K);
115UN = NaN*zeros(M,K);
116QN = NaN*zeros(M,K);
117RN = NaN*zeros(M,K);
118TN = NaN*zeros(M,K);
119CN = NaN*zeros(1,K);
120
121istSpaceShift = zeros(1,M);
122for ist=1:M
123 if ist==1
124 istSpaceShift(ist) = 0;
125 else
126 istSpaceShift(ist) = istSpaceShift(ist-1) + size(sn.space{ist-1},2);
127 end
128end
129
130for k=1:K
131 refsf = sn.stationToStateful(sn.refstat(k));
132 XN(k) = probSysState*arvRates(wset,refsf,k);
133end
134
135for ist=1:M
136 isf = sn.stationToStateful(ist);
137 ind = sn.stationToNode(ist);
138 for k=1:K
139 TN(ist,k) = probSysState*depRates(wset,isf,k);
140 QN(ist,k) = probSysState*StateSpaceAggr(wset,(ist-1)*K+k);
141 end
142 if sn.nodetype(ind) ~= NodeType.Source
143 switch sched(ist)
144 case SchedStrategy.INF
145 for k=1:K
146 UN(ist,k) = QN(ist,k);
147 end
148 case {SchedStrategy.PS, SchedStrategy.DPS, SchedStrategy.GPS, SchedStrategy.LPS}
149 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
150 for k=1:K
151 if ~isempty(PH{ist}{k})
152 % There are cases where due to remove of
153 % immediate transitions or due to cutoff the
154 % utilization estimator based on arrivals or
155 % departures can be under-estimated. E.g.:
156 % UNarv_ik doesn't work well with example_stateDependentRouting_3
157 % UNdep_ik doesn't work well with test_OQN_JMT_6
158 % Therefore, we take the maximum of the two
159 % Note: the two estimators are normally
160 % identical.
161 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
162 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); % this is valid because CS in LINE is in a separate node
163 UN(ist,k) = max(UNarv_ik,UNdep_ik);
164 end
165 end
166 else % lld/cd/ljd cases
167 ind = sn.stationToNode(ist);
168 UN(ist,1:K) = 0;
169 for st = wset
170 [ni,nir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
171 if ni>0
172 for k=1:K
173 UN(ist,k) = UN(ist,k) + probSysState(st)*nir(k)*sn.schedparam(ist,k)/(nir*sn.schedparam(ist,:)');
174 end
175 end
176 end
177 end
178 case SchedStrategy.PAS
179 % Pass-and-swap (order-independent): utilization is the
180 % time-average number of in-service jobs per class divided by
181 % the number of servers. sir already counts the positions whose
182 % marginal rate increment is positive (toMarginal PAS branch),
183 % so a single job served by multiple server types still counts
184 % as one in-service job (not 1/rate).
185 ind = sn.stationToNode(ist);
186 UN(ist,1:K) = 0;
187 for st = wset
188 [~,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
189 for k=1:K
190 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
191 end
192 end
193 otherwise
194 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
195 for k=1:K
196 if ~isempty(PH{ist}{k})
197 % There are cases where due to remove of
198 % immediate transitions or due to cutoff the
199 % utilization estimator based on arrivals or
200 % departures can be under-estimated. E.g.:
201 % UNarv_ik doesn't work well with example_stateDependentRouting_3
202 % UNdep_ik doesn't work well with test_OQN_JMT_6
203 % Therefore, we take the maximum of the two
204 % Note: the two estimators are normally
205 % identical.
206 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
207 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); % this is valid because CS in LINE is in a separate node
208 UN(ist,k) = max(UNarv_ik,UNdep_ik);
209 end
210 end
211 else % lld/cd/ljd cases
212 ind = sn.stationToNode(ist);
213 UN(ist,1:K) = 0;
214 for st = wset
215 [ni,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
216 if ni>0
217 for k=1:K
218 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
219 end
220 end
221 end
222 end
223 end
224 end
225end
226
227for k=1:K
228 for ist=1:M
229 if TN(ist,k)>0
230 RN(ist,k) = QN(ist,k)./TN(ist,k);
231 else
232 RN(ist,k)=0;
233 end
234 end
235 CN(k) = NK(k)./XN(k);
236end
237
238QN(isnan(QN))=0;
239CN(isnan(CN))=0;
240RN(isnan(RN))=0;
241UN(isnan(UN))=0;
242XN(isnan(XN))=0;
243TN(isnan(TN))=0;
244
245runtime = toc(Tstart);
246
247% now update the routing probabilities in nodes with state-dependent routing
248TNcache = zeros(sn.nstateful,K);
249XNcache = zeros(sn.nstateful,K);
250for k=1:K
251 for isf=1:sn.nstateful
252 ind = sncopy.statefulToNode(isf);
253 if sncopy.nodetype(ind) == NodeType.Cache
254 TNcache(isf,k) = probSysState*depRates(wset,isf,k);
255 XNcache(isf,k) = probSysState*arvRates(wset,isf,k);
256 end
257 end
258end
259
260% updates cache actual hit and miss data + retrieval-system expected latency.
261% Hit / miss for class k are derived from departure rates of the configured
262% hitClass / missClass at the cache; for retrieval-aware caches the
263% miss-class departure rate equals the true miss rate because
264% afterEventCache fires the miss event on a retrieval-complete READ.
265retrievalLatencyWarned = false;
266for k=1:K
267 for isf=1:sncopy.nstateful
268 ind = sncopy.statefulToNode(isf);
269 if sncopy.nodetype(ind) == NodeType.Cache
270 np = sncopy.nodeparam{ind};
271 if length(np.hitclass)>=k
272 h = np.hitclass(k);
273 m = np.missclass(k);
274 if h>0 && m>0
275 sncopy.nodeparam{ind}.actualhitprob(k) = TNcache(isf,h)/sum(TNcache(isf,[h,m]));
276 sncopy.nodeparam{ind}.actualmissprob(k) = TNcache(isf,m)/sum(TNcache(isf,[h,m]));
277 else
278 sncopy.nodeparam{ind}.actualhitprob(k) = NaN;
279 sncopy.nodeparam{ind}.actualmissprob(k) = NaN;
280 end
281
282 % The Eq. 8 retrieval-system expected latency is not currently
283 % implemented; report NaN whenever a retrieval system is
284 % configured for this class.
285 expectedLatency = NaN;
286 if isfield(np, 'retrievalSystemQueueIndices') ...
287 && isKey(np.retrievalSystemQueueIndices, int32(k-1)) ...
288 && ~isempty(np.retrievalSystemQueueIndices(int32(k-1)))
289 if ~retrievalLatencyWarned
290 line_warning(mfilename, 'Retrieval-system expected latency is not currently implemented; reporting NaN.');
291 retrievalLatencyWarned = true;
292 end
293 end
294 sncopy.nodeparam{ind}.actualresidt(k) = expectedLatency;
295 end
296 end
297 end
298end
299end
Definition mmt.m:124