LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_sizequad.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_API_QSYS_QSYS_SIZEQUAD_H
6#define LINE_API_QSYS_QSYS_SIZEQUAD_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Per-class mean response time of an M/G/1 queue under FB, PSJF or SRPT for
12 * general job sizes.
13 *
14 * Port of matlab/src/api/qsys/private/qsys_mg1_sizequad.m; the JAR twin is
15 * jline.api.qsys.QsysSizeQuad and the native Python one
16 * line_solver.api.qsys.scheduling._sizequad.
17 *
18 * Each class size law is the two-moment fit used by qsys_mg1_srpt
19 * (exponential, balanced-means H2, Tijms Erlang mixture) plus a point mass for
20 * cs = 0. With that law fixed the result is exact: the inner truncated moments
21 * are closed forms in the regularized incomplete gamma function, and the outer
22 * integral against each class density is a composite 20-point Gauss-Legendre
23 * rule on panels no wider than the smallest standard deviation of the
24 * components active there.
25 *
26 * FB: E[T(x)] = lambda E[min(S,x)^2]/(2(1-rho_x)^2) + x/(1-rho_x)
27 * PSJF: E[T(x)] = x/(1-rho-(x)) + lambda E[S^2;S<=x]/(2(1-rho-(x))(1-rho+(x)))
28 * SRPT: E[T(x)] = lambda E[min(S,x)^2]/(2(1-rho-(x))(1-rho+(x))) + int_0^x dt/(1-rho-(t))
29 * SJF: Wq(x) = lambda E[S^2]/(2(1-rho-(x))(1-rho+(x))) (Mode::SJF; Mode::SJFX gives x Wq(x))
30 *
31 * For SJF an atom of the size law is a non-preemptive priority class served
32 * FCFS (Cobham); elsewhere it is Phipps' (1-rho(x))^2 form.
33 *
34 * The SRPT residence integral is a 20-point Gauss-Legendre rule on panels with
35 * breakpoints at every atom and Erlang window edge, no wider than the smallest
36 * active standard deviation (ResGrid).
37 *
38 * ARITHMETIC. Double precision: the callers convert at the boundary, since
39 * the fitted law, not the arithmetic, bounds what the result means.
40 */
41
42#include <algorithm>
43#include <array>
44#include <cmath>
45#include <cstddef>
46#include <limits>
47#include <memory>
48#include <vector>
49
50#include <boost/math/special_functions/gamma.hpp>
51
52namespace line {
53namespace qsys {
54namespace sizequad {
55
56struct Comp {
57 double w; ///< weight (lambda-weighted when used as a load component)
58 int n; ///< Erlang order, 0 for a point mass
59 double a; ///< Erlang rate, or the atom location when n == 0
60};
61
62inline std::vector<Comp> sizefit(double mu, double cs) {
63 const double c2 = cs * cs, m = 1.0 / mu;
64 if (cs == 0.0) return {{1.0, 0, m}};
65 if (std::abs(c2 - 1.0) < 1e-9) return {{1.0, 1, mu}};
66 if (c2 > 1.0) {
67 const double p = 0.5 * (1.0 + std::sqrt((c2 - 1.0) / (c2 + 1.0)));
68 return {{p, 1, 2.0 * p * mu}, {1.0 - p, 1, 2.0 * (1.0 - p) * mu}};
69 }
70 const int k = static_cast<int>(std::ceil(1.0 / c2));
71 const double p = (1.0 / (1.0 + c2)) * (k * c2 - std::sqrt(k * (1.0 + c2) - double(k) * k * c2));
72 const double a = (k - p) / m;
73 if (p > 0) return {{p, k - 1, a}, {1.0 - p, k, a}};
74 return {{1.0, k, a}};
75}
76
77inline double gq(double n, double z) { return z <= 0.0 ? 1.0 : boost::math::gamma_q(n, z); }
78inline double gp(double n, double z) { return z <= 0.0 ? 0.0 : boost::math::gamma_p(n, z); }
79
80enum class Mode { FB, PSJF, SRPT, SJF, SJFX };
81
82static const double kGlx[10] = {0.07652652113349734, 0.22778585114164507, 0.37370608871541955,
83 0.5108670019508271, 0.636053680726515, 0.7463319064601508,
84 0.8391169718222188, 0.912234428251326, 0.9639719272779138,
85 0.993128599185095};
86static const double kGlw[10] = {0.15275338713072628, 0.14917298647260424, 0.1420961093183824,
87 0.1316886384491769, 0.1181945319615186, 0.1019301198172407,
88 0.08327674157670471, 0.06267204833410879, 0.040601429800386446,
89 0.017614007139150893};
90
91inline std::array<double, 3> window(double n, double a) {
92 const double sd = std::sqrt(n) / a, mn = n / a;
93 return {std::max(0.0, mn - 15.0 * sd), mn + 15.0 * sd + 40.0 / a, sd};
94}
95
96/** rho-(t) = lambda E[S; S < t] for the lambda-weighted components. */
97inline double rho_minus(const std::vector<Comp>& comps, double t) {
98 double r = 0.0;
99 for (const Comp& c : comps) {
100 if (c.n == 0) {
101 if (c.a < t) r += c.w * c.a;
102 } else {
103 r += c.w * c.n / c.a * gp(c.n + 1.0, c.a * t);
104 }
105 }
106 return r;
107}
108
109/** int_u^v dt/(1-rho-(t)), 20-point Gauss-Legendre. */
110inline double gl_sum(const std::vector<Comp>& comps, double u, double v) {
111 const double hw = (v - u) / 2.0, ctr = (u + v) / 2.0;
112 double I = 0.0;
113 for (int g = 0; g < 10; ++g) {
114 I += kGlw[g] * hw / (1.0 - rho_minus(comps, ctr - hw * kGlx[g]));
115 I += kGlw[g] * hw / (1.0 - rho_minus(comps, ctr + hw * kGlx[g]));
116 }
117 return I;
118}
119
120/**
121 * Panel edges P and R(P) = int_0^P dt/(1-rho-(t)) for the SRPT residence
122 * integral: breakpoints at every atom and Erlang window edge, panels no wider
123 * than the smallest standard deviation of the Erlang components active there
124 * (one panel where none is, rho- being constant between atoms).
125 */
126struct ResGrid {
127 std::vector<double> P, C;
128
129 explicit ResGrid(const std::vector<Comp>& comps) {
130 std::vector<double> bp{0.0};
131 std::vector<std::array<double, 3>> erl;
132 for (const Comp& c : comps) {
133 if (c.n == 0) {
134 bp.push_back(c.a);
135 } else {
136 const auto w = window(c.n, c.a);
137 erl.push_back(w);
138 bp.push_back(w[0]);
139 bp.push_back(w[1]);
140 }
141 }
142 std::sort(bp.begin(), bp.end());
143 bp.erase(std::unique(bp.begin(), bp.end()), bp.end());
144 P.push_back(bp[0]);
145 for (std::size_t s = 0; s + 1 < bp.size(); ++s) {
146 const double u = bp[s], v = bp[s + 1];
147 double sc = std::numeric_limits<double>::infinity();
148 for (const auto& e : erl)
149 if (e[0] < v && e[1] > u) sc = std::min(sc, e[2]);
150 const int npan = std::isinf(sc) ? 1 : std::max(1, static_cast<int>(std::ceil((v - u) / sc)));
151 for (int p = 1; p <= npan; ++p) P.push_back(u + (v - u) * p / npan);
152 }
153 C.assign(P.size(), 0.0);
154 for (std::size_t i = 1; i < P.size(); ++i) C[i] = C[i - 1] + gl_sum(comps, P[i - 1], P[i]);
155 }
156
157 /** R(x): cumulative value at the start of the panel holding x plus a rule up to x. */
158 double residence(const std::vector<Comp>& comps, double x) const {
159 if (!(x > 0.0)) return 0.0;
160 std::size_t i = 0;
161 while (i + 2 < P.size() && P[i + 1] < x) ++i;
162 return C[i] + gl_sum(comps, P[i], x);
163 }
164};
165
166inline double response(Mode mode, const std::vector<Comp>& comps, double x, const ResGrid* grid) {
167 double I0 = 0, I1 = 0, M1m = 0, M1p = 0, M2p = 0, Tl = 0;
168 double W0 = 0; // lambda E[S^2]/2, the mean residual work (SJF)
169 for (const Comp& c : comps) {
170 const double lw = c.w, a = c.a;
171 if (c.n == 0) {
172 const double t = std::min(x, a);
173 I0 += lw * t;
174 I1 += lw * t * t / 2.0;
175 if (a < x) M1m += lw * a;
176 if (a <= x) {
177 M1p += lw * a;
178 M2p += lw * a * a;
179 }
180 if (a > x) Tl += lw;
181 W0 += lw * a * a / 2.0;
182 } else {
183 const double n = c.n, z = a * x;
184 const double Q = gq(n, z), p1 = gp(n + 1, z), p2 = gp(n + 2, z);
185 I0 += lw * (x * Q + n / a * p1);
186 I1 += lw * (x * x / 2.0 * Q + n * (n + 1) / (2.0 * a * a) * p2);
187 M1m += lw * n / a * p1;
188 M1p += lw * n / a * p1;
189 M2p += lw * n * (n + 1) / (a * a) * p2;
190 Tl += lw * Q;
191 W0 += lw * n * (n + 1) / (2.0 * a * a);
192 }
193 }
194 if (mode == Mode::FB) return I1 / ((1.0 - I0) * (1.0 - I0)) + x / (1.0 - I0);
195 if (mode == Mode::SJF) return W0 / ((1.0 - M1m) * (1.0 - M1p));
196 if (mode == Mode::SJFX) return x * W0 / ((1.0 - M1m) * (1.0 - M1p));
197 if (mode == Mode::SRPT)
198 return (M2p + x * x * Tl) / (2.0 * (1.0 - M1m) * (1.0 - M1p)) + grid->residence(comps, x);
199 return x / (1.0 - M1m) + M2p / (2.0 * (1.0 - M1m) * (1.0 - M1p));
200}
201
202inline double outer(Mode mode, const std::vector<Comp>& comps, int n, double a, const ResGrid* grid) {
203 const double* glx = kGlx;
204 const double* glw = kGlw;
205 const auto win = window(n, a);
206 const double lo = win[0], hi = win[1];
207 std::vector<double> bp{lo, hi};
208 std::vector<std::array<double, 3>> erl;
209 for (const Comp& c : comps) {
210 if (c.n == 0) {
211 if (c.a > lo && c.a < hi) bp.push_back(c.a);
212 } else {
213 const auto w = window(c.n, c.a);
214 erl.push_back(w);
215 if (w[0] > lo && w[0] < hi) bp.push_back(w[0]);
216 if (w[1] > lo && w[1] < hi) bp.push_back(w[1]);
217 }
218 }
219 std::sort(bp.begin(), bp.end());
220 bp.erase(std::unique(bp.begin(), bp.end()), bp.end());
221 const double lg = std::lgamma(double(n));
222 double W = 0.0;
223 for (std::size_t s = 0; s + 1 < bp.size(); ++s) {
224 const double u = bp[s], v = bp[s + 1];
225 double sc = std::numeric_limits<double>::infinity();
226 for (const auto& e : erl)
227 if (e[0] < v && e[1] > u) sc = std::min(sc, e[2]);
228 const int npan = std::max(1, static_cast<int>(std::ceil((v - u) / sc)));
229 const double h = (v - u) / npan, hw = h / 2.0;
230 for (int p = 0; p < npan; ++p) {
231 const double ctr = u + (p + 0.5) * h;
232 for (int g = 0; g < 10; ++g) {
233 for (int sgn = -1; sgn <= 1; sgn += 2) {
234 const double xs = ctr + sgn * hw * glx[g];
235 const double f = a * std::exp((n - 1) * std::log(a * xs) - a * xs - lg);
236 W += glw[g] * hw * response(mode, comps, xs, grid) * f;
237 }
238 }
239 }
240 }
241 return W;
242}
243
244inline std::vector<double> solve(const std::vector<double>& lambda, const std::vector<double>& mu,
245 const std::vector<double>& cs, Mode mode) {
246 const std::size_t K = lambda.size();
247 std::vector<std::vector<Comp>> fits(K);
248 std::vector<Comp> comps;
249 for (std::size_t i = 0; i < K; ++i) {
250 fits[i] = sizefit(mu[i], cs[i]);
251 for (const Comp& c : fits[i]) comps.push_back({lambda[i] * c.w, c.n, c.a});
252 }
253 std::unique_ptr<ResGrid> grid;
254 if (mode == Mode::SRPT) grid.reset(new ResGrid(comps));
255 std::vector<double> W(K, 0.0);
256 for (std::size_t i = 0; i < K; ++i)
257 for (const Comp& c : fits[i])
258 W[i] += c.w * (c.n == 0 ? response(mode, comps, c.a, grid.get())
259 : outer(mode, comps, c.n, c.a, grid.get()));
260 return W;
261}
262
263/** fb = true for FB, false for PSJF. */
264inline std::vector<double> solve(const std::vector<double>& lambda, const std::vector<double>& mu,
265 const std::vector<double>& cs, bool fb) {
266 return solve(lambda, mu, cs, fb ? Mode::FB : Mode::PSJF);
267}
268
269} // namespace sizequad
270} // namespace qsys
271} // namespace line
272
273#endif // LINE_API_QSYS_QSYS_SIZEQUAD_H
std::array< double, 3 > window(double n, double a)
double outer(Mode mode, const std::vector< Comp > &comps, int n, double a, const ResGrid *grid)
std::vector< double > solve(const std::vector< double > &lambda, const std::vector< double > &mu, const std::vector< double > &cs, Mode mode)
double gp(double n, double z)
double gq(double n, double z)
static const double kGlw[10]
double rho_minus(const std::vector< Comp > &comps, double t)
rho-(t) = lambda E[S; S < t] for the lambda-weighted components.
double response(Mode mode, const std::vector< Comp > &comps, double x, const ResGrid *grid)
static const double kGlx[10]
double gl_sum(const std::vector< Comp > &comps, double u, double v)
int_u^v dt/(1-rho-(t)), 20-point Gauss-Legendre.
std::vector< Comp > sizefit(double mu, double cs)
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
double w
weight (lambda-weighted when used as a load component)
double a
Erlang rate, or the atom location when n == 0.
int n
Erlang order, 0 for a point mass.
Panel edges P and R(P) = int_0^P dt/(1-rho-(t)) for the SRPT residence integral: breakpoints at every...
std::vector< double > P
std::vector< double > C
double residence(const std::vector< Comp > &comps, double x) const
R(x): cumulative value at the start of the panel holding x plus a rule up to x.
ResGrid(const std::vector< Comp > &comps)