Skip to content

Commit 9321d45

Browse files
committedAug 28, 2024
New rbschedule and improved cost function
1 parent 8982a2d commit 9321d45

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed
 

‎DirectedGraph/Schedule.hh

+34-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
******************************************************************************/
1313

1414
#pragma once
15+
#include <algorithm> // for std::find
1516
#include <cassert>
1617
#include <functional>
1718
#include <iostream>
1819
#include <list>
1920
#include <map>
21+
#include <set>
2022
#include <sstream>
2123
#include <string>
2224
#include <vector>
@@ -70,6 +72,16 @@ class schedule {
7072
}
7173
return *this;
7274
}
75+
76+
// A schedule in reverse order
77+
schedule reverse() const
78+
{
79+
schedule<N> S;
80+
for (auto it = fElements.rbegin(); it != fElements.rend(); ++it) {
81+
S.append(*it);
82+
}
83+
return S;
84+
}
7385
};
7486

7587
/**
@@ -189,7 +201,7 @@ inline int schedulingcost(const digraph<N>& G, const schedule<N>& S)
189201
for (const auto& c : G.destinations(n)) {
190202
int t0 = S.order(c.first);
191203
// assert(t1 > t0);
192-
cost += std::abs(t1 - t0); // We may have loops
204+
cost += (t1 - t0) * (t1 - t0); // We may have loops
193205
}
194206
}
195207
return cost;
@@ -232,3 +244,24 @@ inline schedule<N> bfcyclesschedule(const digraph<N>& G)
232244
}
233245
return S;
234246
}
247+
248+
/**
249+
* @brief reverse breadth first schedule for a DAG
250+
*
251+
* @tparam N
252+
* @param G
253+
* @return schedule<N>
254+
*/
255+
template <typename N>
256+
inline schedule<N> rbschedule(const digraph<N>& G)
257+
{
258+
std::vector<std::vector<N>> P = parallelize(reverse(G));
259+
schedule<N> S;
260+
261+
for (uint64_t i = 0; i < P.size(); i++) {
262+
for (const N& n : P[i]) {
263+
S.append(n);
264+
}
265+
}
266+
return S.reverse();
267+
}

‎tests.cpp

+28-30
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ std::string res13()
673673
"G-set{0}->H, H}\n"
674674
"roots(h) : std::vector{A, E}\n"
675675
"leafs(h) : std::vector{D, H}\n"
676-
"DFSchedule {1:D, 2:C, 3:H, 4:G, 5:B, 6:A, 7:F, 8:E}, cost: 11\n"
677-
"BFSchedule {1:D, 2:H, 3:C, 4:G, 5:B, 6:F, 7:A, 8:E}, cost: 13\n";
676+
"DFSchedule {1:D, 2:C, 3:H, 4:G, 5:B, 6:A, 7:F, 8:E}, cost: 23\n"
677+
"BFSchedule {1:D, 2:H, 3:C, 4:G, 5:B, 6:F, 7:A, 8:E}, cost: 25\n";
678678
}
679679

680680
bool check13()
@@ -710,7 +710,7 @@ std::string res14()
710710
return "graph h : Graph {A-set{0}->B, B-set{0}->C, B-set{1}->G, C-set{1}->B, C-set{0}->D, D, "
711711
"E-set{0}->F, "
712712
"F-set{0}->G, G-set{2}->F, G-set{0}->H, H}\n"
713-
"Schedule {1:D, 2:H, 3:G, 4:F, 5:C, 6:B, 7:A, 8:E}, cost: 17\n";
713+
"Schedule {1:D, 2:H, 3:G, 4:F, 5:C, 6:B, 7:A, 8:E}, cost: 47\n";
714714
}
715715

716716
bool check14()
@@ -750,8 +750,8 @@ std::string res15()
750750
return "graph h : Graph {A-set{0}->B, B-set{0}->C, B-set{1}->G, C-set{1}->B, C-set{0}->D, "
751751
"D-set{0}->U, "
752752
"E-set{0}->F, F-set{0}->G, G-set{2}->F, G-set{0}->H, H, U-set{1}->D}\n"
753-
"deep-first : Schedule {1:U, 2:D, 3:H, 4:G, 5:F, 6:C, 7:B, 8:A, 9:E}, cost: 19\n"
754-
"breadth-first : Schedule {1:U, 2:D, 3:H, 4:G, 5:F, 6:C, 7:B, 8:E, 9:A}, cost: 19\n";
753+
"deep-first : Schedule {1:U, 2:D, 3:H, 4:G, 5:F, 6:C, 7:B, 8:A, 9:E}, cost: 49\n"
754+
"breadth-first : Schedule {1:U, 2:D, 3:H, 4:G, 5:F, 6:C, 7:B, 8:E, 9:A}, cost: 45\n";
755755
}
756756

