503 return {
"srvn",
"srvn.ph",
"srvn.cs",
"flat",
"flat.cs",
"flat.ph",
"moment3",
"default"};
515 opt.layer_ssa.verbose =
false;
517 for (
char& ch : opt.interlock_refpath_scope)
518 ch =
char(std::tolower(
static_cast<unsigned char>(ch)));
519 if (opt.interlock_refpath_scope !=
"merging" && opt.interlock_refpath_scope !=
"all")
520 throw InputError(
"Unknown config.interlock_refpath_scope '" +
521 opt.interlock_refpath_scope +
"', use 'merging' or 'all'.");
522 if (std::isnan(opt.interlock_maxpaths) || opt.interlock_maxpaths < 0.0)
523 throw InputError(
"config.interlock_maxpaths must be a non-negative number.");
533 if (opt.method ==
"mw.upper" || opt.method ==
"mw.lower")
return box_bounds();
537 return is_ph_encoding() ? aggregate_ph() : aggregate();
553 if (lqn.nentries == 0)
return entrycdfrespt;
554 if (entrycdfrespt.size() <= lqn.nentries || entrycdfrespt[1].empty()) {
561 if (is_ph_encoding())
563 "getCdfRespT needs the routing encoding of the activity graph, which "
564 "method='srvn.ph' does not build. Rebuild the solver with "
565 "method='srvn.cs' or method='moment3'.");
572 const std::string saved = opt.method;
573 const std::string saved_resolved = lnmethod;
574 opt.method =
"moment3";
575 lnmethod =
"moment3";
579 lnmethod = saved_resolved;
581 return entrycdfrespt;
592 if (opt.ln_transient ==
"decoupled")
return tran_avg_decoupled();
593 if (opt.ln_transient ==
"coupled")
return tran_avg_coupled();
594 throw InputError(
"SolverLN: unknown ln_transient mode '" + opt.ln_transient +
595 "' (use 'coupled' or 'decoupled')");
611 if (results.empty()) iterate();
615 for (std::size_t e = 0; e < ensemble.size(); ++e) {
618 const bool exact_available =
619 opt.layer_solver ==
"mva" || opt.layer_solver ==
"nc";
621 ensemble[e], so, exact_available && !fj_tr[e].active(),
622 [
this, e]() {
return this->solve_layer(e); });
626 row.layer = ensemble[e].name;
633 out.
rows.push_back(row);
640 if (m.empty())
continue;
656 const bool upper = opt.method !=
"mw.lower";
661 const std::size_t N = lqn.nidx;
662 auto blank = [&](std::vector<T>& v, std::vector<bool>& d) {
663 v.assign(N + 1, Tzero());
664 d.assign(N + 1,
false);
672 for (std::size_t i = 1; i <= N; ++i) {
673 if (b.defined_T[i]) {
674 s.
TN[i] = upper ? b.TN_up[i] : b.TN_lo[i];
677 if (b.defined_U[i]) {
678 s.
UN[i] = upper ? b.UN_up[i] : b.UN_lo[i];
688 std::size_t
nlayers()
const {
return ensemble.size(); }
689 const std::vector<qn::Layer<T>>&
layers()
const {
return ensemble; }
701 const std::size_t E = ensemble.size();
706 for (std::size_t e = 0; e < E; ++e) {
709 b.
msz[e] = ensemble[e].nstations;
710 b.
ksz[e] = ensemble[e].nclasses;
740 const std::size_t E = ensemble.size();
741 layer_tran_init.assign(E, std::vector<double>());
742 if (n.
empty())
return;
746 "SolverLN::init_from_marginal: the marginal is " + std::to_string(n.
rows()) +
"x" +
747 std::to_string(n.
cols()) +
" where the block-diagonal union of the layers is " +
748 std::to_string(b.
M) +
"x" + std::to_string(b.
K) +
749 "; the aggregate view is layerBlocks', not the LQN element count");
750 for (std::size_t e = 0; e < E; ++e) {
753 std::vector<double> y(lay.
nstates, 0.0);
755 for (std::size_t i = 0; i < L.
nstations && i < b.
msz[e]; ++i)
756 for (std::size_t r = 0; r < L.
nclasses && r < b.
ksz[e]; ++r) {
757 if (!lay.
enabled[i][r])
continue;
758 const double q = std::max(0.0, n(b.
roff[e] + i, b.
coff[e] + r));
759 y[lay.
qidx[i][r]] = q;
760 if (q > 0.0) any =
true;
766 if (any) layer_tran_init[e] = y;
811 std::vector<qn::Layer<T>> ensemble;
812 std::vector<long> idxhash;
813 std::vector<bool> ignore;
817 std::size_t idx, aidx, node, cls;
819 std::vector<UpdRow> servt_map, thinkt_map, call_map, actthinkt_map;
829 std::vector<UpdRow> arv_call_map;
832 std::size_t idx, tidx_caller, eidx, nodefrom, nodeto, cfrom, cto;
834 std::vector<RouteRow> route_map;
835 std::vector<std::size_t> unique_route_idx;
836 std::vector<std::size_t> route_reset, svc_reset;
839 std::string interlockMethod =
"ilrate";
847 std::size_t idx, node, cls;
851 std::size_t fromentry;
853 std::vector<RefPathRow> refpath_map;
855 std::vector<std::array<std::size_t, 3>> refpath_keys;
856 std::vector<std::size_t> refpath_group;
858 std::vector<double> refpath_stage_prev;
859 std::vector<T> refpath_stage_prev_v;
865 std::map<std::size_t, std::vector<std::size_t>> layer_normclass;
867 std::vector<T> entryvisits;
869 Matrix<double> njobs;
872 std::vector<T> servt, residt, tput, util, thinkt;
873 std::vector<T> callservt, callresidt;
880 bool has_phase2 =
false;
881 std::vector<T> servt_ph1, servt_ph2, prOvertake;
883 std::vector<T> entry_servt_last, entry_servt_ph2;
885 std::set<std::size_t> single_replica_tasks;
886 std::vector<Distrib<T>> servtproc, thinkproc, thinktproc, tputproc, callservtproc;
892 std::vector<fluid::FluidPassage> servtcdf, callservtcdf;
893 std::vector<LnCdf> entrycdfrespt;
894 std::vector<mam::AphPair<T>> entryproc;
896 std::vector<std::vector<std::vector<fluid::FluidPassage>>> cdf_repo;
897 std::vector<double> servt_prev, residt_prev, tput_prev, thinkt_prev;
898 std::vector<double> callservt_prev, callresidt_prev;
899 std::vector<T> servt_prev_v, residt_prev_v, tput_prev_v, thinkt_prev_v, callservt_prev_v;
900 Matrix<T> servtmatrix;
903 Matrix<T> il_all, il_ph1;
904 std::vector<std::vector<std::size_t>> il_common_entries, il_src_all, il_src_ph2;
910 std::vector<std::vector<std::vector<double>>> layer_interlock;
911 std::vector<double> il_num_sources;
913 std::vector<std::vector<LayerResult<T>>> results;
914 std::vector<Matrix<T>> layer_init_sol;
919 std::vector<std::vector<double>> layer_tran_init;
931 std::vector<mva::FjMmt<T>> fj_tr;
932 std::vector<std::vector<T>> fj_lambda;
933 double relax_omega = 1.0;
935 std::shared_ptr<LnStochController<T>> stoch_ctl;
936 long averagingstart = -1;
937 bool hasconverged =
false;
939 bool moment_pass_done =
false;
940 std::vector<double> maxitererr;
941 int iterations_done = 0;
942 bool did_converge =
false;
944 std::size_t NT()
const {
return lqn.tshift + lqn.ntasks; }
946 static T Tzero() {
return num_traits<T>::from_int(0); }
947 static T Tone() {
return num_traits<T>::from_int(1); }
948 static double dbl(
const T& x) {
return num_traits<T>::to_double(x); }
960 reject_unsupported();
963 const std::size_t N = lqn.nidx;
964 ignore.assign(N + 1,
false);
967 std::vector<long> comp(N + 1, -1);
969 for (std::size_t v = 1; v <= N; ++v) {
970 if (comp[v] >= 0)
continue;
971 std::vector<std::size_t> stack{v};
973 while (!stack.empty()) {
974 const std::size_t u = stack.back();
976 for (std::size_t w : lqn.graph.succ(u))
981 for (std::size_t w : lqn.graph.pred(u))
990 std::vector<bool> has_ref(ncomp,
false);
991 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
992 const std::size_t tidx = lqn.tshift + t;
993 if (lqn.sched[tidx] == SchedStrategy::REF) has_ref[comp[tidx]] =
true;
995 for (std::size_t v = 1; v <= N; ++v)
996 if (!has_ref[comp[v]]) ignore[v] =
true;
1003 for (std::size_t i = 1; i <= N; ++i) {
1004 servtproc[i] = lqn.hostdem[i];
1005 thinkproc[i] = lqn.think[i];
1008 for (std::size_t c = 1; c <= lqn.ncalls; ++c)
1009 callservtproc[c] = lqn.hostdem[lqn.callpair_dst[c]];
1013 entryvisits.assign(N + 1, Tone());
1014 refpath_map.clear();
1015 layer_normclass.clear();
1017 njobs = Matrix<double>(NT() + 1, NT() + 1, 0.0);
1021 std::vector<bool> rr(NT() + 1,
false), sr(NT() + 1,
false);
1022 for (
const RouteRow& r : route_map) rr[r.idx] =
true;
1023 for (
const UpdRow& r : thinkt_map) sr[r.idx] =
true;
1024 for (
const UpdRow& r : call_map) sr[r.idx] =
true;
1030 for (
const RefPathRow& r : refpath_map) sr[r.idx] =
true;
1033 for (
const UpdRow& r : arv_call_map) rr[r.idx] =
true;
1034 for (std::size_t i = 1; i <= NT(); ++i) {
1035 if (rr[i] && idxhash[i] >= 0) route_reset.push_back(std::size_t(idxhash[i]));
1036 if (sr[i] && idxhash[i] >= 0) svc_reset.push_back(std::size_t(idxhash[i]));
1038 std::sort(route_reset.begin(), route_reset.end());
1039 route_reset.erase(std::unique(route_reset.begin(), route_reset.end()), route_reset.end());
1040 std::sort(svc_reset.begin(), svc_reset.end());
1041 svc_reset.erase(std::unique(svc_reset.begin(), svc_reset.end()), svc_reset.end());
1044 refpath_keys.clear();
1045 refpath_group.clear();
1046 for (
const RefPathRow& r : refpath_map) refpath_keys.push_back({r.idx, r.node, r.cls});
1047 std::sort(refpath_keys.begin(), refpath_keys.end());
1048 refpath_keys.erase(std::unique(refpath_keys.begin(), refpath_keys.end()),
1049 refpath_keys.end());
1050 for (
const RefPathRow& r : refpath_map) {
1051 const std::array<std::size_t, 3> key{r.idx, r.node, r.cls};
1052 refpath_group.push_back(std::size_t(
1053 std::lower_bound(refpath_keys.begin(), refpath_keys.end(), key) -
1054 refpath_keys.begin()));
1056 if (refpath_map.empty() && interlockMethod ==
"refpath") {
1063 interlockMethod =
"ilrate";
1065 refpath_stage_prev.assign(refpath_keys.size(), std::numeric_limits<double>::quiet_NaN());
1066 refpath_stage_prev_v.assign(refpath_keys.size(), Tzero());
1074 void detect_phase2() {
1076 for (std::size_t a = 1; a <= lqn.nacts; ++a)
1077 if (lqn.actphase[a] > 1) has_phase2 =
true;
1089 bool is_fwd_target(std::size_t eidx)
const {
1090 for (std::size_t c = 1; c <= lqn.ncalls; ++c)
1091 if (lqn.calltype[c] == CallType::FWD && lqn.callpair_dst[c] == eidx)
return true;
1108 double open_arrival_rate_of(std::size_t tidx)
const {
1109 if (lqn.isref[tidx])
return 0.0;
1110 for (std::size_t e : lqn.entriesof[tidx])
1111 if (lqn.issynccaller.any_col(e) || lqn.isasynccaller.any_col(e) || is_fwd_target(e))
1114 for (std::size_t e : lqn.entriesof[tidx]) {
1115 if (!lqn.has_arrival[e])
continue;
1116 const double m = dbl(lqn.arrival[e].mean);
1123 bool has_cache_node(std::size_t e)
const {
1124 for (std::size_t n = 0; n < ensemble[e].nodes.size(); ++n)
1125 if (ensemble[e].nodes[n].nodetype == NodeType::Cache)
return true;
1130 bool async_only_activity(std::size_t aidx)
const {
1131 if (aidx <= lqn.ashift || aidx > lqn.ashift + lqn.nacts)
return false;
1132 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
1133 const std::size_t eidx = lqn.eshift + e;
1134 if (lqn.graph.get(eidx, aidx) == Tzero())
continue;
1135 return lqn.isasynccaller.any_col(eidx) && !lqn.issynccaller.any_col(eidx);
1150 void reject_unsupported()
const {}
1157 void resolve_interlock_method() {
1158 const LnInterlockChoice ch =
1160 interlockMethod = ch.method;
1161 if (!ch.why.empty()) std::cerr <<
"[LINE] Warning: " << ch.why <<
"\n";
1167 void build_layers() {
1174 const std::string requested = ln_requested_method(opt.method);
1175 assert_call_groups(requested ==
"flat.cs");
1176 if (requested ==
"flat.cs") {
1177 lnmethod =
"flat.cs";
1178 resolve_interlock_method();
1182 if (requested ==
"flat.ph") {
1187 ph_laws_ready =
false;
1188 lnmethod =
"flat.ph";
1189 build_layers_ph(
true);
1192 if (requested ==
"srvn.ph" || requested ==
"srvn") {
1193 ph_laws_ready =
false;
1194 if (requested ==
"srvn.ph" || probe_srvn_ph()) {
1195 lnmethod =
"srvn.ph";
1200 lnmethod = (requested ==
"moment3") ?
"moment3" :
"srvn.cs";
1201 resolve_interlock_method();
1202 std::vector<qn::Layer<T>> raw(NT() + 1);
1203 std::vector<bool> present(NT() + 1,
false);
1205 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx) {
1206 if (ignore[hidx])
continue;
1207 build_layer(raw[hidx], {hidx}, lqn.tasksof[hidx],
true,
false);
1208 present[hidx] =
true;
1210 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
1211 const std::size_t tidx = lqn.tshift + t;
1212 if (ignore[tidx] || lqn.isref[tidx])
continue;
1213 bool any_caller = lqn.iscaller.any_row(tidx) || lqn.iscaller.any_col(tidx);
1214 if (!any_caller)
continue;
1216 std::vector<std::size_t> callers;
1217 for (std::size_t ct = 1; ct <= lqn.ntasks; ++ct) {
1218 const std::size_t c = lqn.tshift + ct;
1220 for (std::size_t e : lqn.entriesof[tidx])
1221 if (lqn.iscaller.get(c, e)) calls =
true;
1222 if (calls) callers.push_back(c);
1224 if (callers.empty())
continue;
1225 build_layer(raw[tidx], {tidx}, callers,
false,
false);
1226 present[tidx] =
true;
1229 idxhash.assign(lqn.nidx + 1, -1);
1231 for (std::size_t i = 1; i <= NT(); ++i)
1233 idxhash[i] = next++;
1234 ensemble.push_back(std::move(raw[i]));
1236 layer_init_sol.assign(ensemble.size(), Matrix<T>());
1263 void assert_call_groups(
bool flat)
const {
1264 if (lqn.callgroups.empty())
return;
1267 "Call groups routed by a routing strategy require the squashed layering; use "
1268 "method='flat'. Under srvn the targets never share a submodel, so the dispatch "
1269 "order cannot be represented.");
1270 if (opt.layer_solver !=
"ssa")
1272 "Routed call groups need a layer solver that resolves the strategy from the "
1273 "state; set layer_solver='ssa'. MVA, NC and FLD read the routing matrix, into "
1274 "which refresh_routing has expanded the strategy as a uniform split, and would "
1275 "return that split under a round-robin or JSQ label.");
1278 std::vector<std::size_t> flat_server_set()
const {
1279 std::vector<std::size_t> servers;
1280 for (std::size_t i = 1; i <= NT(); ++i) {
1281 if (lqn.repl[i] > 1.0)
1283 "Flat layering does not support replicated processors or tasks, use the "
1284 "default 'srvn' layering.");
1287 "Flat layering does not support cache tasks, use the default 'srvn' "
1289 if (lqn.hassetup[i])
1291 "Flat layering does not support setup tasks, use the default 'srvn' "
1294 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx)
1295 if (!ignore[hidx] && !lqn.tasksof[hidx].empty()) servers.push_back(hidx);
1296 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
1297 const std::size_t tidx = lqn.tshift + t;
1298 if (ignore[tidx] || lqn.isref[tidx])
continue;
1299 if (!lqn.iscaller.any_row(tidx) && !lqn.iscaller.any_col(tidx))
continue;
1300 bool has_task_caller =
false;
1301 for (std::size_t eidx : lqn.entriesof[tidx])
1302 for (std::size_t c = 1; c <= lqn.ntasks; ++c)
1303 if (lqn.iscaller.get(lqn.tshift + c, eidx)) has_task_caller =
true;
1304 if (has_task_caller) servers.push_back(tidx);
1306 if (servers.empty())
1308 "Flat layering found no server: the model has no processor with tasks.");
1320 void build_flat_layer() {
1321 const std::vector<std::size_t> flat_servers = flat_server_set();
1322 std::vector<std::size_t> flat_callers;
1323 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
1324 const std::size_t tidx = lqn.tshift + t;
1325 if (!ignore[tidx]) flat_callers.push_back(tidx);
1328 build_layer(layer, flat_servers, flat_callers,
false,
true);
1330 ensemble.push_back(std::move(layer));
1331 idxhash.assign(lqn.nidx + 1, -1);
1332 for (std::size_t s : flat_servers) idxhash[s] = 0;
1333 layer_init_sol.assign(ensemble.size(), Matrix<T>());
1341 void build_fork_views() {
1342 fj_tr.assign(ensemble.size(), mva::FjMmt<T>());
1343 fj_lambda.assign(ensemble.size(), {});
1344 for (std::size_t e = 0; e < ensemble.size(); ++e) {
1345 if (!ensemble[e].has_fork())
continue;
1352 if (ensemble[e].sourceIdx != 0)
1354 "SolverLN: layer '" + ensemble[e].name +
1355 "' carries both an AND fork and an open stream (an async call or an entry "
1356 "arrival); the fork-join transform needs a Source of its own");
1358 fj_lambda[e].assign(fj_tr[e].V.classes.size() + 1,
1379 const std::shared_ptr<std::vector<std::vector<std::size_t>>>& chaincols, std::size_t R) {
1380 return [f, cols, chaincols, R](
const std::vector<T>& n) -> std::vector<T> {
1381 const std::size_t L = n.size();
1382 const std::vector<std::vector<std::size_t>>& use =
1383 (L == R || chaincols->empty()) ? cols : *chaincols;
1384 std::vector<T> nop(use.size(), num_traits<T>::from_int(0));
1385 for (std::size_t j = 0; j < use.size(); ++j)
1386 for (std::size_t k = 0; k < use[j].size(); ++k)
1387 if (use[j][k] < L) nop[j] = T(nop[j] + n[use[j][k]]);
1388 const std::vector<T> w = f(nop);
1389 std::vector<T> v(L, num_traits<T>::from_int(1));
1390 if (w.empty())
return v;
1391 for (std::size_t j = 0; j < use.size(); ++j) {
1392 const T wj = w[j < w.size() ? j : w.size() - 1];
1393 for (std::size_t k = 0; k < use[j].size(); ++k)
1394 if (use[j][k] < L) v[use[j][k]] = wj;
1401 static std::vector<T> layer_peak(
const std::vector<T>& peakPerOperand,
1402 const std::vector<std::vector<std::size_t>>& cols,
1404 std::vector<T> peak(R, num_traits<T>::from_int(1));
1405 if (peakPerOperand.empty())
return peak;
1406 for (std::size_t j = 0; j < cols.size(); ++j) {
1407 const T pj = peakPerOperand[j < peakPerOperand.size() ? j : peakPerOperand.size() - 1];
1408 for (std::size_t k = 0; k < cols[j].size(); ++k)
1409 if (cols[j][k] < R) peak[cols[j][k]] = pj;
1423 void apply_host_task_priorities(qn::Layer<T>& m,
const std::vector<std::size_t>& idxSet)
const {
1426 return s == SchedStrategy::HOL || s == SchedStrategy::FCFSPRPRIO ||
1427 s == SchedStrategy::FCFSPIPRIO || s == SchedStrategy::LCFSPRIO ||
1428 s == SchedStrategy::LCFSPRPRIO || s == SchedStrategy::LCFSPIPRIO ||
1429 s == SchedStrategy::PSPRIO || s == SchedStrategy::DPSPRIO ||
1430 s == SchedStrategy::GPSPRIO;
1432 const std::size_t R = m.classes.size();
1433 std::vector<std::size_t> owner(R + 1, 0);
1434 auto own = [&](std::size_t k, std::size_t t) {
1435 if (k >= 1 && k <= R && owner[k] == 0) owner[k] = t;
1437 for (
const auto& a : m.attr_tasks) own(a.first, a.second);
1438 for (
const auto& a : m.attr_entries) own(a.first, lqn.parent[a.second]);
1439 for (
const auto& a : m.attr_activities) own(a.first, lqn.parent[a.second]);
1440 for (
const auto& a : m.attr_calls) own(a[0], lqn.parent[a[2]]);
1441 for (std::size_t s : idxSet) {
1442 if (s < 1 || s > lqn.nhosts || !is_prio(lqn.sched[s]))
continue;
1444 for (std::size_t t = lqn.tshift + 1; t <= lqn.tshift + lqn.ntasks; ++t)
1445 if (lqn.parent[t] == s) pmax = std::max(pmax, lqn.prio[t]);
1446 for (std::size_t k = 1; k <= R; ++k)
1447 if (owner[k] != 0 && lqn.parent[owner[k]] == s)
1448 m.classes[k - 1].prio = pmax - lqn.prio[owner[k]];
1461 void build_layer(qn::Layer<T>& m,
const std::vector<std::size_t>& idxSet,
1462 const std::vector<std::size_t>& callers,
bool ishostlayer,
bool flat) {
1463 const T one = Tone();
1467 const std::size_t idx = idxSet[0];
1468 m.name = flat ? lqn.hashnames[idx] +
".Flat" : lqn.hashnames[idx];
1471 const double rawrepl = lqn.repl[idx];
1472 std::size_t nreplicas = 1;
1473 if (!flat && rawrepl > 1.0 && !callers.empty()) {
1482 for (std::size_t c : callers)
1483 if (lqn.repl[c] != rawrepl) reduce =
false;
1485 for (std::size_t c : callers)
1486 if (lqn.fanout_at(c, idx) < rawrepl) reduce =
false;
1488 nreplicas = reduce ? 1 :
static_cast<std::size_t
>(std::llround(rawrepl));
1489 if (reduce && !ishostlayer) single_replica_tasks.insert(idx);
1491 const bool reduce_fanout = (nreplicas == 1 && rawrepl > 1.0 && !callers.empty());
1492 const std::vector<double>& mult = lqn.maxmult;
1496 m.clientIdx = m.add_station(qn::Station<T>{
"Clients", NodeType::Delay, SchedStrategy::INF,
1497 std::numeric_limits<double>::infinity(),
false, 0});
1498 const std::size_t clientNode = m.node_of_station(m.clientIdx);
1499 m.serverIdx = m.clientIdx + 1;
1503 std::vector<std::vector<std::size_t>> srv(lqn.nidx + 1);
1504 std::vector<std::vector<std::size_t>> srvnode(lqn.nidx + 1);
1505 m.server_idx_of.assign(lqn.nidx + 1, 0);
1506 for (std::size_t sidx : idxSet) {
1507 const bool sishost = sidx <= lqn.nhosts;
1508 srv[sidx].resize(nreplicas);
1509 for (std::size_t r = 0; r < nreplicas; ++r) {
1511 st.name = r == 0 ? lqn.hashnames[sidx]
1512 : lqn.hashnames[sidx] +
"." + std::to_string(r + 1);
1515 lqn.sched[sidx] == SchedStrategy::INF ? NodeType::Delay : NodeType::Queue;
1516 st.sched = lqn.sched[sidx];
1518 st.nservers = lqn.sched[sidx] == SchedStrategy::INF
1519 ? std::numeric_limits<double>::infinity()
1521 st.attr_ishost = flat ? sishost : ishostlayer;
1523 srv[sidx][r] = m.add_station(st);
1524 srvnode[sidx].push_back(m.node_of_station(srv[sidx][r]));
1526 m.server_idx_of[sidx] = srv[sidx][0];
1528 m.host_stations.push_back(srv[sidx][0]);
1530 m.task_stations.push_back(srv[sidx][0]);
1533 const std::vector<std::size_t>& server = srv[idx];
1534 const std::vector<std::size_t>& serverNode = srvnode[idx];
1537 auto servers_for = [&](std::size_t elem) ->
const std::vector<std::size_t>& {
1538 static const std::vector<std::size_t> none;
1539 if (elem >= 1 && elem <= lqn.nidx && !srv[elem].empty())
return srv[elem];
1542 auto server_nodes_for = [&](std::size_t elem) ->
const std::vector<std::size_t>& {
1543 static const std::vector<std::size_t> none;
1544 if (elem >= 1 && elem <= lqn.nidx && !srvnode[elem].empty())
return srvnode[elem];
1548 auto host_is_server = [&](std::size_t tidx_) {
1549 return !servers_for(lqn.parent[tidx_]).empty();
1552 auto is_layer_client = [&](std::size_t tidx_) {
1553 for (std::size_t s : idxSet)
1554 for (std::size_t e : lqn.entriesof[s])
1555 if (lqn.issynccaller.get(tidx_, e))
return true;
1559 auto set_all_servers = [&](std::size_t cl,
const Distrib<T>& d) {
1560 for (std::size_t s : idxSet)
1561 for (std::size_t st : srv[s]) m.set_service(st, cl, d);
1569 std::vector<std::size_t> group_of_call(lqn.ncalls + 1, 0);
1570 std::vector<std::vector<std::size_t>> group_members(lqn.callgroups.size() + 1);
1571 for (std::size_t g = 0; g < lqn.callgroups.size(); ++g) {
1572 const LqnCallGroup& grp = lqn.callgroups[g];
1573 for (std::size_t tgt : grp.
targets)
1574 for (std::size_t cidx : lqn.callsof[grp.
caller])
1575 if (lqn.callpair_dst[cidx] == tgt && lqn.calltype[cidx] == CallType::SYNC &&
1576 group_of_call[cidx] == 0) {
1577 group_of_call[cidx] = g + 1;
1578 group_members[g + 1].push_back(cidx);
1583 std::vector<std::size_t> grp_router(lqn.callgroups.size() + 1, 0);
1584 std::vector<std::size_t> grp_dispatch(lqn.callgroups.size() + 1, 0);
1585 std::vector<std::size_t> grp_class(lqn.callgroups.size() + 1, 0);
1588 std::vector<std::array<std::size_t, 3>> routed_group_sites;
1591 std::vector<std::size_t> acts_in_caller;
1592 for (std::size_t c : callers)
1593 for (std::size_t a : lqn.actsof[c]) acts_in_caller.push_back(a);
1594 bool hasfork =
false, hasjoin =
false;
1595 std::size_t maxfanout = 1;
1596 for (std::size_t a : acts_in_caller) {
1597 if (lqn.actposttype[a] == PrecedenceType::POST_AND) hasfork =
true;
1598 if (lqn.actpretype[a] == PrecedenceType::PRE_AND) hasjoin =
true;
1599 std::size_t nand = 0;
1600 for (std::size_t sx : lqn.graph.succ(a))
1601 if (lqn.actposttype[sx] == PrecedenceType::POST_AND) ++nand;
1602 if (nand > maxfanout) maxfanout = nand;
1604 std::size_t forkNode = 0, joinNode = 0, joinStation = 0;
1605 std::vector<std::size_t> forkRouter;
1607 forkNode = m.add_node(
"Fork_PostAnd", NodeType::Fork,
false);
1608 for (std::size_t f = 1; f <= maxfanout; ++f)
1609 forkRouter.push_back(
1610 m.add_node(
"Fork_PostAnd_" + std::to_string(f), NodeType::Router,
true));
1614 js.name =
"Join_PreAnd";
1615 js.nodetype = NodeType::Join;
1616 js.sched = SchedStrategy::INF;
1617 js.nservers = std::numeric_limits<double>::infinity();
1618 js.attr_ishost =
false;
1620 joinStation = m.add_station(js);
1621 joinNode = m.node_of_station(joinStation);
1622 if (forkNode) m.fj.emplace_back(forkNode, joinNode);
1629 bool iscachelayer = !flat && ishostlayer && !callers.empty();
1630 for (std::size_t c : callers)
1631 if (!lqn.iscache[c]) iscachelayer =
false;
1640 std::size_t cacheNode = 0;
1641 qn::CacheParam<T> cachepar;
1643 const std::size_t ct = callers[0];
1644 cachepar.nitems = lqn.nitems[ct];
1645 cachepar.itemcap = lqn.itemcap[ct];
1646 cachepar.replacestrat = lqn.replacestrat[ct];
1647 cacheNode = m.add_node(lqn.hashnames[ct], NodeType::Cache,
true);
1657 std::vector<std::size_t> async_here;
1658 for (std::size_t c : callers)
1659 for (std::size_t aidx : lqn.actsof[c])
1660 for (std::size_t cidx : lqn.callsof[aidx])
1661 if (lqn.calltype[cidx] == CallType::ASYNC &&
1662 !servers_for(lqn.parent[lqn.callpair_dst[cidx]]).empty())
1663 async_here.push_back(cidx);
1664 std::vector<std::size_t> open_entries;
1665 for (std::size_t c : callers) {
1673 for (std::size_t eidx : lqn.entriesof[c])
1674 if (lqn.has_arrival[eidx]) open_entries.push_back(eidx);
1677 std::size_t sourceStation = 0, sourceNode = 0, sinkNode = 0;
1678 if (!async_here.empty() || !open_entries.empty()) {
1680 src.name =
"Source";
1681 src.nodetype = NodeType::Source;
1683 src.sched = SchedStrategy::EXT;
1685 src.attr_ishost =
false;
1687 sourceStation = m.add_station(src);
1688 sourceNode = m.node_of_station(sourceStation);
1689 sinkNode = m.add_node(
"Sink", NodeType::Sink,
false);
1690 m.sourceIdx = sourceStation;
1691 m.sinkNode = sinkNode;
1695 std::vector<std::size_t> forkClassStack;
1698 std::vector<std::size_t> cls(lqn.nidx + 1, 0);
1699 std::vector<std::size_t> callcls(lqn.ncalls + 1, 0);
1702 std::vector<std::size_t> auxcallcls(lqn.ncalls + 1, 0);
1710 auto caller_needs_class = [&](std::size_t tidx_caller) {
1711 if (host_is_server(tidx_caller)) {
1712 if (lqn.isref[tidx_caller])
return true;
1713 for (std::size_t e : lqn.entriesof[tidx_caller])
1714 if (lqn.issynccaller.any_col(e) || lqn.isasynccaller.any_col(e) ||
1715 is_fwd_target(e) || lqn.has_arrival[e])
1718 return is_layer_client(tidx_caller);
1720 auto caller_acts_visible = [&](std::size_t tidx_caller) {
1721 return host_is_server(tidx_caller) || is_layer_client(tidx_caller);
1729 auto caller_pool = [&](std::size_t tidx_) ->
double {
1730 if (njobs(tidx_, idx) != 0.0)
return njobs(tidx_, idx);
1732 const bool single_replica = reduce_fanout || single_replica_tasks.count(tidx_) > 0;
1733 double nj = single_replica ? mult[tidx_] : mult[tidx_] * lqn.repl[tidx_];
1734 if (std::isinf(nj)) {
1736 for (std::size_t c = 1; c <= NT(); ++c)
1737 if (lqn.taskgraph.get(c, tidx_) != Tzero()) s += mult[c];
1739 if (std::isinf(nj)) {
1741 for (std::size_t c = 1; c <= NT(); ++c)
1742 if (std::isfinite(mult[c])) s2 += mult[c] * lqn.repl[c];
1743 nj = std::min(s2, 1000.0);
1759 std::vector<bool> rp_member(lqn.nidx + 1,
false);
1760 std::vector<bool> rp_spliced(lqn.nidx + 1,
false);
1761 std::vector<bool> rp_hop(lqn.nidx + 1,
false);
1762 std::vector<bool> rp_expand(lqn.ncalls + 1,
false);
1763 std::vector<std::size_t> rp_child_task(lqn.ncalls + 1, 0);
1764 std::vector<std::size_t> rp_child_entry(lqn.ncalls + 1, 0);
1765 std::vector<T> rp_ret_share(lqn.ncalls + 1, Tzero());
1766 std::vector<T> rp_call_mean(lqn.ncalls + 1, Tzero());
1767 std::vector<std::vector<std::size_t>> rp_hop_children(lqn.nidx + 1);
1768 std::vector<double> rp_hop_seed(lqn.nidx + 1, 0.0);
1769 std::vector<std::vector<std::size_t>> rp_member_inbound(lqn.nidx + 1);
1770 std::vector<std::vector<std::size_t>> rp_hop_inbound(lqn.nidx + 1);
1771 std::vector<std::vector<std::size_t>> rp_group_roots;
1772 std::vector<std::size_t> rp_group_reftask;
1773 std::vector<bool> rp_group_head_is_caller;
1775 std::vector<std::size_t> rp_ref_stage;
1776 std::vector<std::size_t> rp_hop_cls(lqn.nidx + 1, 0), rp_hop_ret(lqn.nidx + 1, 0),
1777 rp_ret_cls(lqn.nidx + 1, 0);
1778 std::vector<std::size_t> rp_gate(lqn.ncalls + 1, 0), rp_aux(lqn.ncalls + 1, 0),
1779 rp_resume(lqn.ncalls + 1, 0);
1782 auto host_demand_of_entry = [&](std::size_t eidx_) {
1784 for (std::size_t a_ : lqn.actsof[eidx_]) {
1785 if (lqn.hostdem[a_].disabled)
continue;
1786 const double m_ = dbl(lqn.hostdem[a_].mean);
1787 if (std::isfinite(m_)) d += m_;
1791 const auto in_idxset = [&](std::size_t t_) {
1792 return std::find(idxSet.begin(), idxSet.end(), t_) != idxSet.end();
1799 if (interlockMethod !=
"refpath" || flat || iscachelayer || hasfork || hasjoin)
return;
1802 if (reduce_fanout || nreplicas > 1)
return;
1803 for (std::size_t t_ : callers)
1804 if (lqn.repl[t_] > 1.0 || single_replica_tasks.count(t_) > 0)
return;
1805 const lqn::LqnRefRoutes<T> R =
1807 if (!R.why.empty()) {
1809 std::cerr <<
"[LINE] Warning: layer '" << lqn.hashnames[idx]
1810 <<
"' falls back to the 'ilrate' interlock method: " << R.why
1814 std::vector<std::size_t> keep;
1815 for (std::size_t g_ = 0; g_ < R.groups.size(); ++g_) {
1816 const lqn::LqnRefGroup<T>& G = R.groups[g_];
1817 bool wanted = G.members.size() >= 2;
1820 if (!wanted && opt.interlock_refpath_scope ==
"all" && !G.head_is_caller &&
1823 if (!wanted)
continue;
1828 for (std::size_t t_ : G.members) pool += caller_pool(t_);
1829 const double chainPop = mult[G.reftask] * lqn.repl[G.reftask];
1830 if (!(pool > chainPop))
continue;
1833 if (keep.empty())
return;
1836 std::vector<bool> sMember(lqn.nidx + 1,
false), sSpliced(lqn.nidx + 1,
false),
1837 sHop(lqn.nidx + 1,
false), sExpand(lqn.ncalls + 1,
false);
1838 std::vector<std::size_t> sChildTask(lqn.ncalls + 1, 0),
1839 sChildEntry(lqn.ncalls + 1, 0);
1840 std::vector<T> sRetShare(lqn.ncalls + 1, Tzero()), sCallMean(lqn.ncalls + 1, Tzero());
1841 std::vector<std::vector<std::size_t>> sHopChildren(lqn.nidx + 1),
1842 sMemberInbound(lqn.nidx + 1), sHopInbound(lqn.nidx + 1);
1843 std::vector<double> sHopSeed(lqn.nidx + 1, 0.0);
1844 std::vector<std::vector<std::size_t>> sRoots;
1845 std::vector<std::size_t> sRefTask;
1846 std::vector<bool> sHeadIsCaller;
1847 for (std::size_t g_ : keep) {
1848 const lqn::LqnRefGroup<T>& G = R.groups[g_];
1849 const std::size_t nE = G.entries.size();
1851 std::vector<bool> leads(G.ismember.begin(), G.ismember.end());
1852 for (std::size_t i_ = nE; i_-- > 0;)
1853 for (
const lqn::LqnRefCall<T>& c_ : G.calls)
1854 if (c_.from == i_ && leads[c_.to]) leads[i_] =
true;
1855 for (std::size_t t_ : G.members)
1856 if (!caller_needs_class(t_))
return;
1857 if (G.head_is_caller && !caller_needs_class(G.reftask))
return;
1859 if (!G.head_is_caller && !std::isfinite(lqn.maxmult[G.reftask]))
return;
1860 for (std::size_t i_ = 0; i_ < nE; ++i_) {
1861 if (!leads[i_] || G.ismember[i_])
continue;
1863 if (in_idxset(G.etask[i_]) || lqn.repl[G.etask[i_]] > 1.0)
return;
1865 for (
const lqn::LqnRefCall<T>& c_ : G.calls) {
1866 if (!leads[c_.to])
continue;
1867 if (in_idxset(G.etask[c_.to]))
return;
1870 const std::size_t head = G.reftask;
1871 for (std::size_t t_ : G.members) {
1873 sSpliced[t_] = !(G.head_is_caller && t_ == head);
1875 std::vector<T> inTotMember(lqn.nidx + 1, Tzero()), inTotHop(lqn.nidx + 1, Tzero());
1876 for (
const lqn::LqnRefCall<T>& c_ : G.calls) {
1877 if (!leads[c_.to])
continue;
1878 const std::size_t cidx_ = c_.cidx;
1879 const std::size_t fromEidx = G.entries[c_.from];
1880 sExpand[cidx_] =
true;
1881 sCallMean[cidx_] = lqn.callproc_mean[cidx_];
1882 sHopChildren[fromEidx].push_back(cidx_);
1883 if (G.ismember[c_.to]) {
1884 const std::size_t tv = G.etask[c_.to];
1885 sChildTask[cidx_] = tv;
1886 sMemberInbound[tv].push_back(cidx_);
1887 inTotMember[tv] = T(inTotMember[tv] + c_.vcall);
1889 const std::size_t ev = G.entries[c_.to];
1890 sChildEntry[cidx_] = ev;
1891 sHopInbound[ev].push_back(cidx_);
1892 inTotHop[ev] = T(inTotHop[ev] + c_.vcall);
1894 sRetShare[cidx_] = c_.vcall;
1896 for (
const lqn::LqnRefCall<T>& c_ : G.calls) {
1897 const std::size_t cidx_ = c_.cidx;
1898 if (!sExpand[cidx_])
continue;
1899 const T tot = sChildTask[cidx_] > 0 ? inTotMember[sChildTask[cidx_]]
1900 : inTotHop[sChildEntry[cidx_]];
1902 ? T(sRetShare[cidx_] / tot)
1905 for (std::size_t i_ = 0; i_ < nE; ++i_)
1906 if (leads[i_] && !G.ismember[i_]) {
1907 const std::size_t ev = G.entries[i_];
1909 sHopSeed[ev] = host_demand_of_entry(ev);
1911 if (!G.head_is_caller) {
1915 for (std::size_t ev : lqn.entriesof[G.reftask])
1918 sHopSeed[ev] = host_demand_of_entry(ev);
1921 sRoots.push_back(lqn.entriesof[head]);
1922 sRefTask.push_back(head);
1923 sHeadIsCaller.push_back(G.head_is_caller);
1925 rp_member = sMember;
1926 rp_spliced = sSpliced;
1928 rp_expand = sExpand;
1929 rp_child_task = sChildTask;
1930 rp_child_entry = sChildEntry;
1931 rp_ret_share = sRetShare;
1932 rp_call_mean = sCallMean;
1933 rp_hop_children = sHopChildren;
1934 rp_hop_seed = sHopSeed;
1935 rp_member_inbound = sMemberInbound;
1936 rp_hop_inbound = sHopInbound;
1937 rp_group_roots = sRoots;
1938 rp_group_reftask = sRefTask;
1939 rp_group_head_is_caller = sHeadIsCaller;
1944 for (std::size_t tidx_caller : callers) {
1945 if (caller_needs_class(tidx_caller)) {
1946 double nj = njobs(tidx_caller, idx);
1948 nj = caller_pool(tidx_caller);
1949 njobs(tidx_caller, idx) = nj;
1954 const bool rp_spliced_here = rp_on && rp_spliced[tidx_caller];
1956 jc.name = lqn.hashnames[tidx_caller];
1957 jc.type = JobClassType::CLOSED;
1958 jc.population = rp_spliced_here ? 0.0 : nj;
1959 jc.refstat = m.clientIdx;
1960 jc.completes =
false;
1963 jc.is_ref_class = !rp_spliced_here;
1964 jc.attr_kind = int(LqnElement::TASK);
1965 jc.attr_idx = tidx_caller;
1966 cls[tidx_caller] = m.add_class(jc);
1967 m.attr_tasks.emplace_back(cls[tidx_caller], tidx_caller);
1968 if (lqn.isref[tidx_caller]) {
1969 m.set_service(m.clientIdx, cls[tidx_caller], thinkproc[tidx_caller]);
1970 }
else if (rp_spliced_here) {
1979 thinkt_map.push_back({idx, tidx_caller, m.clientIdx, cls[tidx_caller]});
1982 for (std::size_t eidx : lqn.entriesof[tidx_caller]) {
1984 ec.name = lqn.hashnames[eidx];
1985 ec.type = JobClassType::CLOSED;
1986 ec.population = 0.0;
1987 ec.refstat = m.clientIdx;
1988 ec.completes =
false;
1989 ec.attr_kind = int(LqnElement::ENTRY);
1991 cls[eidx] = m.add_class(ec);
1992 m.attr_entries.emplace_back(cls[eidx], eidx);
1997 for (std::size_t aidx : lqn.actsof[tidx_caller]) {
1998 if (caller_acts_visible(tidx_caller)) {
2000 ac.name = lqn.hashnames[aidx];
2001 ac.type = JobClassType::CLOSED;
2002 ac.population = 0.0;
2003 ac.refstat = m.clientIdx;
2004 ac.completes =
false;
2005 ac.attr_kind = int(LqnElement::ACTIVITY);
2007 cls[aidx] = m.add_class(ac);
2008 m.attr_activities.emplace_back(cls[aidx], aidx);
2012 const std::size_t hidx = lqn.parent[lqn.parent[aidx]];
2013 if (servers_for(hidx).empty())
2014 m.set_service(m.clientIdx, cls[aidx], servtproc[aidx]);
2016 for (std::size_t cidx : lqn.callsof[aidx]) {
2017 if (lqn.calltype[cidx] == CallType::ASYNC) {
2021 const std::size_t adst = lqn.parent[lqn.callpair_dst[cidx]];
2022 if (servers_for(adst).empty())
continue;
2024 oc.name = lqn.callhashnames[cidx];
2025 oc.type = JobClassType::OPEN;
2026 oc.population = std::numeric_limits<double>::infinity();
2027 oc.refstat = sourceStation;
2028 oc.completes =
false;
2029 oc.is_ref_class =
false;
2030 oc.attr_kind = int(LqnElement::CALL);
2032 callcls[cidx] = m.add_class(oc);
2033 m.attr_calls.push_back({callcls[cidx], cidx, lqn.callpair_src[cidx],
2034 lqn.callpair_dst[cidx]});
2042 m.set_service(sourceStation, callcls[cidx],
2045 T minRespTA = Tzero();
2046 for (std::size_t ta : lqn.actsof[adst]) minRespTA += lqn.hostdem[ta].mean;
2047 for (std::size_t st : servers_for(adst)) {
2049 call_map.push_back({idx, cidx, st, callcls[cidx]});
2051 arv_call_map.push_back({idx, cidx, sourceStation, callcls[cidx]});
2054 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
2055 const std::size_t gid = group_of_call[cidx];
2064 if (grp_dispatch[gid] == 0) {
2070 const std::string tag =
2071 lqn.hashnames[aidx] +
".Dispatch" + std::to_string(gid);
2072 grp_router[gid] = m.add_node(tag +
".Router", NodeType::Router,
true);
2075 dc.type = JobClassType::CLOSED;
2076 dc.population = 0.0;
2077 dc.refstat = m.clientIdx;
2078 dc.completes =
false;
2079 dc.attr_kind = int(LqnElement::CALL);
2081 grp_dispatch[gid] = m.add_class(dc);
2084 gc.name = lqn.callhashnames[cidx] +
".Group" + std::to_string(gid);
2085 gc.type = JobClassType::CLOSED;
2086 gc.population = 0.0;
2087 gc.refstat = m.clientIdx;
2088 gc.completes =
false;
2089 gc.attr_kind = int(LqnElement::CALL);
2091 grp_class[gid] = m.add_class(gc);
2093 routed_group_sites.push_back(
2094 {grp_router[gid], grp_dispatch[gid], gid});
2096 callcls[cidx] = grp_dispatch[gid];
2097 m.attr_calls.push_back({callcls[cidx], cidx, lqn.callpair_src[cidx],
2098 lqn.callpair_dst[cidx]});
2099 for (std::size_t st2 : servers_for(lqn.parent[lqn.callpair_dst[cidx]]))
2100 m.set_service(st2, callcls[cidx], callservtproc[cidx]);
2104 cc.name = lqn.callhashnames[cidx];
2105 cc.type = JobClassType::CLOSED;
2106 cc.population = 0.0;
2107 cc.refstat = m.clientIdx;
2108 cc.completes =
false;
2109 cc.attr_kind = int(LqnElement::CALL);
2111 callcls[cidx] = m.add_class(cc);
2112 m.attr_calls.push_back({callcls[cidx], cidx, lqn.callpair_src[cidx],
2113 lqn.callpair_dst[cidx]});
2121 const std::size_t seedidx = flat ? lqn.parent[lqn.callpair_dst[cidx]] : idx;
2122 T minRespT = Tzero();
2123 for (std::size_t ta : lqn.actsof[seedidx]) minRespT += lqn.hostdem[ta].mean;
2124 for (std::size_t st : servers_for(seedidx))
2127 if (lqn.callproc_mean[cidx] !=
2128 num_traits<T>::from_double(
double(nreplicas))) {
2130 xc.name = lqn.callhashnames[cidx] +
".Aux";
2131 xc.type = JobClassType::CLOSED;
2132 xc.population = 0.0;
2133 xc.refstat = m.clientIdx;
2134 xc.completes =
false;
2135 xc.attr_kind = int(LqnElement::CALL);
2137 auxcallcls[cidx] = m.add_class(xc);
2149 auto rp_class = [&](
const std::string& nm,
double pop, std::size_t attr) {
2152 rc.type = JobClassType::CLOSED;
2153 rc.population = pop;
2154 rc.refstat = m.clientIdx;
2155 rc.completes =
false;
2156 rc.is_ref_class =
false;
2159 const std::size_t k_ = m.add_class(rc);
2164 for (std::size_t g_ = 0; g_ < rp_group_reftask.size(); ++g_) {
2165 if (rp_group_head_is_caller[g_]) {
2166 rp_ref_stage.push_back(0);
2169 const std::size_t rt = rp_group_reftask[g_];
2170 const std::size_t k_ =
2171 rp_class(lqn.hashnames[rt] +
".RefPath", mult[rt] * lqn.repl[rt], rt);
2172 m.classes[k_ - 1].is_ref_class =
true;
2173 m.set_service(m.clientIdx, k_, thinkproc[rt]);
2174 rp_ref_stage.push_back(k_);
2176 for (std::size_t ev = 1; ev <= lqn.nidx; ++ev) {
2177 if (!rp_hop[ev])
continue;
2178 const std::size_t k_ = rp_class(lqn.hashnames[ev] +
".RefHop", 0.0, ev);
2180 m.set_service(m.clientIdx, k_,
2182 rp_hop_cls[ev] = k_;
2185 refpath_map.push_back({idx, m.clientIdx, k_, 1, ev, 1.0, 0});
2186 for (std::size_t c_ : rp_hop_children[ev])
2187 refpath_map.push_back({idx, m.clientIdx, k_, 2, c_, -1.0, ev});
2188 refpath_map.push_back({idx, m.clientIdx, k_, 3, lqn.parent[ev], 1.0, 0});
2189 rp_hop_ret[ev] = rp_class(lqn.hashnames[ev] +
".RefHopRet", 0.0, ev);
2191 for (std::size_t tv = 1; tv <= lqn.nidx; ++tv)
2192 if (rp_spliced[tv]) rp_ret_cls[tv] = rp_class(lqn.hashnames[tv] +
".RefRet", 0.0, tv);
2193 for (std::size_t cidx_ = 1; cidx_ <= lqn.ncalls; ++cidx_) {
2194 if (!rp_expand[cidx_])
continue;
2195 if (callcls[cidx_] != 0) {
2196 rp_gate[cidx_] = callcls[cidx_];
2198 if (auxcallcls[cidx_] != 0) rp_aux[cidx_] = auxcallcls[cidx_];
2200 rp_gate[cidx_] = rp_class(lqn.callhashnames[cidx_] +
".RefGate", 0.0, cidx_);
2201 if (rp_call_mean[cidx_] != Tone())
2203 rp_class(lqn.callhashnames[cidx_] +
".RefGate.Aux", 0.0, cidx_);
2208 if (rp_child_task[cidx_] > 0)
2209 refpath_map.push_back(
2210 {idx, m.clientIdx, rp_gate[cidx_], 3, rp_child_task[cidx_], 1.0, 0});
2211 rp_resume[cidx_] = rp_class(lqn.callhashnames[cidx_] +
".RefResume", 0.0, cidx_);
2217 std::size_t curclass;
2223 std::vector<std::size_t> curnodes;
2225 const int atClient = 1, atServer = 2, atCache = 3;
2226 std::vector<int> jobposkey(lqn.nidx + 1, atClient);
2227 std::vector<std::size_t> curclasskey(lqn.nidx + 1, 0);
2228 std::vector<std::vector<std::size_t>> curnodeskey(lqn.nidx + 1);
2235 auto rp_descent = [&](std::size_t cidx_, Ctx st) -> Ctx {
2236 const std::size_t gate = rp_gate[cidx_], res = rp_resume[cidx_], aux = rp_aux[cidx_];
2237 const T m_ = rp_call_mean[cidx_];
2238 const std::size_t enter = rp_child_task[cidx_] > 0 ? cls[rp_child_task[cidx_]]
2239 : rp_hop_cls[rp_child_entry[cidx_]];
2240 if (st.jobpos == atClient)
2241 m.set_route(st.curclass, gate, clientNode, clientNode, Tone());
2243 for (std::size_t nd : st.curnodes) m.set_route(st.curclass, gate, nd, clientNode, Tone());
2246 m.set_route(gate, enter, clientNode, clientNode, Tone());
2248 }
else if (m_ > Tone()) {
2249 m.set_route(gate, enter, clientNode, clientNode, Tone());
2250 m.set_route(res, enter, clientNode, clientNode, T(Tone() - Tone() / m_));
2251 m.set_route(res, aux, clientNode, clientNode, T(Tone() / m_));
2254 m.set_route(gate, enter, clientNode, clientNode, m_);
2255 m.set_route(gate, aux, clientNode, clientNode, T(Tone() - m_));
2256 m.set_route(res, aux, clientNode, clientNode, Tone());
2259 st.jobpos = atClient;
2260 st.curnodes.clear();
2265 std::function<Ctx(std::size_t, std::size_t, Ctx)> recur =
2266 [&](std::size_t tidx_caller, std::size_t aidx, Ctx st) -> Ctx {
2267 jobposkey[aidx] = st.jobpos;
2268 curclasskey[aidx] = st.curclass;
2269 curnodeskey[aidx] = st.curnodes;
2270 const std::vector<std::size_t> nexts = lqn.graph.succ(aidx);
2271 std::size_t lastEntryClass = st.curclass;
2273 bool next_is_fork =
false;
2274 for (std::size_t sx : nexts)
2275 if (lqn.actposttype[sx] == PrecedenceType::POST_AND) next_is_fork =
true;
2276 const Ctx preFork = st;
2277 std::vector<std::size_t> andSuccs;
2278 for (std::size_t sx : nexts)
2279 if (lqn.actposttype[sx] == PrecedenceType::POST_AND) andSuccs.push_back(sx);
2281 for (std::size_t k = 0; k < nexts.size(); ++k) {
2282 const std::size_t nextaidx = nexts[k];
2283 if (next_is_fork) st = preFork;
2284 bool isLoop = lqn.graph.get(aidx, nextaidx) != lqn.dag.get(aidx, nextaidx);
2285 if (lqn.parent[aidx] != lqn.parent[nextaidx]) {
2287 std::size_t cidx = 0;
2288 for (std::size_t c : lqn.callsof[aidx])
2289 if (lqn.callpair_dst[c] == nextaidx) cidx = c;
2290 if (cidx == 0)
continue;
2292 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
2293 const std::size_t gid = group_of_call[cidx];
2297 if (group_members[gid].empty() || group_members[gid][0] != cidx)
continue;
2304 std::vector<std::size_t> tnode2, tstat2, tcall2;
2305 for (std::size_t mc : group_members[gid]) {
2306 const std::size_t tt = lqn.parent[lqn.callpair_dst[mc]];
2307 if (servers_for(tt).empty())
continue;
2308 tnode2.push_back(server_nodes_for(tt)[0]);
2309 tstat2.push_back(servers_for(tt)[0]);
2310 tcall2.push_back(mc);
2312 if (tnode2.size() < 2)
continue;
2313 const std::size_t fromNode2 =
2314 st.jobpos == atClient ? clientNode : st.curnodes[0];
2315 const std::size_t dc = grp_dispatch[gid], gc = grp_class[gid];
2316 m.set_route(st.curclass, dc, fromNode2, grp_router[gid], Tone());
2318 T(Tone() / num_traits<T>::from_int(
int(tnode2.size())));
2319 for (std::size_t d = 0; d < tnode2.size(); ++d) {
2320 m.set_route(dc, dc, grp_router[gid], tnode2[d], share2);
2321 m.set_route(dc, gc, tnode2[d], clientNode, Tone());
2322 m.set_service(tstat2[d], dc, callservtproc[tcall2[d]]);
2323 call_map.push_back({idx, tcall2[d], tstat2[d], dc});
2326 st.jobpos = atClient;
2327 st.curnodes.clear();
2330 if (rp_on && rp_expand[cidx]) {
2333 st = rp_descent(cidx, st);
2336 const std::size_t ctgt = lqn.parent[lqn.callpair_dst[cidx]];
2337 st = route_sync_call(m, idx, cidx, st, server_nodes_for(ctgt),
2338 servers_for(ctgt), callcls, auxcallcls, clientNode,
2339 atClient, atServer, flat);
2343 bool any_entry_succ =
false;
2344 for (std::size_t sx : nexts)
2345 if (sx > lqn.eshift && sx <= lqn.eshift + lqn.nentries) any_entry_succ =
true;
2346 if (!any_entry_succ) {
2347 st.jobpos = jobposkey[aidx];
2348 st.curclass = curclasskey[aidx];
2349 st.curnodes = curnodeskey[aidx];
2351 if (k > 0 && nexts[k - 1] > lqn.eshift && nexts[k - 1] <= lqn.eshift + lqn.nentries)
2352 lastEntryClass = st.curclass;
2353 st.jobpos = atClient;
2354 st.curclass = lastEntryClass;
2355 st.curnodes.clear();
2357 const T w = lqn.graph.get(aidx, nextaidx);
2365 if (iscachelayer && lqn.nitems[aidx] > 0 && cls[nextaidx] != 0) {
2366 const std::size_t readcls = cls[nextaidx];
2367 m.set_route(st.curclass, readcls, clientNode, cacheNode, w);
2368 if (cachepar.pread.size() < m.classes.size())
2369 cachepar.pread.resize(m.classes.size());
2370 cachepar.pread[readcls - 1] = lqn.itemproc[aidx];
2371 const std::vector<std::size_t> hm = lqn.graph.succ(nextaidx);
2373 throw InputError(
"SolverLN: the cache read '" + lqn.names[nextaidx] +
2374 "' needs exactly one hit and one miss successor");
2375 if (cachepar.hitclass.size() < m.classes.size()) {
2376 cachepar.hitclass.resize(m.classes.size(), 0);
2377 cachepar.missclass.resize(m.classes.size(), 0);
2379 cachepar.hitclass[readcls - 1] = cls[hm[0]];
2380 cachepar.missclass[readcls - 1] = cls[hm[1]];
2381 st.jobpos = atCache;
2382 st.curclass = readcls;
2383 st.curnodes.clear();
2384 st = recur(tidx_caller, nextaidx, st);
2388 const bool is_and_join_tail = lqn.actpretype[aidx] == PrecedenceType::PRE_AND;
2390 std::size_t fbranch = 0;
2392 for (std::size_t q = 0; q < andSuccs.size(); ++q)
2393 if (andSuccs[q] == nextaidx) fbranch = q + 1;
2400 const std::size_t hidxOf = lqn.parent[lqn.parent[nextaidx]];
2401 const std::vector<std::size_t>& actStations = servers_for(hidxOf);
2402 const std::vector<std::size_t>& actNodes = server_nodes_for(hidxOf);
2403 const bool actAtServer = !actStations.empty();
2404 const std::size_t from = st.jobpos == atClient ? clientNode
2405 : st.jobpos == atCache ? cacheNode
2408 for (std::size_t r = 0; r < nreplicas; ++r) {
2409 const std::size_t fromNode =
2410 st.jobpos == atClient ? clientNode
2411 : st.jobpos == atCache ? cacheNode
2412 : st.curnodes[std::min(r, st.curnodes.size() - 1)];
2413 const std::size_t toNode = actAtServer ? actNodes[r] : clientNode;
2414 if (next_is_fork && fbranch > 0) {
2415 m.set_route(st.curclass, st.curclass, fromNode, forkNode, Tone());
2416 if (r == 0) forkClassStack.push_back(st.curclass);
2417 m.set_route(st.curclass, st.curclass, forkNode, forkRouter[fbranch - 1],
2419 m.set_route(st.curclass, cls[nextaidx], forkRouter[fbranch - 1], toNode,
2421 }
else if (is_and_join_tail) {
2424 if (forkClassStack.empty())
2425 throw InputError(
"SolverLN: an AND join has no matching fork in '" +
2426 lqn.names[aidx] +
"'");
2427 const std::size_t forkClass = forkClassStack.back();
2428 if (r + 1 == nreplicas) forkClassStack.pop_back();
2429 m.set_route(st.curclass, forkClass, fromNode, joinNode, Tone());
2430 m.set_route(forkClass, cls[nextaidx], joinNode, toNode, Tone());
2432 m.set_route(st.curclass, cls[nextaidx], fromNode, toNode, w);
2435 m.set_service(actStations[r], cls[nextaidx], lqn.hostdem[nextaidx]);
2439 st.jobpos = atServer;
2440 st.curclass = cls[nextaidx];
2441 st.curnodes = actNodes;
2442 servt_map.push_back({idx, nextaidx, actStations[0], cls[nextaidx]});
2444 st.jobpos = atClient;
2445 st.curclass = cls[nextaidx];
2446 st.curnodes.clear();
2447 m.set_service(m.clientIdx, cls[nextaidx], servtproc[nextaidx]);
2448 thinkt_map.push_back({idx, nextaidx, m.clientIdx, cls[nextaidx]});
2450 if (aidx != nextaidx && !isLoop) {
2451 st = recur(tidx_caller, nextaidx, st);
2455 const std::size_t reply =
2456 (rp_on && rp_spliced[tidx_caller]) ? rp_ret_cls[tidx_caller]
2458 if (st.jobpos == atClient) {
2459 m.set_route(st.curclass, reply, clientNode, clientNode, Tone());
2461 for (std::size_t nd : st.curnodes)
2462 m.set_route(st.curclass, reply, nd, clientNode, Tone());
2465 if (!is_aux_class(m.classes[st.curclass - 1].name))
2466 m.classes[st.curclass - 1].completes =
true;
2472 for (std::size_t tidx_caller : callers) {
2473 if (!caller_needs_class(tidx_caller))
continue;
2474 const std::vector<std::size_t>& ents = lqn.entriesof[tidx_caller];
2475 const T share = T(Tone() / num_traits<T>::from_int(
int(ents.size())));
2476 for (std::size_t eidx : ents) {
2477 m.set_route(cls[tidx_caller], cls[eidx], clientNode, clientNode, share);
2478 if (ents.size() > 1)
2479 route_map.push_back({idx, tidx_caller, eidx, m.clientIdx, m.clientIdx,
2480 cls[tidx_caller], cls[eidx]});
2481 Ctx st{cls[eidx], atClient, {}};
2482 recur(tidx_caller, eidx, st);
2490 for (std::size_t g_ = 0; g_ < rp_group_reftask.size(); ++g_) {
2491 if (rp_group_head_is_caller[g_])
continue;
2492 std::vector<std::size_t> roots;
2493 for (std::size_t r_ : rp_group_roots[g_])
2494 if (rp_hop[r_]) roots.push_back(r_);
2495 if (roots.empty())
continue;
2497 const T share = T(Tone() / num_traits<T>::from_int(
int(roots.size())));
2498 for (std::size_t r_ : roots) {
2499 m.set_route(rp_ref_stage[g_], rp_hop_cls[r_], clientNode, clientNode, share);
2500 m.set_route(rp_hop_ret[r_], rp_ref_stage[g_], clientNode, clientNode, Tone());
2504 for (std::size_t ev = 1; ev <= lqn.nidx; ++ev) {
2505 if (!rp_hop[ev])
continue;
2506 Ctx cur{rp_hop_cls[ev], atClient, {}};
2507 for (std::size_t c_ : rp_hop_children[ev]) cur = rp_descent(c_, cur);
2508 m.set_route(cur.curclass, rp_hop_ret[ev], clientNode, clientNode, Tone());
2512 for (std::size_t tv = 1; tv <= lqn.nidx; ++tv) {
2513 if (!rp_spliced[tv])
continue;
2514 for (std::size_t c_ : rp_member_inbound[tv])
2515 if (rp_ret_share[c_] > Tzero())
2516 m.set_route(rp_ret_cls[tv], rp_resume[c_], clientNode, clientNode,
2519 for (std::size_t ev = 1; ev <= lqn.nidx; ++ev) {
2520 if (!rp_hop[ev])
continue;
2521 for (std::size_t c_ : rp_hop_inbound[ev])
2522 if (rp_ret_share[c_] > Tzero())
2523 m.set_route(rp_hop_ret[ev], rp_resume[c_], clientNode, clientNode,
2534 if (sourceStation != 0) {
2535 const T nrep = num_traits<T>::from_int(
int(nreplicas));
2536 for (std::size_t cidx : async_here) {
2537 const std::size_t oc = callcls[cidx];
2538 if (oc == 0)
continue;
2541 const std::vector<std::size_t>& anode =
2542 server_nodes_for(lqn.parent[lqn.callpair_dst[cidx]]);
2543 const T callmean = lqn.callproc_mean[cidx];
2544 if (callmean < Tone()) {
2546 m.set_route(oc, oc, sourceNode, sinkNode, T(Tone() - callmean));
2547 for (std::size_t r = 0; r < anode.size(); ++r) {
2548 m.set_route(oc, oc, sourceNode, anode[r], T(callmean / nrep));
2549 m.set_route(oc, oc, anode[r], sinkNode, Tone());
2553 const T p = T(Tone() / callmean);
2554 for (std::size_t r = 0; r < anode.size(); ++r) {
2555 m.set_route(oc, oc, sourceNode, anode[r], T(Tone() / nrep));
2556 for (std::size_t q = 0; q < anode.size(); ++q)
2557 m.set_route(oc, oc, anode[r], anode[q], T((Tone() - p) / nrep));
2558 m.set_route(oc, oc, anode[r], sinkNode, p);
2562 for (std::size_t eidx : open_entries) {
2564 eo.name = lqn.hashnames[eidx] +
"_Open";
2565 eo.type = JobClassType::OPEN;
2566 eo.population = std::numeric_limits<double>::infinity();
2567 eo.refstat = sourceStation;
2568 eo.completes =
false;
2569 eo.is_ref_class =
false;
2570 eo.attr_kind = int(LqnElement::ENTRY);
2572 const std::size_t ec = m.add_class(eo);
2574 m.set_service(sourceStation, ec, lqn.arrival[eidx]);
2576 std::size_t bound = 0;
2577 for (std::size_t sx : lqn.graph.succ(eidx))
2578 if (bound == 0 && sx > lqn.ashift) bound = sx;
2579 const Distrib<T>& svc = bound != 0 ? servtproc[bound] : servtproc[eidx];
2582 const std::vector<std::size_t>& ostat =
2583 flat ? servers_for(lqn.parent[eidx]) : server;
2584 const std::vector<std::size_t>& onode =
2585 flat ? server_nodes_for(lqn.parent[eidx]) : serverNode;
2586 for (std::size_t r = 0; r < ostat.size(); ++r) {
2587 m.set_service(ostat[r], ec, svc);
2588 m.set_route(ec, ec, sourceNode, onode[r], T(Tone() / nrep));
2589 m.set_route(ec, ec, onode[r], sinkNode, Tone());
2603 std::vector<std::vector<T>> Arows;
2604 std::vector<T> brows;
2605 if (idx < lqn.lincon_A.size() && lqn.lincon_A[idx].rows() > 0) {
2606 const Matrix<T>& Aelem = lqn.lincon_A[idx];
2607 Matrix<T> Alayer(Aelem.rows(), m.classes.size(), Tzero());
2608 const std::vector<std::size_t>& constrained =
2609 ishostlayer ? lqn.tasksof[idx] : lqn.entriesof[idx];
2610 for (std::size_t j = 0; j < constrained.size() && j < Aelem.cols(); ++j) {
2611 std::vector<std::size_t> layerClasses;
2613 for (std::size_t a : lqn.actsof[constrained[j]])
2614 if (cls[a] != 0) layerClasses.push_back(cls[a]);
2616 for (std::size_t c = 1; c <= lqn.ncalls; ++c)
2617 if (lqn.callpair_dst[c] == constrained[j] && callcls[c] != 0)
2618 layerClasses.push_back(callcls[c]);
2620 for (std::size_t k = 0; k < layerClasses.size(); ++k)
2621 for (std::size_t rr = 0; rr < Aelem.rows(); ++rr)
2622 Alayer(rr, layerClasses[k] - 1) =
2623 T(Alayer(rr, layerClasses[k] - 1) + Aelem(rr, j));
2625 for (std::size_t rr = 0; rr < Alayer.rows(); ++rr) {
2626 std::vector<T> row(m.classes.size(), Tzero());
2627 for (std::size_t k = 0; k < m.classes.size(); ++k) row[k] = Alayer(rr, k);
2628 Arows.push_back(row);
2629 brows.push_back(lqn.lincon_b[idx][rr]);
2638 if (rp_on && layer_solver_declares_region()) {
2639 std::vector<std::size_t> srvEntries;
2640 for (std::size_t sidx : idxSet)
2641 for (std::size_t e : lqn.entriesof[sidx]) srvEntries.push_back(e);
2642 for (std::size_t tv = 1; tv <= lqn.nidx; ++tv) {
2643 if (!rp_spliced[tv])
continue;
2644 const double cap = lqn.maxmult[tv];
2645 if (!std::isfinite(cap) || cap <= 0.0)
continue;
2646 std::vector<T> row(m.classes.size(), Tzero());
2650 for (std::size_t a_ : lqn.actsof[tv])
2652 row[cls[a_] - 1] = Tone();
2657 for (std::size_t a_ : lqn.actsof[tv])
2658 for (std::size_t c_ : lqn.callsof[a_])
2659 if (std::find(srvEntries.begin(), srvEntries.end(),
2660 lqn.callpair_dst[c_]) != srvEntries.end() &&
2662 row[callcls[c_] - 1] = Tone();
2667 Arows.push_back(row);
2668 brows.push_back(num_traits<T>::from_double(cap));
2674 for (
const std::vector<T>& row : Arows)
2675 for (
const T& v : row)
2676 if (v != Tzero()) any =
true;
2678 Matrix<T> Alayer(Arows.size(), m.classes.size(), Tzero());
2679 for (std::size_t rr = 0; rr < Arows.size(); ++rr)
2680 for (std::size_t k = 0; k < m.classes.size(); ++k) Alayer(rr, k) = Arows[rr][k];
2684 typename qn::NetworkStruct<T>::Region rg;
2685 const std::size_t M = m.stations.size(), K = m.classes.size();
2686 rg.cap.assign(M, std::vector<double>(K + 1, -1.0));
2687 rg.maxmem.assign(M, -1.0);
2688 rg.members.assign(M,
false);
2690 rg.weight.assign(K, Tone());
2691 rg.size.assign(K, Tone());
2692 for (std::size_t r = 0; r < nreplicas; ++r) rg.members[server[r] - 1] =
true;
2693 rg.lincon_A = Alayer;
2694 rg.lincon_b = brows;
2695 m.regions.push_back(rg);
2702 if (cacheNode != 0) {
2703 cachepar.pread.resize(m.classes.size());
2704 cachepar.hitclass.resize(m.classes.size(), 0);
2705 cachepar.missclass.resize(m.classes.size(), 0);
2706 m.nodeparam[cacheNode] = cachepar;
2714 for (
const std::array<std::size_t, 3>& site : routed_group_sites) {
2715 qn::NodeDef& nd = m.nodes[site[0] - 1];
2716 if (nd.routing.size() < m.classes.size())
2717 nd.routing.resize(m.classes.size(), RoutingStrategy::PROB);
2718 nd.routing[site[1] - 1] = lqn.callgroups[site[2] - 1].strategy;
2730 std::vector<std::pair<std::shared_ptr<std::vector<std::vector<std::size_t>>>,
2731 std::vector<std::vector<std::size_t>>>> deferred_chaincols;
2732 for (std::size_t sidx : idxSet) {
2733 const bool hasld = sidx < lqn.lldscaling.size() && !lqn.lldscaling[sidx].empty();
2734 const bool hascd = sidx < lqn.cdscaling.size() && bool(lqn.cdscaling[sidx]);
2735 const bool hasjd = sidx < lqn.jdscaling.size() && bool(lqn.jdscaling[sidx]);
2736 const bool haspools = sidx < lqn.pools.size() && !lqn.pools[sidx].empty();
2737 if (!(hasld || hascd || hasjd || haspools))
continue;
2738 const bool sishost = sidx <= lqn.nhosts;
2739 const std::vector<std::size_t>& operandIdx =
2740 sishost ? lqn.tasksof[sidx] : lqn.entriesof[sidx];
2741 std::vector<std::vector<std::size_t>> cols(operandIdx.size());
2742 for (std::size_t j = 0; j < operandIdx.size(); ++j) {
2744 for (std::size_t a : lqn.actsof[operandIdx[j]])
2745 if (cls[a] != 0) cols[j].push_back(cls[a] - 1);
2747 for (std::size_t c = 1; c <= lqn.ncalls; ++c)
2748 if (lqn.callpair_dst[c] == operandIdx[j] && callcls[c] != 0)
2749 cols[j].push_back(callcls[c] - 1);
2752 const std::size_t R = m.classes.size();
2753 std::shared_ptr<std::vector<std::vector<std::size_t>>> chaincols =
2754 std::make_shared<std::vector<std::vector<std::size_t>>>();
2755 deferred_chaincols.push_back(std::make_pair(chaincols, cols));
2756 for (std::size_t r = 0; r < nreplicas; ++r) {
2761 qn::Station<T>& stn = m.stations[srv[sidx][r] - 1];
2762 if (hasld) stn.lldscaling = lqn.lldscaling[sidx];
2768 bool one_class_each =
true;
2769 for (std::size_t j = 0; j < cols.size(); ++j)
2770 if (cols[j].size() > 1) one_class_each =
false;
2772 layer_dep_handle(lqn.cdscaling[sidx], cols, chaincols, R);
2773 const std::vector<T> pk = layer_peak(lqn.cdscalingpeak[sidx], cols, R);
2774 if (one_class_each) {
2776 stn.cdscalingpeak = pk;
2779 stn.jdscalingpeak = pk;
2783 stn.jdscaling = layer_dep_handle(lqn.jdscaling[sidx], cols, chaincols, R);
2784 stn.jdscalingpeak = layer_peak(lqn.jdscalingpeak[sidx], cols, R);
2795 const lqn::ServerPools<T>& pl = lqn.pools[sidx];
2796 const Matrix<T> compat = pl.compat;
2797 const std::vector<double> counts = pl.counts;
2798 const std::vector<T> rates = pl.rates;
2800 rates](
const std::vector<T>& nop) {
2801 return std::vector<T>(
2804 stn.jdscaling = layer_dep_handle(etaPool, cols, chaincols, R);
2805 stn.jdscalingpeak = layer_peak(
2806 std::vector<T>(cols.size(), num_traits<T>::from_int(1)), cols, R);
2816 std::vector<std::size_t> nc(m.classes.size(), 0);
2817 for (std::size_t tv = 1; tv <= lqn.nidx; ++tv) {
2818 if (!rp_member[tv] || cls[tv] == 0)
continue;
2819 const std::size_t own = cls[tv];
2820 std::vector<std::size_t> owned{cls[tv]};
2821 for (std::size_t e_ : lqn.entriesof[tv]) owned.push_back(cls[e_]);
2822 for (std::size_t a_ : lqn.actsof[tv]) {
2823 owned.push_back(cls[a_]);
2824 for (std::size_t c_ : lqn.callsof[a_]) {
2825 owned.push_back(callcls[c_]);
2826 owned.push_back(auxcallcls[c_]);
2830 for (std::size_t q_ : owned)
2831 if (q_ != 0 && m.classes[q_ - 1].type == JobClassType::CLOSED) nc[q_ - 1] = own;
2833 layer_normclass[idx] = nc;
2834 ref_path_audit(m, idx, callers, caller_needs_class, rp_spliced, rp_member, rp_hop,
2835 rp_expand, rp_group_head_is_caller, rp_ref_stage, rp_hop_cls,
2836 rp_hop_ret, rp_ret_cls, rp_gate, rp_aux, rp_resume, cls);
2840 apply_host_task_priorities(m, idxSet);
2845 for (std::size_t d = 0; d < deferred_chaincols.size(); ++d) {
2846 const std::vector<std::vector<std::size_t>>& cols = deferred_chaincols[d].second;
2847 std::vector<std::vector<std::size_t>>& out = *deferred_chaincols[d].first;
2848 out.assign(cols.size(), std::vector<std::size_t>());
2849 for (std::size_t j = 0; j < cols.size(); ++j) {
2850 for (std::size_t k = 0; k < cols[j].size(); ++k)
2851 for (std::size_t cc = 0; cc < m.chains.size(); ++cc)
2852 if (cols[j][k] < m.chains[cc].size() && m.chains[cc][cols[j][k]]) {
2854 for (std::size_t q = 0; q < out[j].size(); ++q)
2855 if (out[j][q] == cc) seen =
true;
2856 if (!seen) out[j].push_back(cc);
2877 bool layer_solver_declares_region()
const {
2878 return opt.layer_solver ==
"nc" || opt.layer_solver ==
"fluid" || opt.layer_solver ==
"ssa";
2887 template <
class NeedsClass>
2888 void ref_path_audit(
const qn::Layer<T>& m, std::size_t idx,
2889 const std::vector<std::size_t>& callers,
const NeedsClass& needs_class,
2890 const std::vector<bool>& spliced,
const std::vector<bool>& member,
2891 const std::vector<bool>& hop,
const std::vector<bool>& expand,
2892 const std::vector<bool>& head_is_caller,
2893 const std::vector<std::size_t>& ref_stage,
2894 const std::vector<std::size_t>& hop_cls,
2895 const std::vector<std::size_t>& hop_ret,
2896 const std::vector<std::size_t>& ret_cls,
2897 const std::vector<std::size_t>& gate,
const std::vector<std::size_t>& aux,
2898 const std::vector<std::size_t>& resume,
2899 const std::vector<std::size_t>& cls)
const {
2900 std::set<std::size_t> watch;
2901 for (std::size_t k : ref_stage)
2902 if (k) watch.insert(k);
2903 for (std::size_t e = 1; e < hop.size(); ++e)
2905 if (hop_cls[e]) watch.insert(hop_cls[e]);
2906 if (hop_ret[e]) watch.insert(hop_ret[e]);
2908 for (std::size_t t = 1; t < member.size(); ++t)
2910 if (ret_cls[t]) watch.insert(ret_cls[t]);
2911 if (cls[t]) watch.insert(cls[t]);
2913 for (std::size_t c = 1; c < expand.size(); ++c)
2915 if (gate[c]) watch.insert(gate[c]);
2916 if (aux[c]) watch.insert(aux[c]);
2917 if (resume[c]) watch.insert(resume[c]);
2919 const std::size_t I = m.nodes.size();
2920 for (std::size_t r : watch) {
2921 for (std::size_t n = 1; n <= I; ++n) {
2924 for (
const auto& kv : m.P) {
2925 if (kv.first.first != r || n > kv.second.rows())
continue;
2926 for (std::size_t j = 0; j < kv.second.cols(); ++j) {
2927 const double v = dbl(kv.second(n - 1, j));
2933 std::cerr <<
"[LINE] Warning: refpath layer '" << lqn.hashnames[idx]
2934 <<
"': class '" << m.classes[r - 1].name <<
"' routes out of node "
2935 << n <<
" with a negative probability.\n";
2937 std::cerr <<
"[LINE] Warning: refpath layer '" << lqn.hashnames[idx]
2938 <<
"': class '" << m.classes[r - 1].name <<
"' leaves node " << n
2939 <<
" with total probability " << tot <<
".\n";
2942 std::size_t nref = 0;
2943 for (
const qn::JobClass& jc : m.classes)
2944 if (jc.is_ref_class) ++nref;
2945 std::size_t expect = 0;
2946 for (
bool h : head_is_caller)
2948 for (std::size_t t : callers)
2949 if (!spliced[t] && needs_class(t)) ++expect;
2951 std::cerr <<
"[LINE] Warning: refpath layer '" << lqn.hashnames[idx] <<
"' declares "
2952 << nref <<
" reference classes where " << expect
2953 <<
" were built; a chain holding two makes refresh_chains throw.\n";
2985 std::size_t curclass;
2990 static bool is_aux_class(
const std::string& name) {
2991 return name.size() >= 4 && name.compare(name.size() - 4, 4,
".Aux") == 0;
2993 template <
class Ctx>
2994 Ctx route_sync_call(qn::Layer<T>& m, std::size_t idx, std::size_t cidx, Ctx st,
2995 const std::vector<std::size_t>& tnode,
2996 const std::vector<std::size_t>& tstat,
2997 const std::vector<std::size_t>& callcls,
2998 const std::vector<std::size_t>& auxcallcls, std::size_t clientNode,
2999 int atClient,
int atServer,
bool flat) {
3000 const T one = Tone();
3004 const std::size_t ntgt = tnode.size();
3005 const bool to_this_server = ntgt > 0;
3006 const T nrep = num_traits<T>::from_int(
int(ntgt > 0 ? ntgt : 1));
3007 const T share = T(one / nrep);
3008 const T callmean = lqn.callproc_mean[cidx];
3009 const std::size_t cc = callcls[cidx];
3010 const std::size_t ax = auxcallcls[cidx];
3011 const bool below = callmean < one;
3012 const bool above = callmean > one;
3013 if (st.jobpos == atClient) {
3014 if (to_this_server) {
3016 m.set_route(st.curclass, ax, clientNode, clientNode, T(one - callmean));
3017 for (std::size_t r = 0; r < ntgt; ++r) {
3018 m.set_route(st.curclass, cc, clientNode, tnode[r], T(callmean / nrep));
3019 m.set_route(cc, cc, tnode[r], clientNode, one);
3022 m.set_route(ax, cc, clientNode, clientNode, one);
3024 for (std::size_t r = 0; r < ntgt; ++r) {
3025 m.set_route(st.curclass, cc, clientNode, tnode[r], share);
3026 m.set_route(cc, ax, tnode[r], clientNode, one);
3027 m.set_route(ax, cc, clientNode, tnode[r], T((one - one / callmean) / nrep));
3029 m.set_route(ax, cc, clientNode, clientNode, T(one / callmean));
3031 for (std::size_t r = 0; r < ntgt; ++r) {
3032 m.set_route(st.curclass, cc, clientNode, tnode[r], share);
3033 m.set_route(cc, cc, tnode[r], clientNode, one);
3036 for (std::size_t r = 0; r < ntgt; ++r) {
3037 m.set_service(tstat[r], cc, callservtproc[cidx]);
3038 call_map.push_back({idx, cidx, tstat[r], cc});
3041 st.jobpos = atClient;
3042 st.curnodes.clear();
3045 m.set_route(st.curclass, cc, clientNode, clientNode, one);
3046 if (below || above) {
3047 m.set_route(cc, ax, clientNode, clientNode, one);
3052 st.jobpos = atClient;
3053 st.curnodes.clear();
3054 m.set_service(m.clientIdx, cc, callservtproc[cidx]);
3055 call_map.push_back({idx, cidx, m.clientIdx, cc});
3059 auto fromNode = [&](std::size_t r) {
3060 return st.curnodes[std::min(r, st.curnodes.size() - 1)];
3062 if (to_this_server) {
3071 for (std::size_t r = 0; r < ntgt; ++r) {
3072 m.set_route(st.curclass, ax, fromNode(r), clientNode, T(one - callmean));
3073 m.set_route(st.curclass, cc, fromNode(r), tnode[r], T(callmean / nrep));
3074 m.set_route(cc, cc, tnode[r], clientNode, one);
3076 m.set_route(ax, cc, clientNode, clientNode, one);
3078 st.jobpos = atClient;
3079 st.curnodes.clear();
3085 for (std::size_t r = 0; r < ntgt; ++r) {
3086 m.set_route(st.curclass, cc, fromNode(r), tnode[r], one);
3087 m.set_route(cc, ax, tnode[r], clientNode, one);
3088 m.set_route(ax, cc, clientNode, tnode[r],
3089 T((one - one / callmean) / nrep));
3091 m.set_route(ax, cc, clientNode, clientNode, T(one / callmean));
3095 for (std::size_t r = 0; r < ntgt; ++r) {
3096 m.set_route(st.curclass, cc, fromNode(r), tnode[r], one);
3097 m.set_route(cc, cc, tnode[r], tnode[r], T(one - one / callmean));
3098 m.set_route(cc, ax, tnode[r], clientNode, T(one / callmean));
3102 st.jobpos = atClient;
3103 st.curnodes.clear();
3105 for (std::size_t r = 0; r < ntgt; ++r)
3106 m.set_route(st.curclass, cc, fromNode(r), tnode[r], one);
3110 for (std::size_t r = 0; r < ntgt; ++r)
3111 m.set_route(cc, cc, tnode[r], clientNode, one);
3113 st.jobpos = atClient;
3114 st.curnodes.clear();
3116 st.jobpos = atServer;
3117 st.curnodes = tnode;
3121 for (std::size_t r = 0; r < ntgt; ++r) {
3122 m.set_service(tstat[r], cc, callservtproc[cidx]);
3123 call_map.push_back({idx, cidx, tstat[r], cc});
3126 for (std::size_t nd : st.curnodes)
3127 m.set_route(st.curclass, cc, nd, clientNode, one);
3128 if (below || above) {
3129 m.set_route(cc, ax, clientNode, clientNode, one);
3134 st.jobpos = atClient;
3135 st.curnodes.clear();
3136 m.set_service(m.clientIdx, cc, callservtproc[cidx]);
3137 call_map.push_back({idx, cidx, m.clientIdx, cc});
3152 void build_entry_service_matrix() {
3153 const std::size_t dim = lqn.nidx + lqn.ncalls;
3154 servtmatrix =
Matrix<T>(dim + 1, dim + 1, Tzero());
3155 std::function<void(std::size_t, std::size_t)> rec = [&](std::size_t aidx, std::size_t eidx) {
3156 for (std::size_t nextaidx : lqn.graph.succ(aidx)) {
3157 const bool isLoop = lqn.graph.get(aidx, nextaidx) != lqn.dag.get(aidx, nextaidx);
3158 if (lqn.parent[aidx] != lqn.parent[nextaidx]) {
3159 for (std::size_t cidx : lqn.callsof[aidx])
3160 if (lqn.calltype[cidx] == CallType::SYNC)
3161 servtmatrix(eidx, lqn.nidx + cidx) = Tone();
3162 }
else if (nextaidx != aidx && !isLoop) {
3163 servtmatrix(eidx, nextaidx) = Tone();
3164 rec(nextaidx, eidx);
3168 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
3169 const std::size_t eidx = lqn.eshift + e;
3179 void init_interlock() {
3180 const std::size_t NE = lqn.nentries;
3181 il_all =
Matrix<T>(NE + 1, NE + 1, Tzero());
3182 il_ph1 =
Matrix<T>(NE + 1, NE + 1, Tzero());
3184 std::function<void(std::size_t, std::size_t, T, T, std::vector<bool>&,
int)> trace =
3185 [&](std::size_t eidx, std::size_t root_e, T pall, T pph1, std::vector<bool>& visited,
3187 if (eidx <= lqn.eshift || eidx > lqn.eshift + NE)
return;
3188 const std::size_t e = eidx - lqn.eshift;
3189 if (visited[e])
return;
3191 il_all(root_e, e) = T(il_all(root_e, e) + pall);
3192 il_ph1(root_e, e) = T(il_ph1(root_e, e) + pph1);
3193 for (std::size_t aidx : lqn.actsof[eidx]) {
3194 if (aidx <= lqn.ashift || aidx > lqn.ashift + lqn.nacts)
continue;
3195 const std::size_t a = aidx - lqn.ashift;
3196 if (depth > 0 && lqn.actphase[a] > 1)
continue;
3197 const bool is_ph1 = lqn.actphase[a] <= 1;
3198 for (std::size_t cidx : lqn.callsof[aidx]) {
3199 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
3200 if (!(lqn.callproc_mean[cidx] > Tzero()))
continue;
3201 const std::size_t dst = lqn.callpair_dst[cidx];
3202 if (dst <= lqn.eshift || dst > lqn.eshift + NE)
continue;
3203 trace(dst, root_e, T(pall * lqn.callproc_mean[cidx]),
3204 is_ph1 ? T(pph1 * lqn.callproc_mean[cidx]) : Tzero(), visited,
3210 for (std::size_t e = 1; e <= NE; ++e) {
3211 std::vector<bool> visited(NE + 1,
false);
3212 trace(lqn.eshift + e, e, Tone(), Tone(), visited, 0);
3215 il_common_entries.assign(NT() + 1, {});
3216 il_src_all.assign(NT() + 1, {});
3217 il_src_ph2.assign(NT() + 1, {});
3218 il_num_sources.assign(NT() + 1, 0.0);
3220 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
3221 const std::size_t tidx = lqn.tshift + t;
3222 if (lqn.isref[tidx] || lqn.sched[tidx] == SchedStrategy::INF)
continue;
3223 interlock_for_server(tidx);
3225 for (std::size_t h = 1; h <= lqn.nhosts; ++h) {
3226 if (lqn.sched[h] == SchedStrategy::INF)
continue;
3227 interlock_for_server(h);
3231 std::vector<std::size_t> server_entry_nums(std::size_t serverIdx)
const {
3232 std::vector<std::size_t> out;
3233 if (serverIdx <= lqn.nhosts) {
3234 for (std::size_t tidx : lqn.tasksof[serverIdx])
3235 for (std::size_t se : lqn.entriesof[tidx]) out.push_back(se - lqn.eshift);
3237 for (std::size_t se : lqn.entriesof[serverIdx]) out.push_back(se - lqn.eshift);
3242 std::vector<std::size_t> client_tasks(std::size_t serverIdx)
const {
3243 std::vector<std::size_t> out;
3244 if (serverIdx <= lqn.nhosts)
return lqn.tasksof[serverIdx];
3245 for (std::size_t se : lqn.entriesof[serverIdx])
3246 for (std::size_t ci : lqn.iscaller.col(se))
3247 if (ci > lqn.tshift && ci <= NT()) out.push_back(ci);
3248 std::sort(out.begin(), out.end());
3249 out.erase(std::unique(out.begin(), out.end()), out.end());
3253 std::vector<std::size_t> call_dst_tasks(std::size_t src_eidx, std::size_t target_e)
const {
3254 std::vector<std::size_t> out;
3255 for (std::size_t aidx : lqn.actsof[src_eidx]) {
3256 if (aidx <= lqn.ashift || aidx > lqn.ashift + lqn.nacts)
continue;
3257 for (std::size_t cidx : lqn.callsof[aidx]) {
3258 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
3259 const std::size_t dst = lqn.callpair_dst[cidx];
3260 const std::size_t de = dst - lqn.eshift;
3261 if (de >= 1 && de <= lqn.nentries && il_all(de, target_e) > Tzero())
3262 out.push_back(lqn.parent[dst]);
3265 std::sort(out.begin(), out.end());
3266 out.erase(std::unique(out.begin(), out.end()), out.end());
3270 bool is_branch_point(std::size_t srcX, std::size_t entryA, std::size_t srcY,
3271 std::size_t entryB)
const {
3272 const std::size_t taskA = lqn.parent[entryA], taskB = lqn.parent[entryB];
3273 const std::size_t taskX = lqn.parent[srcX];
3274 if (taskX == taskA && taskX == taskB)
return false;
3275 if (srcX == entryA || srcY == entryB)
return true;
3276 const std::vector<std::size_t> dx = call_dst_tasks(srcX, entryA - lqn.eshift);
3277 const std::vector<std::size_t> dy = call_dst_tasks(srcY, entryB - lqn.eshift);
3278 for (std::size_t a : dx)
3279 for (std::size_t b : dy)
3280 if (a != b)
return true;
3284 void trace_to_server(std::size_t eidx, std::size_t serverIdx, std::vector<bool>& visited,
3285 std::vector<std::size_t>& itasks,
bool isHead)
const {
3286 if (eidx <= lqn.eshift || eidx > lqn.eshift + lqn.nentries)
return;
3287 const std::size_t e = eidx - lqn.eshift;
3288 if (visited[e])
return;
3289 const std::size_t owner = lqn.parent[eidx];
3290 if (owner == serverIdx)
return;
3291 if (serverIdx <= lqn.nhosts && lqn.parent[owner] == serverIdx)
return;
3294 const std::vector<std::size_t> sen = server_entry_nums(serverIdx);
3295 for (std::size_t aidx : lqn.actsof[eidx]) {
3296 if (aidx <= lqn.ashift || aidx > lqn.ashift + lqn.nacts)
continue;
3297 for (std::size_t cidx : lqn.callsof[aidx]) {
3298 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
3299 const std::size_t dst = lqn.callpair_dst[cidx];
3300 const std::size_t dtask = lqn.parent[dst];
3301 bool reaches = dtask == serverIdx ||
3302 (serverIdx <= lqn.nhosts && lqn.parent[dtask] == serverIdx);
3304 const std::size_t de = dst - lqn.eshift;
3305 for (std::size_t sn : sen)
3306 if (il_all(de, sn) > Tzero()) reaches =
true;
3309 trace_to_server(dst, serverIdx, visited, itasks,
false);
3314 if (found && !isHead) {
3315 itasks.push_back(owner);
3316 std::sort(itasks.begin(), itasks.end());
3317 itasks.erase(std::unique(itasks.begin(), itasks.end()), itasks.end());
3322 void interlock_for_server(std::size_t serverIdx) {
3323 const std::vector<std::size_t> sen = server_entry_nums(serverIdx);
3324 if (sen.empty())
return;
3325 const std::vector<std::size_t> cts = client_tasks(serverIdx);
3326 if (cts.empty())
return;
3328 std::vector<std::pair<std::size_t, std::size_t>> pairs;
3329 for (std::size_t ct : cts)
3330 for (std::size_t ce : lqn.entriesof[ct]) {
3331 const std::size_t cen = ce - lqn.eshift;
3332 if (cen < 1 || cen > lqn.nentries)
continue;
3333 for (std::size_t se : sen)
3334 if (il_all(cen, se) > Tzero()) {
3335 pairs.emplace_back(ct, cen);
3339 if (pairs.size() < 2)
return;
3341 std::vector<std::size_t> common;
3342 for (std::size_t i = 0; i < pairs.size(); ++i)
3343 for (std::size_t j = i + 1; j < pairs.size(); ++j) {
3344 if (pairs[i].first == pairs[j].first)
continue;
3345 const std::size_t eA = pairs[i].second, eC = pairs[j].second;
3346 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
3347 const std::size_t tidx = lqn.tshift + t;
3348 for (std::size_t ex : lqn.entriesof[tidx])
3349 for (std::size_t ey : lqn.entriesof[tidx]) {
3350 const std::size_t xn = ex - lqn.eshift, yn = ey - lqn.eshift;
3351 if (xn < 1 || yn < 1 || xn > lqn.nentries || yn > lqn.nentries)
continue;
3352 if (il_all(xn, eA) > Tzero() && il_all(yn, eC) > Tzero() &&
3353 is_branch_point(ex, eA + lqn.eshift, ey, eC + lqn.eshift))
3354 common.push_back(ex);
3358 std::sort(common.begin(), common.end());
3359 common.erase(std::unique(common.begin(), common.end()), common.end());
3360 if (common.empty())
return;
3362 std::vector<std::size_t> interlocked;
3363 for (std::size_t ce : common) {
3364 std::vector<bool> visited(lqn.nentries + 1,
false);
3365 std::vector<std::size_t> it;
3366 trace_to_server(ce, serverIdx, visited, it,
true);
3367 for (std::size_t x : it) interlocked.push_back(x);
3369 std::sort(interlocked.begin(), interlocked.end());
3370 interlocked.erase(std::unique(interlocked.begin(), interlocked.end()), interlocked.end());
3372 std::vector<std::size_t> src_all;
3373 for (std::size_t ce : common) src_all.push_back(lqn.parent[ce]);
3374 std::sort(src_all.begin(), src_all.end());
3375 src_all.erase(std::unique(src_all.begin(), src_all.end()), src_all.end());
3377 std::vector<std::size_t> diff;
3378 for (std::size_t x : src_all)
3379 if (!std::binary_search(interlocked.begin(), interlocked.end(), x)) diff.push_back(x);
3387 std::vector<std::size_t> src_ph2;
3388 for (std::size_t it : interlocked)
3389 for (std::size_t ie : lqn.entriesof[it])
3390 for (std::size_t ci : lqn.iscaller.col(ie))
3391 if (ci > lqn.tshift && ci <= NT() &&
3392 !std::binary_search(interlocked.begin(), interlocked.end(), ci))
3393 src_all.push_back(ci);
3394 std::sort(src_all.begin(), src_all.end());
3395 src_all.erase(std::unique(src_all.begin(), src_all.end()), src_all.end());
3398 for (std::size_t st : src_all) nsrc += lqn.mult[st];
3400 il_common_entries[serverIdx] = common;
3401 il_src_all[serverIdx] = src_all;
3402 il_src_ph2[serverIdx] = src_ph2;
3403 il_num_sources[serverIdx] = nsrc;
3410 const std::size_t N = lqn.nidx;
3411 tput.assign(N + 1, Tzero());
3412 util.assign(N + 1, Tzero());
3413 servt.assign(N + 1, Tzero());
3414 residt.assign(N + 1, Tzero());
3415 thinkt.assign(N + 1, Tzero());
3416 callservt.assign(lqn.ncalls + 1, Tzero());
3417 callresidt.assign(lqn.ncalls + 1, Tzero());
3419 servt_ph1.assign(N + 1, Tzero());
3420 servt_ph2.assign(N + 1, Tzero());
3421 prOvertake.assign(lqn.nentries + 1, Tzero());
3422 build_entry_service_matrix();
3424 relax_omega = (opt.relax ==
"fixed" || opt.relax ==
"adaptive") ? opt.relax_factor : 1.0;
3426 servt_prev.assign(N + 1, std::numeric_limits<double>::quiet_NaN());
3427 residt_prev.assign(N + 1, std::numeric_limits<double>::quiet_NaN());
3428 tput_prev.assign(N + 1, std::numeric_limits<double>::quiet_NaN());
3429 thinkt_prev.assign(N + 1, std::numeric_limits<double>::quiet_NaN());
3430 callservt_prev.assign(lqn.ncalls + 1, std::numeric_limits<double>::quiet_NaN());
3431 callresidt_prev.assign(lqn.ncalls + 1, std::numeric_limits<double>::quiet_NaN());
3432 servt_prev_v.assign(N + 1, Tzero());
3433 residt_prev_v.assign(N + 1, Tzero());
3434 tput_prev_v.assign(N + 1, Tzero());
3435 thinkt_prev_v.assign(N + 1, Tzero());
3436 callservt_prev_v.assign(lqn.ncalls + 1, Tzero());
3438 unique_route_idx.clear();
3439 for (
const RouteRow& r : route_map) unique_route_idx.push_back(r.idx);
3440 std::sort(unique_route_idx.begin(), unique_route_idx.end());
3441 unique_route_idx.erase(std::unique(unique_route_idx.begin(), unique_route_idx.end()),
3442 unique_route_idx.end());
3445 if (interlockMethod ==
"ilrate") init_interlock();
3447 maxitererr.assign(opt.iter_max + 2, 0.0);
3448 averagingstart = -1;
3449 hasconverged =
false;
3450 moment_pass_done =
false;
3453 servtcdf.assign(N + 1, fluid::FluidPassage());
3454 callservtcdf.assign(lqn.ncalls + 1, fluid::FluidPassage());
3455 entrycdfrespt.assign(lqn.nentries + 1, LnCdf());
3456 entryproc.assign(lqn.nentries + 1, mam::AphPair<T>());
3457 cdf_repo.assign(ensemble.size(), std::vector<std::vector<fluid::FluidPassage>>());
3471 const bool stoch = opt.layer_solver ==
"ssa";
3474 cfg.iter_tol = opt.iter_tol;
3475 cfg.relax_burnin = relax_omega;
3476 stoch_ctl = std::make_shared<LnStochController<T>>(cfg);
3479 while (it < opt.iter_max) {
3480 if (!stoch && converged(it))
break;
3482 results.emplace_back(ensemble.size());
3483 for (std::size_t e = 0; e < ensemble.size(); ++e) analyze(it, e);
3486 std::vector<double> jobs(ensemble.size(), 0.0);
3487 for (std::size_t e = 0; e < ensemble.size(); ++e)
3488 jobs[e] = ensemble[e].total_jobs();
3489 const bool stop = stoch_ctl->update(it, results.back(), jobs, servt, residt);
3492 relax_omega = stoch_ctl->relax_omega();
3494 did_converge =
true;
3495 hasconverged =
true;
3500 iterations_done = it;
3503 if (stoch && stoch_ctl && stoch_ctl->averaging_count() > 0) {
3504 const std::vector<LayerResult<T>>& avg = stoch_ctl->averaged_results();
3505 for (std::size_t e = 0; e < results.back().size() && e < avg.size(); ++e)
3506 results.back()[e] = avg[e];
3507 servt = stoch_ctl->averaged_servt();
3508 residt = stoch_ctl->averaged_residt();
3532 Matrix<T> filter_metric(
const qn::Layer<T>& L,
const Matrix<T>& metric,
3533 const std::vector<std::vector<bool>>* zero_mask)
const {
3534 const std::size_t M = L.nstations, K = L.nclasses;
3535 Matrix<T> out(M, K, Tzero());
3536 for (std::size_t i = 0; i < M; ++i)
3537 for (std::size_t k = 0; k < K; ++k)
3538 if (!L.disabled[i][k]) out(i, k) = metric(i, k);
3540 for (std::size_t i = 0; i < M; ++i)
3541 for (std::size_t k = 0; k < K; ++k)
3542 if ((*zero_mask)[i][k]) out(i, k) = Tzero();
3543 for (std::size_t i = 0; i < M; ++i)
3544 for (std::size_t k = 0; k < K; ++k)
3546 for (std::size_t k = 0; k < K; ++k) {
3547 std::size_t c = L.nchains;
3548 for (std::size_t cc = 0; cc < L.nchains; ++cc)
3549 if (L.chains[cc][k]) c = cc;
3550 if (c == L.nchains)
continue;
3551 for (std::size_t i = 0; i < M; ++i)
3552 if (L.visits[c](L.stateful_of_station(i + 1) - 1, k) == Tzero())
3553 out(i, k) = Tzero();
3600 mva::MvaSolution<T> solve_layer_ctmc(std::size_t e) {
3601 qn::Layer<T>& L = ensemble[e];
3606 ctmc::CtmcOptions co;
3611 mva::MvaSolution<T> out;
3612 out.method =
"ctmc";
3631 mva::MvaSolution<T> solve_layer_mam(std::size_t e) {
3632 qn::Layer<T>& L = ensemble[e];
3634 L.refresh_capacity();
3636 mo.method =
"dec.poisson";
3652 mva::MvaSolution<T> solve_layer_nc(std::size_t e) {
3653 if (fj_tr[e].active())
3655 "SolverLN: layer '" + ensemble[e].name +
3656 "' carries a fork, whose fixed point is driven by MVA in this port; solve this "
3657 "model with layer_solver 'mva'");
3658 qn::Layer<T>& L = ensemble[e];
3688 mva::MvaSolution<T> solve_layer_ssa(std::size_t e) {
3689 if (fj_tr[e].active())
3691 "SolverLN: layer '" + ensemble[e].name +
3692 "' carries a fork, whose fixed point is driven by MVA in this port; solve this "
3693 "model with layer_solver 'mva'");
3694 qn::Layer<T>& L = ensemble[e];
3697 mva::MvaSolution<T> out;
3700 out.Q =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3701 out.U =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3702 out.R =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3703 out.Tp =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3704 for (std::size_t i = 0; i < L.nstations; ++i)
3705 for (std::size_t r = 0; r < L.nclasses; ++r) {
3706 out.Q(i, r) = num_traits<T>::from_double(s.QN(i, r));
3707 out.U(i, r) = num_traits<T>::from_double(s.UN(i, r));
3708 out.R(i, r) = num_traits<T>::from_double(s.RN(i, r));
3709 out.Tp(i, r) = num_traits<T>::from_double(s.TN(i, r));
3711 out.C.assign(L.nclasses, Tzero());
3712 out.X.assign(L.nclasses, Tzero());
3713 for (std::size_t r = 0; r < L.nclasses && r < s.XN.size(); ++r) {
3714 out.X[r] = num_traits<T>::from_double(s.XN[r]);
3715 out.C[r] = num_traits<T>::from_double(s.CN[r]);
3720 mva::MvaSolution<T> solve_layer_fluid(std::size_t e) {
3721 if (fj_tr[e].active())
3723 "SolverLN: layer '" + std::to_string(e) +
3724 "' carries a fork, whose fixed point is driven by MVA in this port; solve this "
3725 "model with layer_solver 'mva'");
3726 qn::Layer<T>& L = ensemble[e];
3736 mva::MvaSolution<T> out;
3737 out.method =
"fluid";
3739 out.Q =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3740 out.U =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3741 out.R =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3742 out.Tp =
Matrix<T>(L.nstations, L.nclasses, Tzero());
3743 out.C.assign(L.nclasses, Tzero());
3744 out.X.assign(L.nclasses, Tzero());
3745 detail::ln_fluid_solve(L, opt.layer_fluid, out);
3749 mva::MvaSolution<T> solve_layer(std::size_t e) {
3759 ensemble[e].refresh_capacity();
3767 if (!ensemble[e].regions.empty())
return solve_layer_ctmc(e);
3780 if (has_cache_node(e)) ensemble[e].refresh_rt();
3781 if (opt.layer_solver ==
"fluid")
return solve_layer_fluid(e);
3782 if (opt.layer_solver ==
"nc")
return solve_layer_nc(e);
3783 if (opt.layer_solver ==
"ssa")
return solve_layer_ssa(e);
3784 if (opt.layer_solver !=
"mva")
3786 "' is not available; use 'mva', 'nc', 'fluid' or 'ssa'");
3793 mva::MvaOptions eopt = opt.layer;
3794 if (e < layer_interlock.size()) eopt.interlock = layer_interlock[e];
3795 if (!fj_tr[e].active())
3797 mva::MvaOptions lopt = eopt;
3798 if (lopt.method ==
"default") lopt.method =
"amva";
3800 ensemble[e], fj_tr[e], fj_lambda[e], eopt,
3801 [&lopt](qn::NetworkStruct<T>& V) {
3806 void analyze(
int it, std::size_t e) {
3807 qn::Layer<T>& L = ensemble[e];
3808 const mva::MvaSolution<T> s = solve_layer(e);
3809 LayerResult<T>& r = results[it - 1][e];
3810 r.RN = filter_metric(L, s.R,
nullptr);
3811 std::vector<std::vector<bool>> zmask(L.nstations, std::vector<bool>(L.nclasses,
false));
3812 for (std::size_t i = 0; i < L.nstations; ++i)
3813 for (std::size_t k = 0; k < L.nclasses; ++k)
3815 r.QN = filter_metric(L, s.Q, &zmask);
3816 r.UN = filter_metric(L, s.U, &zmask);
3817 r.TN = filter_metric(L, s.Tp,
nullptr);
3818 r.WN = residence_from_response(L, r.RN);
3828 if (!fj_tr[e].active() && opt.layer_solver ==
"mva") {
3829 Matrix<T> Qch(L.nstations, L.nchains, Tzero());
3830 for (std::size_t c = 0; c < L.nchains; ++c)
3831 for (std::size_t i = 0; i < L.nstations; ++i) {
3833 for (std::size_t k : L.inchain[c]) s2 += s.Q(i, k - 1);
3836 layer_init_sol[e] = Qch;
3841 Matrix<T> residence_from_response(
const qn::Layer<T>& L,
const Matrix<T>& RN)
const {
3842 Matrix<T> V(L.nstations, L.nclasses, Tzero());
3843 for (std::size_t c = 0; c < L.nchains; ++c)
3844 for (std::size_t i = 0; i < L.nstations; ++i) {
3845 const std::size_t sf = L.stateful_of_station(i + 1) - 1;
3846 for (std::size_t k = 0; k < L.nclasses; ++k)
3847 V(i, k) = T(V(i, k) + L.visits[c](sf, k));
3849 Matrix<T> WN(L.nstations, L.nclasses, Tzero());
3850 for (std::size_t i = 0; i < L.nstations; ++i)
3851 for (std::size_t k = 0; k < L.nclasses; ++k) {
3852 if (L.disabled[i][k])
continue;
3853 if (!(RN(i, k) > Tzero()))
continue;
3855 WN(i, k) = RN(i, k);
3859 for (std::size_t cc = 0; cc < L.nchains; ++cc)
3860 if (L.chains[cc][k]) c = cc;
3861 const std::size_t rstat = L.classes[k].refstat;
3863 if (L.refclass[c] > 0) {
3864 den = V(rstat - 1, L.refclass[c] - 1);
3866 for (std::size_t kk : L.inchain[c]) den += V(rstat - 1, kk - 1);
3868 if (den == Tzero())
continue;
3869 WN(i, k) = T(RN(i, k) * V(i, k) / den);
3871 for (std::size_t i = 0; i < L.nstations; ++i)
3872 for (std::size_t k = 0; k < L.nclasses; ++k)
3881 if (interlockMethod ==
"ilrate") update_populations(it);
3882 update_think_times(it);
3884 update_routing_probabilities(it);
3885 for (std::size_t e : route_reset) {
3886 ensemble[e].refresh_chains();
3892 for (std::size_t e : svc_reset) ensemble[e].refresh_rates();
3896 bool converged(
int it) {
3897 const std::size_t E = ensemble.size();
3898 const int iter_min = std::max<int>(2 *
int(E),
int(std::ceil(opt.iter_max / 4.0)));
3899 const int wnd_size = std::max(5,
int(std::ceil(iter_min / 5.0)));
3901 if (it >= iter_min &&
int(results.size()) >= wnd_size) {
3902 const T w = T(Tone() / num_traits<T>::from_int(wnd_size));
3903 for (std::size_t e = 0; e < E; ++e) {
3904 LayerResult<T>& cur = results[results.size() - 1][e];
3905 auto scale = [&](Matrix<T>& A) {
3906 for (std::size_t i = 0; i < A.rows(); ++i)
3907 for (std::size_t j = 0; j < A.cols(); ++j) A(i, j) = T(A(i, j) * w);
3909 Matrix<T> Q = cur.QN, U = cur.UN, R = cur.RN, Tp = cur.TN, W = cur.WN;
3910 scale(Q); scale(U); scale(R); scale(Tp); scale(W);
3911 for (
int k = 1; k < wnd_size; ++k) {
3912 const LayerResult<T>& old = results[results.size() - 1 - k][e];
3913 auto add = [&](Matrix<T>& A,
const Matrix<T>& B) {
3914 for (std::size_t i = 0; i < A.rows(); ++i)
3915 for (std::size_t j = 0; j < A.cols(); ++j) A(i, j) = T(A(i, j) + B(i, j) * w);
3917 add(Q, old.QN); add(U, old.UN); add(R, old.RN); add(Tp, old.TN); add(W, old.WN);
3919 cur.QN = Q; cur.UN = U; cur.RN = R; cur.TN = Tp; cur.WN = W;
3925 for (std::size_t e = 0; e < E; ++e) {
3926 const Matrix<T>& Q = results[results.size() - 1][e].QN;
3927 const Matrix<T>& Q1 = results[results.size() - 2][e].QN;
3928 const double Njobs = ensemble[e].total_jobs();
3929 if (!(Njobs > 0.0))
continue;
3931 for (std::size_t i = 0; i < Q.rows(); ++i)
3932 for (std::size_t j = 0; j < Q.cols(); ++j)
3933 mx = std::max(mx, std::fabs(dbl(Q(i, j)) - dbl(Q1(i, j))));
3936 maxitererr[it] = err;
3938 static_cast<long>(it),
3939 "layer iteration %zu: max queue-length change %.3e (tolerance %.3e)",
3940 static_cast<std::size_t
>(it), err, opt.iter_tol);
3941 if (it == iter_min) {
3943 averagingstart = it;
3947 if (it > iter_min && maxitererr[it] < opt.iter_tol && maxitererr[it - 1] < opt.iter_tol &&
3948 maxitererr[it - 2] < opt.iter_tol) {
3949 if (!hasconverged) {
3950 hasconverged =
true;
3952 did_converge =
true;
3956 hasconverged =
false;
3991 std::string lnmethod;
3993 bool ph_laws_ready =
false;
4010 static std::string ln_requested_method(
const std::string& method) {
4012 for (
char c : method) m +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
4013 if (m.empty() || m ==
"srvn" || m ==
"default" || m ==
"auto")
return "srvn";
4014 if (m ==
"srvn.ph" || m ==
"ph")
return "srvn.ph";
4015 if (m ==
"srvn.cs" || m ==
"srvncs" || m ==
"cs")
return "srvn.cs";
4016 if (m ==
"flat.cs" || m ==
"flatcs" || m ==
"flat" || m ==
"squashed")
return "flat.cs";
4017 if (m ==
"flat.ph" || m ==
"flatph" || m ==
"squashed.ph")
return "flat.ph";
4018 if (m ==
"moment3")
return "moment3";
4025 bool is_srvn_ph()
const {
return lnmethod ==
"srvn.ph"; }
4033 bool is_ph_encoding()
const {
return lnmethod ==
"srvn.ph" || lnmethod ==
"flat.ph"; }
4041 static std::size_t station_idx_of(
const qn::Layer<T>& L, std::size_t elem) {
4042 if (elem >= 1 && elem < L.server_idx_of.size() && L.server_idx_of[elem] != 0)
4043 return L.server_idx_of[elem];
4051 std::size_t station_idx_of_class(
const qn::Layer<T>& L, std::size_t k)
const {
4052 const int kind = L.classes[k].attr_kind;
4053 const std::size_t a = L.classes[k].attr_idx;
4054 if (kind ==
int(LqnElement::ACTIVITY))
4055 return station_idx_of(L, lqn.parent[lqn.parent[a]]);
4056 if (kind ==
int(LqnElement::CALL))
4057 return station_idx_of(L, lqn.parent[lqn.callpair_dst[a]]);
4069 bool probe_srvn_ph() {
4071 ph_assert_supported();
4073 ph_laws_ready =
true;
4075 }
catch (
const std::exception&) {
4076 ph_laws_ready =
false;
4083 std::size_t idx = 0;
4084 bool ishost =
false;
4085 std::vector<std::size_t> callers;
4087 std::vector<std::size_t> class_of_caller;
4088 std::size_t nreplicas = 1;
4090 std::vector<std::size_t> qstations;
4092 std::vector<T> svcmean_by_class;
4094 std::vector<std::pair<std::size_t, long>> open_arrivals;
4104 std::vector<workflow::Workflow<T>> ph_wf, ph_wfhost;
4105 std::vector<std::unordered_map<std::size_t, T>> ph_execs;
4106 std::vector<bool> ph_has_wf;
4107 std::vector<workflow::PhLaw<T>> ph_hostlaw, ph_entrylaw;
4108 std::vector<T> ph_hostmean, ph_entrymean, ph_entryscv;
4109 std::vector<T> ph_share, ph_overlap, ph_setupshare, ph_xdemand;
4110 Matrix<T> ph_ncalls, ph_calltime;
4111 std::vector<T> ph_procresid, ph_actthinkt, ph_calltotal;
4112 std::vector<PHLayer> ph_layers;
4113 std::vector<bool> ph_has_layer;
4116 T act_think_time(std::size_t aidx)
const {
4117 if (aidx >= lqn.actthink.size() || lqn.actthink[aidx].disabled)
return Tzero();
4118 const double m = dbl(lqn.actthink[aidx].mean);
4120 return lqn.actthink[aidx].mean;
4141 double setup_charge(std::size_t tidx)
const {
4142 if (tidx >= lqn.hassetup.size() || !lqn.hassetup[tidx])
return 0.0;
4143 const double s = tidx < lqn.setuptime.size() && !lqn.setuptime[tidx].disabled
4144 ? dbl(lqn.setuptime[tidx].mean) : 0.0;
4145 const double d = tidx < lqn.delayofftime.size() && !lqn.delayofftime[tidx].disabled
4146 ? dbl(lqn.delayofftime[tidx].mean) : 0.0;
4148 const double mult = lqn.mult[tidx];
4149 if (!std::isfinite(mult) || mult <= 0.0)
return 0.0;
4150 if (tidx >= tput.size() || tidx >= util.size())
return s;
4151 const double X = dbl(tput[tidx]);
4153 double rho = dbl(util[tidx]);
4154 if (!std::isfinite(rho) || rho < 0.0) rho = 0.0;
4156 const double b = rho * mult;
4157 const double EI = (std::max(1.0, b) - b) / X;
4158 return s * EI / (EI + d);
4167 double ph_setup_prob(std::size_t eidx)
const {
4168 const std::size_t tidx = lqn.parent[eidx];
4169 if (tidx >= lqn.hassetup.size() || !lqn.hassetup[tidx])
return 0.0;
4170 const double s = tidx < lqn.setuptime.size() && !lqn.setuptime[tidx].disabled
4171 ? dbl(lqn.setuptime[tidx].mean) : 0.0;
4172 const double d = tidx < lqn.delayofftime.size() && !lqn.delayofftime[tidx].disabled
4173 ? dbl(lqn.delayofftime[tidx].mean) : 0.0;
4175 return std::min(1.0, std::max(0.0, setup_charge(tidx) / s));
4179 double ph_host_servers(std::size_t hidx)
const {
4180 if (lqn.sched[hidx] == SchedStrategy::INF)
return 1.0;
4181 const double m = lqn.maxmult[hidx];
4182 return (std::isfinite(m) && m > 0.0) ? m : 1.0;
4186 bool ph_any_caller_of(std::size_t eidx)
const {
4187 return lqn.issynccaller.any_col(eidx) || lqn.isasynccaller.any_col(eidx);
4195 bool ph_open_arrival_only(std::size_t tidx)
const {
4196 if (lqn.isref[tidx])
return false;
4197 for (std::size_t eidx : lqn.entriesof[tidx])
4198 if (ph_any_caller_of(eidx))
return false;
4199 for (std::size_t eidx : lqn.entriesof[tidx])
4200 if (lqn.has_arrival[eidx])
return true;
4205 std::vector<std::size_t> ph_async_calls_into(std::size_t tidx)
const {
4206 std::vector<std::size_t> out;
4207 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
4208 if (lqn.calltype[cidx] != CallType::ASYNC)
continue;
4209 for (std::size_t e : lqn.entriesof[tidx])
4210 if (lqn.callpair_dst[cidx] == e) { out.push_back(cidx);
break; }
4219 void ph_assert_supported(
bool flat =
false)
const {
4222 const std::string mname = flat ?
"flat.ph" :
"srvn.ph";
4228 "method='" + mname +
"' does not support second-phase activities: the composed "
4229 "entry law has no reply point. Use method='default'.");
4235 const std::string why = ph_method_refusal(mname);
4254 std::string ph_method_refusal(
const std::string& method)
const {
4255 if (method !=
"srvn.ph" && method !=
"flat.ph")
return std::string();
4258 if (method ==
"flat.ph") {
4259 for (std::size_t i = 1; i <= NT(); ++i) {
4260 if (lqn.repl[i] > 1.0)
4261 return "method='flat.ph' does not support replicated processors or tasks, "
4262 "whose replicas need a submodel each. Use method='srvn.ph'.";
4263 if (lqn.hassetup[i])
4264 return "method='flat.ph' does not support setup tasks, whose powered-down "
4265 "threads are per-layer state. Use method='srvn.ph'.";
4268 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx)
4269 if (lqn.calltype[cidx] == CallType::FWD)
4270 return "method='" + method +
4271 "' does not support forwarding calls, whose target is not part of the "
4272 "caller's activity graph. Use method='default'.";
4273 for (std::size_t i = 1; i < lqn.iscache.size(); ++i)
4275 return "method='" + method +
4276 "' does not support cache tasks. Use method='default'.";
4277 for (std::size_t i = 1; i < lqn.hassetup.size(); ++i) {
4278 if (!lqn.hassetup[i])
continue;
4279 if (lqn.sched[i] == SchedStrategy::INF || !std::isfinite(lqn.mult[i]))
4280 return "method='" + method +
"': task '" + lqn.names[i] +
4281 "' declares a setup time on an infinite-server task, which holds no "
4282 "thread to power down; give it a finite multiplicity.";
4284 for (std::size_t i = 0; i < lqn.lincon_A.size(); ++i)
4285 if (lqn.lincon_A[i].rows() > 0)
4286 return "method='" + method +
4287 "' does not support admission constraints on a layer station. "
4288 "Use method='default'.";
4289 for (std::size_t i = 1; i < lqn.lldscaling.size(); ++i) {
4290 const char* fname =
nullptr;
4291 if (!lqn.lldscaling[i].empty()) fname =
"a load dependence";
4292 else if (lqn.cdscaling[i]) fname =
"a class dependence";
4293 else if (lqn.jdscaling[i]) fname =
"a joint dependence";
4294 else if (!lqn.pools[i].empty()) fname =
"server pools";
4295 if (fname !=
nullptr)
4296 return "method='" + method +
4297 "' does not support queue-dependent service rates on a layer station ('" +
4298 lqn.names[i] +
"' declares " + fname +
"). Use method='srvn.cs'.";
4300 return std::string();
4304 void ph_init_laws() {
4305 const std::size_t N = lqn.nidx;
4306 ph_wf.assign(N + 1, workflow::Workflow<T>(
"empty"));
4307 ph_wfhost.assign(N + 1, workflow::Workflow<T>(
"empty"));
4308 ph_execs.assign(N + 1, {});
4309 ph_has_wf.assign(N + 1,
false);
4310 ph_hostlaw.assign(N + 1, workflow::PhLaw<T>());
4311 ph_entrylaw.assign(N + 1, workflow::PhLaw<T>());
4312 ph_hostmean.assign(N + 1, Tzero());
4313 ph_entrymean.assign(N + 1, Tzero());
4314 ph_entryscv.assign(N + 1, Tone());
4315 ph_share.assign(N + 1, Tzero());
4316 ph_overlap.assign(N + 1, Tone());
4317 ph_setupshare.assign(N + 1, Tzero());
4318 ph_xdemand.assign(N + 1, Tzero());
4319 ph_ncalls =
Matrix<T>(N + 1, N + 1, Tzero());
4320 ph_calltime =
Matrix<T>(N + 1, N + 1, Tzero());
4321 ph_procresid.assign(N + 1, Tzero());
4322 ph_actthinkt.assign(N + 1, Tzero());
4323 ph_calltotal.assign(N + 1, Tzero());
4324 ph_layers.assign(NT() + 1, PHLayer());
4325 ph_has_layer.assign(NT() + 1,
false);
4327 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
4328 const std::size_t eidx = lqn.eshift + e;
4329 const std::size_t tidx = lqn.parent[eidx];
4330 if (ignore[tidx])
continue;
4332 ph_wf[eidx] = std::move(ew.wf);
4333 ph_execs[eidx] = ew.execs;
4334 ph_has_wf[eidx] =
true;
4336 ph_wfhost[eidx] = std::move(eh.wf);
4346 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
4347 const std::size_t tidx = lqn.tshift + t;
4348 const std::size_t n = lqn.entriesof[tidx].size();
4349 if (n == 0)
continue;
4350 for (std::size_t eidx : lqn.entriesof[tidx])
4351 ph_share[eidx] = num_traits<T>::from_double(1.0 /
double(n));
4362 Distrib<T> ph_call_burst_law(std::size_t cidx)
const {
4363 const double m = dbl(lqn.callproc_mean[cidx]);
4364 const std::size_t eidx = lqn.callpair_dst[cidx];
4366 const T R = T(callservt[cidx] / lqn.callproc_mean[cidx]);
4367 double scvd = dbl(ph_entryscv[eidx]);
4373 const workflow::PhLaw<T> loop =
4387 Distrib<T> ph_station_law(
const workflow::PhLaw<T>& law)
const {
4395 static workflow::PhLaw<T> ph_immediate_law() {
4396 workflow::PhLaw<T> out;
4397 out.alpha.assign(1, Tone());
4411 void ph_compose_entry_laws() {
4412 const std::size_t N = lqn.nidx;
4413 std::vector<T> entry_setup_share(N + 1, Tzero());
4414 ph_overlap.assign(N + 1, Tone());
4416 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
4417 const std::size_t eidx = lqn.eshift + e;
4418 if (!ph_has_wf[eidx])
continue;
4419 workflow::Workflow<T>& w = ph_wf[eidx];
4420 const std::unordered_map<std::size_t, T>& ex = ph_execs[eidx];
4421 T entrysum = Tzero(), procsum = Tzero();
4422 for (std::size_t aidx : lqn.actsof[eidx]) {
4423 T m = T(residt[aidx] + act_think_time(aidx));
4424 const T xa = ex.at(aidx);
4425 procsum += T(xa * m);
4426 const double md = dbl(m);
4427 w.set_activity_demand_mean(
4431 for (std::size_t cidx : lqn.callsof[aidx]) {
4432 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
4433 w.set_activity_demand(lqn.callhashnames[cidx], ph_call_burst_law(cidx));
4434 m = T(m + callservt[cidx]);
4436 entrysum += T(xa * m);
4440 T m1 = mm.first, scv = mm.second;
4447 const T f = T(m1 / procsum);
4448 for (std::size_t i = 0; i < law.S.rows(); ++i)
4449 for (std::size_t j = 0; j < law.S.cols(); ++j) law.S(i, j) = T(law.S(i, j) * f);
4458 const double p = ph_setup_prob(eidx);
4460 const std::size_t tidx = lqn.parent[eidx];
4461 const double sm = dbl(lqn.setuptime[tidx].mean);
4462 double sscv = dbl(lqn.setuptime[tidx].scv);
4466 num_traits<T>::from_double(sm), num_traits<T>::from_double(sscv)));
4467 std::vector<workflow::PhLaw<T>> mix;
4470 std::vector<T> probs;
4471 probs.push_back(num_traits<T>::from_double(p));
4472 probs.push_back(num_traits<T>::from_double(1.0 - p));
4482 entry_setup_share[eidx] = num_traits<T>::from_double(p * sm / denom);
4485 ph_entrylaw[eidx] = law;
4486 ph_entrymean[eidx] = m1;
4487 ph_entryscv[eidx] = scv;
4489 const double r = std::min(1.0, dbl(m1) / dbl(entrysum));
4490 ph_overlap[eidx] = num_traits<T>::from_double(r);
4496 ph_setupshare.assign(N + 1, Tzero());
4497 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
4498 const std::size_t tidx = lqn.tshift + t;
4499 if (ignore[tidx])
continue;
4500 for (std::size_t eidx : lqn.entriesof[tidx])
4501 ph_setupshare[tidx] = T(ph_setupshare[tidx] + ph_share[eidx] * entry_setup_share[eidx]);
4505 ph_ncalls =
Matrix<T>(N + 1, N + 1, Tzero());
4506 ph_calltime =
Matrix<T>(N + 1, N + 1, Tzero());
4507 ph_procresid.assign(N + 1, Tzero());
4508 ph_actthinkt.assign(N + 1, Tzero());
4509 ph_calltotal.assign(N + 1, Tzero());
4510 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
4511 const std::size_t tidx = lqn.tshift + t;
4512 if (ignore[tidx])
continue;
4513 for (std::size_t eidx : lqn.entriesof[tidx]) {
4514 if (!ph_has_wf[eidx])
continue;
4515 const T w = ph_share[eidx];
4516 if (!(dbl(w) > 0.0))
continue;
4517 const std::unordered_map<std::size_t, T>& ex = ph_execs[eidx];
4518 const T r = ph_overlap[eidx];
4519 for (std::size_t aidx : lqn.actsof[eidx]) {
4520 const T xa = ex.at(aidx);
4521 ph_procresid[tidx] = T(ph_procresid[tidx] + w * r * xa * residt[aidx]);
4522 ph_actthinkt[tidx] = T(ph_actthinkt[tidx] + w * r * xa * act_think_time(aidx));
4523 for (std::size_t cidx : lqn.callsof[aidx]) {
4524 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
4525 const std::size_t tgte = lqn.callpair_dst[cidx];
4526 const std::size_t tgtt = lqn.parent[tgte];
4529 ph_ncalls(tidx, tgte) =
4530 T(ph_ncalls(tidx, tgte) + w * xa * lqn.callproc_mean[cidx]);
4531 ph_calltime(tidx, tgtt) =
4532 T(ph_calltime(tidx, tgtt) + w * r * xa * callservt[cidx]);
4533 ph_calltotal[tidx] = T(ph_calltotal[tidx] + w * r * xa * callservt[cidx]);
4541 void build_layers_ph(
bool flat =
false) {
4542 if (!ph_laws_ready) ph_assert_supported(flat);
4546 opt.interlocking =
false;
4547 interlockMethod =
"none";
4551 if (!ph_laws_ready) ph_init_laws();
4554 const std::size_t N = lqn.nidx;
4555 residt.assign(N + 1, Tzero());
4556 servt.assign(N + 1, Tzero());
4557 callservt.assign(lqn.ncalls + 1, Tzero());
4558 callresidt.assign(lqn.ncalls + 1, Tzero());
4559 for (std::size_t aidx = lqn.ashift + 1; aidx <= lqn.ashift + lqn.nacts; ++aidx)
4560 residt[aidx] = lqn.hostdem[aidx].disabled ? Tzero() : lqn.hostdem[aidx].mean;
4561 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
4562 if (lqn.calltype[cidx] != CallType::SYNC && lqn.calltype[cidx] != CallType::ASYNC)
4564 const std::size_t eidx = lqn.callpair_dst[cidx];
4565 callservt[cidx] = T(lqn.callproc_mean[cidx] * ph_hostmean[eidx]);
4566 callresidt[cidx] = callservt[cidx];
4568 ph_compose_entry_laws();
4572 build_ph_flat_layer();
4573 tput.assign(N + 1, Tzero());
4574 util.assign(N + 1, Tzero());
4575 thinkt.assign(N + 1, Tzero());
4576 update_layers_ph(0);
4580 std::vector<qn::Layer<T>> raw(NT() + 1);
4581 std::vector<bool> present(NT() + 1,
false);
4583 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx) {
4584 if (ignore[hidx])
continue;
4585 std::vector<std::size_t> callers;
4586 for (std::size_t tidx : lqn.tasksof[hidx]) {
4587 if (ignore[tidx])
continue;
4588 if (lqn.isref[tidx]) { callers.push_back(tidx);
continue; }
4589 for (std::size_t eidx : lqn.entriesof[tidx])
4590 if (ph_any_caller_of(eidx) || lqn.has_arrival[eidx]) {
4591 callers.push_back(tidx);
4595 if (callers.empty())
continue;
4596 build_layer_ph(raw[hidx], hidx, callers,
true);
4597 present[hidx] =
true;
4599 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
4600 const std::size_t tidx = lqn.tshift + t;
4601 if (ignore[tidx] || lqn.isref[tidx])
continue;
4602 std::vector<std::size_t> callers;
4603 for (std::size_t ct = 1; ct <= lqn.ntasks; ++ct) {
4604 const std::size_t c = lqn.tshift + ct;
4605 if (c == tidx || ignore[c])
continue;
4606 for (std::size_t e : lqn.entriesof[tidx])
4607 if (lqn.issynccaller.get(c, e)) { callers.push_back(c);
break; }
4609 if (callers.empty() && ph_async_calls_into(tidx).empty())
continue;
4610 build_layer_ph(raw[tidx], tidx, callers,
false);
4611 present[tidx] =
true;
4614 idxhash.assign(lqn.nidx + 1, -1);
4616 for (std::size_t i = 1; i <= NT(); ++i)
4618 idxhash[i] = next++;
4619 ensemble.push_back(std::move(raw[i]));
4621 layer_init_sol.assign(ensemble.size(), Matrix<T>());
4626 tput.assign(N + 1, Tzero());
4627 util.assign(N + 1, Tzero());
4628 thinkt.assign(N + 1, Tzero());
4629 update_layers_ph(0);
4645 void build_ph_flat_layer() {
4646 const std::vector<std::size_t> servers = ph_flat_server_set();
4647 const std::size_t nsrv = servers.size();
4651 m.clientIdx = m.add_station(qn::Station<T>{
4652 "Clients", NodeType::Delay, SchedStrategy::INF,
4653 std::numeric_limits<double>::infinity(),
false, 0});
4654 const std::size_t clientNode = m.node_of_station(m.clientIdx);
4655 m.server_idx_of.assign(lqn.nidx + 1, 0);
4657 std::vector<std::size_t> station_of(lqn.nidx + 1, 0);
4658 std::vector<std::size_t> serverNode(nsrv, 0);
4659 std::vector<std::size_t> srvStation(nsrv, 0);
4660 for (std::size_t si = 0; si < nsrv; ++si) {
4661 const std::size_t idx = servers[si];
4662 const bool ishost = idx <= lqn.nhosts;
4664 st.name = lqn.hashnames[idx];
4665 st.nodetype = lqn.sched[idx] == SchedStrategy::INF ? NodeType::Delay : NodeType::Queue;
4666 st.sched = lqn.sched[idx];
4667 st.nservers = lqn.sched[idx] == SchedStrategy::INF
4668 ? std::numeric_limits<double>::infinity()
4670 st.attr_ishost = ishost;
4672 const std::size_t s = m.add_station(st);
4674 station_of[idx] = s;
4675 serverNode[si] = m.node_of_station(s);
4676 m.server_idx_of[idx] = s;
4678 m.host_stations.push_back(s);
4680 m.task_stations.push_back(s);
4683 m.serverIdx = station_of[servers[0]];
4687 std::vector<std::vector<std::size_t>> callers_of(lqn.nidx + 1);
4688 std::vector<std::size_t> all_callers;
4689 for (std::size_t si = 0; si < nsrv; ++si) {
4690 const std::size_t idx = servers[si];
4691 std::vector<std::size_t> cs;
4692 if (idx <= lqn.nhosts) {
4693 for (std::size_t tidx : lqn.tasksof[idx]) {
4694 if (ignore[tidx])
continue;
4695 if (lqn.isref[tidx]) { cs.push_back(tidx);
continue; }
4696 for (std::size_t eidx : lqn.entriesof[tidx])
4697 if (ph_any_caller_of(eidx) || lqn.has_arrival[eidx]) {
4703 for (std::size_t ct = 1; ct <= lqn.ntasks; ++ct) {
4704 const std::size_t c = lqn.tshift + ct;
4705 if (c == idx || ignore[c])
continue;
4706 for (std::size_t e : lqn.entriesof[idx])
4707 if (lqn.issynccaller.get(c, e)) { cs.push_back(c);
break; }
4710 callers_of[idx] = cs;
4711 for (std::size_t c : cs)
4712 if (std::find(all_callers.begin(), all_callers.end(), c) == all_callers.end())
4713 all_callers.push_back(c);
4715 std::sort(all_callers.begin(), all_callers.end());
4718 std::vector<std::size_t> class_of_caller(lqn.nidx + 1, 0);
4720 for (std::size_t c : all_callers) {
4723 double nj = lqn.maxmult[c];
4724 if (std::isinf(nj)) {
4726 for (std::size_t k = 1; k <= NT(); ++k)
4727 if (lqn.taskgraph.get(k, c) != Tzero()) sacc += lqn.maxmult[k];
4729 if (std::isinf(nj) || nj == 0.0) {
4731 for (std::size_t k = 1; k <= NT(); ++k)
4732 if (std::isfinite(lqn.maxmult[k])) s2 += lqn.maxmult[k];
4733 nj = std::min(s2, 1000.0);
4737 jc.name = lqn.hashnames[c];
4738 jc.type = JobClassType::CLOSED;
4740 jc.refstat = m.clientIdx;
4741 jc.is_ref_class =
true;
4742 jc.attr_kind = int(LqnElement::TASK);
4744 const std::size_t k = m.add_class(jc);
4745 class_of_caller[c] = k;
4746 m.attr_tasks.emplace_back(k, c);
4748 const double zt = dbl(ref_think_time(c));
4749 m.set_service(m.clientIdx, k,
4758 for (std::size_t si = 0; si < nsrv; ++si)
4760 for (std::size_t si = 0; si < nsrv; ++si) {
4761 const std::size_t idx = servers[si];
4762 const std::vector<std::size_t>& cs = callers_of[idx];
4763 if (std::find(cs.begin(), cs.end(), c) == cs.end())
continue;
4764 m.set_service(srvStation[si], k,
4768 thinkt_map.push_back({idx, c, m.clientIdx, k});
4769 servt_map.push_back({idx, c, station_of[idx], k});
4774 std::vector<std::vector<std::pair<std::size_t, long>>> open_of(lqn.nidx + 1);
4775 std::size_t sourceStation = 0, sinkNode = 0;
4776 for (std::size_t si = 0; si < nsrv; ++si) {
4777 const std::size_t hidx = servers[si];
4778 if (hidx > lqn.nhosts)
continue;
4779 for (std::size_t c : callers_of[hidx]) {
4785 if (ph_open_arrival_only(c))
continue;
4786 for (std::size_t eidx : lqn.entriesof[c]) {
4787 if (!lqn.has_arrival[eidx])
continue;
4788 if (sourceStation == 0) {
4789 sourceStation = m.add_station(qn::Station<T>{
4790 "Source", NodeType::Source, SchedStrategy::EXT,
4791 std::numeric_limits<double>::infinity(),
false, 0});
4792 m.sourceIdx = sourceStation;
4793 sinkNode = m.add_node(
"Sink", NodeType::Sink,
false);
4794 m.sinkNode = sinkNode;
4797 oc.name = lqn.hashnames[eidx] +
".Open";
4798 oc.type = JobClassType::OPEN;
4799 oc.population = std::numeric_limits<double>::infinity();
4800 oc.refstat = sourceStation;
4801 oc.attr_kind = int(LqnElement::ENTRY);
4803 const std::size_t k = m.add_class(oc);
4804 m.set_service(sourceStation, k, lqn.arrival[eidx]);
4806 for (std::size_t s2 = 0; s2 < nsrv; ++s2)
4809 m.set_service(srvStation[si], k,
4811 open_of[hidx].emplace_back(k,
long(eidx));
4812 m.attr_entries.emplace_back(k, eidx);
4816 for (std::size_t si = 0; si < nsrv; ++si) {
4817 const std::size_t tidx = servers[si];
4818 if (tidx <= lqn.nhosts)
continue;
4819 for (std::size_t cidx : ph_async_calls_into(tidx)) {
4820 if (sourceStation == 0) {
4821 sourceStation = m.add_station(qn::Station<T>{
4822 "Source", NodeType::Source, SchedStrategy::EXT,
4823 std::numeric_limits<double>::infinity(),
false, 0});
4824 m.sourceIdx = sourceStation;
4825 sinkNode = m.add_node(
"Sink", NodeType::Sink,
false);
4826 m.sinkNode = sinkNode;
4828 const std::size_t eidx = lqn.callpair_dst[cidx];
4830 oc.name = lqn.callhashnames[cidx];
4831 oc.type = JobClassType::OPEN;
4832 oc.population = std::numeric_limits<double>::infinity();
4833 oc.refstat = sourceStation;
4834 oc.attr_kind = int(LqnElement::CALL);
4836 const std::size_t k = m.add_class(oc);
4839 for (std::size_t s2 = 0; s2 < nsrv; ++s2)
4842 m.set_service(srvStation[si], k,
4844 open_of[tidx].emplace_back(k, -
long(cidx));
4845 m.attr_calls.push_back({k, cidx, lqn.callpair_src[cidx], eidx});
4846 arv_call_map.push_back({tidx, cidx, sourceStation, k});
4847 call_map.push_back({tidx, cidx, station_of[tidx], k});
4854 for (std::size_t c : all_callers) {
4855 const std::size_t k = class_of_caller[c];
4856 std::size_t prev = clientNode;
4857 bool visited =
false;
4858 for (std::size_t si = 0; si < nsrv; ++si) {
4859 const std::vector<std::size_t>& cs = callers_of[servers[si]];
4860 if (std::find(cs.begin(), cs.end(), c) == cs.end())
continue;
4861 m.set_route(k, k, prev, serverNode[si], Tone());
4862 prev = serverNode[si];
4865 if (visited) m.set_route(k, k, prev, clientNode, Tone());
4867 if (sourceStation != 0) {
4868 const std::size_t srcNode = m.node_of_station(sourceStation);
4869 for (std::size_t si = 0; si < nsrv; ++si)
4870 for (
const auto& oa : open_of[servers[si]]) {
4871 m.set_route(oa.first, oa.first, srcNode, serverNode[si], Tone());
4872 m.set_route(oa.first, oa.first, serverNode[si], sinkNode, Tone());
4875 apply_host_task_priorities(m, servers);
4878 idxhash.assign(lqn.nidx + 1, -1);
4879 for (std::size_t idx : servers) idxhash[idx] = 0;
4881 ensemble.push_back(std::move(m));
4882 layer_init_sol.assign(ensemble.size(), Matrix<T>());
4885 const std::size_t nclasses = ensemble[0].classes.size();
4886 for (std::size_t idx : servers) {
4889 L.ishost = idx <= lqn.nhosts;
4890 L.callers = callers_of[idx];
4891 L.class_of_caller = class_of_caller;
4893 L.qstations.assign(1, station_of[idx]);
4894 L.svcmean_by_class.assign(nclasses + 1, Tzero());
4895 L.open_arrivals = open_of[idx];
4896 L.npop = npop < 1.0 ? 1.0 : npop;
4898 ph_has_layer[idx] =
true;
4911 std::vector<std::size_t> ph_flat_server_set()
const {
4912 for (std::size_t i = 1; i <= NT(); ++i) {
4913 if (lqn.repl[i] > 1.0)
4915 "method='flat.ph' does not support replicated processors or tasks, whose "
4916 "replicas need a submodel each. Use method='srvn.ph'.");
4919 "method='flat.ph' does not support cache tasks. Use method='default'.");
4920 if (lqn.hassetup[i])
4922 "method='flat.ph' does not support setup tasks, whose powered-down threads "
4923 "are per-layer state. Use method='srvn.ph'.");
4925 std::vector<std::size_t> servers;
4926 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx) {
4927 if (ignore[hidx] || lqn.tasksof[hidx].empty())
continue;
4929 for (std::size_t tidx : lqn.tasksof[hidx]) {
4930 if (ignore[tidx])
continue;
4931 if (lqn.isref[tidx]) { any =
true;
break; }
4932 for (std::size_t eidx : lqn.entriesof[tidx])
4933 if (ph_any_caller_of(eidx) || lqn.has_arrival[eidx]) { any =
true;
break; }
4936 if (any) servers.push_back(hidx);
4938 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
4939 const std::size_t tidx = lqn.tshift + t;
4940 if (ignore[tidx] || lqn.isref[tidx])
continue;
4941 bool has_caller =
false;
4942 for (std::size_t ct = 1; ct <= lqn.ntasks && !has_caller; ++ct) {
4943 const std::size_t c = lqn.tshift + ct;
4944 if (c == tidx || ignore[c])
continue;
4945 for (std::size_t e : lqn.entriesof[tidx])
4946 if (lqn.issynccaller.get(c, e)) { has_caller =
true;
break; }
4948 if (!has_caller && ph_async_calls_into(tidx).empty())
continue;
4949 servers.push_back(tidx);
4951 if (servers.empty())
4953 "method='flat.ph' found no server: the model has no processor with tasks.");
4958 void build_layer_ph(qn::Layer<T>& m, std::size_t idx,
4959 const std::vector<std::size_t>& callers,
bool ishost) {
4960 m.name = lqn.hashnames[idx];
4965 const double rawrepl = lqn.repl[idx];
4966 std::size_t nreplicas = 1;
4967 if (rawrepl > 1.0 && !callers.empty()) {
4970 for (std::size_t c : callers)
4971 if (lqn.repl[c] != rawrepl) reduce =
false;
4973 for (std::size_t c : callers)
4974 if (lqn.fanout_at(c, idx) < rawrepl) reduce =
false;
4976 nreplicas = reduce ? 1 :
static_cast<std::size_t
>(std::llround(rawrepl));
4977 if (reduce && !ishost) single_replica_tasks.insert(idx);
4979 const bool reduce_fanout = (nreplicas == 1 && rawrepl > 1.0 && !callers.empty());
4981 m.clientIdx = m.add_station(qn::Station<T>{
4982 "Clients", NodeType::Delay, SchedStrategy::INF,
4983 std::numeric_limits<double>::infinity(),
false, 0});
4984 const std::size_t clientNode = m.node_of_station(m.clientIdx);
4985 m.serverIdx = m.clientIdx + 1;
4989 L.callers = callers;
4990 L.nreplicas = nreplicas;
4991 L.class_of_caller.assign(lqn.nidx + 1, 0);
4992 std::vector<std::size_t> serverNode(nreplicas);
4993 for (std::size_t r = 0; r < nreplicas; ++r) {
4995 st.name = r == 0 ? lqn.hashnames[idx] : lqn.hashnames[idx] +
"." + std::to_string(r + 1);
4996 st.nodetype = lqn.sched[idx] == SchedStrategy::INF ? NodeType::Delay : NodeType::Queue;
4997 st.sched = lqn.sched[idx];
4998 st.nservers = lqn.sched[idx] == SchedStrategy::INF
4999 ? std::numeric_limits<double>::infinity()
5001 st.attr_ishost = ishost;
5003 const std::size_t s = m.add_station(st);
5004 L.qstations.push_back(s);
5005 serverNode[r] = m.node_of_station(s);
5009 for (std::size_t c : callers) {
5010 double nj = njobs(c, idx);
5012 const bool caller_single_replica =
5013 reduce_fanout || single_replica_tasks.count(c) > 0;
5014 nj = caller_single_replica ? lqn.maxmult[c] : lqn.maxmult[c] * lqn.repl[c];
5015 if (std::isinf(nj)) {
5017 for (std::size_t k = 1; k <= NT(); ++k)
5018 if (lqn.taskgraph.get(k, c) != Tzero()) s += lqn.maxmult[k];
5020 if (std::isinf(nj) || nj == 0.0) {
5022 for (std::size_t k = 1; k <= NT(); ++k)
5023 if (std::isfinite(lqn.maxmult[k])) s2 += lqn.maxmult[k] * lqn.repl[k];
5024 nj = std::min(s2, 1000.0);
5030 jc.name = lqn.hashnames[c];
5031 jc.type = JobClassType::CLOSED;
5033 jc.refstat = m.clientIdx;
5034 jc.is_ref_class =
true;
5035 jc.attr_kind = int(LqnElement::TASK);
5037 const std::size_t k = m.add_class(jc);
5038 L.class_of_caller[c] = k;
5039 m.attr_tasks.emplace_back(k, c);
5040 const double zt = dbl(ref_think_time(c));
5041 m.set_service(m.clientIdx, k,
5044 for (std::size_t s : L.qstations)
5049 thinkt_map.push_back({idx, c, m.clientIdx, k});
5050 servt_map.push_back({idx, c, L.qstations[0], k});
5054 std::size_t sourceStation = 0, sinkNode = 0;
5056 for (std::size_t c : callers) {
5063 if (ph_open_arrival_only(c))
continue;
5064 for (std::size_t eidx : lqn.entriesof[c]) {
5065 if (!lqn.has_arrival[eidx])
continue;
5066 if (sourceStation == 0) {
5067 sourceStation = m.add_station(qn::Station<T>{
5068 "Source", NodeType::Source, SchedStrategy::EXT,
5069 std::numeric_limits<double>::infinity(),
false, 0});
5070 m.sourceIdx = sourceStation;
5071 sinkNode = m.add_node(
"Sink", NodeType::Sink,
false);
5072 m.sinkNode = sinkNode;
5075 oc.name = lqn.hashnames[eidx] +
".Open";
5076 oc.type = JobClassType::OPEN;
5077 oc.population = std::numeric_limits<double>::infinity();
5078 oc.refstat = sourceStation;
5079 oc.attr_kind = int(LqnElement::ENTRY);
5081 const std::size_t k = m.add_class(oc);
5082 m.set_service(sourceStation, k, lqn.arrival[eidx]);
5083 for (std::size_t s : L.qstations) {
5087 L.open_arrivals.emplace_back(k,
long(eidx));
5088 m.attr_entries.emplace_back(k, eidx);
5092 for (std::size_t cidx : ph_async_calls_into(idx)) {
5093 if (sourceStation == 0) {
5094 sourceStation = m.add_station(qn::Station<T>{
5095 "Source", NodeType::Source, SchedStrategy::EXT,
5096 std::numeric_limits<double>::infinity(),
false, 0});
5097 m.sourceIdx = sourceStation;
5098 sinkNode = m.add_node(
"Sink", NodeType::Sink,
false);
5099 m.sinkNode = sinkNode;
5101 const std::size_t eidx = lqn.callpair_dst[cidx];
5103 oc.name = lqn.callhashnames[cidx];
5104 oc.type = JobClassType::OPEN;
5105 oc.population = std::numeric_limits<double>::infinity();
5106 oc.refstat = sourceStation;
5107 oc.attr_kind = int(LqnElement::CALL);
5109 const std::size_t k = m.add_class(oc);
5111 for (std::size_t s : L.qstations) {
5115 L.open_arrivals.emplace_back(k, -
long(cidx));
5116 m.attr_calls.push_back({k, cidx, lqn.callpair_src[cidx], eidx});
5117 arv_call_map.push_back({idx, cidx, sourceStation, k});
5118 call_map.push_back({idx, cidx, L.qstations[0], k});
5124 const T share = num_traits<T>::from_double(1.0 /
double(nreplicas));
5125 for (std::size_t c : callers) {
5126 const std::size_t k = L.class_of_caller[c];
5127 for (std::size_t r = 0; r < nreplicas; ++r) {
5128 m.set_route(k, k, clientNode, serverNode[r], share);
5129 m.set_route(k, k, serverNode[r], clientNode, Tone());
5132 for (
const auto& oa : L.open_arrivals) {
5133 const std::size_t k = oa.first;
5134 const std::size_t srcNode = m.node_of_station(sourceStation);
5135 for (std::size_t r = 0; r < nreplicas; ++r) {
5136 m.set_route(k, k, srcNode, serverNode[r], share);
5137 m.set_route(k, k, serverNode[r], sinkNode, Tone());
5140 L.svcmean_by_class.assign(m.classes.size() + 1, Tzero());
5142 for (std::size_t c : callers) {
5143 const double v = njobs(c, idx);
5144 if (std::isfinite(v) && v > 0.0) np += v;
5146 L.npop = np < 1.0 ? 1.0 : np;
5147 apply_host_task_priorities(m, std::vector<std::size_t>{idx});
5150 ph_has_layer[idx] =
true;
5154 workflow::PhLaw<T> ph_service_law(std::size_t idx,
bool ishost, std::size_t c)
const {
5157 std::vector<workflow::PhLaw<T>> laws;
5158 std::vector<T> probs;
5160 for (std::size_t eidx : lqn.entriesof[c]) {
5161 if (ph_hostlaw[eidx].S.rows() == 0 || !(dbl(ph_share[eidx]) > 0.0))
continue;
5162 laws.push_back(ph_hostlaw[eidx]);
5163 probs.push_back(ph_share[eidx]);
5164 tot += dbl(ph_share[eidx]);
5166 if (laws.empty())
return ph_immediate_law();
5167 for (T& p : probs) p = T(p / num_traits<T>::from_double(tot));
5173 bool started =
false;
5174 workflow::PhLaw<T> out;
5175 for (std::size_t eidx : lqn.entriesof[idx]) {
5176 const T n = ph_ncalls(c, eidx);
5178 const workflow::PhLaw<T> lp =
5183 if (!started)
return ph_immediate_law();
5191 T ph_delay_mean(std::size_t idx, std::size_t c)
const {
5201 T z = T(thinkt[c] + ref_think_time(c));
5202 if (!std::isfinite(dbl(z)) || dbl(z) < 0.0) z = Tzero();
5203 z = T(z + ph_actthinkt[c]);
5204 const std::size_t hidx = lqn.parent[c];
5205 if (!ph_served_here(idx, hidx)) z = T(z + ph_procresid[c]);
5206 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
5207 const std::size_t tidx = lqn.tshift + t;
5208 if (!ph_served_here(idx, tidx)) z = T(z + ph_calltime(c, tidx));
5210 if (!std::isfinite(dbl(z)) || dbl(z) < 0.0)
5220 bool ph_served_here(std::size_t idx, std::size_t elem)
const {
5221 if (elem < 1 || elem >= idxhash.size() || idx < 1 || idx >= idxhash.size())
5223 return idxhash[elem] >= 0 && idxhash[elem] == idxhash[idx];
5235 void update_layers_ph(
int) {
5236 for (std::size_t idx = 1; idx <= NT(); ++idx) {
5237 if (idxhash[idx] < 0 || !ph_has_layer[idx])
continue;
5238 PHLayer& L = ph_layers[idx];
5239 qn::Layer<T>& m = ensemble[std::size_t(idxhash[idx])];
5240 for (std::size_t c : L.callers) {
5241 const std::size_t k = L.class_of_caller[c];
5242 const workflow::PhLaw<T> sl = ph_service_law(idx, L.ishost, c);
5244 const Distrib<T> law = ph_station_law(sl);
5245 for (std::size_t s : L.qstations) m.set_service(s, k, law);
5246 const double zd = std::max(dbl(ph_delay_mean(idx, c)),
5248 m.set_service(m.clientIdx, k,
5251 for (
const auto& oa : L.open_arrivals) {
5252 const std::size_t k = oa.first;
5253 if (oa.second > 0) {
5255 L.svcmean_by_class[k] = ph_hostmean[std::size_t(oa.second)];
5258 const std::size_t cidx = std::size_t(-oa.second);
5259 const std::size_t eidx = lqn.callpair_dst[cidx];
5260 L.svcmean_by_class[k] = ph_entrymean[eidx];
5261 const Distrib<T> law = ph_station_law(ph_entrylaw[eidx]);
5262 for (std::size_t s : L.qstations) m.set_service(s, k, law);
5263 const std::size_t aidx = lqn.callpair_src[cidx];
5264 double rate = dbl(tput[aidx]) * dbl(lqn.callproc_mean[cidx]);
5267 m.set_service(m.sourceIdx, k,
5280 static T ph_residence(
const T& Q,
const T& X,
const T& RN) {
5281 const double q = num_traits<T>::to_double(Q), x = num_traits<T>::to_double(X);
5292 static T ph_inflation_of(
const T& R,
const T& S,
double npop) {
5294 const double s = num_traits<T>::to_double(S), r = num_traits<T>::to_double(R);
5296 if (!std::isfinite(f) || f < 1.0) f = 1.0;
5297 if (std::isfinite(npop) && npop >= 1.0 && f > npop) f = npop;
5298 return num_traits<T>::from_double(f);
5302 double ph_layer_pop(
const PHLayer& L, std::size_t idx)
const {
5305 if (L.npop >= 1.0)
return L.npop;
5307 for (std::size_t c : L.callers) {
5308 const double v = njobs(c, idx);
5309 if (std::isfinite(v) && v > 0.0) n += v;
5311 return n < 1.0 ? 1.0 : n;
5327 void update_metrics_ph(
int it) {
5328 const std::size_t N = lqn.nidx;
5329 servt.assign(N + 1, Tzero());
5330 residt.assign(N + 1, Tzero());
5331 callservt.assign(lqn.ncalls + 1, Tzero());
5332 callresidt.assign(lqn.ncalls + 1, Tzero());
5334 std::vector<T> inflNum(N + 1, Tzero()), inflDen(N + 1, Tzero());
5335 std::vector<T> taskTput(N + 1, Tzero()), openTput(N + 1, Tzero());
5338 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx) {
5339 if (idxhash[hidx] < 0 || !ph_has_layer[hidx])
continue;
5340 const PHLayer& L = ph_layers[hidx];
5341 const LayerResult<T>& res = results.back()[std::size_t(idxhash[hidx])];
5342 const double npop = ph_layer_pop(L, hidx);
5343 for (std::size_t c : L.callers) {
5344 const std::size_t k = L.class_of_caller[c];
5345 T X = Tzero(), Q = Tzero();
5346 for (std::size_t s : L.qstations) {
5347 X = T(X + res.TN(s - 1, k - 1));
5348 Q = T(Q + res.QN(s - 1, k - 1));
5350 const T R = ph_residence(Q, X, res.RN(L.qstations[0] - 1, k - 1));
5351 const T f = ph_inflation_of(R, L.svcmean_by_class[k], npop);
5352 if (!std::isfinite(dbl(X)) || dbl(X) < 0.0) X = Tzero();
5358 taskTput[c] = T(taskTput[c] + num_traits<T>::from_double(lqn.repl[c]) * X);
5359 for (std::size_t eidx : lqn.entriesof[c]) {
5360 const T sh = dbl(ph_share[eidx]) > 0.0 ? ph_share[eidx] : Tzero();
5361 const T w = T(sh * X);
5362 inflNum[eidx] = T(inflNum[eidx] + w * f);
5363 inflDen[eidx] = T(inflDen[eidx] + w);
5366 for (
const auto& oa : L.open_arrivals) {
5367 if (oa.second <= 0)
continue;
5368 const std::size_t k = oa.first;
5369 const std::size_t eidx = std::size_t(oa.second);
5370 T X = Tzero(), Q = Tzero();
5371 for (std::size_t s : L.qstations) {
5372 X = T(X + res.TN(s - 1, k - 1));
5373 Q = T(Q + res.QN(s - 1, k - 1));
5375 if (!std::isfinite(dbl(X)) || dbl(X) <= 0.0)
continue;
5376 const T f = ph_inflation_of(ph_residence(Q, X, res.RN(L.qstations[0] - 1, k - 1)),
5377 L.svcmean_by_class[k], npop);
5378 inflNum[eidx] = T(inflNum[eidx] + X * f);
5379 inflDen[eidx] = T(inflDen[eidx] + X);
5380 openTput[eidx] = T(openTput[eidx] + X);
5381 taskTput[lqn.parent[eidx]] = T(taskTput[lqn.parent[eidx]] + X);
5385 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
5386 const std::size_t eidx = lqn.eshift + e;
5389 f = dbl(inflNum[eidx]) / dbl(inflDen[eidx]);
5390 if (!std::isfinite(f) || f < 1.0) f = 1.0;
5391 const T fv = num_traits<T>::from_double(f);
5392 for (std::size_t aidx : lqn.actsof[eidx])
5393 residt[aidx] = T(fv * (lqn.hostdem[aidx].disabled ? Tzero()
5394 : lqn.hostdem[aidx].mean));
5398 std::vector<T> relw(N + 1, Tzero());
5399 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
5400 const std::size_t tidx = lqn.tshift + t;
5401 if (idxhash[tidx] < 0 || !ph_has_layer[tidx])
continue;
5402 const PHLayer& L = ph_layers[tidx];
5403 const LayerResult<T>& res = results.back()[std::size_t(idxhash[tidx])];
5404 const double npop = ph_layer_pop(L, tidx);
5405 for (std::size_t c : L.callers) {
5406 const std::size_t k = L.class_of_caller[c];
5407 T X = Tzero(), Q = Tzero();
5408 for (std::size_t s : L.qstations) {
5409 X = T(X + res.TN(s - 1, k - 1));
5410 Q = T(Q + res.QN(s - 1, k - 1));
5412 if (!std::isfinite(dbl(X)) || dbl(X) < 0.0) X = Tzero();
5413 const T g = ph_inflation_of(ph_residence(Q, X, res.RN(L.qstations[0] - 1, k - 1)),
5414 L.svcmean_by_class[k], npop);
5415 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
5416 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
5417 if (lqn.parent[lqn.callpair_src[cidx]] != c)
continue;
5418 if (lqn.parent[lqn.callpair_dst[cidx]] != tidx)
continue;
5419 const std::size_t eidx = lqn.callpair_dst[cidx];
5420 callservt[cidx] = T(lqn.callproc_mean[cidx] * g * ph_entrymean[eidx]);
5421 callresidt[cidx] = callservt[cidx];
5423 for (std::size_t eidx : lqn.entriesof[tidx])
5424 relw[eidx] = T(relw[eidx] + X * ph_ncalls(c, eidx));
5426 for (
const auto& oa : L.open_arrivals) {
5427 if (oa.second >= 0)
continue;
5428 const std::size_t k = oa.first;
5429 const std::size_t cidx = std::size_t(-oa.second);
5430 const std::size_t eidx = lqn.callpair_dst[cidx];
5432 for (std::size_t s : L.qstations) X = T(X + res.TN(s - 1, k - 1));
5433 const T R = res.RN(L.qstations[0] - 1, k - 1);
5434 if (std::isfinite(dbl(R)) && dbl(R) > 0.0) {
5435 callservt[cidx] = T(R * lqn.callproc_mean[cidx]);
5436 callresidt[cidx] = callservt[cidx];
5438 if (std::isfinite(dbl(X)) && dbl(X) > 0.0) relw[eidx] = T(relw[eidx] + X);
5443 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
5444 const std::size_t tidx = lqn.tshift + t;
5445 const std::vector<std::size_t>& entries = lqn.entriesof[tidx];
5446 if (entries.empty())
continue;
5453 for (std::size_t eidx : entries) tot = T(tot + relw[eidx] + openTput[eidx]);
5455 for (std::size_t eidx : entries)
5456 ph_share[eidx] = T((relw[eidx] + openTput[eidx]) / tot);
5458 for (std::size_t eidx : entries)
5459 ph_share[eidx] = num_traits<T>::from_double(1.0 /
double(entries.size()));
5467 for (std::size_t eidx : entries) tput[eidx] = T(tput[tidx] * ph_share[eidx]);
5473 const T nrep = num_traits<T>::from_double(std::max(1.0, lqn.repl[tidx]));
5475 : T(tput[tidx] / nrep);
5479 for (std::size_t aidx = lqn.ashift + 1; aidx <= lqn.ashift + lqn.nacts; ++aidx) {
5481 if (!std::isfinite(dbl(v)) && it > 1 && !std::isnan(residt_prev[aidx]))
5482 v = residt_prev_v[aidx];
5483 if (relax_omega < 1.0 && it > 1 && !std::isnan(residt_prev[aidx])) {
5484 const T om = num_traits<T>::from_double(relax_omega);
5485 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5486 v = T(om * v + om1 * residt_prev_v[aidx]);
5489 residt_prev[aidx] = dbl(v);
5490 residt_prev_v[aidx] = v;
5492 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
5493 T v = callservt[cidx];
5494 if (!std::isfinite(dbl(v)))
5495 v = (it > 1 && std::isfinite(callservt_prev[cidx])) ? callservt_prev_v[cidx]
5497 if (relax_omega < 1.0 && it > 1 && !std::isnan(callservt_prev[cidx])) {
5498 const T om = num_traits<T>::from_double(relax_omega);
5499 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5500 v = T(om * v + om1 * callservt_prev_v[cidx]);
5502 callservt[cidx] = v;
5503 callresidt[cidx] = v;
5504 callservt_prev[cidx] = dbl(v);
5505 callresidt_prev[cidx] = dbl(v);
5506 callservt_prev_v[cidx] = v;
5514 ph_compose_entry_laws();
5516 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
5517 const std::size_t eidx = lqn.eshift + e;
5518 if (!ph_has_wf[eidx])
continue;
5519 const std::unordered_map<std::size_t, T>& ex = ph_execs[eidx];
5520 for (std::size_t aidx : lqn.actsof[eidx]) {
5521 T sa = T(residt[aidx] + act_think_time(aidx));
5522 for (std::size_t cidx : lqn.callsof[aidx])
5523 if (lqn.calltype[cidx] == CallType::SYNC) sa = T(sa + callservt[cidx]);
5525 servt_prev[aidx] = dbl(sa);
5526 servt_prev_v[aidx] = sa;
5527 tput[aidx] = T(tput[eidx] * ex.at(aidx));
5528 tput_prev[aidx] = dbl(tput[aidx]);
5529 tput_prev_v[aidx] = tput[aidx];
5532 : Distrib<T>::disabled_dist();
5535 servt[eidx] = ph_entrymean[eidx];
5536 residt[eidx] = ph_entrymean[eidx];
5552 void update_think_times_ph(
int it) {
5555 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
5556 const std::size_t tidx = lqn.tshift + t;
5557 if (ignore[tidx])
continue;
5558 const T ztask = ref_think_time(tidx);
5559 if (idxhash[tidx] < 0) {
5564 const double arvrate = open_arrival_rate_of(tidx);
5566 double nja = lqn.maxmult[tidx];
5567 if (!std::isfinite(nja) || nja <= 0.0) {
5569 for (std::size_t c = 1; c <= NT(); ++c) nja = std::max(nja, njobs(tidx, c));
5572 const std::size_t hidx = lqn.parent[tidx];
5573 if (hidx >= 1 && hidx < idxhash.size() && idxhash[hidx] >= 0 &&
5574 ph_has_layer[hidx] && ph_layers[hidx].class_of_caller[tidx] > 0) {
5575 const PHLayer& HL = ph_layers[hidx];
5576 const LayerResult<T>& hr = results.back()[std::size_t(idxhash[hidx])];
5577 const T rr = hr.RN(HL.qstations[0] - 1, HL.class_of_caller[tidx] - 1);
5578 if (!std::isnan(dbl(rr))) hres = rr;
5580 T za = T(num_traits<T>::from_double(nja / arvrate) - hres - ztask);
5581 if (za < floorv) za = floorv;
5582 if (relax_omega < 1.0 && it > 1 && !std::isnan(thinkt_prev[tidx])) {
5583 const T om = num_traits<T>::from_double(relax_omega);
5584 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5585 za = T(om * za + om1 * thinkt_prev_v[tidx]);
5587 tput[tidx] = num_traits<T>::from_double(arvrate);
5589 thinkt_prev[tidx] = dbl(za);
5590 thinkt_prev_v[tidx] = za;
5600 const PHLayer& L = ph_layers[tidx];
5601 const qn::Layer<T>& m = ensemble[std::size_t(idxhash[tidx])];
5602 const LayerResult<T>& r = results.back()[std::size_t(idxhash[tidx])];
5604 for (std::size_t k = 0; k < m.nclasses; ++k) {
5605 const T v = r.UN(L.qstations[0] - 1, k);
5606 if (!std::isnan(dbl(v))) U = T(U + v);
5614 if (dbl(ph_setupshare[tidx]) > 0.0)
5615 U = T(U * (Tone() - ph_setupshare[tidx]));
5618 T X = ph_xdemand[tidx];
5621 double nj = lqn.maxmult[tidx];
5622 if (!std::isfinite(nj) || nj <= 0.0) {
5624 for (std::size_t c = 1; c <= NT(); ++c) nj = std::max(nj, njobs(tidx, c));
5628 if (lqn.sched[tidx] == SchedStrategy::INF) {
5630 z = T((num_traits<T>::from_double(nj) - U) / X - ztask);
5632 const T om = U > Tone() ? T(U - Tone()) : T(Tone() - U);
5633 z = T(num_traits<T>::from_double(nj) * om / X - ztask);
5638 if (z < floorv) z = floorv;
5639 if (it > 1 && !std::isnan(thinkt_prev[tidx]) && !std::isfinite(dbl(z)))
5640 z = thinkt_prev_v[tidx];
5641 if (relax_omega < 1.0 && it > 1 && !std::isnan(thinkt_prev[tidx])) {
5642 const T om = num_traits<T>::from_double(relax_omega);
5643 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5644 z = T(om * z + om1 * thinkt_prev_v[tidx]);
5647 thinkt_prev[tidx] = dbl(z);
5648 thinkt_prev_v[tidx] = z;
5662 LnSolution<T> aggregate_ph() {
5663 const std::size_t N = lqn.nidx;
5665 out.QN.assign(N + 1, Tzero());
5666 out.UN.assign(N + 1, Tzero());
5667 out.RN.assign(N + 1, Tzero());
5668 out.TN.assign(N + 1, Tzero());
5669 out.AN.assign(N + 1, Tzero());
5670 out.WN.assign(N + 1, Tzero());
5671 out.defined_Q.assign(N + 1,
false);
5672 out.defined_U.assign(N + 1,
false);
5673 out.defined_R.assign(N + 1,
false);
5674 out.defined_T.assign(N + 1,
false);
5675 out.defined_A.assign(N + 1,
false);
5676 out.defined_W.assign(N + 1,
false);
5677 out.iterations = iterations_done;
5678 out.converged = did_converge;
5680 std::vector<T> PN(N + 1, Tzero()), UT(N + 1, Tzero());
5681 std::vector<bool> hasPN(N + 1,
false), hasUT(N + 1,
false);
5683 for (std::size_t a = 1; a <= lqn.nacts; ++a) {
5684 const std::size_t aidx = lqn.ashift + a;
5685 const std::size_t tidx = lqn.parent[aidx];
5686 if (ignore[tidx])
continue;
5687 const std::size_t hidx = lqn.parent[tidx];
5688 out.TN[aidx] = tput[aidx];
5689 out.defined_T[aidx] =
true;
5690 out.RN[aidx] = servt[aidx];
5691 out.defined_R[aidx] =
true;
5692 UT[aidx] = T(tput[aidx] * servt[aidx]);
5698 const T hd = lqn.hostdem[aidx].disabled ? Tzero() : lqn.hostdem[aidx].mean;
5699 PN[aidx] = T(tput[aidx] * hd / num_traits<T>::from_double(ph_host_servers(hidx)));
5701 PN[hidx] = T(PN[hidx] + PN[aidx]);
5705 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
5706 const std::size_t eidx = lqn.eshift + e;
5707 const std::size_t tidx = lqn.parent[eidx];
5708 if (ignore[tidx])
continue;
5709 out.TN[eidx] = tput[eidx];
5710 out.defined_T[eidx] =
true;
5711 out.RN[eidx] = servt[eidx];
5712 out.defined_R[eidx] =
true;
5713 UT[eidx] = T(tput[eidx] * servt[eidx]);
5715 for (std::size_t aidx : lqn.actsof[eidx]) {
5716 PN[eidx] = T(PN[eidx] + PN[aidx]);
5723 if (ph_has_wf[eidx]) {
5724 const std::unordered_map<std::size_t, T>& ex = ph_execs[eidx];
5725 for (std::size_t aidx : lqn.actsof[eidx]) {
5726 out.WN[aidx] = T(ph_share[eidx] * ex.at(aidx) * residt[aidx]);
5727 out.defined_W[aidx] =
true;
5730 UT[tidx] = T(UT[tidx] + UT[eidx]);
5734 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
5735 const std::size_t tidx = lqn.tshift + t;
5736 if (ignore[tidx])
continue;
5737 out.TN[tidx] = tput[tidx];
5738 out.defined_T[tidx] =
true;
5741 for (std::size_t aidx : lqn.actsof[tidx]) {
5742 PN[tidx] = T(PN[tidx] + PN[aidx]);
5744 if (out.defined_W[aidx]) { w = T(w + out.WN[aidx]); anyw =
true; }
5746 if (anyw) { out.WN[tidx] = w; out.defined_W[tidx] =
true; }
5749 for (std::size_t hidx = 1; hidx <= lqn.nhosts; ++hidx)
5750 out.defined_T[hidx] =
false;
5752 for (std::size_t idx = 1; idx <= N; ++idx) {
5753 out.QN[idx] = UT[idx];
5754 out.defined_Q[idx] = hasUT[idx];
5755 out.UN[idx] = PN[idx];
5756 out.defined_U[idx] = hasPN[idx];
5762 out.UN[idx] = Tzero();
5763 out.defined_U[idx] =
true;
5764 out.defined_A[idx] =
false;
5765 const bool host = lqn.type[idx] == LqnElement::HOST;
5766 const bool task = lqn.type[idx] == LqnElement::TASK;
5767 const bool entry = lqn.type[idx] == LqnElement::ENTRY;
5768 out.QN[idx] = Tzero();
5769 out.defined_Q[idx] = !host;
5770 out.RN[idx] = Tzero();
5771 out.defined_R[idx] = !host && !task;
5772 out.WN[idx] = Tzero();
5773 out.defined_W[idx] = !host && !entry;
5774 out.TN[idx] = Tzero();
5775 out.defined_T[idx] = !host;
5781 void update_metrics(
int it) {
5782 if (is_ph_encoding()) {
5783 update_metrics_ph(it);
5786 if (lnmethod ==
"moment3") {
5787 update_metrics_moment_based(it);
5790 update_metrics_default(it);
5801 std::size_t layer_refclass(std::size_t idx,
const qn::Layer<T>& L, std::size_t k)
const {
5802 const auto it = layer_normclass.find(idx);
5803 if (it != layer_normclass.end() && k < it->second.size() && it->second[k] > 0)
5804 return it->second[k];
5806 for (std::size_t cc = 0; cc < L.nchains; ++cc)
5807 if (L.chains[cc][k]) c = cc;
5808 return L.refclass[c];
5811 void update_metrics_default(
int it) {
5812 const std::size_t N = lqn.nidx;
5813 servt.assign(N + 1, Tzero());
5814 residt.assign(N + 1, Tzero());
5815 const int iter_min = std::min(30,
int(std::ceil(opt.iter_max / 4.0)));
5816 const bool averaging = averagingstart >= 0 && it >= iter_min;
5817 const int wnd = averaging ? int(it - averagingstart + 1) : 1;
5819 for (
const UpdRow& row : servt_map) {
5820 const std::size_t e = std::size_t(idxhash[row.idx]);
5821 const qn::Layer<T>& L = ensemble[e];
5822 const std::size_t k = row.cls - 1;
5823 const std::size_t refclass_c = layer_refclass(row.idx, L, k);
5824 const std::size_t refstat_k = L.classes[k].refstat;
5826 T sv = Tzero(), rs = Tzero(), tp = Tzero();
5827 const T wT = T(Tone() / num_traits<T>::from_int(wnd));
5828 for (
int w = 0; w < wnd; ++w) {
5829 const LayerResult<T>& r = results[results.size() - 1 - w][e];
5830 sv += r.RN(row.node - 1, k) * wT;
5831 const T TN_ref = (refclass_c > 0 && refstat_k > 0) ? r.TN(refstat_k - 1, refclass_c - 1) : Tzero();
5833 rs += r.QN(row.node - 1, k) / TN_ref * wT;
5835 rs += r.WN(row.node - 1, k) * wT;
5836 tp += r.TN(row.node - 1, k) * wT;
5838 servt[row.aidx] = sv;
5839 residt[row.aidx] = rs;
5840 tput[row.aidx] = tp;
5843 const Distrib<T>& at = lqn.actthink[row.aidx];
5845 servt[row.aidx] = T(servt[row.aidx] + at.mean);
5846 residt[row.aidx] = T(residt[row.aidx] + at.mean);
5854 if (async_only_activity(row.aidx)) residt[row.aidx] = servt[row.aidx];
5856 if (relax_omega < 1.0 && it > 1) {
5857 const T om = num_traits<T>::from_double(relax_omega);
5858 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5859 if (!std::isnan(servt_prev[row.aidx]))
5860 servt[row.aidx] = T(om * servt[row.aidx] + om1 * servt_prev_v[row.aidx]);
5861 if (!std::isnan(residt_prev[row.aidx]))
5862 residt[row.aidx] = T(om * residt[row.aidx] + om1 * residt_prev_v[row.aidx]);
5863 if (!std::isnan(tput_prev[row.aidx]))
5864 tput[row.aidx] = T(om * tput[row.aidx] + om1 * tput_prev_v[row.aidx]);
5866 servt_prev[row.aidx] = dbl(servt[row.aidx]);
5867 residt_prev[row.aidx] = dbl(residt[row.aidx]);
5868 tput_prev[row.aidx] = dbl(tput[row.aidx]);
5869 servt_prev_v[row.aidx] = servt[row.aidx];
5870 residt_prev_v[row.aidx] = residt[row.aidx];
5871 tput_prev_v[row.aidx] = tput[row.aidx];
5873 if (servt[row.aidx] > Tzero() && dbl(servt[row.aidx]) <= 1e10)
5883 servt_ph1.assign(N + 1, Tzero());
5884 servt_ph2.assign(N + 1, Tzero());
5885 for (std::size_t a = 1; a <= lqn.nacts; ++a) {
5886 const std::size_t aidx = lqn.ashift + a;
5887 if (lqn.actphase[a] == 1)
5888 servt_ph1[aidx] = servt[aidx];
5890 servt_ph2[aidx] = servt[aidx];
5897 for (
const UpdRow& row : thinkt_map) {
5898 if (!tputproc[row.aidx].disabled)
continue;
5899 const std::size_t e = std::size_t(idxhash[row.idx]);
5901 const T wT = T(Tone() / num_traits<T>::from_int(wnd));
5902 for (
int w = 0; w < wnd; ++w)
5903 tp += results[results.size() - 1 - w][e].TN(row.node - 1, row.cls - 1) * wT;
5904 tput[row.aidx] = tp;
5909 callservt.assign(lqn.ncalls + 1, Tzero());
5910 callresidt.assign(lqn.ncalls + 1, Tzero());
5911 for (
const UpdRow& row : call_map) {
5912 if (row.node <= 1)
continue;
5913 const std::size_t e = std::size_t(idxhash[row.idx]);
5914 const LayerResult<T>& r = results.back()[e];
5915 callservt[row.aidx] =
5916 T(r.RN(row.node - 1, row.cls - 1) * lqn.callproc_mean[row.aidx]);
5922 const qn::Layer<T>& L = ensemble[e];
5923 const std::size_t k = row.cls - 1;
5924 const std::size_t refclass_c = layer_refclass(row.idx, L, k);
5925 const std::size_t refstat_k = L.classes[k].refstat;
5926 const T TN_ref = (refclass_c > 0 && refstat_k > 0) ? r.TN(refstat_k - 1, refclass_c - 1) : Tzero();
5928 ? T(r.QN(row.node - 1, k) / TN_ref)
5929 : r.WN(row.node - 1, k);
5931 const T rw = region_wait(e, row.cls);
5933 callservt[row.aidx] = T(callservt[row.aidx] + rw);
5934 callresidt[row.aidx] = T(callresidt[row.aidx] + rw);
5936 if (relax_omega < 1.0 && it > 1 && !std::isnan(callservt_prev[row.aidx])) {
5937 const T om = num_traits<T>::from_double(relax_omega);
5938 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
5939 callservt[row.aidx] =
5940 T(om * callservt[row.aidx] + om1 * callservt_prev_v[row.aidx]);
5942 callservt_prev[row.aidx] = dbl(callservt[row.aidx]);
5943 callservt_prev_v[row.aidx] = callservt[row.aidx];
5944 callresidt_prev[row.aidx] = dbl(callresidt[row.aidx]);
5947 resolve_entry_service();
5959 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
5960 const std::size_t eidx = lqn.eshift + e;
5961 servt_ph2[eidx] = (eidx < entry_servt_last.size() && entry_servt_last[eidx] > Tzero())
5962 ? T(entry_servt_ph2[eidx] * servt[eidx] / entry_servt_last[eidx])
5964 servt_ph1[eidx] = T(servt[eidx] - servt_ph2[eidx]);
5966 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
5967 const std::size_t eidx = lqn.eshift + e;
5968 const std::size_t tidx = lqn.parent[eidx];
5970 if (lqn.isref[tidx] || !lqn.issynccaller.any_col(eidx)) {
5971 residt[eidx] = servt[eidx];
5974 T entry_tput = Tzero();
5976 entry_tput = tput[eidx];
5978 entry_tput = tput[tidx];
5984 residt[eidx] = T(servt_ph1[eidx] + prOvertake[e] * servt_ph2[eidx]);
5988 for (
const UpdRow& row : call_map) {
5989 if (row.node <= 1)
continue;
5990 const std::size_t eidx = lqn.callpair_dst[row.aidx];
5993 for (
const UpdRow& row : call_map) {
5994 if (row.node <= 1)
continue;
5995 const std::size_t eidx = lqn.callpair_dst[row.aidx];
5997 callservt[row.aidx] = servt[eidx];
5998 callservtproc[row.aidx] = servtproc[eidx];
5999 }
else if (callservt[row.aidx] > Tzero()) {
6009 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
6010 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
6011 const std::size_t target = lqn.callpair_dst[cidx];
6012 if (target <= lqn.eshift || target > lqn.eshift + lqn.nentries)
continue;
6014 const T eff = residt[target];
6015 if (!(eff > Tzero()))
continue;
6016 const T w = T(eff * lqn.callproc_mean[cidx]);
6017 callservt[cidx] = w;
6018 callresidt[cidx] = w;
6038 static void cdf_moments(
const fluid::FluidPassage& c,
double& m1,
double& m2,
double& m3) {
6040 for (std::size_t i = 0; i + 1 < c.t.size(); ++i) {
6041 const double x = 0.5 * (c.t[i + 1] + c.t[i]);
6042 const double w = c.cdf[i + 1] - c.cdf[i];
6045 m3 += x * x * x * w;
6050 const std::vector<std::vector<fluid::FluidPassage>>& layer_cdf(std::size_t e) {
6051 if (cdf_repo[e].empty()) {
6058 ensemble[e].refresh_rt();
6060 cdf_repo[e] = detail::ln_fluid_cdf_respt(ensemble[e], opt.layer_fluid);
6061 }
catch (
const std::exception&) {
6068 cdf_repo[e] = layer_cdf_exp_fallback(e);
6076 std::vector<std::vector<fluid::FluidPassage>> layer_cdf_exp_fallback(std::size_t e) {
6077 const qn::NetworkStruct<T>& L = ensemble[e];
6078 std::vector<std::vector<fluid::FluidPassage>> out(
6079 L.nstations, std::vector<fluid::FluidPassage>(L.nclasses));
6080 if (results.empty() || e >= results.back().size())
return out;
6081 const Matrix<T>& RN = results.back()[e].RN;
6082 const std::size_t npts = 100;
6083 for (std::size_t i = 0; i < L.nstations; ++i) {
6084 if (L.stations[i].nodetype == qn::NodeType::Source)
continue;
6085 for (std::size_t r = 0; r < L.nclasses; ++r) {
6086 if (L.disabled[i][r])
continue;
6087 const double rn = (i < static_cast<std::size_t>(RN.rows()) &&
6088 r < static_cast<std::size_t>(RN.cols()))
6091 if (!(std::isfinite(rn) && rn > 0.0))
continue;
6092 fluid::FluidPassage& cell = out[i][r];
6093 cell.t.reserve(npts);
6094 cell.cdf.reserve(npts);
6095 for (std::size_t j = 0; j < npts; ++j) {
6097 0.001 + (0.999 - 0.001) *
static_cast<double>(j) / (npts - 1);
6098 cell.cdf.push_back(q);
6099 cell.t.push_back(-std::log(1.0 - q) * rn);
6120 int entry_visit_ratio(std::size_t eidx, T& ratio)
const {
6121 const std::size_t tidx = lqn.parent[eidx];
6122 const std::size_t hidx = lqn.parent[tidx];
6123 if (ignore[tidx] || ignore[hidx] || idxhash[hidx] < 0)
return 0;
6124 if (!lqn.issynccaller.any_col(eidx))
return 1;
6125 const std::size_t hl = std::size_t(idxhash[hidx]);
6126 const qn::Layer<T>& L = ensemble[hl];
6127 const LayerResult<T>& r = results.back()[hl];
6128 T task_tput = Tzero(), entry_tput = Tzero();
6129 for (
const auto& kv : L.attr_tasks)
6130 if (kv.second == tidx) task_tput += r.TN(L.clientIdx - 1, kv.first - 1);
6131 for (
const auto& kv : L.attr_entries)
6132 if (kv.second == eidx) entry_tput += r.TN(L.clientIdx - 1, kv.first - 1);
6134 ratio = T(task_tput / (entry_tput > floor ? entry_tput : floor));
6139 Matrix<T> entry_service_resolvent()
const {
6140 const std::size_t dim = lqn.nidx + lqn.ncalls;
6141 Matrix<T> A(dim + 1, dim + 1, Tzero());
6142 for (std::size_t i = 0; i <= dim; ++i) {
6144 for (std::size_t j = 0; j <= dim; ++j) A(i, j) = T(A(i, j) - servtmatrix(i, j));
6146 return ::line::inverse(A);
6172 void update_metrics_moment_based(
int it) {
6173 const std::size_t N = lqn.nidx;
6174 servt.assign(N + 1, Tzero());
6175 residt.assign(N + 1, Tzero());
6176 callservt.assign(lqn.ncalls + 1, Tzero());
6177 callresidt.assign(lqn.ncalls + 1, Tzero());
6180 for (
const UpdRow& row : servt_map) {
6181 const std::size_t e = std::size_t(idxhash[row.idx]);
6182 const qn::Layer<T>& L = ensemble[e];
6183 const LayerResult<T>& r = results.back()[e];
6184 const std::size_t k = row.cls - 1;
6186 for (std::size_t cc = 0; cc < L.nchains; ++cc)
6187 if (L.chains[cc][k]) c = cc;
6188 const std::size_t refclass_c = L.refclass[c];
6189 const std::size_t refstat_k = L.classes[k].refstat;
6190 const T TN_ref = (refclass_c > 0 && refstat_k > 0) ? r.TN(refstat_k - 1, refclass_c - 1) : Tzero();
6192 tput[row.aidx] = r.TN(row.node - 1, k);
6194 ? T(r.QN(row.node - 1, k) / TN_ref)
6195 : r.WN(row.node - 1, k);
6196 if (!hasconverged) {
6197 servt[row.aidx] = r.RN(row.node - 1, k);
6199 const Distrib<T>& at = lqn.actthink[row.aidx];
6201 servt[row.aidx] = T(servt[row.aidx] + at.mean);
6202 residt[row.aidx] = T(residt[row.aidx] + at.mean);
6207 if (async_only_activity(row.aidx)) residt[row.aidx] = servt[row.aidx];
6209 servtcdf[row.aidx] = layer_cdf(e)[row.node - 1][k];
6213 for (
const UpdRow& row : call_map) {
6214 if (row.node <= 1)
continue;
6215 const std::size_t e = std::size_t(idxhash[row.idx]);
6216 const LayerResult<T>& r = results.back()[e];
6217 callresidt[row.aidx] = r.WN(row.node - 1, row.cls - 1);
6219 callservt[row.aidx] =
6220 T(r.RN(row.node - 1, row.cls - 1) * lqn.callproc_mean[row.aidx]);
6222 callservtcdf[row.aidx] = layer_cdf(e)[row.node - 1][row.cls - 1];
6226 moment3_means_pass(it);
6228 moment3_distribution_pass(it);
6232 void moment3_means_pass(
int it) {
6233 const std::size_t dim = lqn.nidx + lqn.ncalls;
6234 std::vector<T> x(dim + 1, Tzero());
6235 for (std::size_t i = 1; i <= lqn.nidx; ++i) x[i] = residt[i];
6236 for (std::size_t c = 1; c <= lqn.ncalls; ++c) x[lqn.nidx + c] = callresidt[c];
6239 const Matrix<T> Rinv = entry_service_resolvent();
6240 std::vector<T> entry_servt(dim + 1, Tzero());
6241 for (std::size_t i = 1; i <= dim; ++i) {
6243 for (std::size_t j = 1; j <= dim; ++j)
6244 if (Rinv(i, j) != Tzero()) s += Rinv(i, j) * x[j];
6247 for (std::size_t i = 1; i <= lqn.eshift; ++i) entry_servt[i] = Tzero();
6258 for (std::size_t e = 1; e <= lqn.nentries; ++e)
6259 servt[lqn.eshift + e] = entry_servt[lqn.eshift + e];
6262 std::vector<T> entry_residt(dim + 1, Tzero());
6263 for (std::size_t i = lqn.eshift + 1; i <= lqn.eshift + lqn.nentries; ++i) {
6265 for (std::size_t j = 1; j <= dim; ++j)
6266 if (servtmatrix(i, j) != Tzero()) s += servtmatrix(i, j) * x[j];
6267 entry_residt[i] = s;
6270 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
6271 const std::size_t eidx = lqn.eshift + e;
6273 const int state = entry_visit_ratio(eidx, ratio);
6274 if (state == 0)
continue;
6276 residt[eidx] = entry_residt[eidx];
6279 servt[eidx] = T(entry_servt[eidx] * ratio);
6280 residt[eidx] = T(entry_residt[eidx] * ratio);
6283 for (
const UpdRow& row : call_map) {
6284 if (row.node <= 1)
continue;
6285 const std::size_t eidx = lqn.callpair_dst[row.aidx];
6288 for (
const UpdRow& row : call_map) {
6289 if (row.node <= 1)
continue;
6290 const std::size_t eidx = lqn.callpair_dst[row.aidx];
6293 callservt[row.aidx] = servt[eidx];
6294 callservtproc[row.aidx] = servtproc[eidx];
6295 }
else if (callservt[row.aidx] > Tzero()) {
6311 void moment3_distribution_pass(
int it) {
6312 if constexpr (!num_traits<T>::has_transcendental) {
6314 "SolverLN: the 'moment3' method fits an APH to a response-time CDF, which needs "
6315 "square roots and a matrix exponential; rerun with --arith double or real");
6317 const std::size_t dim = lqn.nidx + lqn.ncalls;
6318 const Matrix<T> Rinv = entry_service_resolvent();
6321 mam::AphPair<T> zero_law;
6322 zero_law.alpha.push_back(Tone());
6325 for (std::size_t en = 1; en <= lqn.nentries; ++en) {
6326 const std::size_t eidx = lqn.eshift + en;
6327 std::vector<mam::AphPair<T>> seq;
6328 for (std::size_t fitidx = 1; fitidx <= dim; ++fitidx) {
6329 if (!(Rinv(eidx, fitidx) > Tzero()))
continue;
6333 if (fitidx <= lqn.eshift + lqn.nentries)
continue;
6334 const bool is_call = fitidx > lqn.nidx;
6335 const fluid::FluidPassage& curve =
6336 is_call ? callservtcdf[fitidx - lqn.nidx] : servtcdf[fitidx];
6337 double m1 = 0.0, m2 = 0.0, m3 = 0.0;
6338 cdf_moments(curve, m1, m2, m3);
6342 if (!is_call && !lqn.actthink[fitidx].disabled &&
6344 const Distrib<T>& zt = lqn.actthink[fitidx];
6348 m3 = m3 + 3.0 * m2 * t1 + 3.0 * m1 * t2 + t3;
6349 m2 = m2 + 2.0 * m1 * t1 + t2;
6360 num_traits<T>::from_double(m1), num_traits<T>::from_double(m2),
6361 num_traits<T>::from_double(m3), 10u,
6363 const mam::AphPair<T> law = aph_pair_of_map(fit.aph);
6365 double reps = dbl(Rinv(eidx, fitidx));
6368 if (is_call) reps *= dbl(lqn.callproc_mean[fitidx - lqn.nidx]);
6369 const long whole =
static_cast<long>(std::floor(reps));
6370 const double frac = reps -
static_cast<double>(whole);
6371 for (
long q = 0; q < whole; ++q) seq.push_back(law);
6374 num_traits<T>::from_double(frac),
6375 num_traits<T>::from_double(1.0 - frac),
6379 const std::size_t cidx = fitidx - lqn.nidx;
6380 callservt[cidx] = num_traits<T>::from_double(m1);
6383 servt[fitidx] = num_traits<T>::from_double(m1);
6389 servt[eidx] = Tzero();
6393 entryproc[en] = entry_law;
6396 entrycdfrespt[en] = aph_eval_cdf(entry_law);
6404 std::vector<T> x(dim + 1, Tzero());
6405 for (std::size_t i = 1; i <= lqn.nidx; ++i) x[i] = residt[i];
6406 for (std::size_t c = 1; c <= lqn.ncalls; ++c) x[lqn.nidx + c] = callresidt[c];
6407 for (std::size_t en = 1; en <= lqn.nentries; ++en) {
6408 const std::size_t eidx = lqn.eshift + en;
6410 for (std::size_t j = 1; j <= dim; ++j)
6411 if (servtmatrix(eidx, j) != Tzero()) s += servtmatrix(eidx, j) * x[j];
6413 const int state = entry_visit_ratio(eidx, ratio);
6414 if (state == 0)
continue;
6415 residt[eidx] = state == 2 ? T(s * ratio) : s;
6419 for (
const UpdRow& row : call_map) {
6420 if (row.node <= 1)
continue;
6421 const std::size_t eidx = lqn.callpair_dst[row.aidx];
6422 callservt[row.aidx] = servt[eidx];
6429 moment_pass_done =
true;
6441 static mam::AphPair<T> aph_pair_of_map(
const mam::Map<T>& m) {
6442 mam::AphPair<T> out;
6443 const std::size_t n = m.D0.rows();
6445 out.alpha.assign(n, Tzero());
6446 for (std::size_t i = 0; i < n; ++i) {
6448 for (std::size_t j = 0; j < n; ++j) rowsum += m.D1(i, j);
6450 for (std::size_t j = 0; j < n; ++j) out.alpha[j] = T(m.D1(i, j) / rowsum);
6455 out.alpha[0] = Tone();
6463 static LnCdf aph_eval_cdf(
const mam::AphPair<T>& law) {
6467 const double var = m2 - m1 * m1;
6468 const double sigma = var > 0.0 ? std::sqrt(var) : 0.0;
6469 const double tmax = m1 + 10.0 * sigma;
6470 const std::size_t P = 500;
6473 for (std::size_t k = 0; k < P; ++k) {
6474 const double t = tmax *
static_cast<double>(k) /
static_cast<double>(P - 1);
6476 Matrix<T> St(law.S.rows(), law.S.cols(), Tzero());
6477 for (std::size_t i = 0; i < law.S.rows(); ++i)
6478 for (std::size_t j = 0; j < law.S.cols(); ++j)
6479 St(i, j) = T(law.S(i, j) * num_traits<T>::from_double(t));
6482 for (std::size_t i = 0; i < E.rows(); ++i)
6483 for (std::size_t j = 0; j < E.cols(); ++j) surv += law.alpha[i] * E(i, j);
6484 out.cdf[k] = 1.0 - dbl(surv);
6503 std::vector<T> join_excess()
const {
6504 std::vector<T> excess(lqn.nidx + 1, Tzero());
6505 std::vector<std::size_t> joined;
6506 for (std::size_t tail = 1; tail <= lqn.nidx; ++tail) {
6507 if (lqn.actpretype[tail] != PrecedenceType::PRE_AND)
continue;
6508 for (std::size_t sx : lqn.graph.succ(tail))
6509 if (sx > lqn.ashift && sx <= lqn.ashift + lqn.nacts) joined.push_back(sx);
6511 std::sort(joined.begin(), joined.end());
6512 joined.erase(std::unique(joined.begin(), joined.end()), joined.end());
6513 if (joined.empty())
return excess;
6515 fj::LqnBranchView<T> view;
6516 view.graph =
Matrix<T>(lqn.nidx, lqn.nidx, Tzero());
6517 for (std::size_t i = 1; i <= lqn.nidx; ++i)
6518 for (
const auto& e : lqn.graph.row[i]) view.graph(i - 1, e.first - 1) = e.second;
6519 view.ashift = lqn.ashift;
6520 view.nacts = lqn.nacts;
6521 view.actposttype.assign(lqn.nidx, 0);
6522 for (std::size_t i = 1; i <= lqn.nidx; ++i)
6523 view.actposttype[i - 1] =
static_cast<int>(lqn.actposttype[i]);
6525 for (std::size_t aidx : joined) {
6526 const std::vector<std::vector<std::size_t>> branches =
6528 if (branches.empty())
continue;
6529 std::vector<T> means;
6530 for (
const auto& br : branches) {
6532 for (std::size_t a : br) {
6536 if (a < lqn.callsof.size())
6537 for (std::size_t cidx : lqn.callsof[a])
6538 if (cidx < lqn.calltype.size() && lqn.calltype[cidx] == CallType::SYNC
6539 && cidx < callresidt.size())
6540 s += callresidt[cidx];
6544 if (means.size() == 1)
continue;
6545 std::size_t quorum = means.size();
6546 if (lqn.actquorum[aidx] >= 1 && lqn.actquorum[aidx] <= means.size())
6547 quorum = lqn.actquorum[aidx];
6548 std::vector<T> vars;
6549 for (
const T& m2 : means) vars.push_back(T(m2 * m2));
6551 for (
const T& m2 : means) serial += m2;
6552 if constexpr (num_traits<T>::has_transcendental) {
6554 excess[aidx] = T(q.m - serial);
6557 "SolverLN: the AND-join completion time is a k-th order statistic fitted "
6558 "through a three-point distribution, which needs a square root; use the "
6559 "double or real backend for a model with an AND join");
6566 void resolve_entry_service() {
6567 const std::size_t dim = lqn.nidx + lqn.ncalls;
6568 std::vector<T> x(dim + 1, Tzero());
6569 for (std::size_t i = 1; i <= lqn.nidx; ++i) x[i] = residt[i];
6570 for (std::size_t c = 1; c <= lqn.ncalls; ++c) x[lqn.nidx + c] = callresidt[c];
6571 std::vector<T> entry_servt(dim + 1, Tzero());
6572 for (std::size_t i = 1; i <= dim; ++i) {
6574 for (std::size_t j = 1; j <= dim; ++j)
6575 if (servtmatrix(i, j) != Tzero()) s += servtmatrix(i, j) * x[j];
6581 const std::vector<T> excess = join_excess();
6582 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
6583 const std::size_t eidx = lqn.eshift + e;
6584 for (std::size_t aidx = 1; aidx <= lqn.nidx; ++aidx) {
6585 if (excess[aidx] == Tzero())
continue;
6586 if (servtmatrix(eidx, aidx) > Tzero())
6587 entry_servt[eidx] = T(entry_servt[eidx] + excess[aidx]);
6589 if (entry_servt[eidx] < Tzero()) entry_servt[eidx] = Tzero();
6599 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
6600 const std::size_t eidx = lqn.eshift + e;
6601 const double c = setup_charge(lqn.parent[eidx]);
6603 entry_servt[eidx] = T(entry_servt[eidx] + num_traits<T>::from_double(c));
6609 std::vector<T> x2(dim + 1, Tzero());
6610 for (std::size_t a = 1; a <= lqn.nacts; ++a)
6611 if (lqn.actphase[a] > 1) x2[lqn.ashift + a] = x[lqn.ashift + a];
6612 for (std::size_t c = 1; c <= lqn.ncalls; ++c) {
6613 const std::size_t src = lqn.callpair_src[c];
6614 if (src > lqn.ashift && src <= lqn.ashift + lqn.nacts && lqn.actphase[src - lqn.ashift] > 1)
6615 x2[lqn.nidx + c] = x[lqn.nidx + c];
6617 entry_servt_ph2.assign(dim + 1, Tzero());
6618 for (std::size_t i = lqn.eshift + 1; i <= dim; ++i) {
6620 for (std::size_t j = 1; j <= dim; ++j)
6621 if (servtmatrix(i, j) != Tzero()) s2 += servtmatrix(i, j) * x2[j];
6622 entry_servt_ph2[i] = s2;
6624 entry_servt_last = entry_servt;
6629 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
6630 const std::size_t eidx = lqn.eshift + e;
6631 const std::size_t tidx = lqn.parent[eidx];
6632 const std::size_t hidx = lqn.parent[tidx];
6633 if (ignore[tidx] || ignore[hidx])
continue;
6634 if (idxhash[hidx] < 0)
continue;
6635 const bool has_sync = lqn.issynccaller.any_col(eidx);
6637 servt[eidx] = entry_servt[eidx];
6638 residt[eidx] = entry_servt[eidx];
6641 const std::size_t hl = std::size_t(idxhash[hidx]);
6642 const qn::Layer<T>& L = ensemble[hl];
6643 const LayerResult<T>& r = results.back()[hl];
6644 T task_tput = Tzero(), entry_tput = Tzero();
6645 for (
const auto& kv : L.attr_tasks)
6646 if (kv.second == tidx) task_tput += r.TN(L.clientIdx - 1, kv.first - 1);
6647 for (
const auto& kv : L.attr_entries)
6648 if (kv.second == eidx) entry_tput += r.TN(L.clientIdx - 1, kv.first - 1);
6650 servt[eidx] = T(entry_servt[eidx] * task_tput / entry_tput);
6651 residt[eidx] = servt[eidx];
6655 entryvisits[eidx] = T(entry_tput / task_tput);
6657 servt[eidx] = entry_servt[eidx];
6658 residt[eidx] = entry_servt[eidx];
6674 bool layer_takes_interlock(std::size_t e)
const {
6675 return opt.layer_solver ==
"mva" && e < ensemble.size() &&
6691 std::vector<std::vector<double>> build_layer_interlock(std::size_t e,
6692 const std::vector<std::size_t>& hts,
6693 const std::vector<T>& prIL,
6694 const std::vector<T>& PrIL)
const {
6695 const qn::Layer<T>& L = ensemble[e];
6696 const std::size_t R = L.nclasses;
6697 std::vector<double> cls_ir(R, 0.0);
6698 std::vector<double> cls_pr(R, 0.0);
6699 auto stamp = [&](std::size_t classIdx, std::size_t tidx) {
6700 if (classIdx < 1 || classIdx > R)
return;
6701 for (std::size_t i = 0; i < hts.size(); ++i)
6702 if (hts[i] == tidx) {
6703 cls_ir[classIdx - 1] = dbl(prIL[i]);
6704 cls_pr[classIdx - 1] = dbl(PrIL[i]);
6707 for (
const auto& a : L.attr_tasks) stamp(a.first, a.second);
6708 for (
const auto& a : L.attr_entries) stamp(a.first, lqn.parent[a.second]);
6709 for (
const auto& a : L.attr_activities) stamp(a.first, lqn.parent[a.second]);
6710 for (
const auto& a : L.attr_calls) stamp(a[0], lqn.parent[a[2]]);
6712 std::vector<std::vector<double>> IL(R, std::vector<double>(R, 0.0));
6714 for (std::size_t r = 0; r < R; ++r) {
6716 for (std::size_t sIl = 0; sIl < R; ++sIl) {
6718 IL[r][sIl] = cls_pr[sIl] * cls_ir[sIl] * cls_ir[r];
6722 return any ? IL : std::vector<std::vector<double>>();
6735 std::pair<T, T> interlock_prob(std::size_t client_tidx, std::size_t server_idx,
6736 bool isProcessorHost)
const {
6737 const std::vector<std::size_t>& common = il_common_entries[server_idx];
6738 const double nsrc = il_num_sources[server_idx];
6739 if (nsrc == 0.0 || common.empty())
return std::make_pair(Tzero(), Tzero());
6740 const std::vector<std::size_t>& allsrc = il_src_all[server_idx];
6741 T sum_flow = Tzero();
6742 T sum_pril = Tzero();
6743 for (std::size_t ce : common) {
6744 const std::size_t srcTask = lqn.parent[ce];
6745 const std::size_t cen = ce - lqn.eshift;
6747 double m_src = lqn.mult[srcTask];
6748 if (!std::isfinite(m_src) || m_src < 1.0) m_src = 1.0;
6749 double m_eff = m_src;
6750 if (isProcessorHost) m_eff = (m_src > 3.0) ? (m_src + m_src) : (m_src * m_src);
6751 for (std::size_t dst : lqn.entriesof[client_tidx]) {
6752 const std::size_t dn = dst - lqn.eshift;
6753 if (dn < 1 || dn > lqn.nentries)
continue;
6754 if (!(il_all(cen, dn) > Tzero()))
continue;
6755 T ce_tput = tput[ce];
6757 ce_tput = tput[lqn.actsof[ce][0]];
6761 if (std::find(allsrc.begin(), allsrc.end(), srcTask) != allsrc.end()) {
6762 const T contrib = T(ce_tput * il_all(cen, dn));
6763 sum_flow += contrib;
6764 sum_pril += T(contrib / num_traits<T>::from_double(m_eff));
6768 T client_tput = tput[client_tidx];
6770 for (std::size_t e : lqn.entriesof[client_tidx]) {
6773 et = tput[lqn.actsof[e][0]];
6774 client_tput = T(client_tput + et);
6778 return std::make_pair(Tzero(), Tzero());
6779 const T flow = sum_flow < client_tput ? sum_flow : client_tput;
6780 T IR = T(flow / client_tput);
6781 if (IR > Tone()) IR = Tone();
6782 if (dbl(IR) < 0.0) IR = Tzero();
6785 if (pr > Tone()) pr = Tone();
6786 if (dbl(pr) < 0.0) pr = Tzero();
6787 return std::make_pair(IR, pr);
6790 void update_populations(
int) {
6791 const std::vector<T> residt_orig = residt;
6792 const std::vector<T> callresidt_orig = callresidt;
6793 bool adjusted =
false;
6795 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
6796 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
6797 const std::size_t dst = lqn.callpair_dst[cidx];
6798 const std::size_t server_tidx = lqn.parent[dst];
6799 std::size_t server = 0;
6800 if (server_tidx <= NT() && !il_common_entries[server_tidx].empty()) {
6801 server = server_tidx;
6802 }
else if (server_tidx > lqn.tshift) {
6803 const std::size_t h = lqn.parent[server_tidx];
6804 if (h >= 1 && h <= NT() && !il_common_entries[h].empty()) server = h;
6806 if (server == 0)
continue;
6807 const std::size_t client_tidx = lqn.parent[lqn.callpair_src[cidx]];
6811 const std::pair<T, T> ilp = interlock_prob(client_tidx, server,
false);
6812 const T prIL = T(ilp.first * ilp.second);
6814 const T S = servt[dst];
6815 const T cm = lqn.callproc_mean[cidx];
6816 if (!(cm > Tzero()) || !(callservt[cidx] > Tzero()))
continue;
6817 const T RN = T(callservt[cidx] / cm);
6818 const T W = RN > S ? T(RN - S) : Tzero();
6820 const T RN_adj = T(S + (Tone() - prIL) * W);
6821 const T scale = T(RN_adj / RN);
6822 callservt[cidx] = T(callservt[cidx] * scale);
6823 callresidt[cidx] = T(callresidt[cidx] * scale);
6824 if (callservt[cidx] > Tzero())
6831 layer_interlock.assign(ensemble.size(), {});
6832 for (std::size_t h = 1; h <= lqn.nhosts; ++h) {
6833 if (il_common_entries[h].empty())
continue;
6834 const std::vector<std::size_t>& hts = lqn.tasksof[h];
6835 std::vector<T> prIL(hts.size(), Tzero()), PrIL(hts.size(), Tzero()),
6836 tutil(hts.size(), Tzero());
6837 for (std::size_t i = 0; i < hts.size(); ++i) {
6839 const std::pair<T, T> ilp = interlock_prob(hts[i], h,
true);
6840 prIL[i] = ilp.first;
6841 PrIL[i] = ilp.second;
6842 for (std::size_t e : lqn.entriesof[hts[i]])
6843 for (std::size_t a : lqn.actsof[e]) tutil[i] += tput[a] * lqn.hostdem[a].mean;
6845 T Utot = Tzero(), Uil = Tzero();
6846 for (std::size_t i = 0; i < hts.size(); ++i) {
6852 const T frac = T(Uil / Utot);
6860 if (idxhash[h] >= 0 && layer_takes_interlock(std::size_t(idxhash[h]))) {
6861 const std::size_t e = std::size_t(idxhash[h]);
6862 layer_interlock[e] = build_layer_interlock(e, hts, prIL, PrIL);
6866 for (std::size_t i = 0; i < hts.size(); ++i) {
6874 const T eff = T(prIL[i] * PrIL[i] * frac);
6875 for (std::size_t e : lqn.entriesof[hts[i]])
6876 for (std::size_t a : lqn.actsof[e]) {
6877 const T D = lqn.hostdem[a].mean;
6879 residt[a] = T(D + (Tone() - eff) * (residt[a] - D));
6886 if (!adjusted)
return;
6887 const std::size_t dim = lqn.nidx + lqn.ncalls;
6888 auto esum = [&](
const std::vector<T>& rs,
const std::vector<T>& cr, std::size_t i) {
6890 for (std::size_t j = 1; j <= dim; ++j) {
6891 if (servtmatrix(i, j) == Tzero())
continue;
6892 s += servtmatrix(i, j) * (j <= lqn.nidx ? rs[j] : cr[j - lqn.nidx]);
6896 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
6897 const std::size_t eidx = lqn.eshift + e;
6898 const T oldv = esum(residt_orig, callresidt_orig, eidx);
6900 const T ratio = T(esum(residt, callresidt, eidx) / oldv);
6909 if (!moment_pass_done) {
6910 servt[eidx] = T(servt[eidx] * ratio);
6913 residt[eidx] = T(residt[eidx] * ratio);
6929 T ref_think_time(std::size_t tidx)
const {
6930 if (tidx >= lqn.isref.size() || !lqn.isref[tidx])
return Tzero();
6931 if (lqn.think[tidx].disabled)
return Tzero();
6932 return lqn.think[tidx].mean;
6937 void update_think_times(
int it) {
6940 if (is_ph_encoding()) {
6941 update_think_times_ph(it);
6945 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
6946 const std::size_t tidx = lqn.tshift + t;
6948 const T ztask = ref_think_time(tidx);
6949 if (idxhash[tidx] < 0) {
6953 const double arvrate = open_arrival_rate_of(tidx);
6956 for (std::size_t c = 1; c <= NT(); ++c) nja = std::max(nja, njobs(tidx, c));
6957 if (!(nja > 0.0)) nja = lqn.maxmult[tidx];
6959 for (std::size_t eidx : lqn.entriesof[tidx])
6960 for (std::size_t aidx : lqn.actsof[eidx])
6961 if (!std::isnan(dbl(residt[aidx]))) hres += residt[aidx];
6963 T za = T(num_traits<T>::from_double(nja / arvrate) - hres - ztask);
6964 if (za < floora) za = floora;
6965 if (relax_omega < 1.0 && it > 1 && !std::isnan(thinkt_prev[tidx])) {
6966 const T om = num_traits<T>::from_double(relax_omega);
6967 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
6968 za = T(om * za + om1 * thinkt_prev_v[tidx]);
6970 tput[tidx] = num_traits<T>::from_double(arvrate);
6972 thinkt_prev[tidx] = dbl(za);
6973 thinkt_prev_v[tidx] = za;
6981 const std::size_t e = std::size_t(idxhash[tidx]);
6982 const qn::Layer<T>& L = ensemble[e];
6983 const LayerResult<T>& r = results.back()[e];
6985 for (std::size_t c = 1; c <= NT(); ++c) nj = std::max(nj, njobs(tidx, c));
6987 const std::size_t ts = station_idx_of(L, tidx);
6988 T tp = Tzero(), ut = Tzero();
6989 for (std::size_t k = 0; k < L.nclasses; ++k) {
6990 tp += r.TN(ts - 1, k);
6991 ut += r.UN(ts - 1, k);
6993 tput[tidx] = T(num_traits<T>::from_double(lqn.repl[tidx]) * tp);
6996 if (lqn.sched[tidx] == SchedStrategy::INF) {
6998 raw = tput[tidx] == Tzero()
7000 : T((num_traits<T>::from_double(nj) - util[tidx]) / tput[tidx] - ztask);
7002 const T om = util[tidx] > Tone() ? T(util[tidx] - Tone()) : T(Tone() - util[tidx]);
7003 raw = tput[tidx] == Tzero()
7005 : T(num_traits<T>::from_double(nj) * om / tput[tidx] - ztask);
7008 thinkt[tidx] = raw < floorv ? floorv : raw;
7019 const double c = setup_charge(tidx);
7021 const T withc = T(thinkt[tidx] + num_traits<T>::from_double(c));
7022 thinkt[tidx] = withc < floorv ? floorv : withc;
7026 if (relax_omega < 1.0 && it > 1 && !std::isnan(thinkt_prev[tidx])) {
7027 const double rawd = dbl(thinkt[tidx]);
7029 thinkt_prev[tidx] = rawd;
7030 thinkt_prev_v[tidx] = thinkt[tidx];
7032 const T om = num_traits<T>::from_double(relax_omega);
7033 const T om1 = num_traits<T>::from_double(1.0 - relax_omega);
7034 thinkt[tidx] = T(om * thinkt[tidx] + om1 * thinkt_prev_v[tidx]);
7036 thinkt_prev[tidx] = dbl(thinkt[tidx]);
7037 thinkt_prev_v[tidx] = thinkt[tidx];
7045 void update_layers(
int it) {
7048 if (is_ph_encoding()) {
7049 update_layers_ph(it);
7052 const bool elevator = (it % 2) == 1;
7053 const std::size_t nt = thinkt_map.size();
7054 for (std::size_t r = 0; r < nt; ++r) {
7055 const UpdRow& row = thinkt_map[elevator ? nt - 1 - r : r];
7056 qn::Layer<T>& L = ensemble[std::size_t(idxhash[row.idx])];
7057 if (row.aidx <= NT() && opt.interlocking &&
7058 L.classes[row.cls - 1].type == JobClassType::CLOSED)
7059 L.classes[row.cls - 1].population = njobs(row.aidx, row.idx);
7060 if (row.node == L.clientIdx) {
7061 if (lqn.type[row.aidx] == LqnElement::TASK) {
7062 if (lqn.sched[row.aidx] != SchedStrategy::REF) {
7063 if (!thinktproc[row.aidx].disabled)
7064 L.set_service(row.node, row.cls, thinktproc[row.aidx]);
7066 L.set_service(row.node, row.cls, servtproc[row.aidx]);
7069 L.set_service(row.node, row.cls, servtproc[row.aidx]);
7072 L.set_service(row.node, row.cls, servtproc[row.aidx]);
7075 const std::size_t nc = call_map.size();
7076 for (std::size_t r = 0; r < nc; ++r) {
7077 const UpdRow& row = call_map[elevator ? nc - 1 - r : r];
7078 qn::Layer<T>& L = ensemble[std::size_t(idxhash[row.idx])];
7079 if (row.node == L.clientIdx)
7080 L.set_service(row.node, row.cls, callservtproc[row.aidx]);
7082 L.set_service(row.node, row.cls, servtproc[lqn.callpair_dst[row.aidx]]);
7092 if (interlockMethod ==
"refpath") update_ref_path_stages(it);
7093 for (
const UpdRow& row : arv_call_map) {
7094 qn::Layer<T>& L = ensemble[std::size_t(idxhash[row.idx])];
7095 const Distrib<T>& d = tputproc[lqn.callpair_src[row.aidx]];
7096 if (!d.disabled) L.set_service(row.node, row.cls, d);
7110 void update_ref_path_stages(
int it) {
7111 if (refpath_map.empty())
return;
7112 const std::size_t nkeys = refpath_keys.size();
7113 std::vector<T> stage(nkeys, Tzero());
7114 for (std::size_t r = 0; r < refpath_map.size(); ++r) {
7115 const RefPathRow& row = refpath_map[r];
7119 term = residt[row.elem];
7124 v = entryvisits[row.fromentry];
7125 term = T(callresidt[row.elem] / v);
7129 term = thread_wait(row.elem);
7132 if (!std::isfinite(dbl(term))) term = Tzero();
7133 stage[refpath_group[r]] = T(stage[refpath_group[r]] + term * num_traits<T>::from_double(row.sign));
7135 const double omega = relax_omega;
7136 for (std::size_t k = 0; k < nkeys; ++k) {
7137 if (stage[k] < Tzero()) stage[k] = Tzero();
7139 if (omega < 1.0 && it > 1 && !std::isnan(refpath_stage_prev[k])) {
7140 T prevS = refpath_stage_prev_v[k];
7143 stage[k] = T(num_traits<T>::from_double(omega) * stage[k] +
7144 num_traits<T>::from_double(1.0 - omega) * prevS);
7146 refpath_stage_prev[k] = dbl(stage[k]);
7147 refpath_stage_prev_v[k] = stage[k];
7148 qn::Layer<T>& L = ensemble[std::size_t(idxhash[refpath_keys[k][0]])];
7163 T thread_wait(std::size_t tidx)
const {
7164 if (tidx < 1 || tidx > lqn.nidx)
return Tzero();
7165 if (lqn.sched[tidx] == SchedStrategy::INF || !std::isfinite(lqn.maxmult[tidx]))
return Tzero();
7166 if (idxhash[tidx] < 0 || results.empty())
return Tzero();
7167 const std::size_t e = std::size_t(idxhash[tidx]);
7168 const qn::Layer<T>& L = ensemble[e];
7169 const LayerResult<T>& r = results.back()[e];
7170 const std::size_t ks = station_idx_of(L, tidx);
7172 for (std::size_t c = 0; c < L.nclasses; ++c) Qtot += r.QN(ks - 1, c);
7173 const T Btot = T(num_traits<T>::from_double(lqn.maxmult[tidx]) * util[tidx]);
7174 const T Xtot = T(tput[tidx] / num_traits<T>::from_double(std::max(1.0, lqn.repl[tidx])));
7176 T d = T(Qtot - Btot);
7195 T region_wait(std::size_t e, std::size_t cls)
const {
7196 const qn::Layer<T>& L = ensemble[e];
7197 if (L.regions.empty() || results.empty())
return Tzero();
7198 const LayerResult<T>& r = results.back()[e];
7199 const std::size_t k = cls - 1;
7200 std::size_t c = L.nchains;
7201 for (std::size_t cc = 0; cc < L.nchains; ++cc)
7202 if (L.chains[cc][k]) c = cc;
7203 if (c == L.nchains)
return Tzero();
7207 std::size_t rs = L.serverIdx;
7208 for (std::size_t i = 0; i < L.regions[0].members.size(); ++i)
7209 if (L.regions[0].members[i]) { rs = i + 1;
break; }
7212 T inside = Tzero(), tput_srv = Tzero();
7213 for (std::size_t j = 0; j < L.nclasses; ++j) {
7214 if (!L.chains[c][j])
continue;
7215 const double p = L.classes[j].population;
7216 if (!std::isfinite(p))
return Tzero();
7218 for (std::size_t i = 0; i < L.nstations; ++i) inside += r.QN(i, j);
7219 tput_srv += r.TN(rs - 1, j);
7221 const T deficit = T(num_traits<T>::from_double(pop) - inside);
7222 if (!(deficit > Tzero()) || !(tput_srv > Tzero()))
return Tzero();
7223 return T(deficit / tput_srv);
7227 void update_routing_probabilities(
int) {
7228 for (std::size_t u = 0; u < unique_route_idx.size(); ++u) {
7231 const std::size_t idx = unique_route_idx[unique_route_idx.size() - 1 - u];
7232 qn::Layer<T>& L = ensemble[std::size_t(idxhash[idx])];
7233 bool updated =
false;
7234 for (
const RouteRow& r : route_map) {
7235 if (r.idx != idx)
continue;
7236 if (idxhash[r.tidx_caller] < 0)
continue;
7237 const std::size_t cl = std::size_t(idxhash[r.tidx_caller]);
7238 const qn::Layer<T>& CL = ensemble[cl];
7239 const LayerResult<T>& cr = results.back()[cl];
7242 const std::size_t cs = station_idx_of(CL, r.tidx_caller);
7244 for (std::size_t k = 0; k < CL.nclasses; ++k) Xtot += cr.TN(cs - 1, k);
7245 if (!(Xtot > Tzero()))
continue;
7246 T entry_tput = Tzero();
7247 for (
const auto& a : CL.attr_calls)
7249 entry_tput += cr.TN(station_idx_of(CL, lqn.parent[a[3]]) - 1, a[0] - 1);
7250 L.set_route(r.cfrom, r.cto, r.nodefrom, r.nodeto, T(entry_tput / Xtot));
7253 if (updated) L.refresh_chains();
7272 void require_transient_ready()
const {
7273 if (opt.layer_solver !=
"fluid")
7275 "SolverLN: the layered transient is the transient OF EACH LAYER, which only the "
7276 "fluid layer solver has; rebuild the ensemble with layer_solver 'fluid'");
7280 bool has_finite_horizon()
const {
7281 return std::isfinite(opt.timespan_end) && opt.timespan_end > 0.0;
7286 std::vector<std::vector<std::vector<double>>> Q, U, Tp, R;
7293 void run_layer_transient(std::size_t e,
const std::vector<FluidRateSched>& sched,
7294 LnTranLayer& block, TranTraj& traj) {
7295 qn::Layer<T>& L = ensemble[e];
7297 fluid::FluidOptions fo = opt.layer_fluid;
7298 fo.rate_sched = sched;
7304 if (e < layer_tran_init.size() && !layer_tran_init[e].empty())
7305 fo.init_sol = layer_tran_init[e];
7310 const double t_end =
7312 const std::vector<fluid::FluidTranPoint> pts =
7313 detail::ln_fluid_transient(L, fo, t_end, opt.tran_points, opt.tran_grid);
7314 const std::size_t M = L.nstations, K = L.nclasses, P = pts.size();
7316 auto alloc = [&](std::vector<std::vector<std::vector<double>>>& A) {
7317 A.assign(M, std::vector<std::vector<double>>(K, std::vector<double>(P, 0.0)));
7326 for (std::size_t p = 0; p < P; ++p) {
7327 block.t[p] = pts[p].t;
7328 for (std::size_t i = 0; i < M; ++i)
7329 for (std::size_t r = 0; r < K; ++r) {
7330 const double q = pts[p].QN(i, r), u = pts[p].UN(i, r), x = pts[p].TN(i, r);
7331 block.QN[i][r][p] = q;
7332 block.UN[i][r][p] = u;
7333 block.TN[i][r][p] = x;
7334 traj.Q[i][r][p] = q;
7335 traj.U[i][r][p] = u;
7336 traj.Tp[i][r][p] = x;
7348 LnTranSolution tran_avg_decoupled() {
7349 require_transient_ready();
7350 if (results.empty()) iterate();
7352 out.mode =
"decoupled";
7353 out.layers.resize(ensemble.size());
7354 for (std::size_t e = 0; e < ensemble.size(); ++e) {
7356 run_layer_transient(e, std::vector<FluidRateSched>(), out.layers[e], tj);
7378 LnTranSolution tran_avg_coupled() {
7379 require_transient_ready();
7384 if (!has_finite_horizon())
return tran_avg_decoupled();
7385 if (results.empty()) iterate();
7386 const std::size_t E = ensemble.size();
7389 out.mode =
"coupled";
7390 out.layers.resize(E);
7391 std::vector<TranTraj> traj(E), prev(E);
7392 for (std::size_t e = 0; e < E; ++e)
7393 run_layer_transient(e, std::vector<FluidRateSched>(), out.layers[e], traj[e]);
7394 const std::vector<double> tgrid = out.layers.empty() ? std::vector<double>() : out.
layers[0].t;
7396 for (
long iter = 1; iter <= opt.ln_transient_iter_max; ++iter) {
7398 const std::vector<std::vector<FluidRateSched>> sched =
7399 build_rate_sched(recompute_demand(traj, tgrid), tgrid);
7400 for (std::size_t e = 0; e < E; ++e)
7401 run_layer_transient(e, sched[e], out.layers[e], traj[e]);
7403 for (std::size_t e = 0; e < E; ++e)
7404 for (std::size_t i = 0; i < traj[e].Q.size(); ++i)
7405 for (std::size_t r = 0; r < traj[e].Q[i].size(); ++r)
7406 for (std::size_t p = 0; p < traj[e].Q[i][r].size(); ++p)
7407 gap = std::max(gap, std::fabs(traj[e].Q[i][r][p] - prev[e].Q[i][r][p]));
7408 out.iterations = iter;
7410 if (gap < opt.ln_transient_tol)
break;
7417 std::map<std::size_t, std::vector<double>> thinkt;
7418 std::map<std::size_t, std::vector<double>> callservt;
7429 TranDemand recompute_demand(
const std::vector<TranTraj>& traj,
7430 const std::vector<double>& tgrid)
const {
7432 const std::size_t ng = tgrid.size();
7434 for (std::size_t t = 1; t <= lqn.ntasks; ++t) {
7435 const std::size_t tidx = lqn.tshift + t;
7436 if (idxhash[tidx] < 0 || lqn.isref[tidx])
continue;
7437 const std::size_t e = std::size_t(idxhash[tidx]);
7438 const qn::Layer<T>& L = ensemble[e];
7439 const std::size_t s = station_idx_of(L, tidx) - 1;
7441 for (std::size_t c = 1; c <= NT(); ++c) nj = std::max(nj, njobs(tidx, c));
7443 const double userthink = dbl(ref_think_time(tidx));
7444 std::vector<double> tk(ng, 0.0);
7445 for (std::size_t p = 0; p < ng; ++p) {
7446 double U = 0.0, X = 0.0;
7447 for (std::size_t r = 0; r < L.nclasses; ++r) {
7448 U += traj[e].U[s][r][p];
7449 X += traj[e].Tp[s][r][p];
7452 double v = lqn.sched[tidx] == SchedStrategy::INF
7453 ? (nj - U) / Xs - userthink
7454 : nj * std::fabs(1.0 - U) / Xs - userthink;
7456 tk[p] = v + userthink;
7458 out.thinkt[tidx] = tk;
7461 for (std::size_t cidx = 1; cidx <= lqn.ncalls; ++cidx) {
7462 if (lqn.calltype[cidx] != CallType::SYNC)
continue;
7463 const std::size_t eidx = lqn.callpair_dst[cidx];
7464 const std::size_t tidx = lqn.parent[eidx];
7465 if (tidx > NT() || idxhash[tidx] < 0)
continue;
7466 const std::size_t e = std::size_t(idxhash[tidx]);
7467 const qn::Layer<T>& L = ensemble[e];
7468 const std::size_t s = station_idx_of(L, tidx) - 1;
7469 std::vector<double> Rc(ng, 0.0);
7471 for (std::size_t r = 0; r < L.nclasses; ++r) {
7472 if (L.classes[r].attr_kind !=
int(LqnElement::ENTRY))
continue;
7473 if (L.classes[r].attr_idx != eidx)
continue;
7475 for (std::size_t p = 0; p < ng; ++p) Rc[p] += traj[e].R[s][r][p];
7480 for (std::size_t r = 0; r < L.nclasses; ++r)
7481 for (std::size_t p = 0; p < ng; ++p) Rc[p] += traj[e].R[s][r][p];
7483 const double cm = dbl(lqn.callproc_mean[cidx]);
7484 for (std::size_t p = 0; p < ng; ++p) Rc[p] *= cm;
7485 out.callservt[cidx] = Rc;
7501 std::vector<std::vector<FluidRateSched>> build_rate_sched(
7502 const TranDemand& demand,
const std::vector<double>& tgrid)
const {
7503 std::vector<std::vector<FluidRateSched>> out(ensemble.size());
7504 const bool want_think =
7505 opt.ln_transient_channels ==
"both" || opt.ln_transient_channels ==
"thinkt";
7506 const bool want_call =
7507 opt.ln_transient_channels ==
"both" || opt.ln_transient_channels ==
"callservt";
7509 auto add = [&](std::size_t e, std::size_t station, std::size_t cls,
7510 const std::vector<double>& d) {
7511 if (d.empty())
return;
7512 const double dend = d.back();
7517 const double cap = 20.0;
7522 s.rates.resize(d.size());
7523 for (std::size_t p = 0; p < d.size(); ++p) {
7524 const double v = std::min(std::max(d[p], dend / cap), dend * cap);
7525 s.rates[p] = 1.0 / v;
7527 s.nominal = 1.0 / dend;
7528 out[e].push_back(s);
7532 for (
const UpdRow& row : thinkt_map) {
7533 if (idxhash[row.idx] < 0)
continue;
7534 const std::size_t e = std::size_t(idxhash[row.idx]);
7535 if (row.node != ensemble[e].clientIdx)
continue;
7536 if (lqn.type[row.aidx] != LqnElement::TASK)
continue;
7537 if (lqn.sched[row.aidx] == SchedStrategy::REF)
continue;
7538 const auto it = demand.thinkt.find(row.aidx);
7539 if (it == demand.thinkt.end())
continue;
7540 add(e, row.node, row.cls, it->second);
7543 for (
const UpdRow& row : call_map) {
7544 if (idxhash[row.idx] < 0)
continue;
7545 const std::size_t e = std::size_t(idxhash[row.idx]);
7546 if (row.node != ensemble[e].clientIdx)
continue;
7547 const auto it = demand.callservt.find(row.aidx);
7548 if (it == demand.callservt.end())
continue;
7549 add(e, row.node, row.cls, it->second);
7557 LnSolution<T> aggregate() {
7558 const std::size_t N = lqn.nidx;
7560 auto mk = [&](std::vector<T>& v, std::vector<bool>& d) {
7561 v.assign(N + 1, Tzero());
7562 d.assign(N + 1,
false);
7564 std::vector<T>
QN,
UN, RN, TN, PN, SN, WN, AN;
7565 std::vector<bool> dQ, dU, dR, dT, dP, dS, dW, dA;
7566 mk(QN, dQ); mk(UN, dU); mk(RN, dR); mk(TN, dT);
7567 mk(PN, dP); mk(SN, dS); mk(WN, dW); mk(AN, dA);
7568 std::vector<bool> wn_done(N + 1,
false);
7570 for (std::size_t e = 0; e < ensemble.size(); ++e) {
7571 const qn::Layer<T>& L = ensemble[e];
7572 const LayerResult<T>& r = results.back()[e];
7573 const std::size_t clientIdx = L.clientIdx;
7578 const bool has_host_server = !L.host_stations.empty();
7579 for (std::size_t hs : L.host_stations) {
7580 const std::size_t hidx = L.stations[hs - 1].attr_idx;
7585 for (std::size_t k = 0; k < L.nclasses; ++k) {
7586 if (L.classes[k].completes) {
7587 T t = clientIdx > 0 ? r.TN(clientIdx - 1, k) : Tzero();
7588 const T ts = r.TN(hs - 1, k);
7589 TN[hidx] = T(TN[hidx] + (t > ts ? t : ts));
7591 if (L.classes[k].attr_kind ==
int(LqnElement::ACTIVITY)) {
7593 if (station_idx_of_class(L, k) != hs)
continue;
7594 const std::size_t aidx = L.classes[k].attr_idx;
7595 const std::size_t tidx = lqn.parent[aidx];
7598 PN[aidx] = T(PN[aidx] + r.UN(hs - 1, k));
7599 PN[tidx] = T(PN[tidx] + r.UN(hs - 1, k));
7600 PN[hidx] = T(PN[hidx] + r.UN(hs - 1, k));
7606 for (std::size_t k = 0; k < L.nclasses; ++k) {
7607 const int kind = L.classes[k].attr_kind;
7611 const std::size_t serverIdx = station_idx_of_class(L, k);
7612 if (kind ==
int(LqnElement::TASK)) {
7613 const std::size_t tidx = L.classes[k].attr_idx;
7614 if (has_host_server && !dT[tidx]) {
7616 TN[tidx] = r.TN(clientIdx - 1, k);
7618 }
else if (kind ==
int(LqnElement::ENTRY)) {
7619 const std::size_t eidx = L.classes[k].attr_idx;
7626 if (has_host_server && !dT[eidx]) {
7628 TN[eidx] = r.TN(clientIdx - 1, k);
7630 }
else if (kind ==
int(LqnElement::CALL)) {
7631 const std::size_t cidx = L.classes[k].attr_idx;
7632 const std::size_t aidx = lqn.callpair_src[cidx];
7633 if (lqn.calltype[cidx] == CallType::SYNC) {
7635 SN[aidx] = T(SN[aidx] + r.RN(serverIdx - 1, k) * lqn.callproc_mean[cidx]);
7638 QN[aidx] = T(QN[aidx] + r.QN(serverIdx - 1, k));
7639 }
else if (kind ==
int(LqnElement::ACTIVITY)) {
7640 const std::size_t aidx = L.classes[k].attr_idx;
7641 const std::size_t tidx = lqn.parent[aidx];
7643 QN[tidx] = T(QN[tidx] + r.QN(serverIdx - 1, k));
7646 TN[aidx] = T(TN[aidx] + r.TN(serverIdx - 1, k));
7648 SN[aidx] = T(SN[aidx] + r.RN(serverIdx - 1, k));
7650 RN[aidx] = T(RN[aidx] + r.RN(serverIdx - 1, k));
7653 WN[aidx] = residt[aidx];
7654 if (!wn_done[aidx]) {
7655 WN[tidx] = T(WN[tidx] + residt[aidx]);
7656 wn_done[aidx] =
true;
7658 QN[aidx] = T(QN[aidx] + r.QN(serverIdx - 1, k));
7663 for (std::size_t e = 1; e <= lqn.nentries; ++e) {
7664 const std::size_t eidx = lqn.eshift + e;
7665 const std::size_t tidx = lqn.parent[eidx];
7672 ? T(TN[eidx] * (servt_ph1[eidx] + servt_ph2[eidx]))
7673 : T(TN[eidx] * SN[eidx]);
7676 for (std::size_t a : lqn.actsof[eidx])
7681 if (!lqn.actsof[eidx].empty()) {
7683 PN[eidx] = any ? ps : Tzero();
7685 for (std::size_t a : lqn.actsof[tidx]) {
7687 UN[a] = T(TN[a] * SN[a]);
7689 UN[tidx] = T(UN[tidx] + UN[eidx]);
7701 for (std::size_t i = 1; i <= N; ++i)
7706 const bool host = lqn.type[i] == LqnElement::HOST;
7707 const bool task = lqn.type[i] == LqnElement::TASK;
7708 const bool entry = lqn.type[i] == LqnElement::ENTRY;
7712 dS[i] = !host && !task;
7714 dW[i] = !host && !entry;
7720 s.QN =
UN; s.defined_Q = dU;
7721 s.UN = PN; s.defined_U = dP;
7722 s.RN = SN; s.defined_R = dS;
7723 s.TN = TN; s.defined_T = dT;
7724 s.AN = AN; s.defined_A = dA;
7725 s.WN = WN; s.defined_W = dW;
7726 s.iterations = iterations_done;
7727 s.converged = did_converge;