5#ifndef LINE_SOLVERS_CTMC_SOLVER_CTMC_ANALYZER_H
6#define LINE_SOLVERS_CTMC_SOLVER_CTMC_ANALYZER_H
95 double nominal = std::numeric_limits<double>::quiet_NaN();
282 return {
"default",
"exact",
"mdd"};
287 return method ==
"mdd";
300 if (std::find(valid.begin(), valid.end(), method) == valid.end())
302 "' method is unsupported by this solver");
305 "' method does not enumerate a state space and is served by its "
306 "own analyzer, not by solver_ctmc_analyzer");
337 for (std::size_t i = 0; i <
sn.stations.size(); ++i) {
338 if (
sn.stations[i].cdscaling &&
sn.stations[i].cdscalingpeak.empty())
340 "SolverCTMC: station '" +
sn.stations[i].name +
341 "' declares class-dependent service without a peak rate. Utilization at a "
342 "class-dependent station is reported as T/mu/peak, so pass the peak to "
343 "setClassDependence");
344 if (
sn.stations[i].jdscaling &&
sn.stations[i].jdscalingpeak.empty())
346 "SolverCTMC: station '" +
sn.stations[i].name +
347 "' declares joint-dependent service without a peak rate; pass the peak to "
348 "setJointDependence");
370namespace analyzer_detail {
388std::vector<std::size_t> resolve_cutoff(
const NetworkStruct<T>&
sn,
const CtmcOptions&
opt) {
389 const std::size_t M =
sn.nstations, K =
sn.nclasses;
390 std::vector<std::size_t> cut(K, 0);
391 bool any_open =
false;
392 for (std::size_t k = 0; k < K; ++k)
393 if (!std::isfinite(
sn.njobs()[k])) any_open =
true;
394 if (!any_open)
return cut;
396 if (!
opt.cutoff_mat.empty()) {
400 if (
opt.cutoff_mat.size() != M)
401 throw InputError(
"SolverCTMC: the cutoff matrix must have one row per station");
402 for (std::size_t k = 0; k < K; ++k) {
403 if (std::isfinite(
sn.njobs()[k]))
continue;
404 for (std::size_t i = 0; i < M; ++i) {
405 if (
opt.cutoff_mat[i].size() != K)
407 "SolverCTMC: the cutoff matrix must have one column per class");
408 cut[k] = std::max(cut[k],
opt.cutoff_mat[i][k]);
413 if (!opt.cutoff_vec.empty()) {
414 if (opt.cutoff_vec.size() != K)
415 throw InputError(
"SolverCTMC: the per-class cutoff must have one entry per class");
416 return opt.cutoff_vec;
419 if (opt.cutoff > 0 && std::isfinite(opt.cutoff)) {
420 c =
static_cast<std::size_t
>(opt.cutoff);
421 }
else if (opt.cutoff < 0) {
424 const double e = 1.0 /
static_cast<double>(M * K);
425 c =
static_cast<std::size_t
>(std::ceil(std::pow(6000.0, e)));
428 for (std::size_t k = 0; k < K; ++k)
429 if (!std::isfinite(sn.njobs()[k])) cut[k] = c;
446bool set_retrieval_truncation(
const NetworkStruct<T>& sn,
const std::vector<std::size_t>& cutoff,
447 NetworkStruct<T>& out) {
449 for (
typename std::map<std::size_t, qn::CacheParam<T>>::const_iterator ci =
450 sn.nodeparam.begin();
451 ci != sn.nodeparam.end(); ++ci)
452 if (ci->second.retrieval_capacity > 0 && !ci->second.retrieval_classes.empty()) any =
true;
453 if (!any)
return false;
455 if (sn.nclosedjobs() > 0) {
456 lvl =
static_cast<long>(sn.nclosedjobs()) - 1;
459 for (std::size_t k = 0; k < cutoff.size(); ++k) mx = std::max(mx, cutoff[k]);
460 lvl =
static_cast<long>(mx) - 1;
462 if (lvl < 0) lvl = 0;
464 for (
typename std::map<std::size_t, qn::CacheParam<T>>::iterator ci = out.nodeparam.begin();
465 ci != out.nodeparam.end(); ++ci)
466 if (ci->second.retrieval_capacity > 0) ci->second.max_pending_retrieval = lvl;
494bool annihilate_signal_capacity(
const NetworkStruct<T>& sn, NetworkStruct<T>& out) {
495 std::vector<bool> annihilated(sn.nclasses,
false);
497 for (std::size_t k = 0; k < sn.nclasses && k < sn.issignal.size(); ++k) {
498 if (!sn.issignal[k])
continue;
500 annihilated[k] =
true;
503 if (!any)
return false;
505 for (std::size_t i = 0; i < out.stations.size() && i < out.classcap.size(); ++i) {
508 if (out.stations[i].sched == SchedStrategy::EXT)
continue;
509 for (std::size_t k = 0; k < annihilated.size() && k < out.classcap[i].size(); ++k)
510 if (annihilated[k]) out.classcap[i][k] = 0.0;
551solvers::CacheMetrics<T> cache_metrics(
const NetworkStruct<T>& sn,
const CtmcResult<T>& r,
552 const std::vector<T>& pi,
const Matrix<T>& QN) {
553 solvers::CacheMetrics<T> out =
555 std::vector<T>(), Matrix<T>(), Matrix<T>(), std::vector<T>());
556 if (out.caches.empty() || r.space.empty())
return out;
557 const std::size_t K = sn.nclasses, ns = r.space.size();
558 const double dnan = std::numeric_limits<double>::quiet_NaN();
560 for (std::size_t c = 0; c < out.caches.size(); ++c) {
561 solvers::CacheNodeMetrics<T>& m = out.caches[c];
562 const std::size_t isf = sn.stateful_index(m.node);
563 if (isf == 0)
continue;
564 const qn::CacheParam<T>& cp = sn.nodeparam.find(m.node)->second;
566 std::vector<double> tn(K, 0.0);
567 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
568 const double p = num_traits<T>::to_double(pi[s]);
569 if (p == 0)
continue;
570 if (isf - 1 >= r.dep_rates[s].size())
continue;
571 for (std::size_t k = 0; k < K && k < r.dep_rates[s][isf - 1].size(); ++k)
572 tn[k] += p * num_traits<T>::to_double(r.dep_rates[s][isf - 1][k]);
575 const std::size_t n = cp.nitems;
577 for (std::size_t l = 0; l < cp.itemcap.size(); ++l)
578 if (cp.itemcap[l] > 0) tcc +=
static_cast<std::size_t
>(cp.itemcap[l]);
579 std::vector<std::size_t> rcl, rci, rco;
581 const std::size_t lvw = r.space[0].local[isf - 1].size();
582 const bool retr = !rcl.empty() && lvw >= tcc + n + rcl.size();
583 std::vector<double> drate(K, 0.0), dclass(K, 0.0), inflight(K, 0.0);
592 const std::size_t tail = cp.retrieval_capacity > 0 ? n + rcl.size() : 0;
593 if (n > 0 && tcc > 0 && lvw >= tcc + tail) {
594 const std::size_t coff = lvw - (tcc + tail);
595 const std::size_t h = cp.itemcap.size();
596 Matrix<T> ip(n, h + 1);
597 std::vector<double> acc(n * h, 0.0);
598 std::vector<char> present(n, 0);
599 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
600 const double p = num_traits<T>::to_double(pi[s]);
601 if (p == 0)
continue;
602 const std::vector<T>& lv = r.space[s].local[isf - 1];
603 std::size_t off = coff;
604 for (std::size_t l = 0; l < h; ++l) {
605 const std::size_t cap =
606 cp.itemcap[l] > 0 ?
static_cast<std::size_t
>(cp.itemcap[l]) : 0;
607 std::fill(present.begin(), present.end(), 0);
608 for (std::size_t q = 0; q < cap; ++q) {
609 const long it =
static_cast<long>(num_traits<T>::to_double(lv[off + q]));
610 if (it >= 1 &&
static_cast<std::size_t
>(it) <= n)
611 present[
static_cast<std::size_t
>(it) - 1] = 1;
613 for (std::size_t i = 0; i < n; ++i)
614 if (present[i]) acc[i * h + l] += p;
618 for (std::size_t i = 0; i < n; ++i) {
620 for (std::size_t l = 0; l < h; ++l) {
621 const double v = acc[i * h + l];
622 ip(i, l + 1) = num_traits<T>::from_double(v);
625 ip(i, 0) = num_traits<T>::from_double(miss);
631 const std::size_t aoff = lvw - (n + rcl.size()), boff = aoff + n;
632 std::vector<double> phi(n, 0.0), d1(n, 0.0);
633 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
634 const double p = num_traits<T>::to_double(pi[s]);
635 if (p == 0)
continue;
636 const std::vector<T>& lv = r.space[s].local[isf - 1];
637 for (std::size_t i = 0; i < n; ++i)
638 if (num_traits<T>::to_double(lv[aoff + i]) != 0) phi[i] += p;
639 for (std::size_t j = 0; j < rcl.size(); ++j)
640 d1[rci[j] - 1] += p * num_traits<T>::to_double(lv[boff + j]);
642 m.delayedhitqlen.assign(n, num_traits<T>::from_int(0));
643 m.delayedhitqlenfull.assign(n, num_traits<T>::from_int(0));
644 for (std::size_t i = 0; i < n; ++i) {
645 m.delayedhitqlen[i] = num_traits<T>::from_double(d1[i]);
646 m.delayedhitqlenfull[i] = num_traits<T>::from_double(d1[i] + phi[i]);
648 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
649 const double p = num_traits<T>::to_double(pi[s]);
650 if (p == 0)
continue;
651 const std::vector<T>& lv = r.space[s].local[isf - 1];
652 for (std::size_t j = 0; j < rcl.size(); ++j) {
653 const std::size_t i = rci[j] - 1;
654 const double held = num_traits<T>::to_double(lv[boff + j]);
655 if (held <= 0 || num_traits<T>::to_double(lv[aoff + i]) == 0)
continue;
656 double completes = 0.0;
657 for (std::size_t b = 0; b < ns; ++b) {
658 if (b == s)
continue;
659 const double q = num_traits<T>::to_double(r.Q(s, b));
660 if (q == 0)
continue;
661 if (num_traits<T>::to_double(r.space[b].local[isf - 1][aoff + i]) == 0)
664 if (rco[j] - 1 < K) drate[rco[j] - 1] += p * held * completes;
669 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
670 const double p = num_traits<T>::to_double(pi[s]);
671 if (p == 0)
continue;
672 const std::vector<T>& lv = r.space[s].local[isf - 1];
673 for (std::size_t j = 0; j < rcl.size(); ++j)
675 dclass[rco[j] - 1] += p * num_traits<T>::to_double(lv[boff + j]);
677 for (std::size_t k = 0; k < K; ++k)
678 for (std::size_t i = 0; i < cp.retrieval_classes.size(); ++i) {
679 if (k >= cp.retrieval_classes[i].size())
continue;
680 const std::size_t rc = cp.retrieval_classes[i][k];
681 if (rc == 0 || rc >
static_cast<std::size_t
>(
QN.cols()))
continue;
682 for (std::size_t st = 0; st < static_cast<std::size_t>(
QN.rows()); ++st)
683 inflight[k] += num_traits<T>::to_double(
QN(st, rc - 1));
687 m.hitprob.assign(K, num_traits<T>::from_double(dnan));
688 m.missprob.assign(K, num_traits<T>::from_double(dnan));
689 if (retr) m.delayedprob.assign(K, num_traits<T>::from_double(dnan));
690 for (std::size_t k = 0; k < K; ++k) {
691 if (k >= cp.hitclass.size() || k >= cp.missclass.size())
continue;
692 const std::size_t h = cp.hitclass[k], mi = cp.missclass[k];
693 if (h == 0 || mi == 0 || h > K || mi > K)
continue;
694 const double denom = tn[h - 1] + tn[mi - 1];
695 if (denom <= 0)
continue;
699 const double d = retr ? std::min(drate[k], tn[h - 1]) : 0.0;
700 m.hitprob[k] = num_traits<T>::from_double((tn[h - 1] - d) / denom);
701 m.missprob[k] = num_traits<T>::from_double(tn[mi - 1] / denom);
702 if (retr) m.delayedprob[k] = num_traits<T>::from_double(d / denom);
703 if (retr && cp.retrieval_queues.find(k) != cp.retrieval_queues.end() &&
704 !cp.retrieval_queues.find(k)->second.empty()) {
705 if (m.latency.empty()) m.latency.assign(K, num_traits<T>::from_double(dnan));
706 const double enter = d + tn[mi - 1];
708 m.latency[k] = num_traits<T>::from_double((inflight[k] + dclass[k]) / enter);
726std::vector<double> cache_delayed_hit_rate(
const NetworkStruct<T>& sn, std::size_t ind,
727 const CtmcResult<T>& r,
const std::vector<T>& pi) {
728 const std::size_t K = sn.nclasses;
729 std::vector<double> drate(K, 0.0);
730 const std::size_t isf = sn.stateful_index(ind);
731 if (isf == 0 || r.space.empty())
return drate;
732 const auto it = sn.nodeparam.find(ind);
735 const qn::CacheParam<T>& cp = it->second;
736 if (!(cp.retrieval_capacity > 0))
return drate;
737 std::vector<std::size_t> rcl, rci, rco;
739 if (rcl.empty())
return drate;
740 const std::size_t n = cp.nitems, ns = r.space.size();
742 for (std::size_t l = 0; l < cp.itemcap.size(); ++l)
743 if (cp.itemcap[l] > 0) tcc +=
static_cast<std::size_t
>(cp.itemcap[l]);
744 const std::size_t lvw = r.space[0].local[isf - 1].size();
745 if (lvw < tcc + n + rcl.size())
return drate;
746 const std::size_t aoff = lvw - (n + rcl.size()), boff = aoff + n;
747 for (std::size_t s = 0; s < ns && s < pi.size(); ++s) {
748 const double p = num_traits<T>::to_double(pi[s]);
749 if (p == 0)
continue;
750 const std::vector<T>& lv = r.space[s].local[isf - 1];
751 for (std::size_t j = 0; j < rcl.size(); ++j) {
752 const std::size_t i = rci[j] - 1;
753 const double held = num_traits<T>::to_double(lv[boff + j]);
754 if (held <= 0 || num_traits<T>::to_double(lv[aoff + i]) == 0)
continue;
755 double completes = 0.0;
756 for (std::size_t b = 0; b < ns; ++b) {
757 if (b == s)
continue;
758 const double q = num_traits<T>::to_double(r.Q(s, b));
759 if (q == 0)
continue;
760 if (num_traits<T>::to_double(r.space[b].local[isf - 1][aoff + i]) == 0)
763 if (rco[j] - 1 < K) drate[rco[j] - 1] += p * held * completes;
790void strip_reference_source_column(
const NetworkStruct<T>& sn, std::size_t ind,
791 std::vector<T>& row) {
792 const std::size_t ist = sn.nodes[ind - 1].station;
793 if (ist == 0 || ist > sn.stations.size())
return;
794 if (sn.stations[ist - 1].nodetype != NodeType::Source &&
795 sn.stations[ist - 1].sched != SchedStrategy::EXT)
798 for (std::size_t r = 0; r < sn.nclasses; ++r) w += sn.phasessz_of(ist, r + 1);
802 if (row.size() == w + 1) row.erase(row.begin());
817bool default_init_state(
const NetworkStruct<T>& sn, NetState<T>& init) {
818 const std::size_t R = sn.nclasses;
819 const std::vector<std::size_t>& sfn = sn.stateful_nodes;
820 init.local.assign(sfn.size(), std::vector<T>());
821 for (std::size_t f = 0; f < sfn.size(); ++f) {
822 const std::size_t ind = sfn[f];
831 const typename std::map<std::size_t, Matrix<T>>::const_iterator sp =
832 sn.statespace.find(ind);
833 if (sp != sn.statespace.end() && sp->second.rows() == 1 && sp->second.cols() > 0) {
834 std::vector<T> row(sp->second.cols());
835 for (std::size_t c = 0; c < sp->second.cols(); ++c) row[c] = sp->second(0, c);
836 strip_reference_source_column(sn, ind, row);
850 std::vector<std::size_t> marg(R, 0), mph(R, 1);
851 const std::size_t mist = sn.nodes[ind - 1].station;
852 bool rebuilt =
false;
853 if (row.size() == R && mist != 0) {
854 for (std::size_t r = 0; r < R; ++r) {
855 const double v = num_traits<T>::to_double(row[r]);
856 marg[r] = v > 0.0 ?
static_cast<std::size_t
>(v + 0.5) : 0;
857 mph[r] = sn.phasessz_of(mist, r + 1);
859 std::vector<T> built;
861 init.local[f] = built;
865 if (!rebuilt) init.local[f] = row;
868 const std::size_t ist = sn.nodes[ind - 1].station;
869 std::vector<std::size_t> nmarg(R, 0), ph(R, 1);
871 for (std::size_t r = 0; r < R; ++r) ph[r] = sn.phasessz_of(ist, r + 1);
872 if (sn.stations[ist - 1].nodetype == NodeType::Source) {
880 for (std::size_t r = 0; r < R; ++r)
881 if (!sn.disabled[ist - 1][r]) nmarg[r] = 1;
883 for (std::size_t r = 0; r < R; ++r) {
884 const double nj = sn.njobs()[r];
885 if (std::isfinite(nj) && sn.classes[r].refstat == ist)
886 nmarg[r] =
static_cast<std::size_t
>(nj);
897 for (std::size_t r = 0; r < R; ++r) {
899 if (m0 > nmarg[r]) nmarg[r] = m0;
910std::size_t init_state_index(
const NetworkStruct<T>& sn,
const std::vector<NetState<T>>& space) {
911 const std::size_t npos =
static_cast<std::size_t
>(-1);
912 if (space.empty())
return npos;
914 if (!default_init_state(sn, init))
return npos;
915 for (std::size_t f = 0; f < init.local.size(); ++f) {
918 const std::size_t w = space[0].local[f].size();
919 if (init.local[f].size() > w)
return npos;
920 if (init.local[f].size() < w)
921 init.local[f].insert(init.local[f].begin(), w - init.local[f].size(),
922 num_traits<T>::from_int(0));
924 const std::vector<double> key = ctmc_detail::state_key(init);
925 for (std::size_t s = 0; s < space.size(); ++s)
926 if (ctmc_detail::state_key(space[s]) == key)
return s;
950bool init_state_distribution(
const NetworkStruct<T>& sn,
const std::vector<NetState<T>>& space,
951 std::vector<T>& pi0) {
952 const std::size_t npos =
static_cast<std::size_t
>(-1);
953 pi0.assign(space.size(), num_traits<T>::from_int(0));
954 if (space.empty())
return false;
956 if (!default_init_state(sn, base))
return false;
957 const std::vector<std::size_t>& sfn = sn.stateful_nodes;
961 std::vector<std::vector<std::vector<T>>> rows(sfn.size());
962 std::vector<std::vector<double>> wts(sfn.size());
963 for (std::size_t f = 0; f < sfn.size(); ++f) {
964 const typename std::map<std::size_t, Matrix<T>>::const_iterator ss =
965 sn.statespace.find(sfn[f]);
966 const typename std::map<std::size_t, std::vector<T>>::const_iterator sp =
967 sn.stateprior.find(sfn[f]);
968 if (ss == sn.statespace.end() || sp == sn.stateprior.end() || ss->second.rows() == 0 ||
969 ss->second.rows() != sp->second.size()) {
970 rows[f].push_back(base.local[f]);
971 wts[f].push_back(1.0);
974 for (std::size_t r = 0; r < ss->second.rows(); ++r) {
975 const double w = num_traits<T>::to_double(sp->second[r]);
976 if (!(w > 0.0))
continue;
977 std::vector<T> row(ss->second.cols());
978 for (std::size_t c = 0; c < ss->second.cols(); ++c) row[c] = ss->second(r, c);
979 strip_reference_source_column(sn, sfn[f], row);
980 rows[f].push_back(row);
983 if (rows[f].empty())
return false;
986 std::vector<std::size_t> pick(sfn.size(), 0);
989 NetState<T> cand = base;
991 for (std::size_t f = 0; f < sfn.size(); ++f) {
992 cand.local[f] = rows[f][pick[f]];
993 w *= wts[f][pick[f]];
996 const std::size_t wd = space[0].local[f].size();
997 if (cand.local[f].size() > wd)
return false;
998 if (cand.local[f].size() < wd)
999 cand.local[f].insert(cand.local[f].begin(), wd - cand.local[f].size(),
1000 num_traits<T>::from_int(0));
1002 std::size_t idx = npos;
1003 const std::vector<double> key = ctmc_detail::state_key(cand);
1004 for (std::size_t s = 0; s < space.size() && idx == npos; ++s)
1005 if (ctmc_detail::state_key(space[s]) == key) idx = s;
1006 if (idx == npos)
return false;
1007 pi0[idx] = T(pi0[idx] + num_traits<T>::from_double(w));
1011 for (; f < sfn.size(); ++f) {
1012 if (++pick[f] < rows[f].size())
break;
1015 if (f == sfn.size())
break;
1017 if (!(total > 0.0))
return false;
1027std::vector<std::size_t> weak_components(
const Matrix<T>& Q, std::size_t& ncomp) {
1028 const std::size_t n = Q.rows();
1029 const std::size_t npos =
static_cast<std::size_t
>(-1);
1030 std::vector<std::size_t> comp(n, npos);
1032 for (std::size_t s = 0; s < n; ++s) {
1033 if (comp[s] != npos)
continue;
1034 std::vector<std::size_t> stack{s};
1036 while (!stack.empty()) {
1037 const std::size_t u = stack.back();
1039 for (std::size_t v = 0; v < n; ++v) {
1040 if (comp[v] != npos)
continue;
1043 if (v == u)
continue;
1044 if (num_traits<T>::to_double(Q(u, v)) != 0 ||
1045 num_traits<T>::to_double(Q(v, u)) != 0) {
1058CtmcResult<T> restrict_to(
const CtmcResult<T>& r,
const std::vector<std::size_t>& wset) {
1060 out.Q =
Matrix<T>(wset.size(), wset.size(), num_traits<T>::from_int(0));
1061 for (std::size_t a = 0; a < wset.size(); ++a)
1062 for (std::size_t b = 0; b < wset.size(); ++b) out.Q(a, b) = r.Q(wset[a], wset[b]);
1063 out.space.reserve(wset.size());
1064 out.arv_rates.reserve(wset.size());
1065 out.dep_rates.reserve(wset.size());
1066 for (std::size_t a = 0; a < wset.size(); ++a) {
1067 out.space.push_back(r.space[wset[a]]);
1068 out.arv_rates.push_back(r.arv_rates[wset[a]]);
1069 out.dep_rates.push_back(r.dep_rates[wset[a]]);
1075 out.filt.reserve(r.filt.size());
1076 for (std::size_t f = 0; f < r.filt.size(); ++f) {
1077 Matrix<T> F(wset.size(), wset.size(), num_traits<T>::from_int(0));
1078 for (std::size_t a = 0; a < wset.size(); ++a)
1079 for (std::size_t b = 0; b < wset.size(); ++b) F(a, b) = r.filt[f](wset[a], wset[b]);
1080 out.filt.push_back(F);
1110 const bool pooled =
ctmc_pools(sn_arg, sn_pool);
1115 const bool mapc =
ctmc_mapc(sn_pl, sn_mapc);
1117 if (pooled || mapc) out.
solved_sn = std::make_shared<const NetworkStruct<T>>(sn_in);
1118 out.
cutoff = analyzer_detail::resolve_cutoff(sn_in,
opt);
1122 const bool retr = analyzer_detail::set_retrieval_truncation(sn_in, out.
cutoff, sn_trunc);
1128 const bool sig = analyzer_detail::annihilate_signal_capacity(sn_r, sn_sig);
1146 const std::vector<Sync<T>> sync = refresh_sync(
sn);
1147 const std::vector<qn::GlobalSync<T>> gsync = refresh_global_sync(
sn);
1163 std::vector<NetState<T>> space;
1164 if (!gsync.empty() || !fjsync.empty()) {
1166 if (!analyzer_detail::default_init_state(
sn, init))
1168 "SolverCTMC: the model's initial marking admits no state; check the Place "
1169 "populations against the class reference stations");
1176 space = space_generator(
sn, out.
cutoff,
opt.state_max,
opt.cutoff_mat);
1180 "SolverCTMC: the state space is empty; no state satisfies the model's capacities");
1197 const std::size_t npos =
static_cast<std::size_t
>(-1);
1200 std::size_t init = analyzer_detail::init_state_index(
sn, r.
space);
1202 std::size_t ncomp = 0;
1203 const std::vector<std::size_t> comp = analyzer_detail::weak_components(r.
Q, ncomp);
1212 std::vector<std::size_t> sizes(ncomp, 0);
1213 for (std::size_t s = 0; s < comp.size(); ++s) ++sizes[comp[s]];
1214 pick =
static_cast<std::size_t
>(
1215 std::max_element(sizes.begin(), sizes.end()) - sizes.begin());
1217 std::vector<std::size_t> wset;
1218 for (std::size_t s = 0; s < comp.size(); ++s)
1219 if (comp[s] == pick) wset.push_back(s);
1222 std::size_t moved = npos;
1224 for (std::size_t k = 0; k < wset.size(); ++k)
1225 if (wset[k] == init) {
1230 r = analyzer_detail::restrict_to(r, wset);
1240 static_cast<std::size_t
>(r.
Q.rows()));
1247 out.
cache = analyzer_detail::cache_metrics(
sn, r, out.
pi, out.
avg.QN);
1331 const std::string& method) {
1332 const std::size_t M =
sn.nstations, K =
sn.nclasses;
1334 std::vector<std::vector<bool>> srcmask(M, std::vector<bool>(K,
false));
1335 for (std::size_t i = 0; i < M; ++i)
1336 if (
sn.stations[i].nodetype == NodeType::Source)
1337 for (std::size_t k = 0; k < K; ++k) srcmask[i][k] =
true;
1396 sub.chain_aggregation =
false;
1415 sub.load_concealment =
false;
1419 opt.method,
opt.transform_iter_max);
1447 const std::size_t M =
sn.nstations, K =
sn.nclasses;
1448 const std::vector<std::size_t>& subset =
opt.fes_stations;
1449 if (subset.size() < 2)
1451 "options.config.fes_stations must name at least two stations: collapsing one "
1452 "station into a flow-equivalent server saves nothing.");
1453 if (subset.size() >= M)
1455 "options.config.fes_stations names every station: there is no complement left "
1457 for (std::size_t i : subset)
1459 throw InputError(
"options.config.fes_stations must be 1-based station indices in 1.." +
1460 std::to_string(M) +
".");
1467 sub.fes_stations.clear();
1474 const std::size_t fesIst = snRed.
nodes[info.
fesNode - 1].station;
1475 std::size_t tableSize = 1;
1476 for (
int c : info.
cutoffs) tableSize *=
static_cast<std::size_t
>(c + 1);
1477 std::vector<double> Pn(tableSize, 0.0);
1478 for (std::size_t r = 0; r < SSq.
rows(); ++r) {
1479 std::vector<int> nvec(K, 0);
1480 for (std::size_t k = 0; k < K; ++k)
1481 nvec[k] =
static_cast<int>(
1499 for (std::size_t k = 0; k < K; ++k) {
1500 out.
QN(i, k) = redAvg.
QN(a, k);
1501 out.
UN(i, k) = redAvg.
UN(a, k);
1502 out.
TN(i, k) = redAvg.
TN(a, k);
1507 Matrix<T> Qsub(Msub, K, zero), Usub(Msub, K, zero);
1508 for (std::size_t idx = 0; idx < tableSize; ++idx) {
1509 if (!(Pn[idx] > 0.0))
continue;
1511 for (std::size_t a = 0; a < Msub && a < cm.
QN[idx].rows(); ++a)
1512 for (std::size_t k = 0; k < K; ++k) {
1513 Qsub(a, k) += T(w * cm.
QN[idx](a, k));
1514 Usub(a, k) += T(w * cm.
UN[idx](a, k));
1517 for (std::size_t a = 0; a < Msub; ++a) {
1519 for (std::size_t k = 0; k < K; ++k) {
1520 out.
QN(i, k) = Qsub(a, k);
1521 out.
UN(i, k) = Usub(a, k);
1525 for (std::size_t c = 0; c <
sn.nchains; ++c) {
1526 const std::size_t isf =
sn.stateful_of_station(i + 1) - 1;
1528 if (isf <
sn.visits[c].rows() && isfFes < snRed.
visits[c].rows() &&
1529 snRed.
visits[c](isfFes, k) > zero)
1530 ratio += T(
sn.visits[c](isf, k) / snRed.
visits[c](isfFes, k));
1532 out.
TN(i, k) = T(redAvg.
TN(fesIst - 1, k) * ratio);
1536 out.
CN.assign(K, zero);
1538 for (std::size_t i = 0; i < M; ++i)
1539 for (std::size_t k = 0; k < K; ++k) {
1540 if (out.
TN(i, k) > zero) out.
RN(i, k) = T(out.
QN(i, k) / out.
TN(i, k));
1541 out.
CN[k] += out.
RN(i, k);
1555 if (
opt.chain_aggregation &&
sn.nchains <
sn.nclasses)
What a solver observed about the Cache nodes of a model.
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::vector< NodeDef > nodes
every node, in creation order
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
static void step(const char *fmt,...)
Write one progress line.
Host-aware memory pre-gate for SolverCTMC.
Steady-state distribution of a continuous-time Markov chain.
Worst-case log-size of the CTMC state space induced by a NetworkStruct.
Port of matlab/src/solvers/CTMC/ctmc_stationary.m: the single entry point for the stationary distribu...
The exception types the port throws.
Flow-equivalent-server aggregation: replace a station subset by one station.
Per-station metrics of the ISOLATED subnetwork at every population state, the companion of fes_comput...
Port of matlab/src/api/fj/sn_fj_validate.m and matlab/src/io/@@ModelAdapter/fjtag....
Running progress log of a LINE solver run (the "solver console").
Dense matrix and non-owning view.
bool sn_has_nonmarkov(const qn::NetworkStruct< T > &sn, bool preserve_det=false)
Whether any law in the struct would be replaced, so a caller can skip copying the struct when there i...
SnPnAvgRates< T > sn_pn_avg_rates(const qn::NetworkStruct< T > &sn, const Matrix< T > &QN, const Matrix< T > &TN, const Matrix< T > &AN, const Matrix< T > &RN)
Port of sn_pn_avg_rates: rewrite the place rows of TN, AN and RN so that they agree with the recovere...
@ Ph
Bernstein density fit: a genuine phase-type, shape-carrying.
void sn_nonmarkov_toph(qn::NetworkStruct< T > &sn, const NonmarkovOptions &opts=NonmarkovOptions())
Replace every non-Markovian service and firing law by a Markovian surrogate.
mva::AvgResult< T > solver_ctmc_fes_aggregation(const NetworkStruct< T > &sn, const CtmcOptions &opt)
Solve with a station subset replaced by a FLOW-EQUIVALENT SERVER, then recover the collapsed stations...
bool ctmc_pools(const qn::NetworkStruct< T > &in, qn::NetworkStruct< T > &out)
Rewrite a copy of in into out when it has pooled stations; false, leaving out untouched,...
void ctmc_eliminate_vanishing(CtmcResult< T > &res)
Port of the "now remove immediate transitions" block of solver_ctmc.m (:812-870): eliminate the vanis...
mva::AvgResult< T > solver_ctmc_chain_aggregation(const NetworkStruct< T > &sn, const CtmcOptions &opt)
Solve the CHAIN-AGGREGATED model and map its metrics back to the classes.
void check_method(const std::string &method)
Port of runAnalyzerChecks' method gate.
std::vector< std::string > list_valid_methods()
Port of SolverCTMC.listValidMethods.
void ctmc_check_support(const NetworkStruct< T > &sn)
Refuse the constructs this port generates a chain for but does not MODEL.
bool ctmc_mapc(const qn::NetworkStruct< T > &in, qn::NetworkStruct< T > &out)
Rewrite a copy of in into out when it has a MAP service at an FCFS station; false,...
void make_infgen(Matrix< T > &Q)
Port of ctmc_makeinfgen: turn an off-diagonal rate matrix into a generator.
mva::AvgResult< T > solver_ctmc_avg_table(const NetworkStruct< T > &sn, const CtmcSolution< T > &d, const std::string &method)
Port of @@SolverCTMC/runAnalyzer.m's result assembly: solve, then apply the metric filter @@NetworkSo...
CtmcAvg< T > solver_ctmc_avg_from_pi(const NetworkStruct< T > &sn, const CtmcResult< T > &r, const std::vector< T > &pivec, bool stationary=true)
Port of solver_ctmc_avg_from_pi: map a state distribution to mean metrics.
mva::AvgResult< T > solver_ctmc_load_concealment(const NetworkStruct< T > &sn, const CtmcOptions &opt)
Solve by LOAD CONCEALMENT, the iterated transformation.
CtmcSolution< T > solve_struct(const NetworkStruct< T > &sn_arg, const CtmcOptions &opt, const std::vector< qn::FjSync< T > > &fjsync)
Build the chain of ONE struct, solve it, reduce it.
std::vector< NetState< T > > ctmc_filter_regions(const NetworkStruct< T > &sn, const std::vector< NetState< T > > &space)
The states of space a DROP region admits, in their original order.
CtmcResult< T > solver_ctmc(const NetworkStruct< T > &sn, const std::vector< NetState< T > > &space, const std::vector< Sync< T > > &sync, const std::vector< qn::GlobalSync< T > > &gsync=std::vector< qn::GlobalSync< T > >(), bool want_filtration=false, const std::vector< qn::FjSync< T > > &fjsync=std::vector< qn::FjSync< T > >())
Port of the generator assembly of solver_ctmc.m.
CtmcSolution< T > solver_ctmc_analyzer(const NetworkStruct< T > &sn_in, const CtmcOptions &opt)
Port of solver_ctmc_analyzer.m plus the fork-join wrapper of @@SolverCTMC/runAnalyzer....
mva::AvgResult< T > solver_ctmc_run_analyzer(const NetworkStruct< T > &sn, const CtmcOptions &opt)
Solve and format in one call, for a caller with no use for the chain.
CtmcStationaryResult< T > ctmc_stationary(const Matrix< T > &Q, std::size_t init_index=static_cast< std::size_t >(-1))
constexpr std::size_t CTMC_DEFAULT_CUTOFF
SolverOptions.m:107: the per-class state-space cutoff SolverCTMC defaults an open or mixed model to.
bool is_stateless_method(const std::string &method)
True for a method whose analyzer is not the explicit-generator one.
Matrix< T > ctmc_state_space_aggr(const NetworkStruct< T > &sn, const std::vector< NetState< T > > &space)
Port of StateSpaceAggr: the per-(station, class) job counts of every state, as an (nstates x nstation...
std::vector< NetState< T > > reachable_space_generator(const NetworkStruct< T > &sn, const NetState< T > &init, const std::vector< Sync< T > > &sync, const std::vector< qn::GlobalSync< T > > &gsync=std::vector< qn::GlobalSync< T > >(), std::size_t maxst=3000000, const std::vector< qn::FjSync< T > > &fjsync=std::vector< qn::FjSync< T > >(), const std::vector< std::size_t > &cutoff=std::vector< std::size_t >(), const std::vector< std::vector< std::size_t > > &cutoff_mat=std::vector< std::vector< std::size_t > >())
Port of State.reachableSpaceGenerator: the states reachable from init.
std::size_t ljd_linearize(const std::vector< int > &nvec, const std::vector< int > &cutoffs)
Linearized index of a per-class population vector, for Limited Joint Dependence (LJD) tables.
FesConditionalMetrics< T > fes_compute_metrics(const Matrix< T > &L, const std::vector< int > &mi, const std::vector< bool > &isDelay, const std::vector< int > &cutoffs)
Per-station metrics of the ISOLATED subnetwork at every population state, the companion of fes_comput...
FesAggregateResult< T > fes_aggregate(const qn::NetworkStruct< T > &sn, const std::vector< std::size_t > &subsetIndices, const FesOptions &options=FesOptions())
Flow-equivalent-server aggregation: replace a station subset by one station.
@ REPLY
completes a synchronous call, releasing a held server
constexpr double CTMC_DEFAULT_SAFETY_FRACTION
Fraction of available memory the solver may target.
CtmcGateResult ctmc_memory_gate(double log_nstates, bool force=false, double safety_fraction=CTMC_DEFAULT_SAFETY_FRACTION)
Decide whether a state space of log-size log_nstates can be solved here.
double ctmc_state_space_logsize(const qn::NetworkStruct< T > &sn, const CtmcSizeOptions &opt=CtmcSizeOptions())
Worst-case log state-space size of sn.
Matrix< T > sn_get_residt_from_respt(const qn::NetworkStruct< T > &L, const Matrix< T > &RN)
Port of sn_get_residt_from_respt: the per-JOB residence time.
Matrix< T > filter_metric(const qn::NetworkStruct< T > &L, const Matrix< T > &metric, MetricKind kind, const std::vector< std::vector< bool > > *zero_mask)
Port of filterMetric: what @@NetworkSolver/getAvg does between the analyzer and the caller.
Matrix< T > sn_get_arvr_from_tput(const qn::NetworkStruct< T > &L, const Matrix< T > &TN)
void cache_retrieval_class_map(const CacheParam< T > &cp, std::vector< std::size_t > &rc_list, std::vector< std::size_t > &rc_items, std::vector< std::size_t > &rc_orig)
Port of State.cacheRetrievalClassMap: the canonical order of a cache's retrieval classes,...
bool from_marginal_node_first(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< std::size_t > &n, const std::vector< std::size_t > &phases, std::vector< T > &out)
The FIRST row from_marginal_node emits, BUILT rather than enumerated.
std::size_t state_initial_occupancy(const NetworkStruct< T > &sn, std::size_t ind, std::size_t r)
Port of State.initialOccupancy: the class-r jobs node ind holds in the DECLARED initial state,...
FeatureSet ctmc_feature_set(const std::string &method)
SolverCTMC.getFeatureSet, the reference's 104 MATLAB names in full.
void feature_gate(const std::string &solver, const FeatureSet &declared, const NetworkStruct< T > &sn, const std::string &requested_method="", const std::string &resolved_method="")
runAnalyzerChecks: refuse a model the solver does not declare, by name.
FjTagged< T > fj_tag(const NetworkStruct< T > &sn)
Port of ModelAdapter.fjtag.
CacheMetrics< T > cache_metrics_of(const qn::NetworkStruct< T > &sn, const std::vector< T > &hitprob, const std::vector< T > &missprob, const std::vector< T > &delayedprob, const std::vector< T > &latency, const Matrix< T > &hitproblist, const Matrix< T > &itemprob, const std::vector< T > &listcost)
Assemble CacheMetrics from what a cache analyzer returned.
mva::AvgResult< T > transform_solve_lc(const qn::NetworkStruct< T > &sn, InnerSolve inner_solve, const std::string &method, std::size_t iter_max=1000)
LOAD CONCEALMENT (Birman-Kogan Algorithm 2) as a transformation, and the first ITERATED one.
void fj_foldback(const qn::NetworkStruct< T > &sn, Avg &a, const std::vector< std::size_t > &fjclassmap, std::size_t korig)
Reduce the augmented metrics onto the original classes.
mva::AvgResult< T > transform_solve_chains(const qn::NetworkStruct< T > &sn, InnerSolve inner_solve, const std::string &method)
Chain aggregation: collapse every chain onto a single class, solve, and map the chain metrics back on...
bool has_fork_join(const qn::NetworkStruct< T > &sn)
Whether the model needs the tag augmentation at all.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Collapse every chain onto one class, port of ModelAdapter.aggregateChains.
Replace every non-Markovian service and firing law by a Markovian surrogate.
Ports of matlab/src/api/sn/sn_pn_firing_rates.m and sn_pn_avg_rates.m.
Port of solver_ctmc.m: the infinitesimal generator of a queueing network, assembled from the enumerat...
Finite Capacity Regions in SolverCTMC: the DROP rule, as a filter on the enumerated state space,...
Port of solver_ctmc_mapc.m: the pair form of a MAP (and MMPP2) service at an FCFS station,...
Port of solver_ctmc_pools.m: rewrite every heterogeneous-server station (Queue.addServerType) into th...
The DECLARED side of the gate: one feature set per solver.
The SolverMVA class surface: @@SolverMVA/runAnalyzer.m and the gates around it.
Port of the MATLAB +State package: the encoding that turns a station's state row into marginal job co...
Port of the event half of MATLAB's +State package: the successor states an event produces at one node...
options.config.nonmkv and friends.
std::size_t order
nonmkvorder, the phase budget
PhFit phfit
which surrogate family
What sn_pn_avg_rates rewrites in place; empty tables are left empty.
The mean performance metrics a stationary vector maps to.
The SolverCTMC knobs this port honours.
bool force
options.force: downgrade the memory pre-gate's refusal to a warning.
double memory_safety_fraction
options.memorySafetyFraction: share of available memory a solve may target.
std::vector< std::size_t > cutoff_vec
per-class override, or empty
std::size_t nonmkv_order
options.config.nonmkvorder: the phase budget sn_nonmarkov_toph spends on a non-Markovian service law.
bool load_concealment
options.config.transform='lc': solve by LOAD CONCEALMENT.
std::size_t transform_iter_max
Sweep cap for an iterated transformation; the kernel's own default.
bool stiff
options.stiff, which SolverOptions defaults to TRUE and the transient passes to ctmc_transient as use...
std::vector< std::vector< std::size_t > > cutoff_mat
options.cutoff AS A (station x class) MATRIX, or empty.
std::vector< std::size_t > fes_stations
options.config.fes_stations: 1-BASED station indices to collapse into a flow-equivalent server before...
std::size_t state_max
refuse a space larger than this
double fau_delta
options.config.fau_delta: occupancy below which a state is dropped.
double cutoff
< 0 = not given
double timestep
options.timestep: the FIXED OUTPUT STEP of a transient analysis.
std::string transient_method
options.config.transient_method: "ode" (the default) integrates the forward equation,...
std::size_t fau_ngrid
options.config.fau_ngrid: output grid size when timestep is unset.
std::vector< CtmcRateSched > rate_sched
options.config.rate_sched: the TIME-INHOMOGENEOUS transient.
bool chain_aggregation
options.config.chain_aggregation: solve the CHAIN-AGGREGATED model.
std::size_t ctmc_tv_ngrid
options.config.ctmc_tv_ngrid: uniform grid size of the rate_sched propagator.
double fau_epsilon
options.config.fau_epsilon: total probability mass the whole grid may discard under "fau".
bool keep_filtration
Keep the per-synchronization EVENT FILTRATION alongside Q.
One entry of options.config.rate_sched: the rate of (station, class) follows the piecewise-linear sch...
std::vector< double > rates
std::vector< double > tgrid
The generator, the state space it is indexed by, and the event rates.
std::vector< NetState< T > > space
row i of Q is space[i]
Matrix< T > Q
(n x n) infinitesimal generator
Everything one CTMC solve produces.
std::string warning
Set when the chain is a reducible mixture solved from an invented seed.
std::vector< std::size_t > cutoff
the per-class cutoff actually used
solvers::CacheMetrics< T > cache
What the chain says about the model's Cache nodes: exact, since the hit and miss shares are read off ...
std::shared_ptr< const NetworkStruct< T > > solved_sn
The struct the chain was built on, when it differs from the caller's in its state LAYOUT: set when he...
std::vector< std::size_t > fjclassmap
fjclassmap when the model was fork-join, empty otherwise: the ORIGINAL class of each auxiliary siblin...
std::vector< T > pi
stationary distribution over chain.space
std::vector< T > pi
stationary distribution, length N
std::string warning
Empty unless the chain is an UNSEEDED reducible mixture.
static constexpr double Zero
What fes_aggregate returns.
The per-population tables the conditional sum is taken over.
std::vector< Matrix< T > > QN
Indexed by the 0-based linearized population state; each (M_sub x K).
std::vector< Matrix< T > > UN
Indexed by the 0-based linearized population state; each (M_sub x K).
Everything needed to map an FES result back onto the original model.
Matrix< T > isolatedDemands
(M_sub x K)
std::vector< std::size_t > subsetIndices
1-based, as given
std::vector< std::size_t > complementIndices
1-based
std::vector< bool > isolatedIsDelay
std::vector< int > isolatedServers
std::size_t fesNode
1-based node index of the FES in the new model
std::vector< int > cutoffs
The gate verdict, plus the message the caller reports either way.
Options the estimator reads; only the cutoff matters.
double cutoff
< 0 or non-finite = not given, take the solver default
The metrics getAvg returns, after filtering.
Matrix< T > RN
response time, per visit
Matrix< T > UN
utilization
Matrix< T > WN
residence time, per job
std::string method
the method asked for
std::string actualmethod
the algorithm that ran
Matrix< T > QN
queue length
std::vector< T > CN
system response time per class
std::vector< T > XN
system throughput per class
solvers::CacheMetrics< T > cache
What the cache branches observed, EMPTY on a model with no Cache node and on every solver that does n...
Matrix< T > AN
arrival rate
One fork firing synchronization: sn.fjsync{k}.
The augmented struct and everything needed to read its results back.
std::vector< FjSync< T > > fjsync
std::vector< std::size_t > fjclassmap
fjclassmap[a-1] is the ORIGINAL class of auxiliary class a, 0 for originals.
One network state: the per-stateful-node local rows it is composed of.
Every Cache node of the model, in node order; empty on a model with none.