LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
rr_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_RR_INFO_H
6#define LINE_LANG_QN_RR_INFO_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * `State.rrInfo`: the quantum-phase block of a round-robin station.
12 *
13 * Round robin with a finite quantum (Haverkort 1998, Sect. 6.4) is FCFS with a
14 * time slice: the job in service runs until its service completes or its
15 * quantum expires; on expiry it is preempted (resume, its service phase kept)
16 * and rejoins the TAIL of the queue, and a fresh quantum starts at every entry
17 * into service. There is no switch overhead.
18 *
19 * An RR station keeps the FCFS preempt-resume layout [buffer | server | vars]
20 * (the buffer holds right-aligned (class, phase) pairs, the oldest job on the
21 * right) and adds, in the shared node-block column group `nvars[ind-1][2R]`,
22 * the joint counts m(r,k,q) of the class-r jobs IN SERVICE whose service is in
23 * phase k and whose current quantum is in phase q:
24 *
25 * col(r,k,q) = off[r] + k*Q[r] + q (0-based), off[r] = sum_{s<r} K[s]*Q[s]
26 *
27 * The server counts of the layout are sum_q m(r,k,q); the block refines them.
28 *
29 * IT LIVES IN ITS OWN HEADER for the reason `polling_info.h` does: `from_marginal`
30 * (state.h) enumerates the block and the event handlers (state_events.h) move
31 * it, and neither header can include the other.
32 */
33
34#include <cstddef>
35#include <vector>
36
40#include "line/util/error.h"
41#include "line/util/matrix.h"
42
43namespace line {
44namespace qn {
45
46/**
47 * The derived description of an RR station's quantum block, MATLAB's `rinfo`.
48 *
49 * K, Q per-class service and quantum phase counts
50 * qT, qt quantum sub-generator D0 (Q x Q) and exit-rate vector -D0*1
51 * qa quantum initial probability vector
52 * markovian false for a quantum with no renewal phase-type form (Det,
53 * Uniform, MAP, ME, ...), sized as one placeholder phase so the
54 * model still builds; the state-space handlers refuse it
55 * off 0-based block offset of each class, width the block width
56 * boff 0-based column of the block inside the local-variable block,
57 * the sum of the modulation and routing columns
58 */
59template <class T>
60struct RrInfo {
61 std::vector<std::size_t> K, Q, off;
62 std::vector<Matrix<T>> qT;
63 std::vector<std::vector<T>> qt, qa;
64 std::vector<bool> markovian;
65 std::size_t width = 0;
66 std::size_t boff = 0;
67 bool valid = false;
68
69 /** 0-based column of m(r,k,q) inside the block; r, k, q 0-based. */
70 std::size_t col(std::size_t r, std::size_t k, std::size_t q) const { return off[r] + k * Q[r] + q; }
71};
72
73/** True for the renewal phase-type families a quantum can be walked in. */
75 switch (p) {
83 return true;
84 default:
85 return false;
86 }
87}
88
89/**
90 * Port of `State.rrInfo`. Returns an invalid description for a node that is
91 * not an RR station. A class with no quantum raises LINE:input:Model, as the
92 * reference does: the slice sizes the state and has no meaningful default.
93 */
94template <class T>
95RrInfo<T> rr_info(const NetworkStruct<T>& sn, std::size_t ind) {
96 RrInfo<T> ri;
97 if (ind == 0 || ind > sn.nodes.size()) return ri;
98 const std::size_t ist = sn.nodes[ind - 1].station;
99 if (ist == 0 || sn.stations[ist - 1].sched != lang::SchedStrategy::RR) return ri;
100 const std::size_t R = sn.nclasses;
101 const Station<T>& st = sn.stations[ist - 1];
102 ri.valid = true;
103 ri.K.assign(R, 1);
104 ri.Q.assign(R, 1);
105 ri.off.assign(R, 0);
106 ri.qT.resize(R);
107 ri.qt.resize(R);
108 ri.qa.resize(R);
109 ri.markovian.assign(R, true);
110 const T one = num_traits<T>::from_int(1), zero = num_traits<T>::from_int(0);
111 for (std::size_t r = 0; r < R; ++r) {
112 ri.K[r] = sn.phasessz_of(ist, r + 1);
113 // Raises LINE:input:Model for a class with no quantum.
114 (void)sn.rr_quantum_phases(ist, r + 1);
115 const lang::Distrib<T>& d = st.quantum[r];
116 // Only a renewal phase-type quantum has a state-space form: a MAP or
117 // MMPP2 would carry a phase across quanta, and ME/RAP have no
118 // probabilistic phases.
119 if (!rr_quantum_is_ph(d.type) || d.D0.rows() == 0) {
120 ri.markovian[r] = false;
121 ri.Q[r] = 1;
122 ri.qT[r] = Matrix<T>(1, 1, -one);
123 ri.qt[r] = std::vector<T>(1, one);
124 ri.qa[r] = std::vector<T>(1, one);
125 continue;
126 }
127 const std::size_t Q = d.D0.rows();
128 ri.Q[r] = Q;
129 ri.qT[r] = d.D0;
130 ri.qt[r].assign(Q, zero);
131 for (std::size_t i = 0; i < Q; ++i) {
132 T s = zero;
133 for (std::size_t j = 0; j < Q; ++j) s += d.D0(i, j);
134 ri.qt[r][i] = -s;
135 }
136 mam::Map<T> mp;
137 mp.D0 = d.D0;
138 mp.D1 = d.D1;
139 ri.qa[r] = mam::map_pie(mp);
140 }
141 std::size_t c = 0;
142 for (std::size_t r = 0; r < R; ++r) {
143 ri.off[r] = c;
144 c += ri.K[r] * ri.Q[r];
145 }
146 ri.width = c;
147 if (ind <= sn.nvars.size())
148 for (std::size_t j = 0; j < 2 * R && j < sn.nvars[ind - 1].size(); ++j) ri.boff += sn.nvars[ind - 1][j];
149 return ri;
150}
151
152/** Refuses an RR station whose quantum is not phase-type (LINE:unsupported:Det). */
153template <class T>
154void rr_require_ph(const RrInfo<T>& ri) {
155 for (bool b : ri.markovian)
156 if (!b)
157 throw UnsupportedError(ErrorKind{"Det"},
158 "Round-robin with a non-phase-type quantum (e.g. Det) has no state-space form; "
159 "use a phase-type quantum (Exp, Erlang, HyperExp, APH, Coxian, PH) or a "
160 "simulation solver.");
161}
162
163} // namespace qn
164} // namespace line
165
166#endif // LINE_LANG_QN_RR_INFO_H
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
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.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:524
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
bool rr_quantum_is_ph(lang::ProcessType p)
True for the renewal phase-type families a quantum can be walked in.
Definition rr_info.h:74
void rr_require_ph(const RrInfo< T > &ri)
Refuses an RR station whose quantum is not phase-type (LINE:unsupported:Det).
Definition rr_info.h:154
RrInfo< T > rr_info(const NetworkStruct< T > &sn, std::size_t ind)
Port of State.rrInfo.
Definition rr_info.h:95
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 derived description of an RR station's quantum block, MATLAB's rinfo.
Definition rr_info.h:60
std::size_t width
Definition rr_info.h:65
std::vector< std::vector< T > > qt
Definition rr_info.h:63
std::vector< std::vector< T > > qa
Definition rr_info.h:63
std::vector< std::size_t > off
Definition rr_info.h:61
std::vector< std::size_t > K
Definition rr_info.h:61
std::size_t col(std::size_t r, std::size_t k, std::size_t q) const
0-based column of m(r,k,q) inside the block; r, k, q 0-based.
Definition rr_info.h:70
std::vector< std::size_t > Q
Definition rr_info.h:61
std::size_t boff
Definition rr_info.h:66
std::vector< bool > markovian
Definition rr_info.h:64
std::vector< Matrix< T > > qT
Definition rr_info.h:62
One station of the network.
std::vector< Distrib< T > > quantum
Queue.setQuantum(class, distrib): the per-class time slice of a round-robin (SchedStrategy::RR) stati...