5#ifndef LINE_SOLVERS_NC_SOLVER_NC_OPEN_CDFRESPT_H
6#define LINE_SOLVERS_NC_SOLVER_NC_OPEN_CDFRESPT_H
66inline const char* nc_open_use() {
67 return " Use SolverMAM (single queue), SolverFluid, SolverLDES or SolverJMT for this model.";
72std::vector<std::vector<double> > nc_open_traffic(
const qn::NetworkStruct<T>& sn) {
73 const std::size_t M = sn.nstations, K = sn.nclasses;
74 std::vector<std::vector<double> > L(M, std::vector<double>(K, 0.0));
75 for (std::size_t c = 0; c < sn.nchains; ++c) {
76 std::vector<std::size_t> inchain;
77 for (std::size_t r = 0; r < K; ++r)
78 if (sn.chains[c][r]) inchain.push_back(r);
79 if (inchain.empty())
continue;
80 const std::size_t ref = sn.classes[inchain[0]].refstat - 1;
82 for (std::size_t r : inchain) {
83 if (sn.disabled[ref][r])
continue;
84 const double v = num_traits<T>::to_double(sn.rates(ref, r));
85 if (std::isfinite(v)) lamc += v;
87 if (!(lamc > 0.0))
continue;
88 const Matrix<T>& vis = sn.visits[c];
89 const std::size_t refsf = sn.stateful_of_station(ref + 1) - 1;
91 for (std::size_t r : inchain) vref += num_traits<T>::to_double(vis(refsf, r));
92 if (!(vref > 0.0))
continue;
93 for (std::size_t i = 0; i < M; ++i) {
94 const std::size_t isf = sn.stateful_of_station(i + 1) - 1;
95 for (std::size_t r : inchain) L[i][r] = lamc * num_traits<T>::to_double(vis(isf, r)) / vref;
103std::vector<bool> nc_open_feedback(
const qn::NetworkStruct<T>& sn) {
104 const std::size_t K = sn.nclasses;
105 const std::size_t nsf = sn.rt.rows() / K;
106 std::vector<std::vector<bool> > A(nsf, std::vector<bool>(nsf,
false));
107 for (std::size_t i = 0; i < nsf; ++i)
108 for (std::size_t j = 0; j < nsf; ++j) {
110 for (std::size_t r = 0; r < K && !any; ++r)
111 for (std::size_t s = 0; s < K && !any; ++s)
112 any = num_traits<T>::to_double(sn.rt(i * K + r, j * K + s)) > 0.0;
118 for (std::size_t i = 0; i < sn.nstations; ++i)
119 if (sn.stations[i].nodetype == qn::NodeType::Source) {
120 const std::size_t isf = sn.stateful_of_station(i + 1) - 1;
121 for (std::size_t p = 0; p < nsf; ++p) A[p][isf] =
false;
123 for (std::size_t k = 0; k < nsf; ++k)
124 for (std::size_t i = 0; i < nsf; ++i) {
125 if (!A[i][k])
continue;
126 for (std::size_t j = 0; j < nsf; ++j)
127 if (A[k][j]) A[i][j] =
true;
129 std::vector<bool> fb(sn.nstations,
false);
130 for (std::size_t i = 0; i < sn.nstations; ++i) {
131 const std::size_t isf = sn.stateful_of_station(i + 1) - 1;
144 return p == ProcessType::EXP || p == ProcessType::ERLANG || p == ProcessType::HYPEREXP ||
145 p == ProcessType::PH || p == ProcessType::APH || p == ProcessType::COXIAN ||
146 p == ProcessType::COX2;
162 "solver_nc_open_cdfrespt: the sojourn-time laws are matrix exponentials and transform "
163 "inversions on a logarithmic grid; this backend has no transcendental arithmetic");
166 const std::size_t M =
sn.nstations, K =
sn.nclasses;
167 const std::string use = detail::nc_open_use();
171 "solver_nc_open_cdfrespt serves open networks only.");
173 if (nd.
nodetype == qn::NodeType::Fork)
175 "getCdfRespT has no exact per-visit law for an open fork-join network." + use);
180 "getCdfRespT on an open network reads no load-, class- or joint-dependent rate." + use);
182 for (std::size_t r = 0; r < K; ++r) {
183 const std::size_t ref =
sn.classes[r].refstat - 1;
184 if (
sn.stations[ref].nodetype != qn::NodeType::Source ||
sn.disabled[ref][r])
continue;
188 "getCdfRespT on an open network needs Poisson arrivals; class '" +
sn.classes[r].name +
189 "' arrives as a non-exponential process, which breaks the product form." + use);
192 const std::vector<std::vector<double> > L = detail::nc_open_traffic(
sn);
193 const std::vector<bool> fb = detail::nc_open_feedback(
sn);
194 typedef std::function<std::vector<double>(
const std::vector<double>&)> Tab;
195 std::vector<std::vector<mam::Map<T> > > laws(M, std::vector<
mam::Map<T> >(K));
196 std::vector<std::vector<Tab> > tabs(M, std::vector<Tab>(K));
197 std::vector<std::vector<double> > means(M, std::vector<double>(K, 0.0));
198 for (std::size_t i = 0; i < M; ++i) {
200 if (st.
nodetype == qn::NodeType::Source)
continue;
201 const std::string name = st.
name;
202 std::vector<std::size_t> rpos;
203 for (std::size_t r = 0; r < K; ++r)
204 if (L[i][r] > 0.0) rpos.push_back(r);
205 if (rpos.empty())
continue;
206 bool finite = i <
sn.cap.size() && std::isfinite(
sn.cap[i]);
207 if (i <
sn.classcap.size())
208 for (std::size_t r : rpos)
209 if (r <
sn.classcap[i].size() && std::isfinite(
sn.classcap[i][r])) finite =
true;
212 "getCdfRespT: station '" + name +
213 "' has a finite buffer, which drops or blocks jobs and breaks the product form." + use);
215 auto need_ph = [&](std::size_t r) {
217 if (!detail::nc_open_is_renewal_ph(p))
219 "getCdfRespT: station '" + name +
"' serves class '" +
sn.classes[r].name +
222 if (st.
sched == SchedStrategy::INF) {
223 for (std::size_t r : rpos) {
228 }
else if (st.
sched == SchedStrategy::FCFS) {
229 double mumin = std::numeric_limits<double>::infinity(), mumax = 0.0;
230 for (std::size_t r : rpos) {
233 "getCdfRespT: FCFS station '" + name +
"' serves class '" +
sn.classes[r].name +
234 "' with a non-exponential law, which breaks the product form." + use);
236 mumin = std::min(mumin, mu);
237 mumax = std::max(mumax, mu);
239 if (mumax - mumin > 1e-9 * mumax)
241 "getCdfRespT: FCFS station '" + name +
242 "' has class-dependent service rates, which break the product form." + use);
243 const T mu =
sn.rates(i, rpos[0]);
245 if (!std::isfinite(cc) || cc <= 0.0) cc = 1.0;
248 for (std::size_t r : rpos) lsum += L[i][r];
252 "getCdfRespT: station '" + name +
"' is unstable, so it has no stationary response time.");
258 law.
D0(0, 0) = T(-gap);
260 law.
D0(1, 1) = T(-mu);
262 const T alpha[2] = {PC, T(one - PC)};
264 for (std::size_t p = 0; p < 2; ++p) {
265 const T ex = T(-(law.
D0(p, 0) + law.
D0(p, 1)));
266 for (std::size_t q = 0; q < 2; ++q) law.
D1(p, q) = T(ex * alpha[q]);
268 for (std::size_t r : rpos) {
272 }
else if (st.
sched == SchedStrategy::PS || st.
sched == SchedStrategy::LCFSPR) {
276 "getCdfRespT: " + disc +
" station '" + name +
"' has " +
277 std::to_string(
static_cast<long long>(c)) +
278 " servers; the exact sojourn law is known for a single server only." + use);
281 "getCdfRespT: jobs can return to " + disc +
" station '" + name +
282 "'; its sojourn depends on the arrivals after the tagged job, which feedback "
283 "makes correlated with the state it found, so no exact law is known." + use);
284 double lam = 0.0, rho = 0.0;
285 std::vector<mam::Map<T> > svk(K);
286 for (std::size_t r : rpos) {
294 "getCdfRespT: station '" + name +
"' is unstable, so it has no stationary response time.");
295 for (std::size_t r : rpos)
297 if (st.
sched == SchedStrategy::PS) {
299 std::vector<std::size_t> off(K, 0);
301 for (std::size_t r : rpos) {
303 n += svk[r].D0.rows();
305 std::vector<double> alpha(n, 0.0);
307 std::vector<std::vector<double> > pies(K);
308 for (std::size_t r : rpos) {
311 const std::size_t nk = svk[r].D0.rows();
312 for (std::size_t p = 0; p < nk; ++p) {
313 alpha[off[r] + p] = (L[i][r] / lam) * pies[r][p];
314 for (std::size_t q = 0; q < nk; ++q)
318 for (std::size_t r : rpos) {
319 std::vector<double> tag(n, 0.0);
320 for (std::size_t p = 0; p < pies[r].size(); ++p) tag[off[r] + p] = pies[r][p];
321 tabs[i][r] = [lam, alpha, Tm, tag](
const std::vector<double>& t) {
330 std::vector<Matrix<T> > D;
333 std::vector<qsys::MmapGk1Service<T> > svc;
334 for (std::size_t r : rpos) {
338 for (std::size_t j = 0; j < rpos.size(); ++j) {
339 tabs[i][rpos[j]] = [D, svc, j](
const std::vector<double>& t) {
343 std::vector<double> F(t.size(), 0.0);
344 for (std::size_t p = 0; p < t.size(); ++p)
352 "getCdfRespT on an open network supports FCFS, INF, PS and LCFS-PR stations; station '" +
354 ", whose per-visit sojourn law is not known in product form." + use);
361 for (std::size_t i = 0; i < M; ++i)
362 for (std::size_t r = 0; r < K; ++r) Tmax = std::max(Tmax, means[i][r]);
363 if (!(Tmax > 0.0))
return out;
364 auto eval = [&](std::size_t i, std::size_t r,
const std::vector<double>& t, std::vector<double>& F) {
365 if (laws[i][r].order() > 0) {
369 F.assign(t.size(), 0.0);
382 std::vector<double> tend(4), Fend(4, 1.0);
383 for (std::size_t j = 0; j < 4; ++j) tend[j] = Tmax * std::pow(10.0,
static_cast<double>(j + 1));
384 for (std::size_t i = 0; i < M; ++i)
385 for (std::size_t r = 0; r < K; ++r) {
386 std::vector<double> Fe;
387 if (!eval(i, r, tend, Fe))
continue;
388 for (std::size_t j = 0; j < 4; ++j) Fend[j] = std::min(Fend[j], Fe[j]);
390 std::size_t iend = 4;
391 for (std::size_t j = 0; j < 4; ++j)
398 std::cerr <<
"[LINE] Warning: the response time CDFs hold only " << Fend[3]
399 <<
" of their mass on the computed support.\n";
401 const std::size_t npts = 100;
402 const double lo = std::log10(Tmax) - 2.0, hi = std::log10(tend[iend]);
403 std::vector<double> tset(npts);
404 out.
tset.assign(npts, zero);
405 for (std::size_t j = 0; j < npts; ++j) {
406 tset[j] = std::pow(10.0, lo + (hi - lo) *
static_cast<double>(j) /
static_cast<double>(npts - 1));
409 for (std::size_t i = 0; i < M; ++i)
410 for (std::size_t r = 0; r < K; ++r) {
411 std::vector<double> F;
412 if (!eval(i, r, tset, F))
continue;
414 for (std::size_t j = 0; j < npts; ++j) {
416 A(j, 1) = out.
tset[j];
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Cumulative distribution of the inter-arrival time of a MAP.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
const char * sched_to_text(SchedStrategy s)
const char * process_to_text(ProcessType p)
The MATLAB ProcessType name, as sn.procid prints it.
T map_mean(const Map< T > &m)
Mean inter-arrival time, 1/lambda.
std::vector< T > map_cdf(const Map< T > &m, const std::vector< T > &points)
Cumulative distribution of the inter-arrival time at the given points.
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
OiOpenCdfRespT< T > solver_nc_open_cdfrespt(const qn::NetworkStruct< T > &sn)
Port of solver_nc_open_cdfrespt.m.
Mg1PsResult qsys_mg1_ps(double lambda, const std::vector< double > &alpha, const Matrix< double > &Tmat, const Mg1PsOptions &opt)
Sojourn-time distribution of the M/G/1 processor-sharing queue.
MmapGk1LcfsprResult< T > qsys_mmapgk1_lcfspr(const std::vector< Matrix< T > > &MMAP, const std::vector< MmapGk1Service< T > > &svc, const MmapGk1LcfsprOptions< T > &opt)
MMAP[K]/G[K]/1 LCFS-PR, per type, by Takine's matrix product form.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Sojourn-time distribution of the M/G/1 processor-sharing queue.
The MMAP[K]/G[K]/1 LCFS-PR queue: K customer types with class-dependent GENERAL service under preempt...
Per-visit response-time distributions of an OPEN network of order-independent (OI) and BCMP stations:...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
static constexpr double FineTol
A MAP as the pair of matrices (D0, D1).
Return value of solver_oi_open_cdfrespt: RD{ist,r} = [F(t) t] and the shared grid.
std::vector< std::vector< Matrix< T > > > RD
One job class of the network.
double population
infinite for an open class
One station of the network.
double nservers
may be infinite (a Delay, or an inf-scheduled task)
CdScaling< T > jdscaling
sn.jdscaling for this station: MATLAB's Station.ljdScaling, the JOINT dependence map eta_i(n),...
std::vector< T > lldscaling
sn.lldscaling for this station: the multiplier at population 1, 2, ... Empty when the station is not ...
CdScaling< T > cdscaling
sn.cdscaling for this station: the class-dependence map, empty when unset.
Options of qsys_mg1_ps, mirroring the reference's name-value pairs.
std::vector< double > tagAlpha
Initial vector of the TAGGED job's requirement, PH(tagAlpha, T), on the phase-type path only (empty: ...
std::vector< double > t
times to evaluate the sojourn distribution at
std::vector< double > cdfUncond
The MATLAB name-value options.
Return value of qsys_mmapgk1_lcfspr, mirroring the MATLAB struct.
std::vector< std::vector< T > > respCDF
[type][point], P(W_k <= t)
One marked type's service law, in any of the three forms the reference accepts: a Distribution object...