Skip to content

Commit

Permalink
Finished new topsort version
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Apr 8, 2024
1 parent 1a9d2ec commit 7ee0ef9
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 24 deletions.
6 changes: 3 additions & 3 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ auto match_scc_ts_visitor_ = Util::Overload {
LIB::BasePWMap scc_res = scc.calculate();
LIB::BaseTopSort ts(buildSortFromSCC(scc, scc_res), d);
LIB::BasePWMap ts_res = ts.calculate();
buildJson(match_res, scc.transformResult(scc_res));
buildJson(match_res, scc_res, ts_res);
return MapBaseType(ts_res);
},
[](LIB::CanonSBG a, Util::MD_NAT b, Util::MD_NAT c, bool d) {
Expand All @@ -340,7 +340,7 @@ auto match_scc_ts_visitor_ = Util::Overload {
LIB::CanonPWMap scc_res = scc.calculate();
LIB::CanonTopSort ts(buildSortFromSCC(scc, scc_res), d);
LIB::CanonPWMap ts_res = ts.calculate();
buildJson(match_res, scc.transformResult(scc_res));
buildJson(match_res, scc_res, ts_res);
return MapBaseType(ts_res);
},
[](auto a, auto b, auto c, auto d) {
Expand Down Expand Up @@ -702,7 +702,7 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const


case Eval::Func::ts:
if (eval_args.size() == 2) {
if (eval_args.size() == 1) {
arity_ok = true;

SBGBaseType g = std::visit(EvalGraph{}, eval_args[0]);
Expand Down
25 changes: 16 additions & 9 deletions sbg/sbg_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,14 @@ void SBGTopSort<Set>::updateStatus()
set_unordered(unordered().difference(dom()));
set_not_dependant(unordered().difference(mapB().image()));

if (debug()) {
Util::SBG_LOG << "smap: " << smap() << "\n";
Util::SBG_LOG << "mapB: " << mapB() << "\n";
Util::SBG_LOG << "mapD: " << mapD() << "\n";
Util::SBG_LOG << "end: " << end() << "\n";
Util::SBG_LOG << "new_end: " << new_end() << "\n\n";
}

return;
}

Expand Down Expand Up @@ -1086,7 +1094,7 @@ template CanonDSBG buildSortFromSCC(const CanonSCC &scc, const CanonPWMap &rmap)

template<typename Set>
void buildJson(
const Set &matching, std::set<Set> scc
const Set &matching, const PWMap<Set> &scc, const PWMap<Set> &order
)
{
// 1. Parse a JSON string into DOM.
Expand All @@ -1102,14 +1110,13 @@ void buildJson(

rapidjson::Value &s = d["scc"];
std::stringstream ss2;
for (const Set &s : scc)
ss2 << s;
ss2 << scc;
s.SetString(ss2.str().c_str(), strlen(ss2.str().c_str()), d.GetAllocator());

//rapidjson::Value &o = d["order"];
//std::stringstream ss3;
//ss3 << order;
//o.SetString(ss3.str().c_str(), strlen(ss3.str().c_str()), d.GetAllocator());
rapidjson::Value &o = d["order"];
std::stringstream ss3;
ss3 << order;
o.SetString(ss3.str().c_str(), strlen(ss3.str().c_str()), d.GetAllocator());

// 3. Stringify the DOM
FILE *fp = fopen("output.json", "w");
Expand All @@ -1125,11 +1132,11 @@ void buildJson(

template void buildJson
(
const UnordSet &match, std::set<UnordSet> scc
const UnordSet &match, const BasePWMap &scc, const BasePWMap &order
);
template void buildJson
(
const OrdSet &match, std::set<OrdSet> scc
const OrdSet &match, const CanonPWMap &scc, const CanonPWMap &order
);

} // namespace LIB
Expand Down
8 changes: 4 additions & 4 deletions sbg/sbg_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#include <iostream>
#include <unordered_set>

#include <rapidjson/document.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/prettywriter.h>
#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/prettywriter.h"
#include "sbg/sbg.hpp"
#include "util/logger.hpp"

Expand Down Expand Up @@ -278,7 +278,7 @@ DSBGraph<Set> buildSortFromSCC(const SBGSCC<Set> &scc, const PWMap<Set> &rmap);

template<typename Set>
void buildJson(
const Set &matching, std::set<Set> scc
const Set &matching, const PWMap<Set> &scc, const PWMap<Set> &order
);

} // namespace LIB
Expand Down
10 changes: 5 additions & 5 deletions test/topsort1.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Topological sort test, with a simple recursion
// Topological sort test, with a simple recursion, and a self recursion on the right

N = 10000

Expand All @@ -10,24 +10,24 @@ E1 = 1
E2 = N-1+E1
E3 = N-1+E2

off1d = N
off3b = N-1

off1d = N
off2d = N-1
off3d = 1

V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3-1]} -> 0*x+3
, {[V3:1:V3]} -> 0*x+4>>;
, {[V3:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+0, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x-off3b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+0, {[E2+1:1:E3]} -> 1*x+off3d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2>>;

sort(
V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3-1]} -> 0*x+3
, {[V3:1:V3]} -> 0*x+4>>;
, {[V3:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+0, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x-off3b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+0, {[E2+1:1:E3]} -> 1*x+off3d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2>>;
, <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3]} -> 0*x+3>>
)
7 changes: 4 additions & 3 deletions test/topsort2.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ off3d = 1
off4d = r(V3, 1)-E4

V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3]} -> 0*x+3>>;
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3-1]} -> 0*x+3
, {[V3:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+0, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x-off3b
, {[E3+1:1:E4]} -> 1*x+off4b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+0, {[E2+1:1:E3]} -> 1*x+off3d
Expand All @@ -29,11 +30,11 @@ Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2, {[E3+1:1:E4]

sort(
V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3]} -> 0*x+3>>;
Vmap %= <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3-1]} -> 0*x+3
, {[V3:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+0, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x-off3b
, {[E3+1:1:E4]} -> 1*x+off4b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+0, {[E2+1:1:E3]} -> 1*x+off3d
, {[E3+1:1:E4]} -> 1*x+off4d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2, {[E3+1:1:E4]} -> 0*x+3>>;
, <<{[1:1:V1]} -> 0*x+1, {[V1+1:1:V2]} -> 0*x+2, {[V2+1:1:V3]} -> 0*x+3>>
)
61 changes: 61 additions & 0 deletions test/topsort3.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Topological sort test corresponding to the following model:

/*
model test
constant Integer N=10;
Real a[N], b[N-1], c[N-1];
equation
for i in 1:N-1 loop
a[i] = a[i+1] - (b[i] + c[i]);
b[i] = k1*a[i+1];
c[i] = k2*a[i+1];
end for;
a[N] = 1000;
end test;
*/

N = 10

V1 = N
V2 = N-1+V1
V3 = N-1+V2

E1 = N-1
E2 = N-1+E1
E3 = N-1+E2
E4 = N-1+E3
E5 = N-1+E4

off1b = r(V1, 1)-r(E1, 1)-1
off2b = r(V1, 1)-r(E2, 1)-1
off3b = r(V1, 1)-r(E3, 1)-1
off4b = r(V2, 1)-r(E4, 1)
off5b = r(V3, 1)-r(E5, 1)

off1d = r(V1, 1)-r(E1, 1)
off2d = r(V2, 1)-r(E2, 1)
off3d = r(V3, 1)-r(E3, 1)
off4d = r(V1, 1)-r(E4, 1)
off5d = r(V1, 1)-r(E5, 1)

V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1-1]} -> 0*x+1, {[V1:1:V1]} -> 0*x+2, {[V1+1:1:V2]} -> 0*x+3
, {[V2+1:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} -> 1*x+off3b
, {[E3+1:1:E4]} -> 1*x+off4b, {[E4+1:1:E5]} -> 1*x+off5b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x+off3d
, {[E3+1:1:E4]} -> 1*x+off4d, {[E4+1:1:E5]} -> 1*x+off5d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2]} -> 0*x+2, {[E2+1:1:E3]} -> 0*x+3
, {[E3+1:1:E4]} -> 0*x+4, {[E4+1:1:E5]} -> 0*x+5>>;

