1function [QN,UN,RN,TN,CN,XN,totiter] = solver_mam_basic_mmap_inner(sn, options, lambda)
2% [QN,UN,RN,TN,CN,XN,TOTITER] = SOLVER_MAM_BASIC_MMAP_INNER(SN, OPTIONS, LAMBDA)
4% MAM/
MMAP fork-join decomposition kernel parameterised by per-
class arrival
5% rates LAMBDA. Performs the departure-process refinement loop only;
6% population enforcement (closed networks)
is the wrapper
's responsibility.
8% Copyright (c) 2012-2026, Imperial College London
11config = options.config;
12if ~isfield(config, 'fj_sync_q_len
')
13 config.fj_sync_q_len = 2;
15if ~isfield(config, 'etaqa_trunc
')
16 config.etaqa_trunc = 8;
23V = cellsum(sn.visits);
33% Build FJ synchronization map
34fjSyncMap = sn_build_fj_sync_map(sn);
36% Prepare PH service distributions
41 case SchedStrategy.EXT
42 TN(ist,:) = sn.rates(ist,:);
43 TN(ist,isnan(TN(ist,:))) = 0;
44 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
46 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
47 pie{ist}{k} = map_pie(PH{ist}{k});
48 D0{ist,k} = PH{ist}{k}{1};
49 if any(isnan(D0{ist,k}))
50 D0{ist,k} = -GlobalConstants.Immediate;
52 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
55 case SchedStrategy.INF
57 pie{ist}{k} = map_pie(PH{ist}{k});
58 D0{ist,k} = PH{ist}{k}{1};
59 if any(isnan(D0{ist,k}))
60 D0{ist,k} = -GlobalConstants.Immediate;
62 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
67 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
68 pie{ist}{k} = map_pie(PH{ist}{k});
69 D0{ist,k} = PH{ist}{k}{1};
70 if any(isnan(D0{ist,k}))
71 D0{ist,k} = -GlobalConstants.Immediate;
73 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
79it_max = options.iter_max;
81 % Initialize departure processes (node-indexed: DEP{ind,r})
85 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
86 if sn.isstation(ind) && ~isForkJoin
87 ist = sn.nodeToStation(ind);
89 if V(ist,r) > 0 && lambda(r) > 0
90 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
92 % Self-looping classes are handled separately by the
93 % closed-network wrapper (their metrics are pinned at the
94 % reference station). They must not inject a saturating
95 % arrival stream into the shared-queue decomposition, so
96 % give them a vanishing-rate departure process.
97 DEP{ind,r} = map_exponential(1/GlobalConstants.Zero);
99 DEP{ind,r} = PH{ist}{r};
105 DEP{ind,r} = map_exponential(1/lambda(r));
107 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
114 % Compute arrival processes with FJ synchronization
115 ARV = solver_mam_traffic_mmap(sn, DEP, config, fjSyncMap);
119 ind = sn.stationToNode(ist);
120 switch sn.nodetype(ind)
123 TN(ist,k) = lambda(k);
129 if ~isempty(ARV{ind}) && iscell(ARV{ind})
130 if length(ARV{ind}{1}) > config.space_max
132 line_printf('\nArrival process at node %d
is now at %d states. Compressing.
', ind, length(ARV{ind}{1}));
134 ARV{ind} = mmap_compress(ARV{ind}, config);
137 finiteCapUsed = false;
139 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
140 isFiniteCap = isfinite(sn.cap(ist));
143 [isMmck, muMmck] = mam_detect_mmck(sn, ist, K, ARV{ind});
145 aggrLambda_ist = sum(mmap_lambda(ARV{ind}), 'omitnan
');
146 exactRes = qsys_mmck(aggrLambda_ist, muMmck, sn.nservers(ist), capK);
147 meanQ_fc = exactRes.meanQueueLength;
148 lossProb_fc = exactRes.lossProbability;
150 [meanQ_fc, lossProb_fc, ~] = mam_truncate_renorm( ...
151 {ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, capK);
153 lambdaInflow = mmap_lambda(ARV{ind});
154 lambdaInflow(isnan(lambdaInflow)) = 0;
155 TN_eff = lambdaInflow * (1 - lossProb_fc);
157 S_actual = zeros(1, K);
159 S_actual(k) = map_mean(PH{ist}{k}) * sn.nservers(ist);
162 Savg_eff = sum(TN_eff .* S_actual, 'omitnan
') / sumTN;
163 Wq = max(0, meanQ_fc / sumTN - Savg_eff);
168 TN(ist,k) = TN_eff(k);
169 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
171 RN(ist,k) = Wq + S_actual(k);
172 QN(ist,k) = TN(ist,k) * RN(ist,k);
178 finiteCapUsed = true;
180 rho_ist_classes = mmap_lambda(ARV{ind}) .* arrayfun(@(k) map_mean(PH{ist}{k}), 1:K);
181 rho_ist_classes(isnan(rho_ist_classes)) = 0;
182 % Exclude self-looping classes: they are pinned
183 % by the closed wrapper and must not drive the
184 % FCFS station into the saturation branch.
185 rho_ist = sum(rho_ist_classes(~sn.isslc));
186 if rho_ist < 1 - GlobalConstants.FineTol
187 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms
', 1, 'ncDistr
', 2);
189 QN(ist,k) = sum(Qret{k});
192 % Saturation: bound queue lengths so the
193 % wrapper's bisection can recognise overload
194 % without NaNs propagating from MMAPPH1FCFS.
196 if isfinite(sn.njobs(k))
197 QN(ist,k) = sn.njobs(k);
199 QN(ist,k) = 1/GlobalConstants.FineTol;
203 TN(ist,:) = mmap_lambda(ARV{ind});
205 case SchedStrategy.PS
206 TN(ist,:) = mmap_lambda(ARV{ind});
208 UN(ist,k) = TN(ist,k) * S(ist,k);
210 % Self-looping
classes are pinned by the closed
211 % wrapper and must not count toward the PS sharing
212 % denominator (a single permanent SLC job would
213 % otherwise drive the queue to saturation).
214 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,~sn.isslc))]);
216 QN(ist,k) = UN(ist,k)/(1-Uden);
222 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
223 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
224 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
230 case SchedStrategy.INF
231 if ~isempty(ARV{ind}) && iscell(ARV{ind})
232 TN(ist,:) = mmap_lambda(ARV{ind});
236 UN(ist,k) = S(ist,k)*TN(ist,k);
237 QN(ist,k) = TN(ist,k)*S(ist,k);
238 RN(ist,k) = S(ist,k);
241 case SchedStrategy.EXT
242 % Source: TN already set above
247 if it >= 3 && max(abs(QN(:)-QN_1(:))./(QN_1(:)+GlobalConstants.FineTol)) < options.iter_tol
251 % Update departure processes
253 ind = sn.stationToNode(ist);
254 switch sn.nodetype(ind)
256 if ~isempty(ARV{ind}) && iscell(ARV{ind})
258 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
260 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
264 etaqa_n = config.etaqa_trunc;
265 etaqa_sz = (etaqa_n+1)*na*ns;
266 rho = sum(UN(ist,:));
267 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
269 DEP{ind,r} = qbd_depproc_etaqa(A, Srv, etaqa_n);
270 DEP{ind,r} = map_normalize(DEP{ind,r});
277 if V(ist,r) > 0 && lambda(r) > 0
278 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
281 case SchedStrategy.PS
283 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
287 etaqa_n = config.etaqa_trunc;
288 etaqa_sz = (etaqa_n+1)*na*ns;
289 rho = sum(UN(ist,:));
290 if V(ist,r) > 0 && lambda(r) > 0
291 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
293 DEP{ind,r} = qbd_depproc_etaqa_ps(A, Srv, etaqa_n);
294 DEP{ind,r} = map_normalize(DEP{ind,r});
301 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
309 DEP{ind,r} = map_exponential(1/TN(ist,r));
318 line_printf(
'\nMAM FJ parametric decomposition completed in %d iterations.', it);
321% Join: derive QN/RN from parallel branch means
322for joinIdx = find(sn.nodetype == NodeType.Join)
'
323 joinStat = sn.nodeToStation(joinIdx);
327 syncGroups = unique(fjSyncMap.nodeSync(joinIdx, :));
328 syncGroups = syncGroups(syncGroups > 0);
330 if TN(joinStat, r) <= 0
336 branchNodes = find(fjSyncMap.nodeSync(joinIdx, :) == gid);
337 branchRt = zeros(1, numel(branchNodes));
338 branchTput = zeros(1, numel(branchNodes));
340 for b = 1:numel(branchNodes)
341 branchStat = sn.nodeToStation(branchNodes(b));
342 if isnan(branchStat) || RN(branchStat, r) <= 0
346 branchRt(used) = RN(branchStat, r);
347 branchTput(used) = TN(branchStat, r);
349 branchRt = branchRt(1:used);
350 branchTput = branchTput(1:used);
351 if numel(branchRt) < 2
354 lambdai = 1 ./ branchRt;
356 for pow = 0:(numel(branchRt) - 1)
357 maxBranchRt = maxBranchRt + (-1)^pow * sum(1 ./ sum(nchoosek(lambdai, pow + 1), 2));
359 syncDelay = syncDelay + max(maxBranchRt - mean(branchRt), 0);
360 joinArrivalRate = joinArrivalRate + sum(branchTput);
362 RN(joinStat, r) = syncDelay;
363 QN(joinStat, r) = joinArrivalRate * syncDelay;