757757
bool check15()
@@ -907,36 +907,34 @@ bool check19()
907907
void test20(std::ostream& ss)
908908
{
909909
digraph<char> g;
910-
g.add('Z', 'Y')
911-
.add('Y', 'X')
912-
.add('Y', 'J')
913-
.add('X', 'W')
914-
.add('W', 'C')
915-
.add('W', 'J')
916-
.add('J', 'I')
917-
.add('C', 'B')
918-
.add('B', 'A')
919-
.add('Z', 'G')
920-
.add('G', 'E')
921-
.add('G', 'F');
910+
g.add('Z', 'H').add('Z', 'X').add('Z', 'Y').add('Y', 'F').add('Y', 'G');
911+
g.add('X', 'V').add('X', 'W').add('W', 'D').add('W', 'E').add('V', 'U').add('V', 'C');
912+
g.add('U', 'A').add('U', 'B');
913+
914+
auto s1 = dfschedule(g);
915+
auto s2 = bfschedule(g);
916+
auto s3 = spschedule(g);
917+
auto s4 = rbschedule(g);
918+
922919
ss << "critical path of g = " << criticalpath(g, 'Z') << '\n';
923-
ss << " deepfirst of g = " << dfschedule(g) << '\n';
924-
ss << "breadth first of g = " << bfschedule(g) << '\n';
925-
ss << "superschedule of g = " << spschedule(g) << '\n';
920+
ss << " deepfirst of g = " << s1 << " cost:" << schedulingcost(g, s1) << '\n';
921+
ss << "breadth first of g = " << s2 << " cost:" << schedulingcost(g, s2) << '\n';
922+
ss << "superschedule of g = " << s3 << " cost:" << schedulingcost(g, s3) << '\n';
923+
ss << "revbfschedule of g = " << s4 << " cost:" << schedulingcost(g, s4) << '\n';
926924
}
927925

928926
std::string res20()
929927
{
930-
return "critical path of g = std::vector{A, B, C, W, X, Y, Z}\n"
931-
" deepfirst of g = Schedule {1:E, 2:F, 3:G, 4:I, 5:J, 6:A, 7:B, 8:C, 9:W, 10:X, "
932-
"11:Y, "
933-
"12:Z}\n"
934-
"breadth first of g = Schedule {1:A, 2:E, 3:F, 4:I, 5:B, 6:G, 7:J, 8:C, 9:W, 10:X, "
935-
"11:Y, "
936-
"12:Z}\n"
937-
"superschedule of g = Schedule {1:A, 2:I, 3:B, 4:J, 5:C, 6:W, 7:X, 8:F, 9:E, 10:Y, "
938-
"11:G, "
939-
"12:Z}\n";
928+
return "critical path of g = std::vector{A, U, V, X, Z}\n"
929+
" deepfirst of g = Schedule {1:H, 2:C, 3:A, 4:B, 5:U, 6:V, 7:D, 8:E, 9:W, 10:X, "
930+
"11:F, "
931+
"12:G, 13:Y, 14:Z} cost:235\n"
932+
"breadth first of g = Schedule {1:A, 2:B, 3:C, 4:D, 5:E, 6:F, 7:G, 8:H, 9:U, 10:W, "
933+
"11:Y, 12:V, 13:X, 14:Z} cost:361\n"
934+
"superschedule of g = Schedule {1:B, 2:A, 3:E, 4:U, 5:D, 6:C, 7:W, 8:G, 9:V, 10:F, "
935+
"11:X, 12:Y, 13:H, 14:Z} cost:121\n"
936+
"revbfschedule of g = Schedule {1:B, 2:A, 3:U, 4:E, 5:D, 6:C, 7:W, 8:V, 9:G, 10:F, "
937+
"11:Y, 12:X, 13:H, 14:Z} cost:107\n";
940938
}
941939

942940
bool check20()

0 commit comments

Comments
 (0)
Please sign in to comment.