LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1_psjf.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_MG1_PSJF_H
6#define LINE_API_QSYS_QSYS_MG1_PSJF_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * M/G/1 under PSJF (preemptive shortest job first).
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mg1_psjf.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mg1_psjf.java.
15 *
16 * Priority follows the original size, so a job of size x is delayed only by
17 * work of size at most x (Wierman and Harchol-Balter, SIGMETRICS 2003,
18 * Sec. 3.2):
19 *
20 * rho(x) = lambda int_0^x t f(t) dt
21 * m2(x) = lambda int_0^x t^2 f(t) dt
22 * E[T(x)] = x/(1-rho(x)) + m2(x)/(2 (1-rho(x))^2)
23 *
24 * As in qsys_mg1_fb, MATLAB has an exponential path -- closed-form truncated
25 * moments of the mixture, class mean by quadrature over [0, 20/mu_k] -- and a
26 * general path that integrates exactly over the two-moment fit of each class
27 * size law (qsys_sizequad.h), using the left/right limits rho-(x), rho+(x) so
28 * an atom is served as a preemptive priority class. The former general path
29 * ranked whole classes by their mean size, which is not PSJF.
30 *
31 * ARITHMETIC. exp and the adaptive quadrature make this transcendental.
32 */
33
34#include <algorithm>
35#include <cstddef>
36#include <numeric>
37#include <vector>
38
43#include "line/num/number.h"
44#include "line/util/error.h"
45
46namespace line {
47namespace qsys {
48
49namespace detail {
50
51/** E[T(x)] under PSJF for a mixture-of-exponentials job-size law. */
52template <class T>
53T mg1_psjf_response(const T& x, const std::vector<T>& mu, const std::vector<T>& p,
54 const T& lambda_total) {
55 const T one = num_traits<T>::from_int(1);
56 const T two = num_traits<T>::from_int(2);
57 T m1_x = num_traits<T>::from_int(0), m2_x = num_traits<T>::from_int(0);
58 for (std::size_t i = 0; i < mu.size(); ++i) {
59 const T mi = mu[i];
60 const T e = num_exp(T(-mi * x));
61 m1_x += p[i] * (one / mi - (one / mi + x) * e);
62 m2_x += p[i] * (two / (mi * mi) - (two / (mi * mi) + two * x / mi + x * x) * e);
63 }
64 const T rho_x = lambda_total * m1_x;
65 if (rho_x >= one)
66 throw NumericError(ErrorKind{"Instability"}, "qsys_mg1_psjf: truncated load reaches one, the mean is infinite");
67 return x / (one - rho_x) + lambda_total * m2_x / (two * (one - rho_x) * (one - rho_x));
68}
69
70} // namespace detail
71
72/**
73 * @brief M/G/1 under PSJF (preemptive shortest job first).
74 *
75 * @param lambda per-class arrival rates
76 * @param mu per-class service rates
77 * @param cs per-class coefficients of variation of the service time
78 */
79template <class T>
80Mg1DisciplineResult<T> qsys_mg1_psjf(const std::vector<T>& lambda, const std::vector<T>& mu,
81 const std::vector<T>& cs) {
83 "qsys_mg1_psjf requires transcendental arithmetic");
84 detail::mg1_discipline_check("qsys_mg1_psjf", lambda, mu, cs);
85 const std::size_t K = lambda.size();
86 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
87 const T two = num_traits<T>::from_int(2);
88 const T cs_tol = T(num_traits<T>::from_double(1e-6));
89
90 bool all_exp = true;
91 for (std::size_t i = 0; i < K; ++i)
92 if (!(num_abs(T(cs[i] - one)) < cs_tol)) all_exp = false;
93
95 r.W.assign(K, zero);
96
97 if (all_exp) {
98 T lambda_total = zero;
99 for (const T& v : lambda) lambda_total += v;
100 std::vector<T> p(K);
101 for (std::size_t i = 0; i < K; ++i) p[i] = lambda[i] / lambda_total;
102 const T reltol = T(num_traits<T>::from_double(1e-8));
103 const T abstol = T(num_traits<T>::from_double(1e-10));
104 for (std::size_t k = 0; k < K; ++k) {
105 const T mu_k = mu[k];
106 const T x_max = num_traits<T>::from_int(20) / mu_k;
107 r.W[k] = detail::num_integral<T>(
108 [&](const T& x) {
109 return detail::mg1_psjf_response(x, mu, p, lambda_total) * mu_k *
110 detail::num_exp(T(-mu_k * x));
111 },
112 zero, x_max, reltol, abstol);
113 }
114 } else {
115 // General service: exact over the two-moment fit of each class size
116 // law (MATLAB private/qsys_mg1_sizequad.m), evaluated in double.
117 std::vector<double> l(K), m(K), c(K);
118 for (std::size_t i = 0; i < K; ++i) {
119 l[i] = num_traits<T>::to_double(lambda[i]);
120 m[i] = num_traits<T>::to_double(mu[i]);
121 c[i] = num_traits<T>::to_double(cs[i]);
122 }
123 const std::vector<double> Wd = sizequad::solve(l, m, c, false);
124 for (std::size_t k = 0; k < K; ++k) r.W[k] = T(num_traits<T>::from_double(Wd[k]));
125 }
126 r.rhohat = detail::mg1_discipline_rhohat(lambda, r.W);
127 return r;
128}
129
130} // namespace qsys
131} // namespace line
132
133#endif // LINE_API_QSYS_QSYS_MG1_PSJF_H
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
std::vector< double > solve(const std::vector< double > &lambda, const std::vector< double > &mu, const std::vector< double > &cs, Mode mode)
Mg1DisciplineResult< T > qsys_mg1_psjf(const std::vector< T > &lambda, const std::vector< T > &mu, const std::vector< T > &cs)
M/G/1 under PSJF (preemptive shortest job first).
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_abs(const T &v)
Definition number.h:184
Number-type abstraction for the templated API port.
M/G/1 under SETF (shortest elapsed time first), the non-preemptive counterpart of FB/LAS.
Adaptive quadrature for the qsys functions whose MATLAB originals call integral(),...
Per-class mean response time of an M/G/1 queue under FB, PSJF or SRPT for general job sizes.
Shared return type and arithmetic helpers for the templated qsys port.
std::vector< T > W
per-class mean response time
T rhohat
Q/(1+Q) with Q = sum_k lambda_k W_k.