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 % 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,:);
72 else
73 line_debug('CTMC is irreducible, using full state space');
74 end
75else
76 % development version
77
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);
83
84 if size(pis,1)==1
85 probSysState = pi;
86 else
87 wset = scc == scc(initState);
88 InfGen = InfGen(wset, wset);
89 StateSpace = StateSpace(wset,:);
90 probSysState = pis(scc(initState),scc == scc(initState));
91 end
92end
93probSysState(probSysState<GlobalConstants.Zero)=0;
94probSysState = probSysState/sum(probSysState);
95
96XN = NaN*zeros(1,K);
97UN = NaN*zeros(M,K);
98QN = NaN*zeros(M,K);
99RN = NaN*zeros(M,K);
100TN = NaN*zeros(M,K);
101CN = NaN*zeros(1,K);
102
103istSpaceShift = zeros(1,M);
104for ist=1:M
105 if ist==1
106 istSpaceShift(ist) = 0;
107 else
108 istSpaceShift(ist) = istSpaceShift(ist-1) + size(sn.space{ist-1},2);
109 end
110end
111
112for k=1:K
113 refsf = sn.stationToStateful(sn.refstat(k));
114 XN(k) = probSysState*arvRates(wset,refsf,k);
115end
116
117for ist=1:M
118 isf = sn.stationToStateful(ist);
119 ind = sn.stationToNode(ist);
120 for k=1:K
121 TN(ist,k) = probSysState*depRates(wset,isf,k);
122 QN(ist,k) = probSysState*StateSpaceAggr(wset,(ist-1)*K+k);
123 end
124 if sn.nodetype(ind) ~= NodeType.Source
125 switch sched(ist)
126 case SchedStrategy.INF
127 for k=1:K
128 UN(ist,k) = QN(ist,k);
129 end
130 case {SchedStrategy.PS, SchedStrategy.DPS, SchedStrategy.GPS, SchedStrategy.LPS}
131 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
132 for k=1:K
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
142 % identical.
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);
146 end
147 end
148 else % lld/cd/ljd cases
149 ind = sn.stationToNode(ist);
150 UN(ist,1:K) = 0;
151 for st = wset
152 [ni,nir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
153 if ni>0
154 for k=1:K
155 UN(ist,k) = UN(ist,k) + probSysState(st)*nir(k)*sn.schedparam(ist,k)/(nir*sn.schedparam(ist,:)');
156 end
157 end
158 end
159 end
160 otherwise
161 if isempty(sn.lldscaling) && isempty(sn.cdscaling) && ~sn_has_joint_dependence(sn)
162 for k=1:K
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
172 % identical.
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);
176 end
177 end
178 else % lld/cd/ljd cases
179 ind = sn.stationToNode(ist);
180 UN(ist,1:K) = 0;
181 for st = wset
182 [ni,~,sir] = State.toMarginal(sn, ind, StateSpace(st,(istSpaceShift(ist)+1):(istSpaceShift(ist)+size(sn.space{ist},2))));
183 if ni>0
184 for k=1:K
185 UN(ist,k) = UN(ist,k) + probSysState(st)*sir(k)/S(ist);
186 end
187 end
188 end
189 end
190 end
191 end
192end
193
194for k=1:K
195 for ist=1:M
196 if TN(ist,k)>0
197 RN(ist,k) = QN(ist,k)./TN(ist,k);
198 else
199 RN(ist,k)=0;
200 end
201 end
202 CN(k) = NK(k)./XN(k);
203end
204
205QN(isnan(QN))=0;
206CN(isnan(CN))=0;
207RN(isnan(RN))=0;
208UN(isnan(UN))=0;
209XN(isnan(XN))=0;
210TN(isnan(TN))=0;
211
212runtime = toc(Tstart);
213
214% now update the routing probabilities in nodes with state-dependent routing
215TNcache = zeros(sn.nstateful,K);
216for k=1: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);
221 end
222 end
223end
224
225% updates cache actual hit and miss data
226for k=1:K
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);
233 if h> 0 && m > 0
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]));
236 end
237 end
238 end
239 end
240end
241end
Definition mmt.m:92