LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_mam_basic_fj.m
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)
3%
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.
7%
8% Supports open networks with general FJ topologies and heterogeneous
9% service distributions at parallel queues.
10%
11% Copyright (c) 2012-2026, Imperial College London
12% All rights reserved.
13
14config = options.config;
15if ~isfield(config, 'fj_sync_q_len')
16 config.fj_sync_q_len = 2;
17end
18if ~isfield(config, 'etaqa_trunc')
19 config.etaqa_trunc = 8;
20end
21
22PH = sn.proc;
23I = sn.nnodes;
24M = sn.nstations;
25K = sn.nclasses;
26C = sn.nchains;
27N = sn.njobs';
28V = cellsum(sn.visits);
29S = 1./sn.rates;
30
31QN = zeros(M,K);
32UN = zeros(M,K);
33RN = zeros(M,K);
34TN = zeros(M,K);
35CN = zeros(1,K);
36XN = zeros(1,K);
37
38% Build FJ synchronization map
39fjSyncMap = sn_build_fj_sync_map(sn);
40
41% Determine per-chain arrival rates
42lambda = zeros(1,K);
43for c=1:C
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);
48end
49
50% Prepare PH service distributions
51pie = {};
52D0 = {};
53for ist=1:M
54 switch sn.sched(ist)
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}
59 for k=1:K
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;
67 pie{ist}{k} = 1;
68 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
69 end
70 end
71 case SchedStrategy.INF
72 for k=1:K
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;
77 pie{ist}{k} = 1;
78 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
79 end
80 end
81 case SchedStrategy.PS
82 for k=1:K
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;
88 pie{ist}{k} = 1;
89 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
90 end
91 end
92 end
93end
94
95it_max = options.iter_max;
96for it=1:it_max
97 % Initialize departure processes (node-indexed: DEP{ind,r})
98 if it == 1
99 DEP = cell(I,K);
100 for ind=1:I
101 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
102 if sn.isstation(ind) && ~isForkJoin
103 ist = sn.nodeToStation(ind);
104 for r=1:K
105 if V(ist,r) > 0 && lambda(r) > 0
106 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
107 else
108 DEP{ind,r} = PH{ist}{r};
109 end
110 end
111 else
112 % Non-station nodes or Fork/Join: pass-through
113 % map_exponential takes MEAN interarrival time = 1/rate
114 for r=1:K
115 if lambda(r) > 0
116 DEP{ind,r} = map_exponential(1/lambda(r));
117 else
118 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
119 end
120 end
121 end
122 end
123 end
124
125 % Compute arrival processes with FJ synchronization
126 ARV = solver_mam_traffic_fj(sn, DEP, config, fjSyncMap);
127
128 QN_1 = QN;
129 for ist=1:M
130 ind = sn.stationToNode(ist);
131 switch sn.nodetype(ind)
132 case NodeType.Join
133 % Join: throughput from synced arrival rate, no queueing
134 if ~isempty(ARV{ind}) && iscell(ARV{ind})
135 arrRates = mmap_lambda(ARV{ind});
136 for k=1:K
137 TN(ist,k) = arrRates(k);
138 UN(ist,k) = 0;
139 QN(ist,k) = 0;
140 RN(ist,k) = 0;
141 end
142 end
143 case NodeType.Queue
144 if ~isempty(ARV{ind}) && iscell(ARV{ind})
145 % Compress arrival process if too large
146 if length(ARV{ind}{1}) > config.space_max
147 if options.verbose
148 line_printf('\nArrival process at node %d is now at %d states. Compressing.', ind, length(ARV{ind}{1}));
149 end
150 ARV{ind} = mmap_compress(ARV{ind});
151 end
152
153 switch sn.sched(ist)
154 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
155 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms', 1, 'ncDistr', 2);
156 for k=1:K
157 QN(ist,k) = sum(Qret{k});
158 end
159 TN(ist,:) = mmap_lambda(ARV{ind});
160 case SchedStrategy.PS
161 TN(ist,:) = mmap_lambda(ARV{ind});
162 for k=1:K
163 UN(ist,k) = TN(ist,k) * S(ist,k);
164 end
165 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,:))]);
166 for k=1:K
167 QN(ist,k) = UN(ist,k)/(1-Uden);
168 end
169 end
170
171 for k=1:K
172 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
173 % add number of jobs at the surrogate delay server
174 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
175 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
176 end
177 end
178 otherwise
179 switch sn.sched(ist)
180 case SchedStrategy.INF
181 if ~isempty(ARV{ind}) && iscell(ARV{ind})
182 TN(ist,:) = mmap_lambda(ARV{ind});
183 end
184 for k=1:K
185 if TN(ist,k) > 0
186 UN(ist,k) = S(ist,k)*TN(ist,k);
187 QN(ist,k) = TN(ist,k)*S(ist,k);
188 RN(ist,k) = S(ist,k);
189 end
190 end
191 case SchedStrategy.EXT
192 % Source: already set TN above
193 end
194 end
195 end
196
197 % Check convergence
198 if it >= 3 && max(abs(QN(:)-QN_1(:))./(QN_1(:)+GlobalConstants.FineTol)) < options.iter_tol
199 break;
200 end
201
202 % Update departure processes
203 for ist=1:M
204 ind = sn.stationToNode(ist);
205 switch sn.nodetype(ind)
206 case NodeType.Queue
207 if ~isempty(ARV{ind}) && iscell(ARV{ind})
208 switch sn.sched(ist)
209 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
210 for r=1:K
211 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
212 Srv = PH{ist}{r};
213 na = length(A{1});
214 ns = length(Srv{1});
215 etaqa_n = config.etaqa_trunc;
216 etaqa_sz = (etaqa_n+1)*na*ns;
217 rho = sum(UN(ist,:));
218 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
219 try
220 DEP{ind,r} = qbd_depproc_etaqa(A, Srv, etaqa_n);
221 DEP{ind,r} = map_normalize(DEP{ind,r});
222 catch
223 DEP{ind,r} = Srv;
224 end
225 else
226 DEP{ind,r} = Srv;
227 end
228 if V(ist,r) > 0 && lambda(r) > 0
229 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
230 end
231 end
232 case SchedStrategy.PS
233 for r=1:K
234 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
235 Srv = PH{ist}{r};
236 na = length(A{1});
237 ns = length(Srv{1});
238 etaqa_n = config.etaqa_trunc;
239 etaqa_sz = (etaqa_n+1)*na*ns;
240 rho = sum(UN(ist,:));
241 if V(ist,r) > 0 && lambda(r) > 0
242 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
243 try
244 DEP{ind,r} = qbd_depproc_etaqa_ps(A, Srv, etaqa_n);
245 DEP{ind,r} = map_normalize(DEP{ind,r});
246 catch
247 DEP{ind,r} = Srv;
248 end
249 else
250 DEP{ind,r} = Srv;
251 end
252 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
253 end
254 end
255 end
256 end
257 case NodeType.Join
258 % Join: departure = synchronized arrival (pass-through)
259 % map_exponential takes MEAN interarrival time = 1/rate
260 for r=1:K
261 if TN(ist,r) > 0
262 DEP{ind,r} = map_exponential(1/TN(ist,r));
263 end
264 end
265 end
266 % Fork nodes keep their initial departure processes (pass-through)
267 end
268end
269
270totiter = it;
271if options.verbose
272 line_printf('\nMAM FJ parametric decomposition completed in %d iterations.', it);
273end
274
275% Post-processing
276CN = sum(RN,1);
277QN(isnan(QN)) = 0;
278RN(isnan(RN)) = 0;
279UN(isnan(UN)) = 0;
280TN(isnan(TN)) = 0;
281end
Definition mmt.m:124