LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_qdamva.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_PFQN_QDAMVA_H
6#define LINE_API_PFQN_QDAMVA_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * QD-AMVA: queue-dependent approximate mean value analysis.
12 *
13 * Port of matlab/src/api/pfqn/pfqn_qdamva.m, the queue-dependent AMVA of
14 * Casale, Perez and Wang (IFIP PERFORMANCE 2015), on a closed multiclass
15 * product-form network.
16 *
17 * A Schweitzer/Bard core in which the class-r demand at station k is scaled by
18 * the queue-dependence term g_k evaluated at the ARRIVAL-INSTANT total queue
19 * length, `g = pfqn_lldfun(1 + delta * rowsum(Q), mu)`.
20 *
21 * SETTING `mu` TO A CONSTANT ROW RECOVERS PLAIN SCHWEITZER AMVA ONLY FOR A
22 * SINGLE CLASS. `pfqn_lldfun` does skip a constant row, so g == 1 there, but the
23 * residence time that remains is `1 + delta * rowsum(Q)` with ONE aggregate
24 * delta = (sum(N)-1)/sum(N) applied to the whole arrival-instant queue, where
25 * Bard-Schweitzer shrinks the TAGGED class alone:
26 *
27 * 1 + sum_{s != r} Q(k,s) + (N(r)-1)/N(r) * Q(k,r)
28 *
29 * The two coincide iff K == 1. Measured over 40 random three-class instances,
30 * pfqn_qdamva(L,N,Z,ones) departs from pfqn_bs by up to 0.217 in absolute queue
31 * length, and is the LESS accurate of the two on single-server multiclass models
32 * (mean relative error on Q 0.069 against 0.056 at R = 3), the aggregate delta
33 * buying nothing once g == 1. This is the QD-AMVA closure, not a defect of the
34 * port, but do not use the function as a Schweitzer oracle for K > 1.
35 *
36 * MU IS A DIMENSIONLESS RATE MULTIPLIER, NOT A RATE. mu(k,n) is the factor by
37 * which station k serves faster when it holds n jobs. Two traps follow from
38 * `pfqn_lldfun` and are the reference's, not this port's:
39 *
40 * - it SKIPS a station whose mu row is constant (its `range(...) > 0` gate),
41 * so a single-server station must be a row of ones and a c-server station
42 * `min(1..smax, c)`. Passing a c-server station a constant row silently
43 * returns g = 1, i.e. a single server.
44 * - smax = mu.cols() must be at least ceil(sum(N)) or the interpolation
45 * clamps the population and the top of the rate curve is never reached.
46 *
47 * Delay stations are carried in Z, not as rows of L. Closed classes only: an
48 * infinite N(r) is not supported.
49 *
50 * Arithmetic: TRANSCENDENTAL-GATED, inherited whole from `pfqn_lldfun` -- the
51 * softmin it evaluates is not an element of the field generated by the inputs.
52 */
53
54#include <algorithm>
55#include <cmath>
56#include <cstddef>
57#include <vector>
58
60#include "line/util/error.h"
61#include "line/util/matrix.h"
62#include "line/num/number.h"
63
64namespace line {
65namespace pfqn {
66
67/** What `pfqn_qdamva` returns: the fixed point and how it was reached. */
68template <class T>
70 Matrix<T> Q; ///< (M x R) mean queue lengths
71 Matrix<T> X; ///< (1 x R) per-class throughputs
72 Matrix<T> U; ///< (M x R) per-class utilizations, carrying the g scaling
73 Matrix<T> R; ///< (M x R) per-class residence times, Q = X .* R
74 std::size_t iter = 0;
75};
76
77/**
78 * @brief QD-AMVA: queue-dependent approximate mean value analysis.
79 *
80 * @param L (M x R) service demand matrix.
81 * @param N (R) population vector, finite.
82 * @param Z (R) think time vector; empty means no think time.
83 * @param mu (M x smax) queue-dependent rate multipliers; empty means none.
84 * @param Q0 (M x R) initial guess; empty means the reference's demand split.
85 * @param tol convergence tolerance on the queue lengths (default 1e-6).
86 * @param maxiter maximum number of iterations (default 10000).
87 */
88template <class T>
89QdAmvaResult<T> pfqn_qdamva(const Matrix<T>& L, const std::vector<T>& N, const std::vector<T>& Z,
90 const Matrix<T>& mu, const Matrix<T>& Q0, double tol = 1e-6,
91 std::size_t maxiter = 10000) {
93 "pfqn_qdamva requires transcendental arithmetic");
94 const std::size_t M = static_cast<std::size_t>(L.rows());
95 const std::size_t K = static_cast<std::size_t>(L.cols());
96 if (N.size() != K)
97 throw InputError("pfqn_qdamva: the population vector must have one entry per class");
98 if (!Z.empty() && Z.size() != K)
99 throw InputError("pfqn_qdamva: the think-time vector must have one entry per class");
100
101 const T zero = num_traits<T>::from_int(0);
102 const T one = num_traits<T>::from_int(1);
103
104 QdAmvaResult<T> out;
105 out.Q = Matrix<T>(M, K, zero);
106 out.X = Matrix<T>(1, K, zero);
107 out.U = Matrix<T>(M, K, zero);
108 out.R = Matrix<T>(M, K, zero);
109
110 T Ntot = zero;
111 for (std::size_t r = 0; r < K; ++r) Ntot += N[r];
112 // delta is undefined on an empty population, and the reference returns the
113 // zero queue rather than dividing by it.
114 if (!(num_traits<T>::to_double(Ntot) > 0.0)) return out;
115
116 if (Q0.rows() == static_cast<int>(M) && Q0.cols() == static_cast<int>(K)) {
117 out.Q = Q0;
118 } else {
119 // Ltot = 0 for a class with no demand anywhere: L/Ltot is a NaN the
120 // iteration never recovers from. Such a column arises routinely in a
121 // layered fixed point, where a caller can start with no work at the
122 // layer station, so the column is left at zero instead.
123 for (std::size_t r = 0; r < K; ++r) {
124 T tot = zero;
125 for (std::size_t k = 0; k < M; ++k) tot += L(k, r);
126 if (!(num_traits<T>::to_double(tot) > 0.0)) continue;
127 for (std::size_t k = 0; k < M; ++k) out.Q(k, r) = T(L(k, r) / tot * N[r]);
128 }
129 }
130
131 const T delta = T((Ntot - one) / Ntot);
132 // Q*10 as the sentinel, as the reference notes, stalls on an all-zero seed:
133 // the loop would exit before its first pass. Offset instead.
134 Matrix<T> Qprev = out.Q;
135 for (std::size_t k = 0; k < M; ++k)
136 for (std::size_t r = 0; r < K; ++r)
137 Qprev(k, r) = T(out.Q(k, r) + num_traits<T>::from_double(10.0 * (1.0 + tol)));
138
139 std::vector<T> Ak(M, zero);
140 while (out.iter < maxiter) {
141 double gap = 0.0;
142 for (std::size_t k = 0; k < M; ++k)
143 for (std::size_t r = 0; r < K; ++r)
144 gap = std::max(gap, std::fabs(num_traits<T>::to_double(out.Q(k, r)) -
145 num_traits<T>::to_double(Qprev(k, r))));
146 if (out.iter > 0 && gap <= tol) break;
147 ++out.iter;
148 Qprev = out.Q;
149
150 // The arrival-instant total queue length, class independent.
151 for (std::size_t k = 0; k < M; ++k) {
152 T rowsum = zero;
153 for (std::size_t r = 0; r < K; ++r) rowsum += out.Q(k, r);
154 Ak[k] = T(one + delta * rowsum);
155 }
156 const std::vector<T> g = pfqn_lldfun(Ak, mu, std::vector<double>());
157
158 for (std::size_t r = 0; r < K; ++r) {
159 for (std::size_t k = 0; k < M; ++k) {
160 T rowsum = zero;
161 for (std::size_t s = 0; s < K; ++s) rowsum += out.Q(k, s);
162 out.R(k, r) = T(L(k, r) * g[k] * (one + delta * rowsum));
163 }
164 T denom = Z.empty() ? zero : Z[r];
165 for (std::size_t k = 0; k < M; ++k) denom += out.R(k, r);
166 out.X(0, r) = (num_traits<T>::to_double(denom) > 0.0) ? T(N[r] / denom) : zero;
167 for (std::size_t k = 0; k < M; ++k) {
168 out.Q(k, r) = T(out.X(0, r) * out.R(k, r));
169 out.U(k, r) = T(L(k, r) * g[k] * out.X(0, r));
170 }
171 }
172 }
173 return out;
174}
175
176} // namespace pfqn
177} // namespace line
178
179#endif // LINE_API_PFQN_QDAMVA_H
InputError(const std::string &what)
Definition error.h:39
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
The exception types the port throws.
Dense matrix and non-owning view.
QdAmvaResult< T > pfqn_qdamva(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &mu, const Matrix< T > &Q0, double tol=1e-6, std::size_t maxiter=10000)
QD-AMVA: queue-dependent approximate mean value analysis.
Definition pfqn_qdamva.h:89
std::vector< T > pfqn_lldfun(const std::vector< T > &n, const Matrix< T > &lldscaling, const std::vector< double > &nservers)
AMVA-QD limited-load-dependence function.
Definition pfqn_lldfun.h:81
Number-type abstraction for the templated API port.
AMVA-QD limited-load-dependence function.
What pfqn_qdamva returns: the fixed point and how it was reached.
Definition pfqn_qdamva.h:69
Matrix< T > U
(M x R) per-class utilizations, carrying the g scaling
Definition pfqn_qdamva.h:72
Matrix< T > X
(1 x R) per-class throughputs
Definition pfqn_qdamva.h:71
Matrix< T > Q
(M x R) mean queue lengths
Definition pfqn_qdamva.h:70
Matrix< T > R
(M x R) per-class residence times, Q = X .* R
Definition pfqn_qdamva.h:73