LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
polling_info.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_LANG_QN_POLLING_INFO_H
6#define LINE_LANG_QN_POLLING_INFO_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * `State.pollingInfo` and the controller description it returns.
12 *
13 * IT LIVES IN ITS OWN HEADER because both sides of the State package need it
14 * and neither can include the other: `from_marginal` (state.h) has to ENUMERATE
15 * the controller configurations a station can occupy, and the event handlers
16 * (state_events.h) have to MOVE between them. Leaving it in state_events.h left
17 * `from_marginal` unable to emit the controller columns at all, so a polling
18 * station's states were built one block too narrow and the enumerated space
19 * contained no controller -- the station then behaved as a capacity-one queue.
20 */
21
22#include <algorithm>
23#include <cstddef>
24#include <string>
25#include <vector>
26
30#include "line/util/error.h"
31#include "line/util/matrix.h"
32
33namespace line {
34namespace qn {
35
38
39/**
40 * Port of `State.pollingInfo`: the derived description of a polling controller.
41 *
42 * The controller lives in the shared node block of the local variables and
43 * holds, in order, [pos, swk, ctr]. Each column is materialized ONLY when the
44 * discipline needs it, so a polling station never carries state its dynamics
45 * cannot distinguish:
46 *
47 * pos the buffer the server is at or heading to. Needed only when some
48 * switchover is non-immediate: while a job is in service pos equals
49 * that job's class, and while the station is empty with immediate
50 * switchovers the position is unobservable.
51 * swk 0 when the server sits at pos, else the phase of the switchover into
52 * it. Same condition as pos.
53 * ctr the visit budget. Every discipline except EXHAUSTIVE bounds a visit.
54 *
55 * An Immediate switchover is deliberately NOT a state: its rate is ~1e8, and
56 * taking that literally would make the generator stiff and add a spurious
57 * state per buffer. `polling_next` folds it into the enclosing transition.
58 *
59 * EVERY POLLING STATION HAS A CONTROLLER, whichever API declared it and even
60 * when none did. The reference returns [] only for a node that is not a polling
61 * station and otherwise defaults to EXHAUSTIVE with every switchover immediate,
62 * so `NetworkStruct::effective_polling` resolves the two C++ spellings --
63 * `set_polling` writing `sn.pollingparam`, and setPollingType / setSwitchover
64 * writing the Station fields -- into one description.
65 *
66 * BUILDING NO CONTROLLER WAS NOT A DEGRADED MODEL BUT A CRASH. Reading only
67 * `pollingparam` left every station declared the other way (the JSON reader
68 * included) with `valid = false` and, with it, an EMPTY `polled`. `polling_next`
69 * indexes `polled[pos-1]` with no size test, and an empty std::vector<bool>
70 * holds a NULL word pointer, so that index segfaults rather than returning a
71 * garbage bit -- which is why the failure surfaced as a SIGSEGV inside
72 * `after_event_station_arv` and not as a wrong number.
73 */
74template <class T>
77 std::size_t pk = 1;
78 std::vector<bool> polled, has_sw;
79 std::vector<std::size_t> ksw;
80 std::vector<Matrix<T>> sw_d0, sw_d1;
81 std::vector<std::vector<T>> sw_pie;
82 // True where the switchover INTO the buffer is not phase-type: sized as one
83 // placeholder phase with no sw_d0/sw_d1 (see polling_require_ph).
84 std::vector<bool> non_ph;
85 std::size_t off = 0, width = 0;
86 // 0-based columns inside the local-variable block, or npos when absent.
87 std::size_t ipos = static_cast<std::size_t>(-1);
88 std::size_t iswk = static_cast<std::size_t>(-1);
89 std::size_t ictr = static_cast<std::size_t>(-1);
90 bool valid = false;
91
92 // The extensions (`State.pollingInfo` beyond the cyclic controller). The
93 // FULL controller vector is [pos, swk, ctr, src, tht, trt_1..trt_R]; `keep`
94 // says which of its 5+R entries are materialized, `loc[k]` the column (0
95 // based within the controller block) of full entry k, or npos.
97 std::size_t thr_class = 0; ///< 1-based THRESHOLD class, 0 when none
98 long thr_l = 0;
100 bool tpreempt = false;
101 bool table = false, ld = false, repeats = false, ext = false;
102 std::vector<std::size_t> ring; ///< 1-based classes, length M
103 std::size_t M = 0;
104 std::vector<std::size_t> tk; ///< timer phases per class (0 = none)
105 std::vector<Matrix<T>> t_d0, t_d1;
106 std::vector<std::vector<T>> t_pie;
107 std::vector<bool> t_nonph;
108 std::vector<bool> swclass; ///< the class drives a SWITCH event
109 std::vector<std::size_t> pos_of; ///< 1-based first position of each class
110 std::size_t canon_pos = 0, src_default = 0;
111 std::vector<bool> keep;
112 std::vector<std::size_t> loc;
113 std::size_t isrc = static_cast<std::size_t>(-1);
114 std::size_t itht = static_cast<std::size_t>(-1);
115 std::vector<std::size_t> itrt;
116};
117
118/**
119 * Refuses a polling station whose switchover legs are not all phase-type.
120 * Called by the state-space walkers (the event functions of state_events.h and
121 * the SSA NRM engine) before they read sw_d0/sw_d1: polling_info sizes a
122 * non-Markovian leg as one placeholder phase and has no matrices to walk.
123 */
124template <class T>
126 for (bool b : pi.non_ph)
127 if (b)
128 throw UnsupportedError(ErrorKind{"NonExponential"},
129 "Polling switchover distributions must be phase-type or Markovian.");
130 for (bool b : pi.t_nonph)
131 if (b)
132 throw UnsupportedError(
133 ErrorKind{"NonExponential"},
134 "Polling visit timers must be phase-type or Markovian in a state-space solver; approximate a "
135 "deterministic timer by an Erlang (e.g. Erlang.fitMeanAndOrder(T, 10)), or use LDES.");
136}
137
138template <class T>
140 const std::size_t R = sn.nclasses;
142 const std::size_t ist = sn.nodes[ind - 1].station;
143 if (ist == 0 || sn.stations[ist - 1].sched != SchedStrategy::POLLING) return pi;
144 // Whichever API declared the controller, and a default when neither did.
145 const typename NetworkStruct<T>::PollingParam pp = sn.effective_polling(ist);
146 pi.valid = true;
147 pi.ptype = pp.ptype;
148 pi.pk = pp.pk;
149
150 // A class disabled at this station can never hold a job, so it is dropped
151 // from the cyclic order rather than polled forever.
152 // `NetworkStruct::polling_polled`, the same set `refresh_local_vars` sizes
153 // the controller columns over.
154 pi.polled = sn.polling_polled(ist);
155 bool any = false;
156 for (std::size_t r = 0; r < R; ++r) any = any || pi.polled[r];
157 if (!any)
158 throw InputError("polling_info: the polling station has no class with an enabled service");
159
160 pi.order = pp.order;
161 pi.tmode = pp.timer_scope;
162 pi.tpreempt = pp.timer_preempt;
163 std::vector<std::size_t> polled_list;
164 for (std::size_t r = 1; r <= R; ++r)
165 if (pi.polled[r - 1]) polled_list.push_back(r);
167 pi.thr_class = pp.order_class;
168 pi.thr_l = pp.order_threshold;
169 }
170 pi.table = !pp.table.empty();
172 if (pi.table && pi.ld)
173 throw InputError(ErrorKind{"Model"},
174 "A polling table cannot be combined with a load-dependent polling order.");
175 if (pi.table) {
176 for (std::size_t c : pp.table)
177 if (c < 1 || c > R || !pi.polled[c - 1])
178 throw InputError(ErrorKind{"Model"}, "The polling table of station " + std::to_string(ist) +
179 " lists a class that is not served there.");
180 for (std::size_t q : polled_list)
181 if (std::find(pp.table.begin(), pp.table.end(), q) == pp.table.end())
182 throw InputError(ErrorKind{"Model"}, "The polling table of station " + std::to_string(ist) +
183 " must list every class served there at least once.");
184 pi.ring = pp.table;
185 } else {
186 for (std::size_t r = 1; r <= R; ++r) pi.ring.push_back(r);
187 }
188 pi.M = pi.ring.size();
189 if (pi.table) {
190 std::vector<std::size_t> u = pi.ring;
191 std::sort(u.begin(), u.end());
192 pi.repeats = std::unique(u.begin(), u.end()) != u.end();
193 }
195 (pi.thr_class < 1 || pi.thr_class > R || !pi.polled[pi.thr_class - 1]))
196 throw InputError(ErrorKind{"Model"}, "The THRESHOLD polling order of station " + std::to_string(ist) +
197 " names a class that is not served there.");
198
199 // Legs: indexed by the position arrived at (cyclic/table) or by the class
200 // left (load-dependent order), `NetworkStruct::polling_leg`.
201 const std::size_t L = pi.ld ? R : pi.M;
202 pi.has_sw.assign(L, false);
203 pi.ksw.assign(L, 0);
204 pi.sw_d0.assign(L, Matrix<T>());
205 pi.sw_d1.assign(L, Matrix<T>());
206 pi.sw_pie.assign(L, std::vector<T>());
207 pi.non_ph.assign(L, false);
208 std::vector<const lang::Distrib<T>*> legs(L, nullptr);
209 for (std::size_t l = 1; l <= L; ++l) {
210 const lang::Distrib<T>* d = sn.polling_leg(ist, pp, l);
211 legs[l - 1] = d;
212 if (d == nullptr) continue; // folded, never a state
213 pi.has_sw[l - 1] = true;
215 pi.ksw[l - 1] = 1;
216 pi.sw_pie[l - 1] = std::vector<T>(1, T(1));
217 pi.non_ph[l - 1] = true;
218 continue;
219 }
220 pi.ksw[l - 1] = d->D0.rows();
221 pi.sw_d0[l - 1] = d->D0;
222 pi.sw_d1[l - 1] = d->D1;
223 mam::Map<T> mp;
224 mp.D0 = d->D0;
225 mp.D1 = d->D1;
226 pi.sw_pie[l - 1] = mam::map_pie(mp);
227 }
228 bool anysw = false;
229 for (std::size_t l = 0; l < L; ++l) anysw = anysw || pi.has_sw[l];
230
231 pi.tk.assign(R, 0);
232 pi.t_d0.assign(R, Matrix<T>());
233 pi.t_d1.assign(R, Matrix<T>());
234 pi.t_pie.assign(R, std::vector<T>());
235 pi.t_nonph.assign(R, false);
237 for (std::size_t r : polled_list) {
238 if (pp.timer.size() < r || pp.timer[r - 1].disabled)
239 throw InputError(ErrorKind{"Model"}, "Polling station " + std::to_string(ist) +
240 " declares a visit timer without a law for class " +
241 std::to_string(r) + ".");
242 const lang::Distrib<T>& d = pp.timer[r - 1];
243 if (!lang::process_is_markovian(d.type) || d.D0.rows() == 0) {
244 pi.tk[r - 1] = 1;
245 pi.t_pie[r - 1] = std::vector<T>(1, T(1));
246 pi.t_nonph[r - 1] = true;
247 continue;
248 }
249 pi.tk[r - 1] = d.D0.rows();
250 pi.t_d0[r - 1] = d.D0;
251 pi.t_d1[r - 1] = d.D1;
252 mam::Map<T> mp;
253 mp.D0 = d.D0;
254 mp.D1 = d.D1;
255 pi.t_pie[r - 1] = mam::map_pie(mp);
256 }
258 if (pi.ld)
259 throw InputError(ErrorKind{"Model"},
260 "A GLOBAL (timed-token) polling timer needs a fixed visit order; it cannot be "
261 "combined with a load-dependent polling order.");
262 if (!anysw)
263 throw InputError(ErrorKind{"Model"},
264 "A GLOBAL (timed-token) polling timer needs at least one timed switchover: with "
265 "zero walk times the token would rotate in zero time.");
266 }
267 }
268
269 pi.swclass.assign(R, false);
270 for (std::size_t r : polled_list) {
271 if (pi.ld) {
272 pi.swclass[r - 1] = anysw;
273 } else {
274 for (std::size_t p = 0; p < pi.M; ++p)
275 if (pi.ring[p] == r && pi.has_sw[p]) pi.swclass[r - 1] = true;
276 }
277 if (pi.tmode != lang::PollingTimer::NONE) pi.swclass[r - 1] = true;
278 }
279 pi.pos_of.assign(R, 0);
280 for (std::size_t r : polled_list)
281 for (std::size_t p = 0; p < pi.M; ++p)
282 if (pi.ring[p] == r) {
283 pi.pos_of[r - 1] = p + 1;
284 break;
285 }
286 pi.canon_pos = pi.pos_of[polled_list[0] - 1];
287
288 bool distinct = false;
289 if (pi.ld) {
290 std::size_t first = 0;
291 for (std::size_t l = 1; l <= L; ++l) {
292 if (!pi.has_sw[l - 1]) continue;
293 if (first == 0) {
294 first = l;
295 pi.src_default = l;
296 continue;
297 }
298 if (!distinct) distinct = NetworkStruct<T>::polling_leg_differs(*legs[first - 1], *legs[l - 1]);
299 }
300 }
301 pi.ext = pi.table || pi.ld || pi.tmode != lang::PollingTimer::NONE;
302
303 pi.keep.assign(5 + R, false);
304 pi.keep[0] = anysw || pi.repeats;
305 pi.keep[1] = anysw;
307 pi.keep[3] = distinct;
308 pi.keep[4] = pi.tmode != lang::PollingTimer::NONE;
309 for (std::size_t r = 0; r < R; ++r)
310 pi.keep[5 + r] = pi.tmode == lang::PollingTimer::GLOBAL && pi.polled[r];
311 pi.loc.assign(5 + R, static_cast<std::size_t>(-1));
312 std::size_t c = 0;
313 for (std::size_t k = 0; k < 5 + R; ++k)
314 if (pi.keep[k]) pi.loc[k] = c++;
315 pi.ipos = pi.loc[0];
316 pi.iswk = pi.loc[1];
317 pi.ictr = pi.loc[2];
318 pi.isrc = pi.loc[3];
319 pi.itht = pi.loc[4];
320 pi.itrt.assign(pi.loc.begin() + 5, pi.loc.end());
321 pi.width = c;
322 std::size_t off = 0;
323 if (ind <= sn.nvars.size())
324 for (std::size_t j = 0; j < 2 * R && j < sn.nvars[ind - 1].size(); ++j)
325 off += sn.nvars[ind - 1][j];
326 pi.off = off;
327 return pi;
328}
329} // namespace qn
330} // namespace line
331
332#endif // LINE_LANG_QN_POLLING_INFO_H
InputError(const std::string &what)
Definition error.h:105
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
static bool polling_leg_differs(const lang::Distrib< T > &a, const lang::Distrib< T > &b)
True when two timed legs are not the same law (distinctLaws).
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
PollingOrder
Visit-order rules of a polling server, with the values of MATLAB PollingOrder.
Definition lang_types.h:404
@ THRESHOLD
a priority buffer above its threshold, else SKIPEMPTY
Definition lang_types.h:408
@ CYCLIC
fixed cyclic order (or the polling table)
Definition lang_types.h:405
PollingTimer
Visit-timer scopes of a polling server, with the values of MATLAB PollingTimer.
Definition lang_types.h:412
@ GLOBAL
a timed token: per-class target rotation timers
Definition lang_types.h:415
@ NONE
no visit timer
Definition lang_types.h:413
PollingType
Polling service disciplines, with the values of MATLAB PollingType.
Definition lang_types.h:391
@ EXHAUSTIVE
serve until the queue empties
Definition lang_types.h:393
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:524
bool process_is_markovian(ProcessType p)
ProcessType.isMarkovian: true when sn.proc carries an exact matrix representation of the law,...
Definition lang_types.h:713
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
Definition map_moment.h:89
void polling_require_ph(const PollingInfo< T > &pi)
Refuses a polling station whose switchover legs are not all phase-type.
PollingInfo< T > polling_info(const NetworkStruct< T > &sn, std::size_t ind)
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Matrix< T > D0
The (D0,D1) pair when the type carries one directly.
Definition lang_types.h:921
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D1
Definition map_moment.h:55
Matrix< T > D0
Definition map_moment.h:54
The polling controller of a POLLING station, keyed by station index.
std::size_t pk
the K of K-LIMITED
std::vector< lang::Distrib< T > > timer
std::vector< std::size_t > table
The extensions, as the Station fields of the same names.
Port of State.pollingInfo: the derived description of a polling controller.
std::vector< std::size_t > ring
1-based classes, length M
std::vector< std::vector< T > > t_pie
std::vector< std::size_t > itrt
std::vector< bool > swclass
the class drives a SWITCH event
lang::PollingOrder order
std::vector< bool > has_sw
std::vector< std::size_t > loc
std::vector< bool > t_nonph
std::vector< std::size_t > pos_of
1-based first position of each class
std::vector< std::size_t > ksw
std::vector< bool > keep
std::vector< bool > non_ph
std::vector< std::vector< T > > sw_pie
std::vector< std::size_t > tk
timer phases per class (0 = none)
std::size_t thr_class
1-based THRESHOLD class, 0 when none
lang::PollingTimer tmode
std::vector< Matrix< T > > sw_d0
std::vector< Matrix< T > > t_d1
std::vector< Matrix< T > > sw_d1
std::vector< bool > polled
std::vector< Matrix< T > > t_d0
lang::PollingType ptype