sort(
V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1-1]} -> 0*x+1, {[V1:1:V1]} -> 0*x+2, {[V1+1:1:V2]} -> 0*x+3
, {[V2+1:1:V3]} -> 0*x+4>>;
mapB %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} -> 1*x+off3b
, {[E3+1:1:E4]} -> 1*x+off4b, {[E4+1:1:E5]} -> 1*x+off5b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x+off3d
, {[E3+1:1:E4]} -> 1*x+off4d, {[E4+1:1:E5]} -> 1*x+off5d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2]} -> 0*x+2, {[E2+1:1:E3]} -> 0*x+3
, {[E3+1:1:E4]} -> 0*x+4, {[E4+1:1:E5]} -> 0*x+5>>;
)
52 changes: 52 additions & 0 deletions test/topsort4.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Topological sort test, with two "merged" simple recursions

N = 10

V1 = N
V2 = N+V1
V3 = N+V2

E1 = 1
E2 = N-1+E1
E3 = N-1+E2
E4 = 1+E3
E5 = N-1+E4
E6 = N-1+E5

off1b = r(V2, 1)-r(E1, 1)-N+1
off2b = r(V1, 1)-r(E2, 1)-1
off3b = r(V2, 1)-r(E3, 1)
off4b = r(V3, 1)-r(E4, 1)-N+1
off5b = r(V2, 1)-r(E5, 1)-1
off6b = r(V3, 1)-r(E6, 1)

