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)
4% Copyright (c) 2012-2026, Imperial College London
10% qn_json = jsonencode(sn);
11% sn = NetworkStruct.fromJSON(qn_json)
15M = sn.nstations; %number of stations
16K = sn.nclasses; %number of classes
18NK = sn.njobs
'; % initial population per class
24line_debug('CTMC analyzer starting: nstations=%d, nclasses=%d, njobs=%s
', M, K, mat2str(NK));
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
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
32% if the initial state does not reflect the final size of the state
33% vectors, attempt to correct it
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}];
42 line_debug('Saving CTMC data to file (options.keep=
true)
');
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),'\
','\\
'))
51wset = 1:length(InfGen);
53line_debug('State space built: %d states, solving CTMC
', length(InfGen));
55use_ctmc_solve_stable = true;
56if use_ctmc_solve_stable
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);
63 line_debug('CTMC
is reducible: %d connected components
', nConnComp);
64 % the matrix was reducible
65 initState = matchrow(StateSpace, cell2mat(sn.state'));
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);
73 % determine the weakly connected component associated to the initial state
74 wset = find(connComp == connComp(initState));
77 line_debug(
'Using component %d with %d states (from initial state)', connComp(initState), length(wset));
79 line_debug(
'Using largest component with %d states (initial state removed by stochcomp)', length(wset));
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,:,:);
91 line_debug(
'CTMC is irreducible, using full state space');
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);
105 wset = scc == scc(initState);
106 InfGen = InfGen(wset, wset);
107 StateSpace = StateSpace(wset,:);
108 probSysState = pis(scc(initState),scc == scc(initState));
111probSysState(probSysState<GlobalConstants.Zero)=0;
112probSysState = probSysState/sum(probSysState);
121istSpaceShift = zeros(1,M);
124 istSpaceShift(ist) = 0;
126 istSpaceShift(ist) = istSpaceShift(ist-1) + size(sn.space{ist-1},2);
131 refsf = sn.stationToStateful(sn.refstat(k));
132 XN(k) = probSysState*arvRates(wset,refsf,k);
135% see _kb/06-solver-catalog.md (G-network signals) for rationale
136inDropRegion = false(1,M);
137if isfield(sn,'nregions
') && sn.nregions > 0
139 if sn.regionrule(f) == DropStrategy.DROP
140 memb = any(sn.region{f} ~= -1, 2); % stations constrained by region f
142 inDropRegion(1:min(M,numel(memb))) = inDropRegion(1:min(M,numel(memb))) | memb(1:min(M,numel(memb)));
148 isf = sn.stationToStateful(ist);
149 ind = sn.stationToNode(ist);
151 TN(ist,k) = probSysState*depRates(wset,isf,k);
152 QN(ist,k) = probSysState*StateSpaceAggr(wset,(ist-1)*K+k);
154 if sn.nodetype(ind) ~= NodeType.Source
155 % see _kb/06-solver-catalog.md (G-network signals)
for rationale
156 canDropClass = isinf(sn.njobs(:)
') & (isfinite(sn.cap(ist)) | isfinite(sn.classcap(ist,:)) | inDropRegion(ist));
157 signalLoss = ctmc_signal_lossy(sn, arvRates, probSysState, wset, isf);
158 canDropClass = canDropClass | signalLoss;
160 case SchedStrategy.INF
162 UN(ist,k) = QN(ist,k);
164 case {SchedStrategy.PS, SchedStrategy.DPS, SchedStrategy.GPS, SchedStrategy.LPS}
165 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && isempty(sn.jdscaling)
167 if ~isempty(PH{ist}{k})
168 % see _kb/06-solver-catalog.md (Utilization conventions) for rationale
169 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); % this is valid because CS in LINE is in a separate node
171 UN(ist,k) = UNdep_ik;
173 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
174 UN(ist,k) = max(UNarv_ik,UNdep_ik);
178 else % lld/cd/ljd cases
179 % see _kb/06-solver-catalog.md (Utilization conventions) for rationale
180 ind = sn.stationToNode(ist);
182 if ~isempty(sn.lldscaling) && ist <= size(sn.lldscaling,1)
183 ceff = max(ceff, max(sn.lldscaling(ist,:)));
187 [ni,nir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
190 if ~isempty(sn.lldscaling) && ist <= size(sn.lldscaling,1)
191 lldnow = sn.lldscaling(ist, min(max(sum(ni),1), size(sn.lldscaling,2)));
194 UN(ist,k) = UN(ist,k) + probSysState(st)*nir(k)*sn.schedparam(ist,k)/(nir*sn.schedparam(ist,:)')*lldnow/ceff;
199 case SchedStrategy.PAS
200 % see _kb/06-solver-catalog.md (Utilization conventions)
for rationale
201 ind = sn.stationToNode(ist);
204 [~,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
206 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
210 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && isempty(sn.jdscaling)
212 if ~isempty(PH{ist}{k})
213 % see _kb/06-solver-catalog.md (Utilization conventions)
for rationale
214 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); %
this is valid because CS in LINE
is in a separate node
216 UN(ist,k) = UNdep_ik;
218 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
219 UN(ist,k) = max(UNarv_ik,UNdep_ik);
223 else % lld/cd/ljd cases
224 % see _kb/06-solver-catalog.md (Utilization conventions)
for rationale
225 ind = sn.stationToNode(ist);
227 if ~isempty(sn.lldscaling) && ist <= size(sn.lldscaling,1)
228 ceff = max(ceff, max(sn.lldscaling(ist,:)));
232 [ni,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
235 if ~isempty(sn.lldscaling) && ist <= size(sn.lldscaling,1)
236 lldnow = sn.lldscaling(ist, min(max(sum(ni),1), size(sn.lldscaling,2)));
241 UN(ist,k) = UN(ist,k) + probSysState(st)*(sir(k)/sirtot)*lldnow/ceff;
248 % see _kb/06-solver-catalog.md (G-network signals) for rationale
249 if any(signalLoss) && sched(ist) ~= SchedStrategy.INF && ...
250 isempty(sn.lldscaling) && isempty(sn.cdscaling) && isempty(sn.jdscaling)
251 UNb = ctmc_signal_busy(sn, ind, ist, sched(ist), S(ist), StateSpace, istSpaceShift, wset, probSysState);
252 UN(ist,signalLoss) = UNb(signalLoss);
257% see _kb/06-solver-catalog.md (Utilization conventions) for rationale
258if ~isempty(sn.cdscaling) && ~any(isinf(sn.njobs))
260 if length(sn.cdscaling) >= ist && ~isempty(sn.cdscaling{ist})
262 bmax = sn.cdscalingpeak(ist,k);
263 if isfinite(sn.rates(ist,k)) && sn.rates(ist,k) > 0 && bmax > 0
264 UN(ist,k) = TN(ist,k) / sn.rates(ist,k) / bmax;
273% Joint-dependence (non-product-
form) utilization normalization: Util=T*S/peak
274%
using the declared sn.jdscalingpeak, mirroring the
class-dependence block.
275if ~isempty(sn.jdscaling) && ~any(isinf(sn.njobs))
277 if length(sn.jdscaling) >= ist && ~isempty(sn.jdscaling{ist})
279 bmax = sn.jdscalingpeak(ist,k);
280 if isfinite(sn.rates(ist,k)) && sn.rates(ist,k) > 0 && bmax > 0
281 UN(ist,k) = TN(ist,k) / sn.rates(ist,k) / bmax;
291% see _kb/06-solver-catalog.md (True BAS blocking)
for rationale
292if ~isempty(sn.isbasblocking)
294 ind = sn.stationToNode(ist);
295 % see _kb/06-solver-catalog.md (True BAS blocking) for rationale
296 if numel(sn.isbasblocking) < ind || sn.isbasblocking(ind) ~= 1
297 continue % no blocked marker at this station
299 % sn.isstation spans nnodes physical
nodes plus one virtual entry per
300 % finite capacity region; restrict to physical
nodes to conform to connmatrix
301 isstationNode = sn.isstation(1:sn.nnodes);
302 dests = find(sn.connmatrix(ind,:) == 1 & isstationNode(:)' == 1);
304 continue % ambiguous destination: leave the job where it sits
306 jst = sn.nodeToStation(dests(1));
307 if isnan(jst) || jst < 1
310 cols = (istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2));
311 blocked = StateSpace(wset, cols(end)) == 1; % marker
is the trailing
column
313 % Only the held job itself moves, not the whole queue at ist: a blocked
314 % state holds exactly one completed job, so cap the per-state count at 1.
315 shift = probSysState(blocked) * min(StateSpaceAggr(wset(blocked),(ist-1)*K+k), 1);
317 QN(ist,k) = QN(ist,k) - shift;
318 QN(jst,k) = QN(jst,k) + shift;
327 RN(ist,k) = QN(ist,k)./TN(ist,k);
332 CN(k) = NK(k)./XN(k);
342runtime = toc(Tstart);
344% now update the routing probabilities in
nodes with state-dependent routing
345TNcache = zeros(sn.nstateful,K);
346XNcache = zeros(sn.nstateful,K);
348 for isf=1:sn.nstateful
349 ind = sncopy.statefulToNode(isf);
350 if sncopy.nodetype(ind) == NodeType.Cache
351 TNcache(isf,k) = probSysState*depRates(wset,isf,k);
352 XNcache(isf,k) = probSysState*arvRates(wset,isf,k);
357% updates cache actual hit and miss data + retrieval-system expected latency.
358% Hit / miss
for class k are derived from departure rates of the configured
359% hitClass / missClass at the cache;
for retrieval-aware caches the
360% miss-
class departure rate equals the true miss rate because
361% afterEventCache fires the miss
event on a retrieval-complete READ.
362retrievalLatencyWarned =
false;
364 for isf=1:sncopy.nstateful
365 ind = sncopy.statefulToNode(isf);
366 if sncopy.nodetype(ind) == NodeType.Cache
367 np = sncopy.nodeparam{ind};
368 if length(np.hitclass)>=k
372 sncopy.nodeparam{ind}.actualhitprob(k) = TNcache(isf,h)/sum(TNcache(isf,[h,m]));
373 sncopy.nodeparam{ind}.actualmissprob(k) = TNcache(isf,m)/sum(TNcache(isf,[h,m]));
375 sncopy.nodeparam{ind}.actualhitprob(k) = NaN;
376 sncopy.nodeparam{ind}.actualmissprob(k) = NaN;
379 % The Eq. 8 retrieval-system expected latency
is not currently
380 % implemented; report NaN whenever a retrieval system
is
381 % configured
for this class.
382 expectedLatency = NaN;
383 if isfield(np,
'retrievalSystemQueueIndices') ...
384 && isKey(np.retrievalSystemQueueIndices, int32(k-1)) ...
385 && ~isempty(np.retrievalSystemQueueIndices(int32(k-1)))
386 if ~retrievalLatencyWarned
387 line_warning(mfilename, 'Retrieval-system expected latency
is not currently implemented; reporting NaN.');
388 retrievalLatencyWarned = true;
391 sncopy.nodeparam{ind}.actualresidt(k) = expectedLatency;