5#ifndef LINE_SOLVERS_MAM_SOLVER_MAM_TRAFFIC_H
6#define LINE_SOLVERS_MAM_SOLVER_MAM_TRAFFIC_H
145namespace traffic_detail {
163 if (v.size() < 3)
throw InputError(
"solver_mam_traffic: a split flow carries no marking");
167 m.Dc.assign(v.begin() + 2, v.end());
173Mmap<T> compress_or_refuse(
const Mmap<T>& m) {
174 if constexpr (num_traits<T>::has_transcendental) {
178 "solver_mam_traffic: the superposition passed config.space_max and must be compressed, "
179 "which needs transcendental arithmetic (aph2_fit); raise space_max or run at double");
185Mmap<T> silent_mmap(std::size_t nclasses) {
186 const T
zero = num_traits<T>::from_int(0);
190 m.Dc.assign(nclasses, Matrix<T>(1, 1, zero));
202Mmap<T> dep_cell(
const DepTable<T>& DEP, std::size_t i, std::size_t r) {
203 if (i >=
DEP.size() || r >= DEP[i].size())
return silent_mmap<T>(1);
204 const Map<T>& d =
DEP[i][r];
205 if (d.order() == 0)
return silent_mmap<T>(1);
206 for (std::size_t a = 0; a < d.D0.rows(); ++a)
207 for (std::size_t b = 0; b < d.D0.cols(); ++b)
208 if (npfqn::detail::num_isnan(d.D0(a, b)))
return silent_mmap<T>(1);
212 m.Dc.assign(1, d.D1);
226T total_rate(
const Mmap<T>& m) {
227 const T
zero = num_traits<T>::from_int(0);
229 for (std::size_t i = 0; i < m.D1.rows() && silent; ++i)
230 for (std::size_t j = 0; j < m.D1.cols(); ++j)
231 if (!(m.D1(i, j) == zero)) {
235 if (silent)
return zero;
243void reject_opaque_node(
const qn::NetworkStruct<T>& sn, std::size_t a,
bool fj_aware) {
245 const char* what =
nullptr;
247 case qn::NodeType::Router: what =
"Router";
break;
248 case qn::NodeType::Logger: what =
"Logger";
break;
249 case qn::NodeType::Cache: what =
"Cache";
break;
250 case qn::NodeType::Place: what =
"Place";
break;
251 case qn::NodeType::Transition: what =
"Transition";
break;
252 case qn::NodeType::Region: what =
"Region";
break;
253 case qn::NodeType::Fork:
254 case qn::NodeType::Join:
255 if (fj_aware)
return;
257 std::string(
"solver_mam_traffic: node '") + sn.nodes[a].name +
258 "' is a Fork or a Join, which the plain traffic step has no branch for; the "
259 "fork-join variant is solver_mam_traffic_mmap, which synchronizes the join with "
263 throw UnsupportedError(ErrorKind{
"NodeType"}, std::string(
"solver_mam_traffic: node '") + sn.nodes[a].name +
"' is a " +
265 " node. The reference's node switch has no branch for it, so it emits "
266 "no outgoing link and every node downstream of it is described as "
267 "receiving no traffic at all; that is a wrong traffic table with no "
268 "symptom, not a conservative approximation");
273namespace traffic_detail {
277 std::vector<bool> is_ncs;
278 std::vector<std::size_t> to_ncs;
279 std::vector<std::size_t> keep;
280 std::size_t inc_count = 0;
284NcsIndex build_ncs_index(
const qn::NetworkStruct<T>& sn) {
285 const std::size_t I = sn.nof_nodes(), R = sn.nclasses;
287 x.is_ncs.assign(I,
false);
288 x.to_ncs.assign(I, 0);
289 for (std::size_t a = 0; a < I; ++a) {
290 if (sn.nodes[a].nodetype == qn::NodeType::ClassSwitch)
continue;
292 x.to_ncs[a] = ++x.inc_count;
293 for (std::size_t r = 0; r < R; ++r) x.keep.push_back(a * R + r);
300Mmap<T> superpose_classes(
const DepTable<T>& DEP, std::size_t row, std::size_t R,
301 const TrafficConfig& config) {
302 Mmap<T> d = dep_cell(DEP, row, 0);
303 for (std::size_t r = 1; r < R; ++r) {
305 if (d.order() > config.space_max) d = compress_or_refuse(d);
312Matrix<T> split_probabilities(
const Matrix<T>& rtncs, std::size_t inc, std::size_t Inc,
314 Matrix<T>
P(R, Inc * R, num_traits<T>::from_int(0));
315 for (std::size_t r = 0; r < R; ++r)
316 for (std::size_t jnc = 0; jnc < Inc; ++jnc)
317 for (std::size_t s = 0; s < R; ++s)
318 P(r, jnc * R + s) = rtncs(inc * R + r, jnc * R + s);
327Mmap<T> no_flow_arrival(
const std::vector<std::vector<Mmap<T>>>& LINKS, std::size_t inc,
329 for (std::size_t jnc = 0; jnc < LINKS.size(); ++jnc)
330 if (LINKS[jnc][inc].order() != 0)
return LINKS[jnc][inc];
331 return silent_mmap<T>(R);
346 const std::size_t I =
sn.nof_nodes(), K =
sn.nclasses;
349 m.
node_sync.assign(I, std::vector<std::size_t>(I, 0));
350 for (
const std::pair<std::size_t, std::size_t>& fjp :
sn.fj) {
351 const std::size_t f = fjp.first - 1, j = fjp.second - 1;
352 const std::size_t gid = ++m.
ngroups;
355 for (std::size_t a = 0; a < I; ++a) {
356 if (a == f || a == j)
continue;
357 bool from_fork =
false, to_join =
false;
358 for (std::size_t k = 0; k < K; ++k) {
359 if (
sn.rtnodes(f * K + k, a * K + k) > zero) from_fork =
true;
360 if (
sn.rtnodes(a * K + k, j * K + k) > zero) to_join =
true;
362 if (from_fork && to_join) m.
node_sync[j][a] = gid;
382 namespace td = traffic_detail;
383 const std::size_t I =
sn.nof_nodes(), R =
sn.nclasses;
384 if (R == 0)
throw InputError(
"solver_mam_traffic: the model has no classes");
386 const td::NcsIndex x = td::build_ncs_index(
sn);
387 const std::size_t Inc = x.inc_count;
392 std::vector<std::vector<Mmap<T>>> LINKS(Inc, std::vector<
Mmap<T>>(Inc));
393 for (std::size_t a = 0; a < I; ++a) {
394 if (!x.is_ncs[a])
continue;
396 if (ty != qn::NodeType::Source && ty != qn::NodeType::Delay && ty != qn::NodeType::Queue) {
397 td::reject_opaque_node(
sn, a,
false);
400 const std::size_t inc = x.to_ncs[a] - 1;
401 const std::size_t ist =
sn.nodes[a].station;
403 throw InputError(
"solver_mam_traffic: node '" +
sn.nodes[a].name +
404 "' serves jobs but carries no station index");
405 const Mmap<T> dep = td::superpose_classes(DEP, ist - 1, R, config);
406 const Matrix<T> Psplit = td::split_probabilities(rtncs, inc, Inc, R);
407 const std::vector<npfqn::Mmap<T>> F =
409 for (std::size_t jnc = 0; jnc < Inc; ++jnc)
414 std::vector<Mmap<T>> ARV(I);
415 for (std::size_t a = 0; a < I; ++a) {
416 if (!x.is_ncs[a] ||
sn.nodes[a].nodetype == qn::NodeType::Source)
continue;
417 const std::size_t inc = x.to_ncs[a] - 1;
418 std::vector<Mmap<T>> flows;
419 for (std::size_t jnc = 0; jnc < Inc; ++jnc) {
420 const Mmap<T>& lk = LINKS[jnc][inc];
421 if (lk.
order() == 0)
continue;
427 else if (flows.size() == 1) ARV[a] = flows[0];
428 else ARV[a] = td::no_flow_arrival(LINKS, inc, R);
451 namespace td = traffic_detail;
452 const std::size_t I =
sn.nof_nodes(), R =
sn.nclasses;
453 if (R == 0)
throw InputError(
"solver_mam_traffic_mmap: the model has no classes");
455 throw InputError(
"solver_mam_traffic_mmap: the sync map is not indexed over the nodes");
457 const td::NcsIndex x = td::build_ncs_index(
sn);
458 const std::size_t Inc = x.inc_count;
461 std::vector<std::vector<std::size_t>> syncNCS(Inc, std::vector<std::size_t>(Inc, 0));
462 for (std::size_t a = 0; a < I; ++a) {
463 if (!x.is_ncs[a])
continue;
464 for (std::size_t b = 0; b < I; ++b)
465 if (x.is_ncs[b] && fjSyncMap.
node_sync[a][b] > 0)
466 syncNCS[x.to_ncs[a] - 1][x.to_ncs[b] - 1] = fjSyncMap.
node_sync[a][b];
469 std::vector<std::vector<Mmap<T>>> LINKS(Inc, std::vector<
Mmap<T>>(Inc));
470 for (std::size_t a = 0; a < I; ++a) {
471 if (!x.is_ncs[a])
continue;
473 if (ty != qn::NodeType::Source && ty != qn::NodeType::Delay &&
474 ty != qn::NodeType::Queue && ty != qn::NodeType::Fork && ty != qn::NodeType::Join) {
475 td::reject_opaque_node(
sn, a,
true);
478 const std::size_t inc = x.to_ncs[a] - 1;
479 const Mmap<T> dep = td::superpose_classes(DEP, a, R, config);
480 const Matrix<T> Psplit = td::split_probabilities(rtncs, inc, Inc, R);
481 const std::vector<npfqn::Mmap<T>> F =
483 for (std::size_t jnc = 0; jnc < Inc; ++jnc)
487 std::vector<Mmap<T>> ARV(I);
488 for (std::size_t a = 0; a < I; ++a) {
489 if (!x.is_ncs[a] ||
sn.nodes[a].nodetype == qn::NodeType::Source)
continue;
490 const std::size_t inc = x.to_ncs[a] - 1;
493 std::map<std::size_t, std::vector<Mmap<T>>> groups;
494 std::vector<Mmap<T>> independent;
495 for (std::size_t jnc = 0; jnc < Inc; ++jnc) {
496 const Mmap<T>& lk = LINKS[jnc][inc];
497 if (lk.
order() == 0)
continue;
500 const std::size_t gid = syncNCS[inc][jnc];
501 if (gid == 0) independent.push_back(lk);
502 else groups[gid].push_back(lk);
505 std::vector<Mmap<T>> flows;
506 for (
const auto& g : groups) {
507 Mmap<T> synced = g.second.front();
508 for (std::size_t f = 1; f < g.second.size(); ++f) {
510 if (synced.
order() > config.
space_max) synced = td::compress_or_refuse(synced);
512 flows.push_back(synced);
514 flows.insert(flows.end(), independent.begin(), independent.end());
517 else if (flows.size() == 1) ARV[a] = flows[0];
518 else ARV[a] = td::no_flow_arrival(LINKS, inc, R);
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
Stochastic complement of a DTMC partition, a port of matlab/lib/kpctoolbox/mc/dtmc_stochcomp....
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
The option and result types SolverMAM shares with its analyzers.
Maximum of two independent MAPs, and its marked k-stage generalization.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
Compression of a marked MAP into a smaller representation, and the two M3A primitives it is built fro...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
NodeType
Node kinds, with the values of MATLAB NodeType.
FjSyncMap sn_build_fj_sync_map(const qn::NetworkStruct< T > &sn)
sn_build_fj_sync_map.
Mmap< T > mmap_normalize(const Mmap< T > &in)
Clamp negative off-diagonal and per-class entries to zero and rebuild D1 and the diagonal of D0 from ...
Mmap< T > mmap_max(const Mmap< T > &a, const Mmap< T > &b, unsigned k)
MMAP of the maximum over k synchronization rounds of two independent MMAPs.
@ MixtureOrder1
'default', 'mixture', 'mixture.order1'
std::vector< std::vector< Map< T > > > DepTable
DEP{i,r}, the departure process of class r from i in (D0,D1) form.
TrafficConfig traffic_config(const MamOptions &opt)
The traffic step's view of SolverOptions('MAM').
std::vector< T > mmap_lambda(const Mmap< T > &m)
Alias kept for parity with the MATLAB name.
std::vector< Mmap< T > > solver_mam_traffic_mmap(const qn::NetworkStruct< T > &sn, const DepTable< T > &DEP, const TrafficConfig &config, const FjSyncMap &fjSyncMap)
Port of solver_mam_traffic_mmap.m, the fork-join aware traffic step.
Mmap< T > mmap_compress(const Mmap< T > &in, MmapCompressMethod method)
Compress an MMAP (mmap_compress.m).
Mmap< T > mmap_super(const Mmap< T > &a, const Mmap< T > &b)
Superposition of two MMAPs: the phase process is the product chain, and the class list of the result ...
std::vector< Mmap< T > > solver_mam_traffic(const qn::NetworkStruct< T > &sn, const DepTable< T > &DEP, const TrafficConfig &config)
Port of solver_mam_traffic.m.
Matrix< T > dtmc_stochcomp(const Matrix< T > &P, const std::vector< std::size_t > &keep)
Stochastic complement of a DTMC partition, a port of matlab/lib/kpctoolbox/mc/dtmc_stochcomp....
std::vector< Mmap< T > > npfqn_traffic_split_cs(const Mmap< T > &MMAP, const Matrix< T > &P)
Splitting of a marked MAP departure flow with class switching.
std::vector< Matrix< T > > Mmap
An MMAP as the MATLAB cell {D0, D1, D1^(1), ..., D1^(R)}.
mam::Mmap< T > npfqn_traffic_merge(const std::vector< mam::Mmap< T > > &flows, const MergeConfig &config=MergeConfig())
Merge a list of MMAPs carrying the same classes.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Superposition of several marked arrival flows into one.
Splitting of a marked MAP departure flow with class switching.
static constexpr double FineTol
sn_build_fj_sync_map: which incoming flows at a Join must be synchronized.
std::vector< std::size_t > join_of_group
1-based node index, by group
std::vector< std::size_t > fork_of_group
1-based node index, by group
std::vector< std::vector< std::size_t > > node_sync
The options SolverMAM reads.
An MMAP: the underlying MAP plus the per-class arrival matrices.
std::size_t classes() const
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
std::size_t order() const
The fields of options.config the traffic step reads.
std::size_t fj_sync_q_len
config.fj_sync_q_len, the join's synchronization queue; MATLAB's default.