off1d = r(V1, 1)-r(E1, 1)-N+1
off2d = r(V2, 1)-r(E2, 1)
off3d = r(V1, 1)-r(E3, 1)
off4d = r(V2, 1)-r(E4, 1)-N+1
off5d = r(V3, 1)-r(E5, 1)
off6d = r(V2, 1)-r(E6, 1)

V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1-1]} -> 0*x+1, {[V1:1:V1]} -> 0*x+2, {[V1+1:1:V1+1]} -> 0*x+3
, {[V1+2:1:V2-1]} -> 0*x+4, {[V2:1:V2]} -> 0*x+5, {[V2+1:1:V2+1]} -> 0*x+6
, {[V2+2:1:V3]} -> 0*x+7>>;
mapB %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} -> 1*x+off3b
, {[E3+1:1:E4]} -> 1*x+off4b, {[E4+1:1:E5]} -> 1*x+off5b, {[E5+1:1:E6]} -> 1*x+off6b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x+off3d
, {[E3+1:1:E4]} -> 1*x+off4d, {[E4+1:1:E5]} -> 1*x+off5d, {[E5+1:1:E6]} -> 1*x+off6d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2
, {[E3+1:1:E4]} -> 0*x+3, {[E4+1:1:E5], [E5+1:1:E6]} -> 0*x+4>>;

sort(
V %= {[1:1:V1], [V1+1:1:V2], [V2+1:1:V3]};
Vmap %= <<{[1:1:V1-1]} -> 0*x+1, {[V1:1:V1]} -> 0*x+2, {[V1+1:1:V1+1]} -> 0*x+3
, {[V1+2:1:V2-1]} -> 0*x+4, {[V2:1:V2]} -> 0*x+5, {[V2+1:1:V2+1]} -> 0*x+6
, {[V2+2:1:V3]} -> 0*x+7>>;
mapB %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} -> 1*x+off3b
, {[E3+1:1:E4]} -> 1*x+off4b, {[E4+1:1:E5]} -> 1*x+off5b, {[E5+1:1:E6]} -> 1*x+off6b>>;
mapD %= <<{[1:1:E1]} -> 1*x+off1d, {[E1+1:1:E2]} -> 1*x+off2d, {[E2+1:1:E3]} -> 1*x+off3d
, {[E3+1:1:E4]} -> 1*x+off4d, {[E4+1:1:E5]} -> 1*x+off5d, {[E5+1:1:E6]} -> 1*x+off6d>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2], [E2+1:1:E3]} -> 0*x+2
, {[E3+1:1:E4]} -> 0*x+3, {[E4+1:1:E5], [E5+1:1:E6]} -> 0*x+4>>;
)

0 comments on commit 7ee0ef9

Please sign in to comment.