Commit 35736103 authored by ehebrard's avatar ehebrard
Browse files

valgrind

parent 29b02a65
......@@ -80,7 +80,7 @@ int main(int argc, char *argv[]) {
}
auto ub{osp::getUb(duration, resource)};
S.setUpperBound(ub);
S.setUpperBound(1850);
// vector<task> scope;
// vector<int> demand;
......@@ -118,33 +118,50 @@ int main(int argc, char *argv[]) {
cout << S << endl;
auto u{S.getVariable(START(1), END(0))};
S.addConstraint(u, 0);
S.updateDistances();
cout << S << endl;
cout << "save\n";
// auto u{S.getVariable(START(1), END(0))};
// S.addConstraint(u, 0);
ReversibleObject::env->save();
S.addPrecedence(END(0), START(1), 0);
cout << S.graph << endl;
S.addPrecedence(END(4), START(5), 0);
// cout << "merge\n";
S.addPrecedence(END(2), START(4), 0);
for (auto i{0}; i < S.numTask(); ++i)
S.graph.merge(START(i), END(i), duration[i]);
cout << S.graph << endl;
S.updateDistances();
cout << S << endl;
cout << "restore to level " << ReversibleObject::env->level() << "\n";
// cout << "save\n";
//
// ReversibleObject::env->save();
//
// cout << S.graph << endl;
ReversibleObject::env->restore(ReversibleObject::env->level());
// // cout << "merge\n";
//
// for (auto i{0}; i < S.numTask(); ++i)
// S.graph.merge(START(i), END(i), duration[i]);
//
// cout << S.graph << endl;
//
// cout << "restore to level " << ReversibleObject::env->level() << "\n";
//
// ReversibleObject::env->restore(ReversibleObject::env->level());
//
// cout << S << endl;
//
// cout << "OK!\n";
cout << S << endl;
// exit(1);
cout << "OK!\n";
// cons[0]->propagate();
// S.updateDistances();
//
// cons[0]->propagate();
for (auto c : cons) {
c->propagate();
S.updateDistances();
cout << S << endl;
}
}
#include "SparseGraph.hpp"
......@@ -4,8 +4,10 @@
#include <vector>
#include "Global.hpp"
#include "SparseSet.hpp"
using namespace std;
namespace schedcl {
......
......@@ -2,6 +2,7 @@
#define _SCHEDCL_GLOBAL_HPP
#include <limits>
#include <sys/resource.h>
using namespace std;
using event = int;
......@@ -89,6 +90,8 @@ public:
}
};
double cpu_time(void);
} // namespace SCHEDCL
#endif // _SCHEDCL_SCHEDULING_HPP
This diff is collapsed.
#ifndef _schedcl_SGRAPH_HPP
#define _schedcl_SGRAPH_HPP
#include <map>
#include <vector>
#include <boost/dynamic_bitset.hpp>
#include "DistanceVariable.hpp"
#include "SparseSet.hpp"
// #define DEBUG_SG true
// #define DEBUG_MERGE true
namespace schedcl {
using namespace std;
template <typename T> struct Arc {
int endPoint;
DistanceVariable<T> *var;
int rank;
T offset{0};
T length() const;
int neighbor() const;
int next(const int x) const;
// bool isPointer() const;
ostream &display(ostream &os) const;
};
template <typename T> T Arc<T>::length() const { return var->get() + offset; }
template <typename T> int Arc<T>::neighbor() const { return endPoint; }
template <typename T> int Arc<T>::next(const int x) const { return endPoint; }
// template <typename T> bool Arc<T>::isPointer() const { return var->endPoint
// != ; }
template <typename T> ostream &Arc<T>::display(ostream &os) const {
os << "(" << neighbor() << ":" << length() << ")@" << rank;
return os;
}
/*
Implementation of a graph
- It supports [adding new Arcs/not really]; reducing Arcs' weights; and
merging two
vertices
- Reversible
*/
template <typename T>
class SparseGraph : public ReversibleObject, public Explainer<T> {
public:
// // nor really needed, should probably remove
// Schedule<T> &sched;
// currently active vertex
SparseSet vertices;
// the adjency matrix
vector<vector<Arc<T>>> neighbor[2];
// vector<vector<Arc<T>>> successor;
size_t arcCount() const;
size_t vertexCount() const;
size_t size() const;
SparseGraph();
void initialise(Schedule<T> &s);
// restore to the last saved state (restore vertex merging, Arc weights
// are
// restored via variables directly)
void undo() override;
// add the Arc defined by the vairable v
void addArc(DistanceVariable<T> *v);
// merges vertices a and b
void merge(const int a, const int b, const T w = 0);
ostream &display(ostream &os) const;
protected:
size_t numArc{0};
// the merge map
vector<int> representant;
// // tail[i] == last vertex mapped to i
// vector<int> tail;
// // next[i] == next vertex mapped to the same int as i (i, if there is
// // none)
// vector<int> next;
// // prev[i] == prev vertex mapped to the same int as i (i, if there is
// // none)
// vector<int> prev;
// The removed vertex is the first one out of vertices. The first int in
// trail
// gives the number of new forward Arcs for its representant x, the
// second
// the number of backward Arcs. They must be last in x's neighbor list
vector<int> trail;
void remove(const bool n, const int x, const int i);
// void removePredecessor(const int x, const int i);
// add
void add(const bool n, const int z, Arc<T> &e, const int i);
// void addPredecessor(const int z, Arc<T> &e, const int i);
// add the opposite of the i-th predecessor of x
// void removeVertex(const int x);
// void unionArcs(const int x, const int y);
void split(const int a);
private:
boost::dynamic_bitset<> buffer;
#ifdef DEBUG_SG
void verify(const char *msg);
#endif
};
// declare a new time point in the graph
template <typename T> SparseGraph<T>::SparseGraph() : ReversibleObject() {}
template <typename T> void SparseGraph<T>::addArc(DistanceVariable<T> *v) {
auto pto{static_cast<int>(neighbor[PREDECESSOR][v->to].size())};
auto pfrom{static_cast<int>(neighbor[SUCCESSOR][v->from].size())};
neighbor[SUCCESSOR][v->from].push_back({v->to, v, pto});
neighbor[PREDECESSOR][v->to].push_back({v->from, v, pfrom});
++numArc;
}
template <typename T> void SparseGraph<T>::initialise(Schedule<T> &sched) {
cout << "INIT GRAPH\n";
auto n{sched.numEvent()};
auto old_n{vertexCount()};
if (n > 0) {
buffer.resize(n, 0);
neighbor[PREDECESSOR].resize(n);
neighbor[SUCCESSOR].resize(n);
// for (auto v : sched.variables) {
for (auto i{numArc}; i < sched.variables.size(); ++i) {
auto v{sched.variables[i]};
assert(v->from >= 0 and v->from < n);
assert(v->to >= 0 and v->to < n);
addArc(v);
}
vertices.reserve(n);
vertices.fill();
representant.resize(n);
// tail.resize(n);
// next.resize(n);
// prev.resize(n);
for (auto i = old_n; i < n; ++i) {
representant[i]
// = tail[i] = next[i] = prev[i]
= i;
}
}
#ifdef DEBUG_SG
cout << *this << endl;
verify("init");
#endif
}
template <typename T> size_t SparseGraph<T>::arcCount() const { return numArc; }
template <typename T> size_t SparseGraph<T>::vertexCount() const {
return vertices.count();
}
template <typename T> size_t SparseGraph<T>::size() const {
return vertices.capacity();
}
// remove the i-th successor of x
template <typename T>
void SparseGraph<T>::remove(const bool n, const int x, const int i) {
#ifdef DEBUG_SG
cout << "remove " << i << "-th successor of " << x << endl;
#endif
assert(representant[x] == x);
// swap with last
if (i < static_cast<int>(neighbor[n][x].size()) - 1) {
// override with the last successor
neighbor[n][x][i] = neighbor[n][x].back();
// update the oposite Arc
int j = neighbor[n][x][i].rank;
neighbor[1 - n][neighbor[n][x][i].neighbor()][j].rank = i;
}
neighbor[n][x].pop_back();
#ifdef DEBUG_SG
cout << *this << endl;
#endif
}
// // remove the i-th successor of x
// template <typename T>
// void SparseGraph<T>::removePredecessor(const int x, const int i) {
//
// #ifdef DEBUG_SG
// cout << "remove " << i << "-th predecessor of " << x << endl;
// #endif
//
// assert(representant[x] == x);
//
// if (i < static_cast<int>(predecessor[x].size()) - 1) {
// // override with the last predecessor
// predecessor[x][i] = predecessor[x].back();
// // update the oposite Arc
// int j = predecessor[x][i].rank;
// successor[predecessor[x][i].from()][j].rank = i;
// }
//
// predecessor[x].pop_back();
//
// #ifdef DEBUG_SG
// cout << *this << endl;
// #endif
// }
//
template <typename T>
void SparseGraph<T>::merge(const int x, const int y, const T d) {
#ifdef DEBUG_SG
cout << "merge " << y << " to " << x << "\n";
#endif
ReversibleObject::save();
trail.push_back(y);
buffer.set(x);
for (auto &e : neighbor[SUCCESSOR][x]) {
// cout << x << " " << e << endl;
auto z{e.neighbor()};
assert(z >= 0);
assert(z < buffer.size());
buffer.set(z);
}
// int count{0};
for (auto &e : neighbor[SUCCESSOR][y]) {
auto z{e.neighbor()};
if (not buffer[z]) {
#ifdef DEBUG_SG
cout << "move " << e << " to " << x << "'s successors\n";
#endif
auto pfrom{static_cast<int>(neighbor[SUCCESSOR][x].size())};
neighbor[SUCCESSOR][x].push_back({z, e.var, e.rank, e.offset + d});
neighbor[PREDECESSOR][z][e.rank] = {x, e.var, pfrom, e.offset + d};
// neighbor[PREDECESSOR][z][e.rank].rank = pfrom;
// neighbor[PREDECESSOR][z][e.rank].endPoint = x;
// predecessor[z].push_back({e.var, pfrom});
// ++count;
} else {
#ifdef DEBUG_SG
cout << "remove " << e << "\n";
#endif
remove(PREDECESSOR, z, e.rank);
--numArc;
}
}
// trail.push_back(count);
buffer.reset();
// cout << buffer.size() << endl;
buffer.set(x);
for (auto &e : neighbor[PREDECESSOR][x]) {
auto z{e.neighbor()};
assert(z >= 0);
assert(z < buffer.size());
buffer.set(z);
}
for (auto &e : neighbor[PREDECESSOR][y]) {
auto z{e.neighbor()};
if (not buffer[z]) {
#ifdef DEBUG_SG
cout << "move " << e << " to " << x << "'s predecessors\n";
#endif
auto pto{static_cast<int>(neighbor[PREDECESSOR][x].size())};
neighbor[PREDECESSOR][x].push_back({z, e.var, e.rank, e.offset - d});
neighbor[SUCCESSOR][z][e.rank] = {x, e.var, pto, e.offset - d};
// neighbor[SUCCESSOR][z][e.rank].rank = pto;
// neighbor[PREDECESSOR][z][e.rank].endPoint = x;
// successor[z].push_back({e.var, pfrom});
} else {
#ifdef DEBUG_SG
cout << "remove " << e << "\n";
#endif
remove(SUCCESSOR, z, e.rank);
--numArc;
}
}
representant[y] = x;
vertices.remove_back(y);
#ifdef DEBUG_SG
cout << *this << endl;
verify("after merge");
#endif
cout << vertices << endl;
}
// add the opposite of the i-th predecessor of x
template <typename T>
void SparseGraph<T>::add(const bool n, const int z, Arc<T> &e, const int i) {
#ifdef DEBUG_SG
if (n == SUCCESSOR)
cout << "add " << e << " to successor[" << z << "], at rank " << i << "\n";
else
cout << "add " << e << " to predecessor[" << z << "], at rank " << i
<< "\n";
#endif
auto f{neighbor[n][z][i]};
neighbor[1 - n][f.neighbor()][f.rank].rank =
static_cast<int>(neighbor[n][z].size());
neighbor[n][z].push_back(f);
neighbor[n][z][i] = e;
#ifdef DEBUG_SG
cout << *this << endl;
#endif
}
// restore to the last saved state
template <typename T> void SparseGraph<T>::undo() {
#ifdef DEBUG_SG
cout << "undo\n";
#endif
auto y{trail.back()};
trail.pop_back();
split(y);
}
template <typename T> void SparseGraph<T>::split(const int y) {
#ifdef DEBUG_MERGE
cout << "split " << y << " from " << representant[y] << endl;
#endif
assert(not vertices.has(y));
auto x{representant[y]};
representant[y] = y;
int i{static_cast<int>(neighbor[PREDECESSOR][y].size())};
while (i-- > 0) {
auto e{neighbor[PREDECESSOR][y][i]};
auto z{e.neighbor()};
cout << e;
auto f{neighbor[PREDECESSOR][x].back()};
if (e.var == f.var) {
#ifdef DEBUG_MERGE
cout << " (was moved to " << x << "'s predecessors)\n";
#endif
neighbor[PREDECESSOR][x].pop_back();
neighbor[SUCCESSOR][z][e.rank] = {y, e.var, i, e.offset};
// neighbor[SUCCESSOR][z][e.rank].rank = i;
// neighbor[SUCCESSOR][z][e.rank].endPoint = y;
// --j;
} else {
#ifdef DEBUG_MERGE
cout << " (was already in " << x << "'s predecessors)\n";
#endif
Arc<T> g{y, e.var, i, e.offset};
add(SUCCESSOR, z, g, e.rank);
++numArc;
}
}
i = static_cast<int>(neighbor[SUCCESSOR][y].size());
// j = static_cast<int>(successor[x].size()) - 1;
while (i-- > 0) {
auto e{neighbor[SUCCESSOR][y][i]};
auto z{e.neighbor()};
cout << e;
auto f{neighbor[SUCCESSOR][x].back()};
if (f.var == e.var) {
#ifdef DEBUG_MERGE
cout << " (was moved to " << x << "'s successor)\n";
#endif
neighbor[SUCCESSOR][x].pop_back();
neighbor[PREDECESSOR][z][e.rank] = {y, e.var, i, e.offset};
// neighbor[PREDECESSOR][z][e.rank].rank = i;
// neighbor[PREDECESSOR][z][e.rank].endPoint = y;
// --j;
} else {
#ifdef DEBUG_MERGE
cout << " (was already in " << x << "'s successor)\n";
#endif
Arc<T> g{y, e.var, i, e.offset};
add(PREDECESSOR, z, g, e.rank);
++numArc;
}
}
vertices.add(y);
#ifdef DEBUG_MERGE
cout << *this << endl;
verify("after split");
#endif
vertices.add(y);
}
template <typename T> ostream &SparseGraph<T>::display(ostream &os) const {
// vector<T> dbuffer(vertexCount(), -INFTY);
//
// os << " ";
// for (auto i{0}; i < vertexCount(); ++i)
// // if (vertices.has(i))
// os << " " << setw(4) << i;
// os << endl;
// for (auto i{0}; i < vertexCount(); ++i) {
// // if (vertices.has(i)) {
//
// os << setw(4) << i << ":";
// for (auto &e : successor[i])
// dbuffer[e.to()] = e.length();
// for (auto j{0}; j < vertexCount(); ++j)
// if (dbuffer[j] != -INFTY) {
// if (dbuffer[j] == INFTY) {
// os << " oo";
// } else {
// os << " " << setw(4) << dbuffer[j];
// }
// } else {
// os << " ";
// }
// for (auto &e : successor[i])
// dbuffer[e.to()] = -INFTY;
// os << endl;
// // }
// }
#ifdef DEBUG_SG
for (auto i{0}; i < vertexCount(); ++i) {
os << setw(3) << i << ":";
for (auto ej : neighbor[SUCCESSOR][i]) {
os << " " << ej;