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 StateSpace = StateSpace(wset,:);
85 line_debug(
'CTMC is irreducible, using full state space');
90 % we now find the initial state and then solver the CTMC allowing
for the
91 %
case where it
is reducible
92 initState = matchrow(StateSpace, cell2mat(sn.state
'));
93 pi0 = zeros(1,length(InfGen)); pi0(initState) = 1.0;
94 [pi,pis,~,scc,~] = ctmc_solve_reducible(InfGen, pi0, options);
99 wset = scc == scc(initState);
100 InfGen = InfGen(wset, wset);
101 StateSpace = StateSpace(wset,:);
102 probSysState = pis(scc(initState),scc == scc(initState));
105probSysState(probSysState<GlobalConstants.Zero)=0;
106probSysState = probSysState/sum(probSysState);
115istSpaceShift = zeros(1,M);
118 istSpaceShift(ist) = 0;
120 istSpaceShift(ist) = istSpaceShift(ist-1) + size(sn.space{ist-1},2);
125 refsf = sn.stationToStateful(sn.refstat(k));
126 XN(k) = probSysState*arvRates(wset,refsf,k);
130 isf = sn.stationToStateful(ist);
131 ind = sn.stationToNode(ist);
133 TN(ist,k) = probSysState*depRates(wset,isf,k);
134 QN(ist,k) = probSysState*StateSpaceAggr(wset,(ist-1)*K+k);
136 if sn.nodetype(ind) ~= NodeType.Source
138 case SchedStrategy.INF
140 UN(ist,k) = QN(ist,k);
142 case {SchedStrategy.PS, SchedStrategy.DPS, SchedStrategy.GPS, SchedStrategy.LPS}
143 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
145 if ~isempty(PH{ist}{k})
146 % There are cases where due to remove of
147 % immediate transitions or due to cutoff the
148 % utilization estimator based on arrivals or
149 % departures can be under-estimated. E.g.:
150 % UNarv_ik doesn't work well with example_stateDependentRouting_3
151 % UNdep_ik doesn
't work well with test_OQN_JMT_6
152 % Therefore, we take the maximum of the two
153 % Note: the two estimators are normally
155 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
156 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); % this is valid because CS in LINE is in a separate node
157 UN(ist,k) = max(UNarv_ik,UNdep_ik);
160 else % lld/cd/ljd cases
161 ind = sn.stationToNode(ist);
164 [ni,nir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
167 UN(ist,k) = UN(ist,k) + probSysState(st)*nir(k)*sn.schedparam(ist,k)/(nir*sn.schedparam(ist,:)');
173 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
175 if ~isempty(PH{ist}{k})
176 % There are cases where due to remove of
177 % immediate transitions or due to cutoff the
178 % utilization estimator based on arrivals or
179 % departures can be under-estimated. E.g.:
180 % UNarv_ik doesn
't work well with example_stateDependentRouting_3
181 % UNdep_ik doesn't work well with test_OQN_JMT_6
182 % Therefore, we take the maximum of the two
183 % Note: the two estimators are normally
185 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
186 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); %
this is valid because CS in LINE
is in a separate node
187 UN(ist,k) = max(UNarv_ik,UNdep_ik);
190 else % lld/cd/ljd cases
191 ind = sn.stationToNode(ist);
194 [ni,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
197 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
209 RN(ist,k) = QN(ist,k)./TN(ist,k);
214 CN(k) = NK(k)./XN(k);
224runtime = toc(Tstart);
226% now update the routing probabilities in
nodes with state-dependent routing
227TNcache = zeros(sn.nstateful,K);
229 for isf=1:sn.nstateful
230 ind = sncopy.statefulToNode(isf);
231 if sncopy.nodetype(ind) == NodeType.Cache
232 TNcache(isf,k) = probSysState*depRates(wset,isf,k);
237% updates cache actual hit and miss data
239 for isf=1:sncopy.nstateful
240 ind = sncopy.statefulToNode(isf);
241 if sncopy.nodetype(ind) == NodeType.Cache
242 if length(sncopy.nodeparam{ind}.hitclass)>=k
243 h = sncopy.nodeparam{ind}.hitclass(k);
244 m = sncopy.nodeparam{ind}.missclass(k);
246 sncopy.nodeparam{ind}.actualhitprob(k) = TNcache(isf,h)/sum(TNcache(isf,[h,m]));
247 sncopy.nodeparam{ind}.actualmissprob(k) = TNcache(isf,m)/sum(TNcache(isf,[h,m]));