Skip to content

Commit

Permalink
Determinism by redefining std::less<Tree> instead of passing a compar…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
orlarey committed Jan 18, 2025
1 parent 35ea4ed commit f3cab45
Show file tree
Hide file tree
Showing 29 changed files with 430 additions and 408 deletions.
19 changes: 8 additions & 11 deletions compiler/DirectedGraph/DirectedGraph.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
// to represent the time dependency between computations.
//===========================================================

template <typename N, typename Comparator = std::less<N>>
template <typename N>
class digraph {
using TWeights = std::set<int>;
using TDestinations = std::map<N, TWeights, Comparator>;
using TDestinations = std::map<N, TWeights>;

static inline const TWeights gEmptyWeights;

Expand All @@ -38,8 +38,8 @@ class digraph {
// have integer values attached.
class internalgraph {
private:
std::set<N, Comparator> fNodes; // {n1,n2,...}
std::map<N, TDestinations, Comparator> fConnections; // {(ni -{d1,d2,...}-> nj),...}
std::set<N> fNodes; // {n1,n2,...}
std::map<N, TDestinations> fConnections; // {(ni -{d1,d2,...}-> nj),...}

public:
#if 0
Expand Down Expand Up @@ -70,13 +70,10 @@ class digraph {
//----------------------------------------------------------------------

// returns the set of nodes of the graph
[[nodiscard]] const std::set<N, Comparator>& nodes() const { return fNodes; }
[[nodiscard]] const std::set<N>& nodes() const { return fNodes; }

// returns the set of nodes of the graph
[[nodiscard]] const std::map<N, TDestinations, Comparator>& connections() const
{
return fConnections;
}
[[nodiscard]] const std::map<N, TDestinations>& connections() const { return fConnections; }

// Returns the destinations of node n in the graph
[[nodiscard]] const TDestinations& destinations(const N& n) const
Expand Down Expand Up @@ -208,10 +205,10 @@ class digraph {
//--------------------------------------------------------------------------

// returns the set of nodes of the graph
[[nodiscard]] const std::set<N, Comparator>& nodes() const { return fContent->nodes(); }
[[nodiscard]] const std::set<N>& nodes() const { return fContent->nodes(); }

// returns the set of nodes of the graph
[[nodiscard]] const std::map<N, TDestinations, Comparator>& connections() const
[[nodiscard]] const std::map<N, TDestinations>& connections() const
{
return fContent->connections();
}
Expand Down
202 changes: 99 additions & 103 deletions compiler/DirectedGraph/DirectedGraphAlgorythm.hh

Large diffs are not rendered by default.

71 changes: 35 additions & 36 deletions compiler/DirectedGraph/Schedule.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
*
* @tparam N
*/
template <typename N, typename Comparator = std::less<N>>
template <typename N>
class schedule {
private:
std::vector<N> fElements; // ordered set of elements
std::map<N, int, Comparator>
fOrder; // order of each element (starting at 1, 0 indicates not in schedule)
std::vector<N> fElements; // ordered set of elements
std::map<N, int> fOrder; // order of each element (starting at 1, 0 indicates not in schedule)

public:
// number of elements in the schedule
Expand Down Expand Up @@ -66,7 +65,7 @@ class schedule {
}

// append all the elements of a schedule
schedule& append(const schedule<N, Comparator>& S)
schedule& append(const schedule<N>& S)
{
for (const N& n : S.elements()) {
append(n);
Expand All @@ -77,7 +76,7 @@ class schedule {
// A schedule in reverse order
schedule reverse() const
{
schedule<N, Comparator> S;
schedule<N> S;
for (auto it = fElements.rbegin(); it != fElements.rend(); ++it) {
S.append(*it);
}
Expand All @@ -93,8 +92,8 @@ class schedule {
* @param S the schedule
* @return std::ostream& the output stream
*/
template <typename N, typename Comparator = std::less<N>>
inline std::ostream& operator<<(std::ostream& file, const schedule<N, Comparator>& S)
template <typename N>
inline std::ostream& operator<<(std::ostream& file, const schedule<N>& S)
{
std::string sep = "";

Expand All @@ -113,11 +112,11 @@ inline std::ostream& operator<<(std::ostream& file, const schedule<N, Comparator
* @param G the graph we want to schedule
* @return schedule<N> the deep first schedule of G
*/
template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> dfschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> dfschedule(const digraph<N>& G)
{
schedule<N, Comparator> S;
std::set<N, Comparator> V; // set of visited nodes
schedule<N> S;
std::set<N> V; // set of visited nodes

// recursive deep first visit (pseudo local function using a lambda)
std::function<void(const N&)> dfvisit = [&](const N& n) {
Expand Down Expand Up @@ -145,11 +144,11 @@ inline schedule<N, Comparator> dfschedule(const digraph<N, Comparator>& G)
* @return schedule<N> the breadth first schedule of G
*/

template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> bfschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> bfschedule(const digraph<N>& G)
{
std::vector<std::vector<N>> P = parallelize(G);
schedule<N, Comparator> S;
schedule<N> S;

for (uint64_t i = 0; i < P.size(); i++) {
for (const N& n : P[i]) {
Expand All @@ -166,11 +165,11 @@ inline schedule<N, Comparator> bfschedule(const digraph<N, Comparator>& G)
* @param G
* @return schedule<N>
*/
template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> spschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> spschedule(const digraph<N>& G)
{
std::set<N> V; // already scheduled nodes
schedule<N, Comparator> S; // the final schedule
std::set<N> V; // already scheduled nodes
schedule<N> S; // the final schedule

std::list<N> L = recschedule(G); // schedule list with duplicated
for (auto it = L.rbegin(); it != L.rend(); ++it) {
Expand All @@ -193,8 +192,8 @@ inline schedule<N, Comparator> spschedule(const digraph<N, Comparator>& G)
* @param S
* @return int
*/
template <typename N, typename Comparator = std::less<N>>
inline int schedulingcost(const digraph<N, Comparator>& G, const schedule<N, Comparator>& S)
template <typename N>
inline unsigned int schedulingcost(const digraph<N>& G, const schedule<N>& S)
{
unsigned int cost = 0;
for (const N& n : G.nodes()) {
Expand All @@ -215,13 +214,13 @@ inline int schedulingcost(const digraph<N, Comparator>& G, const schedule<N, Com
* @param G the graph we want to schedule
* @return schedule<N> the deep first schedule of G
*/
template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> dfcyclesschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> dfcyclesschedule(const digraph<N>& G)
{
digraph<digraph<N, Comparator>> H = graph2dag(G);
schedule<digraph<N, Comparator>> SH = dfschedule(H);
schedule<N, Comparator> S;
for (const digraph<N, Comparator>& n : SH.elements()) {
digraph<digraph<N>> H = graph2dag(G);
schedule<digraph<N>> SH = dfschedule(H);
schedule<N> S;
for (const digraph<N>& n : SH.elements()) {
S.append(dfschedule(cut(n, 1)));
}
return S;
Expand All @@ -234,13 +233,13 @@ inline schedule<N, Comparator> dfcyclesschedule(const digraph<N, Comparator>& G)
* @param G the graph we want to schedule
* @return schedule<N> the deep first schedule of G
*/
template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> bfcyclesschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> bfcyclesschedule(const digraph<N>& G)
{
digraph<digraph<N, Comparator>> H = graph2dag(G);
schedule<digraph<N, Comparator>> SH = bfschedule(H);
schedule<N, Comparator> S;
for (const digraph<N, Comparator>& n : SH.elements()) {
digraph<digraph<N>> H = graph2dag(G);
schedule<digraph<N>> SH = bfschedule(H);
schedule<N> S;
for (const digraph<N>& n : SH.elements()) {
S.append(dfschedule(cut(n, 1)));
}
return S;
Expand All @@ -253,11 +252,11 @@ inline schedule<N, Comparator> bfcyclesschedule(const digraph<N, Comparator>& G)
* @param G
* @return schedule<N>
*/
template <typename N, typename Comparator = std::less<N>>
inline schedule<N, Comparator> rbschedule(const digraph<N, Comparator>& G)
template <typename N>
inline schedule<N> rbschedule(const digraph<N>& G)
{
std::vector<std::vector<N>> P = parallelize(reverse(G));
schedule<N, Comparator> S;
schedule<N> S;

for (uint64_t i = 0; i < P.size(); i++) {
for (const N& n : P[i]) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/documentator/doc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ static void printLatexHeader(istream& latexheader, const string& faustversion, o
static void printDocMetadata(const Tree expr, ostream& docout)
{
if (gGlobal->gMetaDataSet.count(expr)) {
string sep = "";
set<Tree, CTreeComparator> mset = gGlobal->gMetaDataSet[expr];
string sep = "";
set<Tree> mset = gGlobal->gMetaDataSet[expr];
for (set<Tree>::iterator j = mset.begin(); j != mset.end(); j++) {
docout << sep << unquote(tree2str(*j));
sep = ", ";
Expand Down
6 changes: 3 additions & 3 deletions compiler/draw/sigToGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

using namespace std;

static void recdraw(Tree sig, set<Tree, CTreeComparator>& drawn, ostream& fout);
static void recdraw(Tree sig, set<Tree>& drawn, ostream& fout);
static string nodeattr(Type t);
static string edgeattr(Type t);
static string sigLabel(Tree sig);
Expand All @@ -48,7 +48,7 @@ static string sigLabel(Tree sig);
*/
void sigToGraph(Tree L, ostream& fout)
{
set<Tree, CTreeComparator> alreadyDrawn;
set<Tree> alreadyDrawn;

fout << "strict digraph loopgraph {\n"
<< " rankdir=BT; node [fontsize=10];" << endl;
Expand All @@ -70,7 +70,7 @@ void sigToGraph(Tree L, ostream& fout)
/**
* Draw recursively a signal
*/
static void recdraw(Tree sig, set<Tree, CTreeComparator>& drawn, ostream& fout)
static void recdraw(Tree sig, set<Tree>& drawn, ostream& fout)
{
// cerr << ++gGlobal->TABBER << "ENTER REC DRAW OF " << sig << "$" << *sig << endl;
tvec subsig, coefs;
Expand Down
7 changes: 3 additions & 4 deletions compiler/generator/code_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ bool CodeContainer::getLoopProperty(Tree sig, CodeLoop*& l)
return fLoopProperty.get(sig, l);
}

void CodeContainer::listAllLoopProperties(Tree sig, set<CodeLoop*>& L,
set<Tree, CTreeComparator>& visited)
void CodeContainer::listAllLoopProperties(Tree sig, set<CodeLoop*>& L, set<Tree>& visited)
{
if (visited.count(sig) == 0) {
visited.insert(sig);
Expand Down Expand Up @@ -179,8 +178,8 @@ void CodeContainer::closeLoop(Tree sig)
faustassert(fCurLoop);

// fix the missing dependencies
set<CodeLoop*> L;
set<Tree, CTreeComparator> V;
set<CodeLoop*> L;
set<Tree> V;
listAllLoopProperties(sig, L, V);
for (CodeLoop* l : L) {
fCurLoop->fBackwardLoopDependencies.insert(l);
Expand Down
8 changes: 4 additions & 4 deletions compiler/generator/code_container.hh
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class CodeContainer : public virtual Garbageable {
void printHeader(std::ostream& dst)
{
// defines the metadata we want to print as comments at the begin of in the file
std::set<Tree, CTreeComparator> selectedKeys;
std::set<Tree> selectedKeys;
selectedKeys.insert(tree("name"));
selectedKeys.insert(tree("author"));
selectedKeys.insert(tree("copyright"));
Expand Down Expand Up @@ -280,9 +280,9 @@ class CodeContainer : public virtual Garbageable {

void setLoopProperty(Tree sig, CodeLoop* l); ///< Store the loop used to compute a signal
bool getLoopProperty(Tree sig, CodeLoop*& l); ///< Returns the loop used to compute a signal
void listAllLoopProperties(Tree sig, std::set<CodeLoop*>&,
std::set<Tree, CTreeComparator>&
visited); ///< Returns all the loop used to compute a signal
void listAllLoopProperties(
Tree sig, std::set<CodeLoop*>&,
std::set<Tree>& visited); ///< Returns all the loop used to compute a signal

void printGraphDotFormat(std::ostream& fout);

Expand Down
5 changes: 2 additions & 3 deletions compiler/generator/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static string wdel(const string& s)
void Compiler::generateMetaData()
{
// Add global metadata
for (map<Tree, set<Tree, CTreeComparator>>::iterator i = gGlobal->gMetaDataSet.begin();
for (map<Tree, set<Tree>>::iterator i = gGlobal->gMetaDataSet.begin();
i != gGlobal->gMetaDataSet.end(); i++) {
if (i->first != tree("author")) {
stringstream str1, str2;
Expand All @@ -139,8 +139,7 @@ void Compiler::generateMetaData()
string res2 = unquote(str2.str());
fJSON.declare(res1.c_str(), res2.c_str());
} else {
for (set<Tree, CTreeComparator>::iterator j = i->second.begin(); j != i->second.end();
j++) {
for (set<Tree>::iterator j = i->second.begin(); j != i->second.end(); j++) {
if (j == i->second.begin()) {
stringstream str1, str2;
str1 << *(i->first);
Expand Down
11 changes: 5 additions & 6 deletions compiler/generator/compile_scal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void ScalarCompiler::conditionStatistics(Tree l)

void ScalarCompiler::conditionStatistics(Tree l)
{
map<Tree, int, CTreeComparator>
map<Tree, int>
fConditionStatistics; // used with the new X,Y:enable --> sigEnable(X*Y,Y>0) primitive
for (const auto& p : fConditionProperty) {
for (Tree lc = p.second; !isNil(lc); lc = tl(lc)) {
Expand Down Expand Up @@ -535,7 +535,7 @@ void ScalarCompiler::compileMultiSignal(Tree L)
std::cerr << "Print siglist inst graph topology : " << topology(G) << '\n';
}
// Force a specific scheduling (i.e. compilation order)
schedule<Tree, CTreeComparator> S;
schedule<Tree> S;
switch (gGlobal->gSchedulingStrategy) {
case 0:
S = dfschedule(G);
Expand Down Expand Up @@ -1739,10 +1739,9 @@ string ScalarCompiler::declareRetriveIotaName(Tree clock)

std::string newiotaname = getFreshID("IOTA");
fIotaProperty.set(clock, newiotaname);
fClass->addDeclCode(subst("int \t$0; // IOTA for clock: $1", newiotaname, T(clock)));
fClass->addInitCode(subst("$0 = 0; // init IOTA for clock: $1", newiotaname, T(clock)));
fClass->addPostCode(
Statement("", subst("++$0; // inc IOTA for clock: $1", newiotaname, T(clock))));
fClass->addDeclCode(subst("int \t$0;", newiotaname));
fClass->addInitCode(subst("$0 = 0;", newiotaname));
fClass->addPostCode(Statement("", subst("++$0;", newiotaname)));

return newiotaname;
}
Expand Down
14 changes: 7 additions & 7 deletions compiler/generator/compile_scal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class ScalarCompiler : public Compiler {
property<std::pair<std::string, std::string> >
fInstanceInitProperty; // property added to solve 20101208 kjetil bug

std::map<Tree, Tree, CTreeComparator>
std::map<Tree, Tree>
fConditionProperty; // used with the new X,Y:enable --> sigControl(X*Y,Y>0) primitive

static std::map<std::string, int> fIDCounters;
Tree fSharingKey;
OccMarkup* fOccMarkup;
int fMaxIota;
std::map<std::string, std::string> fIotaCache;
std::map<Tree, int, CTreeComparator> fScheduleOrder;
static std::map<std::string, int> fIDCounters;
Tree fSharingKey;
OccMarkup* fOccMarkup;
int fMaxIota;
std::map<std::string, std::string> fIotaCache;
std::map<Tree, int> fScheduleOrder;

public:
ScalarCompiler(const std::string& name, const std::string& super, int numInputs, int numOutputs)
Expand Down
2 changes: 1 addition & 1 deletion compiler/generator/instructions_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void InstructionsCompiler::conditionStatistics(Tree l)

void InstructionsCompiler::conditionStatistics(Tree l)
{
map<Tree, int, CTreeComparator>
map<Tree, int>
fConditionStatistics; // used with the new X,Y:enable --> sigEnable(X*Y,Y != 0) primitive
for (const auto& p : fConditionProperty) {
for (Tree lc = p.second; !isNil(lc); lc = tl(lc)) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/generator/instructions_compiler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class InstructionsCompiler : public virtual Garbageable {
property<std::pair<std::string, std::string>> fInstanceInitProperty;
property<std::string> fTableProperty;

std::map<Tree, Tree, CTreeComparator>
std::map<Tree, Tree>
fConditionProperty; // used with the new X,Y:enable --> sigControl(X*Y,Y>0) primitive

Tree fSharingKey;
Expand Down
Loading

0 comments on commit f3cab45

Please sign in to comment.