1function [QN,UN,RN,TN,CN,XN,totiter] = solver_mam_basic_fj(sn, options)
2% [QN,UN,RN,TN,CN,XN,TOTITER] = SOLVER_MAM_BASIC_FJ(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_fj.
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;
35% Build FJ synchronization
map
36fjSyncMap = sn_build_fj_sync_map(sn);
38% Determine per-chain arrival rates
41 inchain = sn.inchain{c};
42 lambdas_inchain = sn.rates(sn.refstat(inchain(1)),inchain);
43 lambdas_inchain = lambdas_inchain(isfinite(lambdas_inchain));
44 lambda(inchain) = sum(lambdas_inchain);
47% Prepare PH service distributions
52 case SchedStrategy.EXT
53 TN(ist,:) = sn.rates(ist,:);
54 TN(ist,isnan(TN(ist,:))) = 0;
55 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
57 % divide service time by number of servers and put
58 % later a surrogate delay server in tandem to compensate
59 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
60 pie{ist}{k} = map_pie(PH{ist}{k});
61 D0{ist,k} = PH{ist}{k}{1};
62 if any(isnan(D0{ist,k}))
63 D0{ist,k} = -GlobalConstants.Immediate;
65 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
68 case SchedStrategy.INF
70 pie{ist}{k} = map_pie(PH{ist}{k});
71 D0{ist,k} = PH{ist}{k}{1};
72 if any(isnan(D0{ist,k}))
73 D0{ist,k} = -GlobalConstants.Immediate;
75 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
80 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
81 pie{ist}{k} = map_pie(PH{ist}{k});
82 D0{ist,k} = PH{ist}{k}{1};
83 if any(isnan(D0{ist,k}))
84 D0{ist,k} = -GlobalConstants.Immediate;
86 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
92it_max = options.iter_max;
94 % Initialize departure processes (node-indexed: DEP{ind,r})
98 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
99 if sn.isstation(ind) && ~isForkJoin
100 ist = sn.nodeToStation(ind);
102 if V(ist,r) > 0 && lambda(r) > 0
103 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
105 DEP{ind,r} = PH{ist}{r};
109 % Non-station
nodes or Fork/Join: pass-through
110 % map_exponential takes MEAN interarrival time = 1/rate
113 DEP{ind,r} = map_exponential(1/lambda(r));
115 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
122 % Compute arrival processes with FJ synchronization
123 ARV = solver_mam_traffic_fj(sn, DEP, config, fjSyncMap);
127 ind = sn.stationToNode(ist);
128 switch sn.nodetype(ind)
130 % Join: throughput from synced arrival rate, no queueing
131 if ~isempty(ARV{ind}) && iscell(ARV{ind})
132 arrRates = mmap_lambda(ARV{ind});
134 TN(ist,k) = arrRates(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}
207 [Ret{1:2*K}] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}},
'stDistrPH');
209 % define a ph
for the arrival process of
class r
210 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
214 % define a ph
for the service process of
class r
219 rho = sum(UN(ist,:));
221 Afull = AQ/max(rho, GlobalConstants.FineTol);
222 pfullonarrival = 1 - (Afull)/(1+Afull);
224 A = mmap_scale(A, map_mean(A)-map_mean(Srv));
230 DEP0ir = [Srv{1}, sum(rho)*tS*pieA;
233 DEP1ir = [(1-sum(rho))*Srv{2}, zSA;
236 DEP{ind,r} = map_normalize({DEP0ir, DEP1ir});
237 if V(ist,r) > 0 && lambda(r) > 0
238 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
241 case SchedStrategy.PS
242 % For PS, use simpler departure process update
244 if V(ist,r) > 0 && lambda(r) > 0
245 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
251 % Join: departure =
synchronized arrival (pass-through)
252 % map_exponential takes MEAN interarrival time = 1/rate
255 DEP{ind,r} = map_exponential(1/TN(ist,r));
259 % Fork
nodes keep their initial departure processes (pass-through)
265 line_printf(
'\nMAM FJ parametric decomposition completed in %d iterations.', it);