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'));
66 % determine the weakly connected component associated to the initial state
67 wset = find(connComp == connComp(initState));
68 line_debug(
'Using component %d with %d states (from initial state)', connComp(initState), length(wset));
69 probSysState = ctmc_solve(InfGen(wset, wset), options);
70 InfGen = InfGen(wset, wset);
71 StateSpace = StateSpace(wset,:);
73 line_debug(
'CTMC is irreducible, using full state space');
78 % we now find the initial state and then solver the CTMC allowing
for the
79 %
case where it
is reducible
80 initState = matchrow(StateSpace, cell2mat(sn.state
'));
81 pi0 = zeros(1,length(InfGen)); pi0(initState) = 1.0;
82 [pi,pis,~,scc,~] = ctmc_solve_reducible(InfGen, pi0, options);
87 wset = scc == scc(initState);
88 InfGen = InfGen(wset, wset);
89 StateSpace = StateSpace(wset,:);
90 probSysState = pis(scc(initState),scc == scc(initState));
93probSysState(probSysState<GlobalConstants.Zero)=0;
94probSysState = probSysState/sum(probSysState);
103istSpaceShift = zeros(1,M);
106 istSpaceShift(ist) = 0;
108 istSpaceShift(ist) = istSpaceShift(ist-1) + size(sn.space{ist-1},2);
113 refsf = sn.stationToStateful(sn.refstat(k));
114 XN(k) = probSysState*arvRates(wset,refsf,k);
118 isf = sn.stationToStateful(ist);
119 ind = sn.stationToNode(ist);
121 TN(ist,k) = probSysState*depRates(wset,isf,k);
122 QN(ist,k) = probSysState*StateSpaceAggr(wset,(ist-1)*K+k);
124 if sn.nodetype(ind) ~= NodeType.Source
126 case SchedStrategy.INF
128 UN(ist,k) = QN(ist,k);
130 case {SchedStrategy.PS, SchedStrategy.DPS, SchedStrategy.GPS, SchedStrategy.LPS}
131 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
133 if ~isempty(PH{ist}{k})
134 % There are cases where due to remove of
135 % immediate transitions or due to cutoff the
136 % utilization estimator based on arrivals or
137 % departures can be under-estimated. E.g.:
138 % UNarv_ik doesn't work well with example_stateDependentRouting_3
139 % UNdep_ik doesn
't work well with test_OQN_JMT_6
140 % Therefore, we take the maximum of the two
141 % Note: the two estimators are normally
143 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
144 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); % this is valid because CS in LINE is in a separate node
145 UN(ist,k) = max(UNarv_ik,UNdep_ik);
148 else % lld/cd/ljd cases
149 ind = sn.stationToNode(ist);
152 [ni,nir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
155 UN(ist,k) = UN(ist,k) + probSysState(st)*nir(k)*sn.schedparam(ist,k)/(nir*sn.schedparam(ist,:)');
161 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
163 if ~isempty(PH{ist}{k})
164 % There are cases where due to remove of
165 % immediate transitions or due to cutoff the
166 % utilization estimator based on arrivals or
167 % departures can be under-estimated. E.g.:
168 % UNarv_ik doesn
't work well with example_stateDependentRouting_3
169 % UNdep_ik doesn't work well with test_OQN_JMT_6
170 % Therefore, we take the maximum of the two
171 % Note: the two estimators are normally
173 UNarv_ik = probSysState*arvRates(wset,isf,k)*map_mean(PH{ist}{k})/S(ist);
174 UNdep_ik = TN(ist,k)*map_mean(PH{ist}{k})/S(ist); %
this is valid because CS in LINE
is in a separate node
175 UN(ist,k) = max(UNarv_ik,UNdep_ik);
178 else % lld/cd/ljd cases
179 ind = sn.stationToNode(ist);
182 [ni,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
185 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
197 RN(ist,k) = QN(ist,k)./TN(ist,k);
202 CN(k) = NK(k)./XN(k);
212runtime = toc(Tstart);
214% now update the routing probabilities in
nodes with state-dependent routing
215TNcache = zeros(sn.nstateful,K);
217 for isf=1:sn.nstateful
218 ind = sncopy.statefulToNode(isf);
219 if sncopy.nodetype(ind) == NodeType.Cache
220 TNcache(isf,k) = probSysState*depRates(wset,isf,k);
225% updates cache actual hit and miss data
227 for isf=1:sncopy.nstateful
228 ind = sncopy.statefulToNode(isf);
229 if sncopy.nodetype(ind) == NodeType.Cache
230 if length(sncopy.nodeparam{ind}.hitclass)>=k
231 h = sncopy.nodeparam{ind}.hitclass(k);
232 m = sncopy.nodeparam{ind}.missclass(k);
234 sncopy.nodeparam{ind}.actualhitprob(k) = TNcache(isf,h)/sum(TNcache(isf,[h,m]));
235 sncopy.nodeparam{ind}.actualmissprob(k) = TNcache(isf,m)/sum(TNcache(isf,[h,m]));