324 const std::size_t K =
sn.nclasses;
325 const std::size_t I =
sn.nodes.size();
326 const std::vector<std::vector<double>> Vn = fj_detail::node_visit_sum(
sn);
328 const double inf = std::numeric_limits<double>::infinity();
340 for (std::size_t i = 0; i < I; ++i)
341 if (V.
nodes[i].nodetype == NodeType::Fork) V.
nodes[i].stateful =
true;
343 for (std::size_t i = 0; i < I; ++i)
349 std::size_t f = 0, j = 0, r = 0, w = 1;
351 std::vector<std::size_t> wlink;
352 std::vector<std::size_t> branchheads;
353 std::vector<std::vector<std::size_t>> branchsets;
354 std::vector<std::vector<std::size_t>> auxmatrix;
356 std::size_t quorum = 0;
358 std::vector<ForkInfo> info;
360 for (std::size_t f = 1; f <= I; ++f) {
361 if (
sn.nodes[f - 1].nodetype != NodeType::Fork)
continue;
363 for (std::size_t p = 0; p <
sn.fj.size(); ++p)
364 if (
sn.fj[p].first == f) j =
sn.fj[p].second;
365 const std::size_t w =
static_cast<std::size_t
>(
sn.nodes[f - 1].tasks_per_link);
367 for (std::size_t r = 1; r <= K; ++r) {
368 if (!(Vn[f - 1][r - 1] > 0))
continue;
373 fi.w = w == 0 ? 1 : w;
376 for (std::size_t nd = 1; nd <= I; ++nd)
378 (nd - 1) * K + (r - 1))) > 0)
379 fi.branchheads.push_back(nd);
380 const std::size_t B = fi.branchheads.size();
383 "fj_tag: the Fork node '" +
sn.nodes[f - 1].name +
384 "' has a single output link for class '" +
sn.classes[r - 1].name +
385 "'. A degenerate fork is not a fork -- remove it, or give it a second branch");
394 std::vector<std::size_t> wv(B, 0);
396 for (std::size_t b = 0; b < B; ++b) {
399 wv[b] =
static_cast<std::size_t
>(lv + 0.5);
400 if (wv[b] == 0) wv[b] = 1;
401 if (wv[b] != wv[0]) uniform =
false;
413 fi.branchsets.assign(B, std::vector<std::size_t>());
414 for (std::size_t b = 0; b < B; ++b) {
415 std::vector<std::size_t> visitset(1, fi.branchheads[b]);
416 std::vector<std::size_t> frontier(1, fi.branchheads[b]);
417 while (!frontier.empty()) {
418 const std::size_t cn = frontier.front();
419 frontier.erase(frontier.begin());
420 if (
sn.nodes[cn - 1].nodetype == NodeType::Fork)
422 "fj_tag: nested fork-join (the Fork node '" +
sn.nodes[cn - 1].name +
423 "' sits on a branch of '" +
sn.nodes[f - 1].name +
424 "') is not supported: a sibling would need a tag from each enclosing "
425 "fork and this transform mints one tag dimension");
426 if (
sn.nodes[cn - 1].nodetype == NodeType::Join && cn != j)
428 "fj_tag: overlapping fork-join pairs (the Join node '" +
429 sn.nodes[cn - 1].name +
"' sits on a branch of '" +
430 sn.nodes[f - 1].name +
"', which closes at '" +
sn.nodes[j - 1].name +
431 "') are not supported");
432 for (std::size_t nd = 1; nd <= I; ++nd)
433 for (std::size_t s = 1; s <= K; ++s) {
435 sn.rtnodes((cn - 1) * K + (r - 1), (nd - 1) * K + (s - 1))) >
440 "fj_tag: class switching between the Fork node '" +
441 sn.nodes[f - 1].name +
"' and its Join is not supported: the "
442 "sibling classes are minted per ORIGINAL class, so a sibling "
443 "that switches has no auxiliary twin to switch into");
444 if (nd == j)
continue;
445 if (std::find(visitset.begin(), visitset.end(), nd) == visitset.end()) {
446 visitset.push_back(nd);
447 frontier.push_back(nd);
455 std::vector<std::size_t> canreach(1, j);
459 for (std::size_t a = 0; a < visitset.size(); ++a) {
460 const std::size_t cn = visitset[a];
461 if (std::find(canreach.begin(), canreach.end(), cn) != canreach.end())
463 for (std::size_t b2 = 0; b2 < canreach.size(); ++b2)
465 sn.rtnodes((cn - 1) * K + (r - 1),
466 (canreach[b2] - 1) * K + (r - 1))) > 0) {
467 canreach.push_back(cn);
473 for (std::size_t a = 0; a < visitset.size(); ++a)
474 if (std::find(canreach.begin(), canreach.end(), visitset[a]) == canreach.end())
476 "fj_tag: the Join node '" +
sn.nodes[j - 1].name +
477 "' is unreachable from the branch node '" +
478 sn.nodes[visitset[a] - 1].name +
479 "'; a sibling trapped there never joins and its parent never completes");
480 fi.branchsets[b] = visitset;
485 for (std::size_t cc = 0; cc <
sn.chains.size(); ++cc)
486 if (
sn.chains[cc][r - 1]) { c = cc + 1;
break; }
489 tot =
sn.njobs()[r - 1];
491 for (std::size_t s = 0; s < K; ++s)
492 if (
sn.chains[c - 1][s]) tot +=
sn.njobs()[s];
495 std::size_t emitted = 0;
496 for (std::size_t b = 0; b < B; ++b) emitted += fi.wlink.empty() ? fi.w : fi.wlink[b];
498 fi.open = !std::isfinite(tot);
499 fi.quorum = kq < emitted ? kq : 0;
504 }
else if (fi.quorum > 0) {
507 "A quorum (PARTIAL) join lets the stragglers of a fork outlive their parent, so "
508 "the native CTMC needs a cutoff to bound the sibling groups in progress.");
509 Tt = std::max(
static_cast<std::size_t
>(tot + 0.5), tag_pool);
511 Tt =
static_cast<std::size_t
>(tot + 0.5);
515 std::size_t aux_ref =
sn.classes[r - 1].refstat;
518 for (std::size_t b = 0; b < B && !found; ++b)
519 for (std::size_t x = 0; x < fi.branchsets[b].size() && !found; ++x) {
520 const std::size_t cn = fi.branchsets[b][x];
521 if (
sn.nodes[cn - 1].station != 0 &&
sn.nodes[cn - 1].nodetype != NodeType::Join) {
522 aux_ref =
sn.nodes[cn - 1].station;
528 throw InputError(
"fj_tag: the chain of class '" +
sn.classes[r - 1].name +
529 "' routed through '" +
sn.nodes[f - 1].name +
530 "' carries no jobs, so no fork firing can ever occur");
532 fi.auxmatrix.assign(B, std::vector<std::size_t>(Tt, 0));
533 for (std::size_t t = 1; t <= Tt; ++t)
534 for (std::size_t b = 0; b < B; ++b) {
536 ac.
name =
sn.classes[r - 1].name +
"_f" + std::to_string(f) +
"_b" +
537 std::to_string(b + 1) +
"_t" + std::to_string(t);
538 ac.
type = JobClassType::CLOSED;
544 fi.auxmatrix[b][t - 1] = a;
548 for (std::size_t x = 0; x < fi.branchsets[b].size(); ++x) {
549 const std::size_t cn = fi.branchsets[b][x];
550 const std::size_t ist =
sn.nodes[cn - 1].station;
551 if (ist == 0 ||
sn.nodes[cn - 1].nodetype == NodeType::Join)
continue;
559 typename std::map<std::size_t, SetupDelayOffParam<T>>::iterator sdp =
563 if (r - 1 < P.setup.size() && !P.setup[r - 1].disabled) {
566 P.setup[a - 1] = law;
568 if (r - 1 < P.delayoff.size() && !P.delayoff[r - 1].disabled) {
570 if (P.delayoff.size() < a)
572 P.delayoff[a - 1] = law;
575 std::vector<std::size_t>& par = V.
stations[ist - 1].server_parallelism;
576 if (r - 1 < par.size() && par[r - 1] > 1) {
577 const std::size_t deg = par[r - 1];
578 if (par.size() < a) par.resize(a, 1);
586 for (std::size_t x = 0; x < fi.branchsets[b].size(); ++x) {
587 const std::size_t cn = fi.branchsets[b][x];
588 for (std::size_t nd = 1; nd <= I; ++nd) {
589 const T p =
sn.get_route(r, r, cn, nd);
598 const std::size_t Kaug = V.
classes.size();
604 for (std::size_t row = 0; row < info.size(); ++row) {
605 const ForkInfo& fi = info[row];
606 for (std::size_t b = 0; b < fi.auxmatrix.size(); ++b)
607 for (std::size_t t = 0; t < fi.auxmatrix[b].size(); ++t) {
608 const std::size_t a = fi.auxmatrix[b][t];
621 for (std::size_t i = 0; i < V.
stations.size(); ++i) {
632 for (std::size_t row = 0; row < info.size(); ++row) {
633 const ForkInfo& fi = info[row];
634 for (std::size_t b = 0; b < fi.auxmatrix.size(); ++b)
635 for (std::size_t t = 0; t < fi.auxmatrix[b].size(); ++t) {
636 const std::size_t a = fi.auxmatrix[b][t];
637 for (std::size_t x = 0; x < fi.branchsets[b].size(); ++x) {
638 const std::size_t cn = fi.branchsets[b][x];
639 if (
sn.nodes[cn - 1].station == 0)
continue;
646 for (std::size_t i = 0; i < I; ++i) {
648 if (nd.
routing.empty())
continue;
649 if (nd.
routing.size() < Kaug) nd.
routing.resize(Kaug, RoutingStrategy::PROB);
650 for (std::size_t row = 0; row < info.size(); ++row) {
651 const ForkInfo& fi = info[row];
652 for (std::size_t b = 0; b < fi.auxmatrix.size(); ++b)
653 for (std::size_t t = 0; t < fi.auxmatrix[b].size(); ++t)
654 nd.
routing[fi.auxmatrix[b][t] - 1] =
655 sn.nodes[i].routing.size() >= fi.r ?
sn.nodes[i].routing[fi.r - 1]
656 : RoutingStrategy::PROB;
663 for (
typename std::map<std::size_t,
Matrix<T>>::iterator it = V.
csmatrix.begin();
667 for (std::size_t x = 0; x < old.
rows() && x < Kaug; ++x)
668 for (std::size_t y = 0; y < old.
cols() && y < Kaug; ++y) C(x, y) = old(x, y);
688 for (std::size_t r = 1; r <= K; ++r) {
689 std::size_t corig = 0, cnew = 0;
690 for (std::size_t cc = 0; cc <
sn.chains.size(); ++cc)
691 if (
sn.chains[cc][r - 1]) { corig = cc + 1;
break; }
692 for (std::size_t cc = 0; cc < V.
chains.size(); ++cc)
693 if (V.
chains[cc][r - 1]) { cnew = cc + 1;
break; }
694 if (corig == 0 || cnew == 0)
continue;
695 for (std::size_t isf = 1; isf <= V.
stateful_nodes.size(); ++isf) {
697 const std::size_t isf_old =
sn.stateful_index(ind);
699 V.
visits[cnew - 1](isf - 1, r - 1) =
sn.visits[corig - 1](isf_old - 1, r - 1);
703 V.
visits[cnew - 1](isf - 1, r - 1) =
707 for (std::size_t i = 0; i < I; ++i)
708 V.
nodevisits[cnew - 1](i, r - 1) =
sn.nodevisits[corig - 1](i, r - 1);
710 for (std::size_t row = 0; row < info.size(); ++row) {
711 const ForkInfo& fi = info[row];
712 std::size_t cnew = 0;
713 for (std::size_t cc = 0; cc < V.
chains.size(); ++cc)
714 if (V.
chains[cc][fi.r - 1]) { cnew = cc + 1;
break; }
715 if (cnew == 0)
continue;
716 for (std::size_t b = 0; b < fi.auxmatrix.size(); ++b) {
717 std::vector<std::size_t> support = fi.branchsets[b];
718 support.push_back(fi.j);
723 const std::size_t wb = fi.wlink.empty() ? fi.w : fi.wlink[b];
724 for (std::size_t t = 0; t < fi.auxmatrix[b].size(); ++t) {
725 const std::size_t a = fi.auxmatrix[b][t];
726 for (std::size_t c2 = 0; c2 < V.
nchains; ++c2) {
727 for (std::size_t x = 0; x < V.
visits[c2].rows(); ++x)
728 V.
visits[c2](x, a - 1) = zero;
729 for (std::size_t x = 0; x < V.
nodevisits[c2].rows(); ++x)
732 for (std::size_t i = 0; i < V.
stations.size(); ++i)
734 for (std::size_t x = 0; x < support.size(); ++x) {
735 const std::size_t cn = support[x];
742 if (V.
nodes[cn - 1].station != 0)
744 static_cast<double>(wb);
756 std::vector<std::size_t> reqb(fi.auxmatrix.size(), fi.w);
757 if (!fi.wlink.empty())
758 for (std::size_t b = 0; b < reqb.size() && b < fi.wlink.size(); ++b)
759 reqb[b] = fi.wlink[b];
769 jp.
quorum[fi.r] = fi.quorum;
771 jp.
debtwidth += fi.auxmatrix.empty() ? 0 : fi.auxmatrix[0].size();
778 for (std::size_t b = 0; b < fi.branchsets.size(); ++b)
779 for (std::size_t x = 0; x < fi.branchsets[b].size(); ++x) {
780 const std::size_t cn = fi.branchsets[b][x];
781 if (
sn.nodes[cn - 1].station != 0 &&
sn.nodes[cn - 1].nodetype != NodeType::Join)
782 V.
classcap[
sn.nodes[cn - 1].station - 1][fi.r - 1] = 0.0;
787 for (std::size_t i = 0; i < V.
stations.size() && i < V.
cap.size(); ++i) {
788 if (std::isfinite(V.
cap[i]))
continue;
791 for (std::size_t a = 0; a < V.
classcap[i].size(); ++a) {
792 if (!std::isfinite(V.
classcap[i][a])) allfin =
false;
795 if (allfin) V.
cap[i] = rowsum;
799 for (std::size_t row = 0; row < info.size(); ++row) {
800 const ForkInfo& fi = info[row];
801 const std::size_t Tt = fi.auxmatrix.empty() ? 0 : fi.auxmatrix[0].size();
802 for (std::size_t t = 1; t <= Tt; ++t) {
809 for (std::size_t b = 0; b < fi.auxmatrix.size(); ++b)
810 e.
auxclasses.push_back(fi.auxmatrix[b][t - 1]);