1function [QN,UN,RN,TN,CN,XN,totiter] = solver_mam_basic_mmap(sn, options)
2% [QN,UN,RN,TN,CN,XN,TOTITER] = SOLVER_MAM_BASIC_MMAP(SN, OPTIONS)
4% MAM decomposition solver with fork-join synchronization via mmap_max.
5% Follows the solver_mam.m (dec.mmap) iteration pattern with FJ-aware
6% traffic analysis
using solver_mam_traffic_mmap.
8% Supports open networks with general FJ topologies and heterogeneous
9% service distributions at parallel queues.
11% Copyright (c) 2012-2026, Imperial College London
14config = options.config;
15if ~isfield(config,
'fj_sync_q_len')
16 config.fj_sync_q_len = 2;
18if ~isfield(config, 'etaqa_trunc')
19 config.etaqa_trunc = 8;
38% Build FJ synchronization
map
39fjSyncMap = sn_build_fj_sync_map(sn);
41% Determine per-chain arrival rates
44 inchain = sn.inchain{c};
45 lambdas_inchain = sn.rates(sn.refstat(inchain(1)),inchain);
46 lambdas_inchain = lambdas_inchain(isfinite(lambdas_inchain));
47 lambda(inchain) = sum(lambdas_inchain);
50% Prepare PH service distributions
55 case SchedStrategy.EXT
56 TN(ist,:) = sn.rates(ist,:);
57 TN(ist,isnan(TN(ist,:))) = 0;
58 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
60 % divide service time by number of servers and put
61 % later a surrogate delay server in tandem to compensate
62 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
63 pie{ist}{k} = map_pie(PH{ist}{k});
64 D0{ist,k} = PH{ist}{k}{1};
65 if any(isnan(D0{ist,k}))
66 D0{ist,k} = -GlobalConstants.Immediate;
68 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
71 case SchedStrategy.INF
73 pie{ist}{k} = map_pie(PH{ist}{k});
74 D0{ist,k} = PH{ist}{k}{1};
75 if any(isnan(D0{ist,k}))
76 D0{ist,k} = -GlobalConstants.Immediate;
78 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
83 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
84 pie{ist}{k} = map_pie(PH{ist}{k});
85 D0{ist,k} = PH{ist}{k}{1};
86 if any(isnan(D0{ist,k}))
87 D0{ist,k} = -GlobalConstants.Immediate;
89 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
95it_max = options.iter_max;
97 % Initialize departure processes (node-indexed: DEP{ind,r})
101 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
102 if sn.isstation(ind) && ~isForkJoin
103 ist = sn.nodeToStation(ind);
105 if V(ist,r) > 0 && lambda(r) > 0
106 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
108 DEP{ind,r} = PH{ist}{r};
112 % Non-station
nodes or Fork/Join: pass-through
113 % map_exponential takes MEAN interarrival time = 1/rate
116 DEP{ind,r} = map_exponential(1/lambda(r));
118 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
125 % Compute arrival processes with FJ synchronization
126 ARV = solver_mam_traffic_mmap(sn, DEP, config, fjSyncMap);
130 ind = sn.stationToNode(ist);
131 switch sn.nodetype(ind)
133 % Join: throughput = arrival rate (flow conservation)
135 TN(ist,k) = lambda(k);
141 if ~isempty(ARV{ind}) && iscell(ARV{ind})
142 % Compress arrival process
if too large
143 if length(ARV{ind}{1}) > config.space_max
145 line_printf(
'\nArrival process at node %d is now at %d states. Compressing.', ind, length(ARV{ind}{1}));
147 ARV{ind} = mmap_compress(ARV{ind});
151 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
152 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}},
'ncMoms', 1,
'ncDistr', 2);
154 QN(ist,k) = sum(Qret{k});
156 TN(ist,:) = mmap_lambda(ARV{ind});
157 case SchedStrategy.PS
158 TN(ist,:) = mmap_lambda(ARV{ind});
160 UN(ist,k) = TN(ist,k) * S(ist,k);
162 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,:))]);
164 QN(ist,k) = UN(ist,k)/(1-Uden);
169 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
170 % add number of jobs at the surrogate delay server
171 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
172 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
177 case SchedStrategy.INF
178 if ~isempty(ARV{ind}) && iscell(ARV{ind})
179 TN(ist,:) = mmap_lambda(ARV{ind});
183 UN(ist,k) = S(ist,k)*TN(ist,k);
184 QN(ist,k) = TN(ist,k)*S(ist,k);
185 RN(ist,k) = S(ist,k);
188 case SchedStrategy.EXT
189 % Source: already set TN above
195 if it >= 3 && max(abs(QN(:)-QN_1(:))./(QN_1(:)+GlobalConstants.FineTol)) < options.iter_tol
199 % Update departure processes
201 ind = sn.stationToNode(ist);
202 switch sn.nodetype(ind)
204 if ~isempty(ARV{ind}) && iscell(ARV{ind})
206 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
208 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
212 etaqa_n = config.etaqa_trunc;
213 etaqa_sz = (etaqa_n+1)*na*ns;
214 rho = sum(UN(ist,:));
215 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
217 DEP{ind,r} = qbd_depproc_etaqa(A, Srv, etaqa_n);
218 DEP{ind,r} = map_normalize(DEP{ind,r});
225 if V(ist,r) > 0 && lambda(r) > 0
226 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
229 case SchedStrategy.PS
231 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
235 etaqa_n = config.etaqa_trunc;
236 etaqa_sz = (etaqa_n+1)*na*ns;
237 rho = sum(UN(ist,:));
238 if V(ist,r) > 0 && lambda(r) > 0
239 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
241 DEP{ind,r} = qbd_depproc_etaqa_ps(A, Srv, etaqa_n);
242 DEP{ind,r} = map_normalize(DEP{ind,r});
249 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
255 % Join: departure =
synchronized arrival (pass-through)
256 % map_exponential takes MEAN interarrival time = 1/rate
259 DEP{ind,r} = map_exponential(1/TN(ist,r));
263 % Fork
nodes keep their initial departure processes (pass-through)
269 line_printf(
'\nMAM FJ parametric decomposition completed in %d iterations.', it);
272% Join
nodes represent synchronization delay, not service demand. Recover
273% their queue length and response time from the parallel branch means.
274for joinIdx = find(sn.nodetype == NodeType.Join)
'
275 joinStat = sn.nodeToStation(joinIdx);
279 syncGroups = unique(fjSyncMap.nodeSync(joinIdx, :));
280 syncGroups = syncGroups(syncGroups > 0);
282 if TN(joinStat, r) <= 0
288 branchNodes = find(fjSyncMap.nodeSync(joinIdx, :) == gid);
289 branchRt = zeros(1, numel(branchNodes));
290 branchTput = zeros(1, numel(branchNodes));
292 for b = 1:numel(branchNodes)
293 branchStat = sn.nodeToStation(branchNodes(b));
294 if isnan(branchStat) || RN(branchStat, r) <= 0
298 branchRt(used) = RN(branchStat, r);
299 branchTput(used) = TN(branchStat, r);
301 branchRt = branchRt(1:used);
302 branchTput = branchTput(1:used);
303 if numel(branchRt) < 2
306 lambdai = 1 ./ branchRt;
308 for pow = 0:(numel(branchRt) - 1)
309 maxBranchRt = maxBranchRt + (-1)^pow * sum(1 ./ sum(nchoosek(lambdai, pow + 1), 2));
311 syncDelay = syncDelay + max(maxBranchRt - mean(branchRt), 0);
312 joinArrivalRate = joinArrivalRate + sum(branchTput);
314 RN(joinStat, r) = syncDelay;
315 QN(joinStat, r) = joinArrivalRate * syncDelay;