533 const std::size_t nidx =
lsn.nidx;
534 if (nidx == 0)
throw InputError(
"SolverLDES (native LN engine): empty layered model");
539 for (std::size_t k = 1; k <= nidx && k <
lsn.sched.size(); ++k) {
540 const bool is_host = k >
lsn.hshift && k <=
lsn.hshift +
lsn.nhosts;
541 const bool is_task = k >
lsn.tshift && k <=
lsn.tshift +
lsn.ntasks;
542 if (!is_host && !is_task)
continue;
543 const SchedStrategy sc =
lsn.sched[k];
544 const bool ok = sc == SchedStrategy::FCFS || sc == SchedStrategy::INF ||
545 sc == SchedStrategy::LCFS || sc == SchedStrategy::SIRO ||
546 sc == SchedStrategy::HOL || sc == SchedStrategy::REF ||
547 sc == SchedStrategy::LCFSPRIO ||
548 (is_host && (sc == SchedStrategy::PS || sc == SchedStrategy::PSPRIO ||
549 sc == SchedStrategy::FCFSPRPRIO ||
550 sc == SchedStrategy::FCFSPIPRIO ||
551 sc == SchedStrategy::LCFSPRPRIO ||
552 sc == SchedStrategy::LCFSPIPRIO));
555 for (std::size_t c = 0; c < scname.size(); ++c)
556 scname[c] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(scname[c])));
558 "SolverLDES (native LN engine) does not simulate " + scname +
" scheduling at " +
559 (is_host ?
"processor" :
"task") +
" '" +
lsn.names[k] +
560 "'. The layered engine implements " +
561 (is_host ?
"FCFS, LCFS, SIRO, HOL, FCFSPRIO, LCFSPRIO, FCFSPRPRIO, FCFSPIPRIO, "
562 "LCFSPRPRIO, LCFSPIPRIO, PS, PSPRIO and INF"
563 :
"FCFS, LCFS, SIRO, HOL, FCFSPRIO, LCFSPRIO and INF (a task holds threads, "
564 "it does not divide or preempt them)") +
565 "; a weighted discipline would additionally need the per-task shares the layered "
566 "struct does not carry.");
568 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
571 if (t <
lsn.hassetup.size() &&
lsn.hassetup[t]) {
572 const double m = (t <
lsn.mult.size()) ?
lsn.mult[t] : 1.0;
575 "' declares a setup time on an infinite-server task, which "
576 "holds no thread to power down; give it a finite "
581 if (t <
lsn.pools.size() &&
lsn.pools[t].npools() > 0)
583 "' declares heterogeneous server pools; LDES holds server "
584 "pools on processors only");
590 "SolverLDES (native LN engine) dispatches call groups round robin or JSQ only (activity '" +
593 const std::uint64_t max_events =
598 bool time_stopped =
false;
599 const std::uint64_t base =
600 (o.
seed >= 0) ?
static_cast<std::uint64_t
>(o.
seed) : std::random_device{}();
609 const long long ln_seed =
static_cast<long long>(base);
610 std::vector<Rng> g_host, g_think;
611 g_host.reserve(nidx + 1);
612 g_think.reserve(nidx + 1);
613 for (std::size_t k = 0; k <= nidx; ++k) {
614 g_host.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10 + 1000));
615 g_think.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10));
620 std::vector<Sampler> hostdem(nidx + 1), think(nidx + 1), actthink(nidx + 1);
621 std::vector<bool> has_hostdem(nidx + 1,
false), has_think(nidx + 1,
false),
622 has_actthink(nidx + 1,
false);
625 std::vector<Rng> g_actthink;
626 g_actthink.reserve(nidx + 1);
627 for (std::size_t k = 0; k <= nidx; ++k)
628 g_actthink.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10 + 6000));
629 for (std::size_t a =
lsn.ashift + 1; a <=
lsn.ashift +
lsn.nacts && a <
lsn.actthink.size(); ++a) {
631 actthink[a] = Sampler(
lsn.actthink[a],
"the think time of activity '" +
lsn.names[a] +
"'");
632 has_actthink[a] =
true;
635 for (std::size_t k = 1; k <= nidx; ++k) {
636 if (k <
lsn.hostdem.size() && !
lsn.hostdem[k].disabled &&
638 hostdem[k] = Sampler(
lsn.hostdem[k],
"the host demand of '" +
lsn.names[k] +
"'");
639 has_hostdem[k] =
true;
641 if (k <
lsn.think.size() && !
lsn.think[k].disabled &&
643 think[k] = Sampler(
lsn.think[k],
"the think time of '" +
lsn.names[k] +
"'");
649 std::vector<std::size_t> host_servers(nidx + 1, 1), task_threads(nidx + 1, 1);
650 std::vector<SchedStrategy> host_sched(nidx + 1, SchedStrategy::FCFS);
651 std::vector<SchedStrategy> task_sched(nidx + 1, SchedStrategy::FCFS);
654 for (std::size_t h =
lsn.hshift + 1; h <=
lsn.hshift +
lsn.nhosts; ++h) {
655 const double m = (h <
lsn.mult.size()) ?
lsn.mult[h] : 1.0;
656 if (h <
lsn.sched.size()) host_sched[h] =
lsn.sched[h];
657 host_servers[h] = (std::isfinite(m) && host_sched[h] != SchedStrategy::INF)
658 ? std::max<std::size_t>(1,
static_cast<std::size_t
>(m + 0.5))
659 : std::numeric_limits<std::size_t>::max();
661 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
662 const double m = (t <
lsn.mult.size()) ?
lsn.mult[t] : 1.0;
663 if (t <
lsn.sched.size()) task_sched[t] =
lsn.sched[t];
664 task_threads[t] = (std::isfinite(m) && task_sched[t] != SchedStrategy::INF)
665 ?
static_cast<std::size_t
>(m + 0.5)
666 : std::numeric_limits<std::size_t>::max();
679 std::vector<std::size_t> repl(nidx + 1, 1);
680 for (std::size_t k = 1; k <
lsn.repl.size() && k <= nidx; ++k) {
681 const double r =
lsn.repl[k];
682 repl[k] = (r > 1.0) ?
static_cast<std::size_t
>(r + 0.5) : 1;
684 std::vector<std::size_t> host_slot0(nidx + 1, 0);
685 std::size_t nhost_slots = 0;
686 for (std::size_t h =
lsn.hshift + 1; h <=
lsn.hshift +
lsn.nhosts; ++h) {
687 host_slot0[h] = nhost_slots;
688 nhost_slots += repl[h];
690 if (nhost_slots == 0) nhost_slots = 1;
693 auto host_slot = [&](std::size_t host, std::size_t trep) {
694 return host_slot0[host] + (trep % repl[host]);
699 std::vector<std::size_t> task_slot0(nidx + 1, 0);
700 std::size_t ntask_slots = 0;
701 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
702 task_slot0[t] = ntask_slots;
703 ntask_slots += repl[t];
705 if (ntask_slots == 0) ntask_slots = 1;
706 auto task_slot = [&](std::size_t task, std::size_t trep) {
707 return task_slot0[task] + (trep % repl[task]);
719 auto callee_replica = [&](std::size_t caller_task, std::size_t crep,
720 std::size_t callee_task) -> std::size_t {
721 const std::size_t rb = repl[callee_task];
722 if (rb <= 1)
return 0;
723 std::size_t f =
static_cast<std::size_t
>(
lsn.fanout_at(caller_task, callee_task) + 0.5);
725 const std::size_t ra = repl[caller_task];
726 f = (rb > ra) ? std::max<std::size_t>(1, rb / ra) : 1;
728 f = std::min(std::max<std::size_t>(1, f), rb);
729 const std::size_t k =
730 (f > 1) ?
static_cast<std::size_t
>(uniform01(g_call) *
static_cast<double>(f)) : 0;
731 return ((crep * f) + std::min(k, f - 1)) % rb;
735 const std::size_t a_lo =
lsn.ashift + 1, a_hi =
lsn.ashift +
lsn.nacts;
736 auto is_act = [&](std::size_t k) {
return k >= a_lo && k <= a_hi; };
737 auto wgt = [&](std::size_t i, std::size_t j) {
740 auto pretype = [&](std::size_t a) {
743 auto posttype = [&](std::size_t a) {
746 auto phase_of = [&](std::size_t a) ->
int {
747 const std::size_t k = a -
lsn.ashift;
748 return (k <
lsn.actphase.size() &&
lsn.actphase[k] > 0) ?
lsn.actphase[k] : 1;
752 std::vector<std::vector<std::size_t> > succ_act(nidx + 1);
753 for (std::size_t a = a_lo; a <= a_hi; ++a)
754 for (std::size_t s :
lsn.graph.succ(a))
755 if (is_act(s)) succ_act[a].push_back(s);
763 std::vector<std::vector<double> > or_prob(nidx + 1);
764 for (std::size_t a = a_lo; a <= a_hi; ++a) {
765 const std::vector<std::size_t>& sc = succ_act[a];
766 if (sc.size() < 2)
continue;
769 for (std::size_t s : sc) {
770 const double w = wgt(a, s);
771 if (w != 1.0 && w > 0.0) prob =
true;
775 for (std::size_t s : sc) tot += wgt(a, s);
776 for (std::size_t s : sc) {
777 const double w = wgt(a, s);
778 or_prob[a].push_back((tot > 0.0 && tot != 1.0) ? w / tot : w);
783 std::vector<std::size_t> join_required(nidx + 1, 0);
784 for (std::size_t a = a_lo; a <= a_hi; ++a)
786 ++join_required[succ_act[a][0]];
794 std::vector<std::size_t> join_quorum(nidx + 1, 0);
795 std::vector<std::vector<std::size_t> > join_inputs(nidx + 1);
796 for (std::size_t a = a_lo; a <= a_hi; ++a)
798 join_inputs[succ_act[a][0]].push_back(a);
799 for (std::size_t j = a_lo; j <= a_hi; ++j) {
800 if (join_required[j] == 0)
continue;
801 const std::size_t q = j <
lsn.actquorum.size() ?
lsn.actquorum[j] : 0;
802 join_quorum[j] = (q >= 1 && q < join_required[j]) ? q : join_required[j];
811 std::vector<char> seen(nidx + 1, 0), root_input(nidx + 1, 0);
812 std::deque<std::size_t> todo;
813 for (std::size_t j = a_lo; j <= a_hi; ++j)
814 if (join_required[j] > 0) todo.push_back(j);
815 while (!todo.empty()) {
816 const std::size_t a = todo.front();
818 if (seen[a])
continue;
824 const std::vector<std::size_t>& sc = succ_act[a];
825 const bool choose = sc.size() == 1 || !or_prob[a].empty() ||
828 for (std::size_t s : sc) todo.push_back(s);
830 for (std::size_t j = a_lo; j <= a_hi; ++j) {
831 if (join_quorum[j] >= join_required[j])
continue;
832 for (std::size_t a : join_inputs[j])
835 "SolverLDES (native LN engine): the AND join into '" +
lsn.names[j] +
836 "' declares a quorum of " + std::to_string(join_quorum[j]) +
" of " +
837 std::to_string(join_required[j]) +
" inputs, but its input '" +
838 lsn.names[a] +
"' follows an earlier join without an AND fork in between, "
839 "so the main line of the invocation could arrive late; a quorum join must "
840 "be fed by forked branches only");
848 std::vector<std::size_t> bound(nidx + 1, 0);
849 for (std::size_t e =
lsn.eshift + 1; e <=
lsn.eshift +
lsn.nentries; ++e) {
850 const std::size_t t =
lsn.parent[e];
851 const std::vector<std::size_t>& acts =
852 (t <
lsn.actsof.size()) ?
lsn.actsof[t] : std::vector<std::size_t>();
853 for (std::size_t a : acts)
854 if (wgt(e, a) > 0.0) {
858 if (bound[e] != 0 || acts.empty())
continue;
859 for (std::size_t a : acts) {
862 bool haspred =
false;
863 for (std::size_t o2 : acts)
864 if (o2 != a && wgt(o2, a) > 0.0) {
873 if (bound[e] == 0) bound[e] = acts[0];
877 std::vector<LnCacheState> caches;
878 std::vector<std::size_t> cache_of_slot(ntask_slots, LN_NONE);
879 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
880 if (t >=
lsn.iscache.size() || !
lsn.iscache[t])
continue;
881 const std::size_t n = t <
lsn.nitems.size() ?
lsn.nitems[t] : 0;
882 if (n == 0)
continue;
883 std::vector<int> cap = (t <
lsn.itemcap.size()) ?
lsn.itemcap[t] : std::vector<int>();
884 if (cap.empty()) cap.assign(1, 1);
885 for (std::size_t m = 0; m < repl[t]; ++m) {
889 cs.rs = (t <
lsn.replacestrat.size()) ?
lsn.replacestrat[t]
891 cs.levels.assign(cap.size(), std::list<std::size_t>());
892 cs.retrieval = t <
lsn.hasretrieval.size() &&
lsn.hasretrieval[t];
894 cs.inflight.assign(n, 0);
895 cs.held.assign(n, {});
897 cache_of_slot[task_slot(t, m)] = caches.size();
898 caches.push_back(cs);
901 std::vector<LnCacheAccess> accesses;
902 std::vector<std::size_t> access_of_driver(nidx + 1, LN_NONE);
903 std::vector<std::size_t> branch_reply(nidx + 1, 0);
904 for (std::size_t e =
lsn.eshift + 1; e <=
lsn.eshift +
lsn.nentries; ++e) {
905 const std::size_t t =
lsn.parent[e];
906 if (t <= lsn.tshift || t >
lsn.tshift +
lsn.ntasks)
continue;
907 if (cache_of_slot[task_slot(t, 0)] == LN_NONE)
continue;
908 const std::size_t drv = bound[e];
909 const bool is_item = e <
lsn.itemproc.size() && !
lsn.itemproc[e].empty();
912 if (!is_item)
continue;
913 if (drv == 0 || succ_act[drv].size() < 2)
915 "' has no cache access with a hit and a miss branch");
917 for (std::size_t m = 0; m < repl[t]; ++m) ca.caches.push_back(cache_of_slot[task_slot(t, m)]);
918 ca.hit = succ_act[drv][0];
919 ca.miss = succ_act[drv][1];
921 std::size_t card = e <
lsn.nitems.size() ?
lsn.nitems[e] : 0;
922 if (card == 0) card = caches[ca.caches[0]].nitems;
924 for (std::size_t i = 0; i < card; ++i) {
925 const double v = (i <
lsn.itemproc[e].size())
927 if (std::isnan(v) || v < 0.0)
928 throw InputError(
"SolverLDES (native LN engine): ItemEntry '" +
lsn.names[e] +
929 "' has a popularity that is not a pmf");
931 ca.cdf.push_back(
sum);
934 throw InputError(
"SolverLDES (native LN engine): ItemEntry '" +
lsn.names[e] +
935 "' has a popularity of zero total mass");
936 for (
double& c : ca.cdf) c /=
sum;
937 access_of_driver[drv] = accesses.size();
938 branch_reply[ca.hit] = e;
939 branch_reply[ca.miss] = e;
940 accesses.push_back(ca);
944 for (std::size_t a = a_lo; a <= a_hi; ++a) {
945 if (access_of_driver[a] != LN_NONE)
continue;
946 for (std::size_t s : succ_act[a])
949 "' reads a cache, but no ItemEntry of a cache task with "
950 "a popularity is bound to it");
960 std::vector<std::vector<double> > A;
961 std::vector<double> b;
962 std::map<std::size_t, int> col;
964 std::vector<Lincon> lincon(nidx + 1);
965 std::vector<bool> has_lincon(nidx + 1,
false);
966 for (std::size_t k = 1; k <
lsn.lincon_A.size() && k <= nidx; ++k) {
968 if (A.
rows() == 0)
continue;
969 const bool host = k >
lsn.hshift && k <=
lsn.hshift +
lsn.nhosts;
970 const std::vector<std::size_t>* ops =
nullptr;
971 if (host && k <
lsn.tasksof.size()) ops = &
lsn.tasksof[k];
972 if (!host && k <
lsn.entriesof.size()) ops = &
lsn.entriesof[k];
973 const std::size_t ncols = ops ? ops->size() : 0;
974 if (A.
cols() != ncols)
975 throw InputError(
"SolverLDES (native LN engine): the admission constraint on '" +
976 lsn.names[k] +
"' has " + std::to_string(A.
cols()) +
977 " columns but the server has " + std::to_string(ncols) +
" operands");
978 if (ncols == 0)
continue;
980 lc.A.assign(A.
rows(), std::vector<double>(ncols, 0.0));
981 for (std::size_t r = 0; r < A.
rows(); ++r)
983 for (std::size_t r = 0; r < A.
rows(); ++r)
984 lc.b.push_back((k <
lsn.lincon_b.size() && r <
lsn.lincon_b[k].size())
986 : std::numeric_limits<double>::infinity());
987 for (std::size_t c = 0; c < ncols; ++c) lc.col[(*ops)[c]] =
static_cast<int>(c);
989 has_lincon[k] =
true;
991 auto lincon_col = [&](std::size_t elem, std::size_t operand) ->
int {
992 if (!has_lincon[elem])
return -1;
993 const std::map<std::size_t, int>::const_iterator it = lincon[elem].col.find(operand);
994 return it == lincon[elem].col.end() ? -1 : it->second;
997 auto admits = [&](std::size_t elem,
const std::vector<int>& occ,
int col) {
998 const Lincon& lc = lincon[elem];
999 for (std::size_t r = 0; r < lc.A.size(); ++r) {
1000 double lhs = lc.A[r][
static_cast<std::size_t
>(col)];
1001 for (std::size_t k = 0; k < occ.size(); ++k) lhs += lc.A[r][k] * occ[k];
1002 if (lhs > lc.b[r] + 1e-9)
return false;
1013 std::vector<bool> pooled(nidx + 1,
false);
1014 std::vector<std::vector<double> > pool_capacity(nidx + 1);
1015 std::vector<std::vector<std::vector<bool> > > pool_serves(nidx + 1);
1016 std::vector<std::map<std::size_t, int> > pool_col(nidx + 1);
1017 for (std::size_t h =
lsn.hshift + 1; h <=
lsn.hshift +
lsn.nhosts; ++h) {
1018 if (h >=
lsn.pools.size() ||
lsn.pools[h].npools() == 0)
continue;
1019 if (host_sched[h] == SchedStrategy::INF)
continue;
1021 const std::size_t np = P.npools(),
nc = P.compat.cols();
1022 std::size_t declared = 0;
1023 for (std::size_t p = 0; p < np; ++p) {
1024 const double cnt = (p < P.counts.size()) ? P.counts[p] : 0.0;
1025 const std::size_t c = cnt > 0.0 ?
static_cast<std::size_t
>(cnt) : 0;
1027 pool_capacity[h].push_back(
static_cast<double>(c) * (r > 0.0 ? r : 1.0));
1029 std::vector<bool> sv(
nc,
false);
1030 for (std::size_t j = 0; j <
nc; ++j)
1032 pool_serves[h].push_back(sv);
1034 if (declared != host_servers[h])
1035 throw InputError(
"SolverLDES (native LN engine): processor '" +
lsn.names[h] +
1036 "' declares server pools holding " + std::to_string(declared) +
1037 " servers but has multiplicity " +
1038 (host_servers[h] == std::numeric_limits<std::size_t>::max()
1039 ? std::string(
"Inf") : std::to_string(host_servers[h])) +
1040 "; the pools must partition the servers");
1041 const std::vector<std::size_t>& ops =
1042 (h <
lsn.tasksof.size()) ?
lsn.tasksof[h] : std::vector<std::size_t>();
1043 for (std::size_t j = 0; j < ops.size() && j <
nc; ++j) pool_col[h][ops[j]] =
static_cast<int>(j);
1049 std::priority_queue<LnEvent, std::vector<LnEvent>, LnEventLater> evq;
1050 std::uint64_t seq = 0;
1053 std::deque<LnLine> lines;
1054 std::deque<LnInv> invs;
1055 std::vector<std::size_t> free_lines, free_invs;
1056 std::vector<LnCustomer> custs;
1059 std::vector<std::size_t> host_busy(nhost_slots, 0);
1060 std::vector<std::deque<std::size_t>> host_queue(nhost_slots);
1062 std::vector<std::vector<std::size_t> > pool_jobs(nhost_slots);
1063 std::vector<double> pool_last(nhost_slots, 0.0), pool_rate(nhost_slots, 0.0);
1064 std::vector<std::uint64_t> pool_gen(nhost_slots, 0);
1065 std::vector<std::size_t> host_of_slot(nhost_slots, 0);
1066 for (std::size_t h =
lsn.hshift + 1; h <=
lsn.hshift +
lsn.nhosts; ++h)
1067 for (std::size_t m = 0; m < repl[h]; ++m) host_of_slot[host_slot0[h] + m] = h;
1069 std::vector<std::vector<int> > hocc(nhost_slots), tocc(ntask_slots);
1070 std::vector<std::deque<std::size_t> > hadm(nhost_slots), tadm(ntask_slots);
1071 for (std::size_t h =
lsn.hshift + 1; h <=
lsn.hshift +
lsn.nhosts; ++h)
1073 for (std::size_t m = 0; m < repl[h]; ++m)
1074 hocc[host_slot0[h] + m].assign(lincon[h].A[0].size(), 0);
1078 std::vector<std::vector<bool>> thr_held(ntask_slots);
1079 std::vector<std::vector<ThreadState>> thr_state(ntask_slots);
1080 std::vector<std::vector<double>> thr_setup_t0(ntask_slots);
1081 std::vector<std::vector<std::uint64_t>> thr_gen(ntask_slots);
1082 std::vector<std::deque<std::size_t>> thr_queue(ntask_slots);
1083 std::vector<std::size_t> thr_owner(ntask_slots, 0);
1084 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
1085 const std::size_t n = (task_threads[t] == std::numeric_limits<std::size_t>::max())
1086 ? 0 : task_threads[t];
1087 for (std::size_t m = 0; m < repl[t]; ++m) {
1088 const std::size_t ts = task_slot(t, m);
1090 thr_held[ts].assign(n,
false);
1091 thr_state[ts].assign(n, ThreadState::ACTIVE);
1092 thr_setup_t0[ts].assign(n, 0.0);
1093 thr_gen[ts].assign(n, 0);
1094 if (has_lincon[t]) tocc[ts].assign(lincon[t].A[0].size(), 0);
1101 std::vector<Sampler> setupd(nidx + 1), delayoffd(nidx + 1);
1102 std::vector<bool> has_setup(nidx + 1,
false);
1103 std::vector<Rng> g_setup, g_doff;
1104 g_setup.reserve(nidx + 1);
1105 g_doff.reserve(nidx + 1);
1106 for (std::size_t k = 0; k <= nidx; ++k) {
1107 g_setup.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10 + 2000));
1108 g_doff.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10 + 3000));
1110 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
1111 if (t >=
lsn.hassetup.size() || !
lsn.hassetup[t])
continue;
1112 const bool su = t <
lsn.setuptime.size() && !
lsn.setuptime[t].disabled &&
1114 const bool df = t <
lsn.delayofftime.size() && !
lsn.delayofftime[t].disabled &&
1116 if (!su || !df)
continue;
1117 setupd[t] = Sampler(
lsn.setuptime[t],
"the setup time of '" +
lsn.names[t] +
"'");
1118 delayoffd[t] = Sampler(
lsn.delayofftime[t],
"the delay-off time of '" +
lsn.names[t] +
"'");
1119 has_setup[t] =
true;
1129 std::vector<Rng> g_route, g_item, g_rr;
1130 g_route.reserve(nidx + 1);
1131 for (std::size_t k = 0; k <= nidx; ++k)
1132 g_route.push_back(
engine::ln_rng(ln_seed, 940000 +
static_cast<long long>(k) * 10));
1133 for (std::size_t c = 0; c < caches.size(); ++c) {
1134 g_item.push_back(
engine::ln_rng(ln_seed, 950000 +
static_cast<long long>(c) * 10));
1135 g_rr.push_back(
engine::ln_rng(ln_seed, 960000 +
static_cast<long long>(c) * 10));
1145 std::vector<std::vector<std::size_t> > fwd_dst(nidx + 1);
1146 std::vector<std::vector<double> > fwd_prob(nidx + 1);
1147 for (std::size_t c = 1; c <=
lsn.ncalls && c <
lsn.calltype.size(); ++c) {
1149 const std::size_t src =
lsn.callpair_src[c], dst =
lsn.callpair_dst[c];
1150 if (src == 0 || dst == 0 || src > nidx || dst > nidx)
continue;
1151 fwd_dst[src].push_back(dst);
1152 fwd_prob[src].push_back(c <
lsn.callproc_mean.size()
1156 std::vector<Rng> g_fwd;
1157 g_fwd.reserve(nidx + 1);
1158 for (std::size_t k = 0; k <= nidx; ++k)
1159 g_fwd.push_back(
engine::ln_rng(ln_seed, 970000 +
static_cast<long long>(k) * 10));
1163 std::deque<std::size_t> runnable;
1173 auto task_prio = [&](std::size_t task) ->
int {
1174 return (task <
lsn.prio.size()) ?
lsn.prio[task] : 0;
1176 auto host_req_prio = [&](std::size_t lid) ->
int {
return task_prio(
lsn.parent[lines[lid].act]); };
1177 auto task_req_prio = [&](std::size_t vi) ->
int {
1178 const std::size_t c = invs[vi].caller;
1179 return c == LN_NONE ? 0 : task_prio(invs[lines[c].inv].task);
1185 auto fcfs_prio = [](SchedStrategy sc) {
1187 return sc == SchedStrategy::HOL || sc == SchedStrategy::FCFSPRPRIO ||
1188 sc == SchedStrategy::FCFSPIPRIO;
1190 auto lcfs_prio = [](SchedStrategy sc) {
1191 return sc == SchedStrategy::LCFSPRIO || sc == SchedStrategy::LCFSPRPRIO ||
1192 sc == SchedStrategy::LCFSPIPRIO;
1194 auto preemptive = [](SchedStrategy sc) {
1195 return sc == SchedStrategy::FCFSPRPRIO || sc == SchedStrategy::FCFSPIPRIO ||
1196 sc == SchedStrategy::LCFSPRPRIO || sc == SchedStrategy::LCFSPIPRIO;
1198 auto is_ps = [](SchedStrategy sc) {
1199 return sc == SchedStrategy::PS || sc == SchedStrategy::PSPRIO;
1206 auto prio_enqueue = [](std::deque<std::size_t>& q, std::size_t id,
1207 const std::function<int(std::size_t)>& prio_of,
bool lifo,
1208 const std::function<double(std::size_t)>* key_of) {
1209 const int prio = prio_of(
id);
1210 const double key = key_of ? (*key_of)(id) : 0.0;
1211 std::deque<std::size_t>::iterator it = q.end();
1212 while (it != q.begin()) {
1213 const std::size_t o = *(it - 1);
1214 const int po = prio_of(o);
1215 if (po > prio)
break;
1217 if (!lifo && (!key_of || (*key_of)(o) <= key))
break;
1218 if (lifo && key_of && (*key_of)(o) > key)
break;
1229 auto enqueue = [&](std::deque<std::size_t>& q, std::size_t id, SchedStrategy sc,
1230 const std::function<int(std::size_t)>& prio_of,
double& key, Rng& g_siro,
1231 const std::function<double(std::size_t)>* key_of) {
1232 if (fcfs_prio(sc) || lcfs_prio(sc)) {
1233 prio_enqueue(q,
id, prio_of, lcfs_prio(sc), key_of);
1234 }
else if (sc == SchedStrategy::LCFS) {
1237 if (sc == SchedStrategy::SIRO) key = uniform01(g_siro);
1242 auto dequeue = [&](std::deque<std::size_t>& q, SchedStrategy sc,
1243 const std::function<double(std::size_t)>& key_of) -> std::size_t {
1244 std::deque<std::size_t>::iterator best = q.begin();
1245 if (sc == SchedStrategy::SIRO)
1246 for (std::deque<std::size_t>::iterator it = q.begin(); it != q.end(); ++it)
1247 if (key_of(*it) < key_of(*best)) best = it;
1248 const std::size_t
id = *best;
1252 const std::function<double(std::size_t)> line_key = [&](std::size_t lid) {
1253 return lines[lid].siro_key;
1255 const std::function<double(std::size_t)> inv_key = [&](std::size_t vi) {
1256 return invs[vi].siro_key;
1258 const std::function<double(std::size_t)> host_key = [&](std::size_t lid) {
1259 return lines[lid].host_t0;
1268 std::vector<std::vector<std::size_t> > ps_jobs(nhost_slots);
1269 std::vector<double> ps_last(nhost_slots, 0.0);
1270 std::vector<std::uint64_t> ps_gen(nhost_slots, 0);
1276 auto ps_rates = [&](std::size_t hs) -> std::vector<double> {
1277 const std::vector<std::size_t>& pj = ps_jobs[hs];
1278 const std::size_t n = pj.size();
1279 const std::size_t host = host_of_slot[hs];
1280 const double c =
static_cast<double>(host_servers[host]);
1281 if (host_sched[host] != SchedStrategy::PSPRIO ||
static_cast<double>(n) <= c)
1282 return std::vector<double>(n, n == 0 ? 0.0 : std::min(1.0, c /
static_cast<double>(n)));
1283 std::vector<double> rate(n, 0.0);
1284 std::vector<int> pr(n);
1285 for (std::size_t i = 0; i < n; ++i) pr[i] = task_prio(
lsn.parent[lines[pj[i]].act]);
1286 std::vector<int> levels(pr);
1287 std::sort(levels.begin(), levels.end(), std::greater<int>());
1288 levels.erase(std::unique(levels.begin(), levels.end()), levels.end());
1290 for (std::size_t l = 0; l < levels.size() && left > 0.0; ++l) {
1291 std::size_t cnt = 0;
1292 for (std::size_t i = 0; i < n; ++i) cnt += (pr[i] == levels[l]) ? 1 : 0;
1293 const double alloc = std::min(left,
static_cast<double>(cnt));
1294 for (std::size_t i = 0; i < n; ++i)
1295 if (pr[i] == levels[l]) rate[i] = alloc /
static_cast<double>(cnt);
1301 auto ps_advance = [&](std::size_t hs) {
1302 const double dt = now - ps_last[hs];
1304 const std::vector<double> rate = ps_rates(hs);
1305 for (std::size_t i = 0; i < ps_jobs[hs].size(); ++i) {
1306 LnLine& L = lines[ps_jobs[hs][i]];
1307 L.remaining = std::max(0.0, L.remaining - dt * rate[i]);
1313 auto ps_busy = [&](std::size_t hs, std::size_t n) ->
double {
1314 return static_cast<double>(std::min(n, host_servers[host_of_slot[hs]]));
1318 std::vector<double> tot_q(nidx + 1, 0.0), tot_u(nidx + 1, 0.0);
1319 std::vector<double> cur_q(nidx + 1, 0.0), cur_u(nidx + 1, 0.0);
1320 std::vector<double> last_upd(nidx + 1, 0.0);
1321 std::vector<double> completions(nidx + 1, 0.0), resp_sum(nidx + 1, 0.0),
1322 resp_cnt(nidx + 1, 0.0);
1326 std::vector<std::vector<double> > entry_resp_samples(
lsn.nentries);
1329 std::vector<double> call_busy(
lsn.ncalls + 1, 0.0), call_disp(
lsn.ncalls + 1, 0.0);
1330 std::vector<double> ecls_busy(
lsn.nentries, 0.0);
1338 std::vector<double> act_host_resid(nidx + 1, 0.0);
1343 std::vector<Rng> g_callmult;
1344 g_callmult.reserve(
lsn.ncalls + 1);
1345 for (std::size_t c = 0; c <=
lsn.ncalls; ++c)
1346 g_callmult.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(c) * 10 + 4000));
1357 std::vector<std::vector<std::size_t> > cg_members;
1358 std::vector<lang::RoutingStrategy> cg_strategy;
1359 std::vector<double> cg_mean;
1360 std::vector<std::vector<std::size_t> > cg_next;
1361 std::vector<long> cg_of(
lsn.ncalls + 1, -1);
1362 std::vector<bool> cg_lead(
lsn.ncalls + 1,
false);
1364 if (grp.
caller >=
lsn.callsof.size())
continue;
1365 const std::vector<std::size_t>& calls =
lsn.callsof[grp.
caller];
1366 std::vector<std::size_t> members;
1368 for (std::size_t target : grp.
targets) {
1369 for (std::size_t cidx : calls) {
1371 cg_of[cidx] < 0 && std::find(members.begin(), members.end(), cidx) == members.end()) {
1372 members.push_back(cidx);
1378 if (members.size() < 2)
continue;
1379 const long g =
static_cast<long>(cg_members.size());
1380 for (std::size_t cidx : members) cg_of[cidx] = g;
1381 for (std::size_t cidx : calls)
1382 if (cg_of[cidx] == g) {
1383 cg_lead[cidx] =
true;
1386 cg_members.push_back(members);
1387 cg_strategy.push_back(grp.
strategy);
1388 cg_mean.push_back(mean);
1389 cg_next.push_back(std::vector<std::size_t>(std::max<std::size_t>(1, repl[
lsn.parent[grp.
caller]]), 0));
1392 std::vector<Rng> g_cgcount;
1393 for (std::size_t g = 0; g < cg_members.size(); ++g)
1394 g_cgcount.push_back(
engine::ln_rng(ln_seed, 930000 +
static_cast<long long>(g)));
1397 std::vector<std::size_t> tslot_q(ntask_slots, 0);
1407 auto sample_call_count = [&](std::size_t cidx) -> std::size_t {
1408 const double m = (cidx <
lsn.callproc_mean.size())
1411 if (!(m > 0.0))
return 0;
1412 std::size_t n =
static_cast<std::size_t
>(std::floor(m));
1413 const double frac = m -
static_cast<double>(n);
1414 if (frac > 0.0 && cidx < g_callmult.size() && uniform01(g_callmult[cidx]) < frac) ++n;
1418 auto sample_group_count = [&](std::size_t g) -> std::size_t {
1419 const double m = cg_mean[g];
1420 if (!(m > 0.0))
return 0;
1421 std::size_t n =
static_cast<std::size_t
>(std::floor(m));
1422 const double frac = m -
static_cast<double>(n);
1423 if (frac > 0.0 && uniform01(g_cgcount[g]) < frac) ++n;
1428 auto dispatch_group = [&](std::size_t g, std::size_t caller_task, std::size_t crep,
1429 std::size_t& rep) -> std::size_t {
1430 const std::vector<std::size_t>& members = cg_members[g];
1432 std::vector<std::size_t>& next = cg_next[g];
1433 std::size_t& ptr = next[std::min(crep, next.size() - 1)];
1434 const std::size_t cidx = members[ptr];
1435 ptr = (ptr + 1) % members.size();
1436 rep = callee_replica(caller_task, crep,
lsn.parent[
lsn.callpair_dst[cidx]]);
1439 std::size_t best = members.front(), ties = 0, best_len = std::numeric_limits<std::size_t>::max();
1440 for (std::size_t cidx : members) {
1441 const std::size_t t =
lsn.parent[
lsn.callpair_dst[cidx]];
1442 const std::size_t r = callee_replica(caller_task, crep, t);
1443 const std::size_t len = tslot_q[task_slot(t, r)];
1444 if (len < best_len) {
1449 }
else if (len == best_len && uniform01(g_cgtie) *
static_cast<double>(++ties) < 1.0) {
1457 auto touch = [&](std::size_t k) {
1458 const double dt = now - last_upd[k];
1460 tot_q[k] += cur_q[k] * dt;
1461 tot_u[k] += cur_u[k] * dt;
1465 auto push_ev = [&](LnEvent e) {
1477 std::uint64_t done = 0;
1478 const std::uint64_t warm_events =
1479 static_cast<std::uint64_t
>(
static_cast<double>(max_events) * 0.2);
1481 const bool has_warm_deadline = std::isfinite(clock_budget);
1482 const auto warm_deadline = std::chrono::steady_clock::now() +
1483 std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(
1484 has_warm_deadline ? 0.2 * std::max(clock_budget, 0.0) : 0.0));
1485 std::uint64_t warm_done_at = 0;
1487 double warm_t = 0.0;
1488 long long cycles = 0;
1495 auto reset_stats = [&]() {
1498 warm_done_at = done;
1499 for (std::size_t k = 0; k <= nidx; ++k) {
1503 completions[k] = 0.0;
1506 act_host_resid[k] = 0.0;
1508 for (std::size_t e = 0; e < entry_resp_samples.size(); ++e) entry_resp_samples[e].clear();
1509 std::fill(call_busy.begin(), call_busy.end(), 0.0);
1510 std::fill(call_disp.begin(), call_disp.end(), 0.0);
1511 std::fill(ecls_busy.begin(), ecls_busy.end(), 0.0);
1512 for (std::size_t ci = 0; ci < caches.size(); ++ci) {
1513 caches[ci].reads = 0;
1514 caches[ci].hits = 0;
1515 caches[ci].misses = 0;
1516 caches[ci].delayed = 0;
1527 auto charge_hold = [&](std::size_t vi,
bool end) {
1528 LnInv& v = invs[vi];
1529 const double start = v.hold_t0;
1530 v.hold_t0 = end ? -1.0 : now;
1531 if (!warm || start < 0.0)
return;
1532 const double held = now - std::max(start, warm_t);
1533 if (!(held > 0.0))
return;
1534 if (v.call_idx >= 1 && v.call_idx <=
lsn.ncalls) {
1535 call_busy[v.call_idx] += held;
1536 }
else if (v.entry >
lsn.eshift && v.entry <=
lsn.eshift +
lsn.nentries) {
1537 ecls_busy[v.entry -
lsn.eshift - 1] += held;
1541 auto count_dispatch = [&](std::size_t vi, std::size_t cidx) {
1542 if (!invs[vi].pre && cidx >= 1 && cidx <=
lsn.ncalls) call_disp[cidx] += 1.0;
1546 auto new_line = [&](std::size_t inv, std::size_t act,
bool branch) -> std::size_t {
1548 if (!free_lines.empty()) {
1549 id = free_lines.back();
1550 free_lines.pop_back();
1551 lines[id] = LnLine();
1554 lines.push_back(LnLine());
1556 lines[id].inv = inv;
1557 lines[id].act = act;
1558 lines[id].calls_left = NOTDRAWN;
1559 lines[id].branch = branch;
1562 auto free_line = [&](std::size_t id) { free_lines.push_back(
id); };
1564 auto set_act = [&](std::size_t lid, std::size_t a) {
1565 LnLine& L = lines[lid];
1569 L.calls_left = NOTDRAWN;
1572 auto new_inv =[&](std::size_t entry, std::size_t trep, std::size_t caller,
1573 std::size_t cust) -> std::size_t {
1575 const bool pre = !warm || (caller != LN_NONE && invs[lines[caller].inv].pre);
1577 if (!free_invs.empty()) {
1578 id = free_invs.back();
1579 free_invs.pop_back();
1583 invs.push_back(LnInv());
1585 LnInv& v = invs[id];
1587 v.task =
lsn.parent[entry];
1589 v.ts = task_slot(v.task, trep);
1594 const std::size_t root = new_line(
id, bound[entry],
false);
1595 invs[id].root = root;
1603 auto enter_task = [&](std::size_t vi) {
1604 LnInv& v = invs[vi];
1607 cur_q[v.task] += 1.0;
1615 auto arrive_at_task = [&](std::size_t vi) ->
bool {
1616 LnInv& v = invs[vi];
1617 const int col = lincon_col(v.task, v.entry);
1620 if (!admits(v.task, tocc[v.ts], col)) {
1621 tadm[v.ts].push_back(vi);
1624 ++tocc[v.ts][
static_cast<std::size_t
>(col)];
1631 std::function<void(std::size_t)> advance;
1633 auto drain = [&]() {
1634 while (!runnable.empty()) {
1635 const std::size_t
id = runnable.front();
1636 runnable.pop_front();
1642 std::function<void(std::size_t)> advance_pool;
1643 auto pool_rates = [&](std::size_t hs) {
1644 const std::size_t h = host_of_slot[hs];
1645 const std::vector<std::size_t>& jobs = pool_jobs[hs];
1646 std::vector<double> rate(jobs.size(), 0.0);
1647 for (std::size_t p = 0; p < pool_capacity[h].size(); ++p)
1648 for (std::size_t i = 0; i < jobs.size(); ++i) {
1649 const int j = lines[jobs[i]].pcol;
1650 if (j >= 0 &&
static_cast<std::size_t
>(j) < pool_serves[h][p].size() &&
1651 pool_serves[h][p][
static_cast<std::size_t
>(j)]) {
1652 rate[i] += pool_capacity[h][p];
1659 advance_pool = [&](std::size_t hs) {
1660 const std::vector<double> r = pool_rates(hs);
1661 const double dt = now - pool_last[hs];
1662 for (std::size_t i = 0; i < pool_jobs[hs].size(); ++i) {
1663 LnLine& L = lines[pool_jobs[hs][i]];
1664 L.remaining = std::max(0.0, L.remaining - dt * r[i]);
1666 pool_last[hs] = now;
1673 auto rearm_pool = [&](std::size_t hs) {
1674 const std::size_t h = host_of_slot[hs];
1675 const std::vector<double> r = pool_rates(hs);
1676 double tot = 0.0, tmin = std::numeric_limits<double>::infinity();
1677 for (std::size_t i = 0; i < r.size(); ++i) {
1679 if (r[i] > 0.0) tmin = std::min(tmin, lines[pool_jobs[hs][i]].remaining / r[i]);
1682 pool_rate[hs] = tot;
1684 for (std::size_t m = 0; m < repl[h]; ++m) u += pool_rate[host_slot0[h] + m];
1686 const std::uint64_t g = ++pool_gen[hs];
1687 if (std::isfinite(tmin)) {
1697 auto ps_schedule = [&](std::size_t hs) {
1699 if (ps_jobs[hs].empty())
return;
1700 const std::vector<double> rate = ps_rates(hs);
1701 double tmin = std::numeric_limits<double>::infinity();
1702 for (std::size_t i = 0; i < ps_jobs[hs].size(); ++i)
1703 if (rate[i] > 0.0) tmin = std::min(tmin, lines[ps_jobs[hs][i]].remaining / rate[i]);
1713 std::vector<std::vector<std::size_t> > in_service(nhost_slots);
1714 std::uint64_t host_gen_ctr = 0;
1716 std::uint64_t fork_ctr = 0;
1721 auto begin_host = [&](std::size_t lid, std::size_t hs) {
1722 LnLine& L = lines[lid];
1723 if (L.host_rem < 0.0) L.host_rem = hostdem[L.act].next(g_host[L.act]);
1725 L.host_end = now + L.host_rem;
1726 if (preemptive(host_sched[host_of_slot[hs]])) in_service[hs].push_back(lid);
1727 L.host_gen = ++host_gen_ctr;
1742 auto preempt_for = [&](std::size_t lid, std::size_t hs) ->
bool {
1743 const SchedStrategy sc = host_sched[host_of_slot[hs]];
1744 const bool lifo = lcfs_prio(sc);
1745 const int pa = host_req_prio(lid);
1746 std::vector<std::size_t>& ins = in_service[hs];
1747 std::size_t vi = ins.size();
1749 for (std::size_t i = 0; i < ins.size(); ++i) {
1750 const int pr = host_req_prio(ins[i]);
1751 if (pr >= pa)
continue;
1752 bool better = vi == ins.size() || pr < pv;
1753 if (!better && pr == pv) {
1754 const LnLine& r = lines[ins[i]];
1755 const LnLine& v = lines[ins[vi]];
1756 better = lifo ? r.host_t0 < v.host_t0 : r.host_start > v.host_start;
1763 if (vi == ins.size() && lifo)
1764 for (std::size_t i = 0; i < ins.size(); ++i)
1765 if (host_req_prio(ins[i]) == pa &&
1766 (vi == ins.size() || lines[ins[i]].host_start < lines[ins[vi]].host_start))
1768 if (vi == ins.size())
return false;
1769 const std::size_t victim = ins[vi];
1770 ins.erase(ins.begin() +
static_cast<std::ptrdiff_t
>(vi));
1771 LnLine& v = lines[victim];
1772 v.host_gen = ++host_gen_ctr;
1773 const bool resume = sc == SchedStrategy::FCFSPRPRIO || sc == SchedStrategy::LCFSPRPRIO;
1774 v.host_rem = resume ? std::max(0.0, v.host_end - now) : -1.0;
1775 enqueue(host_queue[hs], victim, sc, host_req_prio, v.siro_key, g_siro_host, &host_key);
1776 begin_host(lid, hs);
1780 auto start_host_service = [&](std::size_t lid) {
1781 LnLine& L = lines[lid];
1782 const std::size_t act = L.act;
1783 const std::size_t host =
lsn.host_of(act);
1784 const std::size_t hs = host_slot(host, invs[L.inv].trep);
1789 const std::size_t t =
lsn.parent[act];
1790 const std::map<std::size_t, int>::const_iterator it = pool_col[host].find(t);
1791 if (it == pool_col[host].end())
1792 throw InputError(
"SolverLDES (native LN engine): activity '" +
lsn.names[act] +
1793 "' runs on a host whose pool declaration does not name its "
1794 "task, so no server can be assigned");
1795 lines[lid].pcol = it->second;
1796 lines[lid].remaining = hostdem[act].next(g_host[act]);
1797 pool_jobs[hs].push_back(lid);
1801 lines[lid].host_rem = -1.0;
1802 if (is_ps(host_sched[host])) {
1805 const std::size_t n0 = ps_jobs[hs].size();
1806 lines[lid].remaining = hostdem[act].next(g_host[act]);
1807 ps_jobs[hs].push_back(lid);
1809 cur_u[host] += ps_busy(hs, n0 + 1) - ps_busy(hs, n0);
1813 if (host_busy[hs] < host_servers[host]) {
1816 begin_host(lid, hs);
1817 }
else if (!(preemptive(host_sched[host]) && preempt_for(lid, hs))) {
1818 enqueue(host_queue[hs], lid, host_sched[host], host_req_prio, lines[lid].siro_key,
1819 g_siro_host, &host_key);
1828 auto request_host = [&](std::size_t lid) {
1829 LnLine& L = lines[lid];
1830 const std::size_t act = L.act;
1831 const std::size_t host =
lsn.host_of(act);
1832 const std::size_t hs = host_slot(host, invs[L.inv].trep);
1834 const int col = lincon_col(host,
lsn.parent[act]);
1837 if (!admits(host, hocc[hs], col)) {
1838 hadm[hs].push_back(lid);
1841 ++hocc[hs][
static_cast<std::size_t
>(col)];
1843 start_host_service(lid);
1846 auto release_host_admission = [&](std::size_t lid) {
1847 LnLine& L = lines[lid];
1848 if (L.hcol < 0)
return;
1849 const std::size_t host =
lsn.host_of(L.act);
1850 const std::size_t hs = host_slot(host, invs[L.inv].trep);
1851 --hocc[hs][
static_cast<std::size_t
>(L.hcol)];
1853 while (!hadm[hs].empty()) {
1854 const std::size_t head = hadm[hs].front();
1855 const int c = lines[head].hcol;
1856 if (!admits(host, hocc[hs], c))
return;
1857 hadm[hs].pop_front();
1858 ++hocc[hs][
static_cast<std::size_t
>(c)];
1859 start_host_service(head);
1864 auto start_setup = [&](std::size_t ts, std::size_t th) {
1865 const std::size_t task = thr_owner[ts];
1866 thr_state[ts][th] = ThreadState::SETUP;
1867 thr_setup_t0[ts][th] = now;
1869 e.t = now + setupd[task].next(g_setup[task]);
1873 e.gen = ++thr_gen[ts][th];
1878 auto start_delayoff = [&](std::size_t ts, std::size_t th) {
1879 const std::size_t task = thr_owner[ts];
1880 thr_state[ts][th] = ThreadState::DELAYOFF;
1882 e.t = now + delayoffd[task].next(g_doff[task]);
1886 e.gen = ++thr_gen[ts][th];
1899 auto acquire_thread = [&](std::size_t vi) ->
bool {
1900 LnInv& v = invs[vi];
1901 const std::size_t task = v.task;
1902 const std::size_t ts = v.ts;
1904 if (task_threads[task] == std::numeric_limits<std::size_t>::max()) {
1906 v.has_thread =
true;
1911 std::vector<bool>& held = thr_held[ts];
1912 std::vector<ThreadState>& st = thr_state[ts];
1916 for (std::size_t th = 0; th < held.size(); ++th) {
1917 if (!held[th] && st[th] == ThreadState::ACTIVE) {
1920 v.has_thread =
true;
1926 for (std::size_t th = 0; th < held.size(); ++th) {
1927 if (!held[th] && st[th] == ThreadState::DELAYOFF) {
1929 st[th] = ThreadState::ACTIVE;
1932 v.has_thread =
true;
1939 v.cold_wait =
static_cast<std::size_t
>(std::count(held.begin(), held.end(),
true)) < held.size();
1940 enqueue(thr_queue[ts], vi, task_sched[task], task_req_prio, invs[vi].siro_key, g_siro_task,
1942 for (std::size_t th = 0; th < held.size(); ++th) {
1943 if (!held[th] && st[th] == ThreadState::OFF) {
1944 start_setup(ts, th);
1952 auto release_thread = [&](std::size_t vi) {
1953 LnInv& v = invs[vi];
1954 const std::size_t ts = v.ts;
1955 const std::size_t th = v.th;
1956 const std::size_t task = thr_owner[ts];
1959 charge_hold(vi,
true);
1960 v.has_thread =
false;
1962 if (th == NOTHR)
return;
1963 thr_held[ts][th] =
false;
1964 if (!thr_queue[ts].empty()) {
1965 const std::size_t nxt = dequeue(thr_queue[ts], task_sched[task], inv_key);
1966 thr_held[ts][th] =
true;
1968 invs[nxt].has_thread =
true;
1969 invs[nxt].hold_t0 = now;
1972 if (invs[nxt].cold_wait && !invs[nxt].started) {
1973 LnInv& w = invs[nxt];
1975 w.entry_t0 = w.task_t0;
1977 tot_q[w.entry] += now - std::max(w.entry_t0, warm_t);
1978 cur_q[w.entry] += 1.0;
1980 runnable.push_back(invs[nxt].root);
1981 }
else if (has_setup[task]) {
1982 start_delayoff(ts, th);
1997 auto finish_act = [&](LnLine& L) {
1998 const std::size_t act = L.act;
1999 const bool seen = !invs[L.inv].pre;
2000 if (seen) completions[act] += 1.0;
2001 if (L.act_t0 >= 0.0) {
2003 resp_sum[act] += now - L.act_t0;
2004 resp_cnt[act] += 1.0;
2023 auto send_reply = [&](std::size_t vi,
bool direct) {
2024 LnInv& v = invs[vi];
2025 if (v.replied)
return;
2027 v.early_reply = !direct;
2029 completions[v.entry] += 1.0;
2030 const double r = now - v.entry_t0;
2031 resp_sum[v.entry] += r;
2032 resp_cnt[v.entry] += 1.0;
2036 if (v.entry >
lsn.eshift && v.entry <=
lsn.eshift +
lsn.nentries)
2037 entry_resp_samples[v.entry -
lsn.eshift - 1].push_back(r);
2043 if (v.caller != LN_NONE && !fwd_dst[v.entry].empty()) {
2044 const double u = uniform01(g_fwd[v.entry]);
2046 for (std::size_t k = 0; k < fwd_dst[v.entry].size(); ++k) {
2047 cum += fwd_prob[v.entry][k];
2049 const std::size_t tgt = fwd_dst[v.entry][k];
2050 const std::size_t caller = v.caller;
2052 const std::size_t rep = callee_replica(v.task, v.trep,
lsn.parent[tgt]);
2054 const std::size_t child = new_inv(tgt, rep, caller, LN_NONE);
2055 if (arrive_at_task(child)) runnable.push_back(invs[child].root);
2060 if (!direct && v.caller != LN_NONE) runnable.push_back(v.caller);
2065 auto cache_insert = [&](std::size_t ci, std::size_t item) {
2066 LnCacheState& cs = caches[ci];
2067 std::list<std::size_t>& lst = cs.levels[0];
2068 const std::size_t cap =
static_cast<std::size_t
>(std::max(0, cs.cap[0]));
2070 if (lst.size() >= cap && !lst.empty()) {
2071 std::size_t r =
static_cast<std::size_t
>(uniform01(g_rr[ci]) *
2072 static_cast<double>(lst.size()));
2073 r = std::min(r, lst.size() - 1);
2074 std::list<std::size_t>::iterator it = lst.begin();
2075 std::advance(it,
static_cast<long>(r));
2078 lst.push_front(item);
2081 lst.push_front(item);
2082 if (lst.size() > cap) lst.pop_back();
2086 auto cache_promote = [&](std::size_t ci, std::size_t item, std::size_t level) {
2087 LnCacheState& cs = caches[ci];
2089 const std::size_t inew = std::min(level + 1, cs.levels.size() - 1);
2093 if (inew <= level) {
2095 cs.levels[level].remove(item);
2096 cs.levels[level].push_front(item);
2100 std::list<std::size_t>& lo = cs.levels[level];
2101 std::list<std::size_t>& hi = cs.levels[inew];
2102 std::size_t kpos = 0;
2103 for (std::list<std::size_t>::iterator it = lo.begin(); it != lo.end(); ++it, ++kpos)
2104 if (*it == item)
break;
2106 if (hi.size() <
static_cast<std::size_t
>(std::max(0, cs.cap[inew]))) {
2107 hi.push_front(item);
2110 std::size_t demoted;
2112 std::size_t r =
static_cast<std::size_t
>(uniform01(g_rr[ci]) *
2113 static_cast<double>(hi.size()));
2114 r = std::min(r, hi.size() - 1);
2115 std::list<std::size_t>::iterator it = hi.begin();
2116 std::advance(it,
static_cast<long>(r));
2120 demoted = hi.back();
2122 hi.push_front(item);
2125 lo.push_front(demoted);
2127 std::list<std::size_t>::iterator it = lo.begin();
2128 std::advance(it,
static_cast<long>(std::min(kpos, lo.size())));
2129 lo.insert(it, demoted);
2132 static const std::size_t HELD =
static_cast<std::size_t
>(-2);
2139 auto access_cache = [&](std::size_t ai, std::size_t lid) -> std::size_t {
2140 const LnCacheAccess& ca = accesses[ai];
2141 LnInv& v = invs[lines[lid].inv];
2142 const std::size_t ci = ca.caches[std::min(v.trep, ca.caches.size() - 1)];
2143 LnCacheState& cs = caches[ci];
2145 const bool seen = !v.pre;
2146 if (seen) ++cs.reads;
2147 const double u = uniform01(g_item[ci]);
2148 std::size_t item = ca.cdf.size() - 1;
2149 for (std::size_t i = 0; i < ca.cdf.size(); ++i)
2150 if (u <= ca.cdf[i]) {
2154 for (std::size_t l = 0; l < cs.levels.size(); ++l)
2155 if (std::find(cs.levels[l].begin(), cs.levels[l].end(), item) != cs.levels[l].end()) {
2156 if (seen) ++cs.hits;
2157 cache_promote(ci, item, l);
2161 if (item < cs.inflight.size() && cs.inflight[item]) {
2162 cs.held[item].push_back(std::make_pair(lid, ai));
2165 if (seen) ++cs.misses;
2166 cs.inflight[item] = 1;
2169 v.fetch_item = item;
2172 if (seen) ++cs.misses;
2173 cache_insert(ci, item);
2177 auto release_fetch = [&](std::size_t vi) {
2178 LnInv& v = invs[vi];
2179 if (!v.fetching)
return;
2181 LnCacheState& cs = caches[v.fetch_cache];
2182 const std::size_t item = v.fetch_item;
2183 cs.inflight[item] = 0;
2184 cache_insert(v.fetch_cache, item);
2185 std::vector<std::pair<std::size_t, std::size_t> > held;
2186 held.swap(cs.held[item]);
2189 for (std::size_t k = 0; k < held.size(); ++k) {
2190 if (!invs[lines[held[k].first].inv].pre) ++caches[v.fetch_cache].delayed;
2191 set_act(held[k].first, accesses[held[k].second].hit);
2192 runnable.push_back(held[k].first);
2203 auto complete_request = [&](std::size_t vi) -> std::size_t {
2204 send_reply(vi,
true);
2206 LnInv& v = invs[vi];
2208 cur_q[v.entry] -= 1.0;
2209 const bool seen = !v.pre;
2210 if (seen) completions[v.task] += 1.0;
2212 cur_q[v.task] -= 1.0;
2216 const std::size_t ts = v.ts, task = v.task;
2217 --tocc[ts][
static_cast<std::size_t
>(v.tcol)];
2219 while (!tadm[ts].empty()) {
2220 const std::size_t head = tadm[ts].front();
2221 const int c = invs[head].tcol;
2222 if (!admits(task, tocc[ts], c))
break;
2223 tadm[ts].pop_front();
2224 ++tocc[ts][
static_cast<std::size_t
>(c)];
2226 runnable.push_back(invs[head].root);
2229 LnInv& w = invs[vi];
2230 const std::size_t caller = w.caller, cust = w.cust;
2231 const bool early = w.early_reply;
2233 free_invs.push_back(vi);
2234 if (cust != LN_NONE) {
2238 const std::size_t rt = custs[cust].ref_task;
2240 resp_sum[rt] += now - custs[cust].t_start;
2241 resp_cnt[rt] += 1.0;
2245 e.t = now + (has_think[rt] ? think[rt].next(g_think[rt]) : 0.0);
2251 return (caller != LN_NONE && !early) ? caller : LN_NONE;
2255 auto finish_line = [&](std::size_t lid) -> std::size_t {
2256 const std::size_t vi = lines[lid].inv;
2257 LnInv& v = invs[vi];
2258 const bool br = lines[lid].branch;
2259 if (v.pending < 0)
return complete_request(vi);
2260 const long rem = v.pending - 1;
2261 if (br) free_line(lid);
2263 invs[vi].pending = -1;
2264 return complete_request(vi);
2266 invs[vi].pending = rem;
2278 auto complete_activity = [&](std::size_t lid) -> std::size_t {
2279 finish_act(lines[lid]);
2283 if (!warm && (done >= warm_events ||
2284 (has_warm_deadline && std::chrono::steady_clock::now() >= warm_deadline)))
2286 const std::size_t act = lines[lid].act;
2287 const std::size_t vi = lines[lid].inv;
2288 if (access_of_driver[act] != LN_NONE) {
2289 const std::size_t nx = access_cache(access_of_driver[act], lid);
2290 if (nx == HELD)
return LN_NONE;
2294 const std::vector<std::size_t>& sc = succ_act[act];
2295 const bool br = lines[lid].branch;
2298 const std::size_t j = sc[0];
2299 const std::uint64_t fid = lines[lid].fork_id;
2303 LnJoin& jn = invs[vi].joins[j];
2304 std::map<std::pair<std::size_t, std::uint64_t>,
long>::iterator it =
2305 jn.discard.find(std::make_pair(act, fid));
2306 if (it == jn.discard.end()) it = jn.discard.find(std::make_pair(act, LN_ANYFORK));
2307 if (it != jn.discard.end()) {
2308 if (--it->second <= 0) jn.discard.erase(it);
2309 if (jn.got.empty() && jn.discard.empty()) invs[vi].joins.erase(j);
2310 return finish_line(lid);
2313 LnJoin& jn = invs[vi].joins[j];
2315 if (br) free_line(lid);
2316 const std::size_t req = join_quorum[j] > 0 ? join_quorum[j] : 1;
2317 if (jn.got.size() >= req) {
2320 std::uint64_t rf = jn.got.begin()->second;
2321 for (
const std::pair<const std::size_t, std::uint64_t>& g : jn.got)
2322 if (g.second != rf) rf = LN_ANYFORK;
2323 for (std::size_t in : join_inputs[j])
2324 if (jn.got.find(in) == jn.got.end()) ++jn.discard[std::make_pair(in, rf)];
2326 if (jn.discard.empty()) invs[vi].joins.erase(j);
2327 if (invs[vi].pending >= 0) invs[vi].pending -=
static_cast<long>(req - 1);
2328 set_act(invs[vi].root, j);
2329 return invs[vi].root;
2333 if (!invs[vi].replied && !br) {
2334 bool reply = branch_reply[act] != 0 && branch_reply[act] == invs[vi].entry;
2335 if (!reply && phase_of(act) == 1)
2336 for (std::size_t s : sc)
2337 if (phase_of(s) == 2) {
2344 if (!reply && sc.empty() && invs[vi].pending > 1) reply =
true;
2345 if (reply) send_reply(vi,
false);
2347 if (invs[vi].replied && invs[vi].fetching) release_fetch(vi);
2348 if (sc.empty())
return finish_line(lid);
2349 if (sc.size() == 1) {
2350 set_act(lid, sc[0]);
2353 if (!or_prob[act].empty()) {
2354 const double u = uniform01(g_route[act]);
2356 std::size_t pick = sc.back();
2357 for (std::size_t k = 0; k < sc.size(); ++k) {
2358 cum += or_prob[act][k];
2369 invs[vi].pending = (invs[vi].pending < 0 ? 1 : invs[vi].pending) +
2370 static_cast<long>(sc.size()) - 1;
2371 if (br) free_line(lid);
2372 const std::uint64_t fork = ++fork_ctr;
2373 std::vector<std::size_t> kids;
2374 for (std::size_t k = 0; k < sc.size(); ++k) {
2375 kids.push_back(new_line(vi, sc[k],
true));
2376 lines[kids.back()].fork_id = fork;
2378 for (std::size_t k = kids.size(); k-- > 1;) runnable.push_front(kids[k]);
2386 auto spawn_request = [&](std::size_t entry, std::size_t rep,
bool pre, std::size_t cidx) {
2387 const std::size_t vi = new_inv(entry, rep, LN_NONE, LN_NONE);
2389 if (pre) invs[vi].pre =
true;
2390 invs[vi].call_idx = cidx;
2391 if (arrive_at_task(vi)) runnable.push_back(invs[vi].root);
2398 advance = [&](std::size_t lid) {
2399 while (lid != LN_NONE) {
2400 const std::size_t vi = lines[lid].inv;
2403 if (!invs[vi].has_thread) {
2404 if (!acquire_thread(vi))
return;
2408 if (!invs[vi].started) {
2409 LnInv& v = invs[vi];
2413 cur_q[v.entry] += 1.0;
2415 LnLine& L = lines[lid];
2416 const std::size_t act = L.act;
2418 lid = complete_request(vi);
2421 if (L.act_t0 < 0.0) {
2429 if (L.dem_done == 0) {
2431 if (has_hostdem[act]) {
2438 if (L.dem_done == 1) {
2440 if (has_actthink[act]) {
2442 e.t = now + actthink[act].next(g_actthink[act]);
2449 const std::vector<std::size_t>& calls =
2450 (act <
lsn.callsof.size()) ?
lsn.callsof[act] : std::vector<std::size_t>();
2451 if (L.call_pos < calls.size()) {
2454 const std::size_t cidx = calls[L.call_pos];
2455 const long grp = cg_of[cidx];
2456 if (grp >= 0 && !cg_lead[cidx]) {
2461 if (L.calls_left == NOTDRAWN)
2462 L.calls_left = (grp >= 0) ? sample_group_count(
static_cast<std::size_t
>(grp)) : sample_call_count(cidx);
2463 if (L.calls_left == 0) {
2465 L.calls_left = NOTDRAWN;
2469 if (L.calls_left == 0) {
2471 L.calls_left = NOTDRAWN;
2474 std::size_t rcidx = cidx;
2477 rcidx = dispatch_group(
static_cast<std::size_t
>(grp), invs[vi].task, invs[vi].trep, rep);
2478 const std::size_t callee =
lsn.callpair_dst[rcidx];
2479 if (grp < 0) rep = callee_replica(invs[vi].task, invs[vi].trep,
lsn.parent[callee]);
2480 count_dispatch(vi, rcidx);
2484 const std::size_t child = new_inv(callee, rep, lid, LN_NONE);
2485 invs[child].call_idx = rcidx;
2486 if (!arrive_at_task(child))
return;
2487 lid = invs[child].root;
2492 spawn_request(callee, rep, invs[vi].pre, rcidx);
2498 L.calls_left = NOTDRAWN;
2499 lid = complete_activity(lid);
2506 auto start_cycle = [&](std::size_t c) {
2507 const std::size_t t = custs[c].ref_task;
2508 custs[c].t_start = now;
2509 const std::size_t vi = new_inv(
lsn.entriesof[t][0], custs[c].repl, LN_NONE, c);
2510 if (arrive_at_task(vi)) advance(invs[vi].root);
2512 bool any_ref =
false;
2513 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks; ++t) {
2514 if (t >=
lsn.isref.size() || !
lsn.isref[t])
continue;
2516 const std::size_t n = (task_threads[t] == std::numeric_limits<std::size_t>::max())
2519 const std::vector<std::size_t>& es =
2520 (t <
lsn.entriesof.size()) ?
lsn.entriesof[t] : std::vector<std::size_t>();
2522 throw InputError(
"SolverLDES (native LN engine): reference task '" +
lsn.names[t] +
2523 "' declares no entry to invoke");
2526 for (std::size_t m = 0; m < repl[t]; ++m) {
2527 for (std::size_t k = 0; k < n; ++k) {
2531 custs.push_back(cu);
2532 start_cycle(custs.size() - 1);
2539 std::vector<Sampler> arrival(nidx + 1);
2540 bool any_arrival =
false;
2541 std::vector<Rng> g_arr;
2542 g_arr.reserve(nidx + 1);
2543 for (std::size_t k = 0; k <= nidx; ++k)
2544 g_arr.push_back(
engine::ln_rng(ln_seed,
static_cast<long long>(k) * 10 + 5000));
2545 for (std::size_t e =
lsn.eshift + 1; e <=
lsn.eshift +
lsn.nentries; ++e) {
2546 if (e >=
lsn.has_arrival.size() || !
lsn.has_arrival[e] || e >=
lsn.arrival.size() ||
2547 lsn.arrival[e].disabled)
2549 arrival[e] = Sampler(
lsn.arrival[e],
"the open arrivals at '" +
lsn.names[e] +
"'");
2551 for (std::size_t m = 0; m < repl[
lsn.parent[e]]; ++m) {
2553 ev.t = now + arrival[e].next(g_arr[e]);
2563 if (!any_ref && !any_arrival)
2564 throw InputError(
"SolverLDES (native LN engine): the model has no reference task and no "
2565 "open arrival, so nothing drives it");
2568 std::uint32_t wall_tick = 0;
2569 while (!evq.empty() && done < max_events) {
2572 time_stopped =
true;
2575 const LnEvent ev = evq.top();
2582 start_cycle(ev.who);
2587 if (ev.kind == 2 || ev.kind == 3) {
2588 const std::size_t ts = ev.who, th = ev.aux;
2589 if (ev.gen != thr_gen[ts][th])
continue;
2591 if (thr_state[ts][th] == ThreadState::DELAYOFF)
2592 thr_state[ts][th] = ThreadState::OFF;
2597 thr_state[ts][th] = ThreadState::ACTIVE;
2598 if (!thr_held[ts][th] && !thr_queue[ts].empty()) {
2599 const std::size_t nxt = dequeue(thr_queue[ts], task_sched[thr_owner[ts]], inv_key);
2600 thr_held[ts][th] =
true;
2601 touch(thr_owner[ts]);
2602 cur_u[thr_owner[ts]] += 1.0;
2603 invs[nxt].has_thread =
true;
2604 invs[nxt].hold_t0 = now;
2611 if (!invs[nxt].started) {
2612 LnInv& w = invs[nxt];
2614 w.entry_t0 = w.cold_wait ? w.task_t0 : std::max(w.task_t0, thr_setup_t0[ts][th]);
2617 tot_q[w.entry] += now - std::max(w.entry_t0, warm_t);
2618 cur_q[w.entry] += 1.0;
2620 advance(invs[nxt].root);
2628 const std::size_t hs = ev.who;
2629 if (ev.gen != pool_gen[hs])
continue;
2630 const std::size_t host = host_of_slot[hs];
2632 std::vector<std::size_t> finished, keep;
2633 for (std::size_t lid : pool_jobs[hs])
2634 (lines[lid].remaining <= 1e-9 ? finished : keep).push_back(lid);
2635 pool_jobs[hs].swap(keep);
2637 for (std::size_t lid : finished) {
2640 if (!invs[lines[lid].inv].pre) {
2641 completions[host] += 1.0;
2642 act_host_resid[lines[lid].act] += now - lines[lid].host_t0;
2644 release_host_admission(lid);
2653 const std::size_t hs = ev.who;
2654 if (ev.gen != ps_gen[hs])
continue;
2655 const std::size_t host = host_of_slot[hs];
2657 std::vector<std::size_t> finished, keep;
2658 for (std::size_t lid : ps_jobs[hs])
2659 (lines[lid].remaining <= 1e-9 ? finished : keep).push_back(lid);
2660 const std::size_t n0 = ps_jobs[hs].size();
2661 ps_jobs[hs].swap(keep);
2663 cur_u[host] -= ps_busy(hs, n0) - ps_busy(hs, ps_jobs[hs].size());
2665 for (std::size_t lid : finished) {
2668 if (!invs[lines[lid].inv].pre) {
2669 completions[host] += 1.0;
2670 act_host_resid[lines[lid].act] += now - lines[lid].host_t0;
2672 release_host_admission(lid);
2688 spawn_request(ev.who, ev.aux,
false, 0);
2690 nx.t = now + arrival[ev.who].next(g_arr[ev.who]);
2697 const std::size_t lid = ev.who;
2698 if (ev.gen != lines[lid].host_gen)
continue;
2699 const std::size_t act = lines[lid].act;
2700 const std::size_t host =
lsn.host_of(act);
2703 const std::size_t hs = host_slot(host, invs[lines[lid].inv].trep);
2708 if (!invs[lines[lid].inv].pre) {
2709 completions[host] += 1.0;
2710 act_host_resid[act] += now - lines[lid].host_t0;
2712 if (preemptive(host_sched[host]))
2713 in_service[hs].erase(std::find(in_service[hs].begin(), in_service[hs].end(), lid));
2714 if (!host_queue[hs].empty()) {
2715 const std::size_t nxt = dequeue(host_queue[hs], host_sched[host], line_key);
2718 begin_host(nxt, hs);
2720 release_host_admission(lid);
2730 if (!time_stopped && done < max_events && evq.empty()) {
2731 for (std::size_t s = 0; s < nhost_slots; ++s)
2732 if (!hadm[s].empty())
2733 throw InputError(
"SolverLDES (native LN engine): the admission constraint on "
2734 "host '" +
lsn.names[host_of_slot[s]] +
"' deadlocked the model: " +
2735 std::to_string(hadm[s].size()) +
2736 " request(s) are blocked with no event left");
2737 for (std::size_t s = 0; s < ntask_slots; ++s)
2738 if (!tadm[s].empty())
2739 throw InputError(
"SolverLDES (native LN engine): the admission constraint on "
2740 "task '" +
lsn.names[thr_owner[s]] +
"' deadlocked the model: " +
2741 std::to_string(tadm[s].size()) +
2742 " request(s) are blocked with no event left");
2749 if (time_stopped && (!warm || done <= warm_done_at || !(now - warm_t > 0.0)))
2751 "SolverLDES (layered engine): the wall-clock budget ran out after " + std::to_string(done) +
2752 " activity completions, before any were measured past the warmup; no valid partial "
2753 "result exists. Raise the budget (--maxtime / options.timeout).");
2756 for (std::size_t k = 1; k <= nidx; ++k) touch(k);
2758 const double span = now - warm_t;
2770 res.WLN =
Matrix<double>(nidx + 1, 1, std::numeric_limits<double>::quiet_NaN());
2771 for (std::size_t k = 1; k <= nidx; ++k) {
2773 res.QLN(k, 0) = tot_q[k] / span;
2786 const bool is_task = (k >
lsn.tshift && k <=
lsn.tshift +
lsn.ntasks);
2787 const std::size_t cap_k = is_task ? task_threads[k] : host_servers[k];
2788 double c = (cap_k == std::numeric_limits<std::size_t>::max())
2790 :
static_cast<double>(is_task ? cap_k * repl[k] : cap_k);
2792 if (!is_task && k <
lsn.maxmult.size() &&
lsn.maxmult[k] > 0.0 &&
lsn.maxmult[k] < c)
2794 res.ULN(k, 0) = tot_u[k] / (span * c);
2795 if (is_task) res.UThreadLN(k, 0) = res.ULN(k, 0);
2801 if (is_task) res.QLN(k, 0) = tot_u[k] / span;
2802 res.TLN(k, 0) = completions[k] / span;
2804 if (is_task && cap_k != std::numeric_limits<std::size_t>::max() && res.TLN(k, 0) > 0.0) {
2805 const double pool =
static_cast<double>(cap_k * repl[k]);
2806 res.ZLN(k, 0) = std::max(0.0, (pool - tot_u[k] / span) / res.TLN(k, 0));
2808 if (k >
lsn.eshift && k <=
lsn.eshift +
lsn.nentries) res.ALN(k, 0) = res.TLN(k, 0);
2810 if (resp_cnt[k] > 0.0) res.RLN(k, 0) = resp_sum[k] / resp_cnt[k];
2812 res.entry_resp_samples.swap(entry_resp_samples);
2817 for (std::size_t vi = 0; vi < invs.size(); ++vi)
2818 if (invs[vi].has_thread) charge_hold(vi,
false);
2819 res.UCallLN.assign(std::max<std::size_t>(1,
lsn.ncalls), 0.0);
2820 res.TCallLN.assign(std::max<std::size_t>(1,
lsn.ncalls), 0.0);
2821 res.UEntryClassLN.assign(std::max<std::size_t>(1,
lsn.nentries), 0.0);
2823 for (std::size_t c = 1; c <=
lsn.ncalls; ++c) {
2824 res.UCallLN[c - 1] = call_busy[c] / span;
2825 res.TCallLN[c - 1] = call_disp[c] / span;
2827 for (std::size_t e = 0; e <
lsn.nentries; ++e) res.UEntryClassLN[e] = ecls_busy[e] / span;
2844 std::vector<double> task_resid(nidx + 1, 0.0);
2845 std::vector<bool> task_has_act(nidx + 1,
false);
2846 for (std::size_t a =
lsn.ashift + 1; a <=
lsn.ashift +
lsn.nacts && a <= nidx; ++a) {
2847 const std::size_t task =
lsn.parent[a];
2848 const double x_task = (task >= 1 && task <= nidx) ? res.TLN(task, 0) : 0.0;
2849 const double resid = (x_task > 0.0) ? act_host_resid[a] / (span * x_task) : 0.0;
2850 res.WLN(a, 0) = resid;
2851 if (task >= 1 && task <= nidx) {
2852 task_resid[task] += resid;
2853 task_has_act[task] =
true;
2856 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks && t <= nidx; ++t)
2857 if (task_has_act[t]) res.WLN(t, 0) = task_resid[t];
2876 auto host_mult = [&](std::size_t k) ->
double {
return lsn.host_reach_mult(k); };
2877 std::vector<double> proc_util(nidx + 1, 0.0), task_proc_util(nidx + 1, 0.0);
2878 for (std::size_t a =
lsn.ashift + 1; a <=
lsn.ashift +
lsn.nacts && a <= nidx; ++a) {
2879 const double d = (a <
lsn.hostdem.size() && !
lsn.hostdem[a].disabled)
2882 proc_util[a] = res.TLN(a, 0) * d;
2883 const std::size_t task =
lsn.parent[a];
2884 if (task >= 1 && task <= nidx) task_proc_util[task] += proc_util[a];
2886 for (std::size_t t =
lsn.tshift + 1; t <=
lsn.tshift +
lsn.ntasks && t <= nidx; ++t)
2887 res.ULN(t, 0) = task_proc_util[t] / host_mult(t);
2888 for (std::size_t e =
lsn.eshift + 1; e <=
lsn.eshift +
lsn.nentries && e <= nidx; ++e) {
2890 const std::vector<std::size_t>& acts =
2891 (e <
lsn.actsof.size()) ?
lsn.actsof[e] : std::vector<std::size_t>();
2892 for (std::size_t i = 0; i < acts.size(); ++i)
2893 if (acts[i] <= nidx) u += proc_util[acts[i]];
2894 res.ULN(e, 0) = u / host_mult(e);
2899 for (std::size_t a =
lsn.ashift + 1; a <=
lsn.ashift +
lsn.nacts && a <= nidx; ++a)
2900 res.ULN(a, 0) = proc_util[a] / host_mult(a);
2904 for (std::size_t ai = 0; ai < accesses.size(); ++ai) {
2905 long long reads = 0, hits = 0, misses = 0, delayed = 0;
2906 for (std::size_t ci : accesses[ai].caches) {
2907 reads += caches[ci].reads;
2908 hits += caches[ci].hits;
2909 misses += caches[ci].misses;
2910 delayed += caches[ci].delayed;
2912 if (reads <= 0 || !(span > 0.0))
continue;
2913 const double n =
static_cast<double>(reads);
2914 res.cache_task.push_back(
lsn.parent[accesses[ai].entry]);
2915 res.cache_entry.push_back(accesses[ai].entry);
2916 res.cache_hit.push_back(
static_cast<double>(hits) / n);
2917 res.cache_miss.push_back(
static_cast<double>(misses) / n);
2918 res.cache_delayed.push_back(
static_cast<double>(delayed) / n);
2919 res.cache_read_rate.push_back(n / span);
2922 res.simulated_time = span;
2923 res.warmup_time = warm_t;
2924 res.completions = cycles;
2925 res.events =
static_cast<long long>(done);
2926 res.timed_out = time_stopped;