From 6a85021f0d0b867ee118fcd8923b1042c0b72b97 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 7 Nov 2020 13:18:21 -0800 Subject: [PATCH] make VPSC debugging code visible to the compiler The compiler can still optimize this out, but this way we ensure it always stays compilable and consistent with the surrounding code. --- lib/vpsc/block.cpp | 96 ++++++++++-------- lib/vpsc/blocks.cpp | 66 ++++++------ lib/vpsc/blocks.h | 2 - lib/vpsc/remove_rectangle_overlap.cpp | 31 +++--- lib/vpsc/solve_VPSC.cpp | 139 ++++++++++++++------------ 5 files changed, 179 insertions(+), 155 deletions(-) diff --git a/lib/vpsc/block.cpp b/lib/vpsc/block.cpp index a65f330c8..cb1dfae99 100644 --- a/lib/vpsc/block.cpp +++ b/lib/vpsc/block.cpp @@ -27,6 +27,10 @@ using std::ofstream; using std::endl; using std::vector; +#ifndef RECTANGLE_OVERLAP_LOGGING + #define RECTANGLE_OVERLAP_LOGGING 0 +#endif + typedef vector::iterator Cit; void Block::addVariable(Variable *v) { @@ -84,10 +88,10 @@ void Block::setUpConstraintHeap(PairingHeap* &h,bool in) { } } void Block::merge(Block* b, Constraint* c) { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<" merging on: "<<*c<<",c->left->offset="<left->offset<<",c->right->offset="<right->offset<left->offset="<left->offset<<",c->right->offset="<right->offset<right->offset - c->left->offset - c->gap; Block *l=c->left->block; Block *r=c->right->block; @@ -96,9 +100,10 @@ void Block::merge(Block* b, Constraint* c) { } else { l->merge(r,c,-dist); } -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" merged block="<<(b->deleted?*this:*b)<findMinInConstraint(); in->merge(b->in); -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" merged heap: "<<*in<left->block; Block *rb=v->right->block; // rb may not be this if called between merge and mergeIn -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<" checking constraint ... "<<*v; - f<<" timestamps: left="<timeStamp<<" right="<timeStamp<<" constraint="<timeStamp<timeStamp < lb->timeStamp) { // block at other end of constraint has been moved since this in->deleteMin(); outOfDate.push_back(v); -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" reinserting out of date (reinsert later)"<deleteMin(); -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"deleteMinInConstraint... "<deleteMin(); @@ -354,14 +361,15 @@ void Block::populateSplitBlock(Block *b, Variable *v, Variable *u) { * Returns the split constraint */ Constraint* Block::splitBetween(Variable* vl, Variable* vr, Block* &lb, Block* &rb) { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<" need to split between: "<<*vl<<" and "<<*vr< *order) { dfsVisit(c->right, order); } } -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<" order="<<*v<timeStamp=++blockTimeCtr; r->setUpInConstraints(); Constraint *c=r->findMinInConstraint(); while (c != NULL && c->slack()<0) { -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<"mergeLeft on constraint: "<<*c<deleteMinInConstraint(); Block *l = c->left->block; if (l->in==NULL) l->setUpInConstraints(); @@ -113,24 +118,26 @@ void Blocks::mergeLeft(Block *r) { removeBlock(l); c=r->findMinInConstraint(); } -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<"merged "<<*r<setUpOutConstraints(); Constraint *c = l->findMinOutConstraint(); while (c != NULL && c->slack()<0) { -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<"mergeRight on constraint: "<<*c<deleteMinOutConstraint(); Block *r = c->right->block; r->setUpOutConstraints(); @@ -144,9 +151,10 @@ void Blocks::mergeRight(Block *l) { removeBlock(r); c=l->findMinOutConstraint(); } -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<"merged "<<*l<deleted=true; @@ -168,11 +176,11 @@ void Blocks::cleanup() { */ void Blocks::split(Block *b, Block *&l, Block *&r, Constraint *c) { b->split(l,r,c); -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"Split left: "<<*l<posn = b->posn; r->wposn = r->posn * r->weight; mergeLeft(l); diff --git a/lib/vpsc/blocks.h b/lib/vpsc/blocks.h index 4e3a428c3..06a29d791 100644 --- a/lib/vpsc/blocks.h +++ b/lib/vpsc/blocks.h @@ -25,9 +25,7 @@ #ifndef SEEN_REMOVEOVERLAP_BLOCKS_H #define SEEN_REMOVEOVERLAP_BLOCKS_H -#ifdef RECTANGLE_OVERLAP_LOGGING #define LOGFILE "cRectangleOverlap.log" -#endif #include #include diff --git a/lib/vpsc/remove_rectangle_overlap.cpp b/lib/vpsc/remove_rectangle_overlap.cpp index 107a91de9..ff94baef7 100644 --- a/lib/vpsc/remove_rectangle_overlap.cpp +++ b/lib/vpsc/remove_rectangle_overlap.cpp @@ -27,6 +27,10 @@ using std::ios; using std::ofstream; using std::endl; +#ifndef RECTANGLE_OVERLAP_LOGGING + #define RECTANGLE_OVERLAP_LOGGING 0 +#endif + #define EXTRA_GAP 0.0001 double Rectangle::xBorder=0; @@ -60,11 +64,10 @@ void removeRectangleOverlap(int n, Rectangle *rs[], double xBorder, double yBord oldX[i]=vs[i]->desiredPosition; } VPSC vpsc_x(n,vs,m,cs); -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"Calling VPSC: Horizontal pass 1"<moveCentreX(vs[i]->position()); @@ -78,11 +81,10 @@ void removeRectangleOverlap(int n, Rectangle *rs[], double xBorder, double yBord Rectangle::setXBorder(Rectangle::xBorder-EXTRA_GAP); m=generateYConstraints(n,rs,vs,cs); VPSC vpsc_y(n,vs,m,cs); -#ifdef RECTANGLE_OVERLAP_LOGGING - f.open(LOGFILE,ios::app); - f<<"Calling VPSC: Vertical pass"<moveCentreY(vs[i]->position()); @@ -96,11 +98,10 @@ void removeRectangleOverlap(int n, Rectangle *rs[], double xBorder, double yBord Rectangle::setYBorder(Rectangle::yBorder-EXTRA_GAP); m=generateXConstraints(n,rs,vs,cs,false); VPSC vpsc_x2(n,vs,m,cs); -#ifdef RECTANGLE_OVERLAP_LOGGING - f.open(LOGFILE,ios::app); - f<<"Calling VPSC: Horizontal pass 2"<moveCentreX(vs[i]->position()); diff --git a/lib/vpsc/solve_VPSC.cpp b/lib/vpsc/solve_VPSC.cpp index 2ea9720ad..7abc9bff8 100644 --- a/lib/vpsc/solve_VPSC.cpp +++ b/lib/vpsc/solve_VPSC.cpp @@ -32,6 +32,10 @@ using std::ostringstream; using std::list; using std::set; +#ifndef RECTANGLE_OVERLAP_LOGGING + #define RECTANGLE_OVERLAP_LOGGING 0 +#endif + IncVPSC::IncVPSC(const unsigned n, Variable *vs[], const unsigned m, Constraint *cs[]) : VPSC(n,vs,m,cs) { inactive.assign(cs,cs+m); @@ -41,10 +45,10 @@ IncVPSC::IncVPSC(const unsigned n, Variable *vs[], const unsigned m, Constraint } VPSC::VPSC(const unsigned n, Variable *vs[], const unsigned m, Constraint *cs[]) : cs(cs), m(m) { bs=new Blocks(n, vs); -#ifdef RECTANGLE_OVERLAP_LOGGING - printBlocks(); - assert(!constraintGraphIsCyclic(n,vs)); -#endif + if (RECTANGLE_OVERLAP_LOGGING) { + printBlocks(); + assert(!constraintGraphIsCyclic(n,vs)); + } } VPSC::~VPSC() { delete bs; @@ -52,16 +56,16 @@ VPSC::~VPSC() { // useful in debugging void VPSC::printBlocks() { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - for(set::iterator i=bs->begin();i!=bs->end();i++) { - Block *b=*i; - f<<" "<<*b<::iterator i=bs->begin();i!=bs->end();i++) { + Block *b=*i; + f<<" "<<*b<cleanup(); for(unsigned i=0;islack()<-0.0000001) { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"Error: Unsatisfied constraint: "<<*cs[i]<slack()>-0.0000001); throw "Unsatisfied constraint"; } @@ -112,10 +116,10 @@ void VPSC::refine() { Block *b=*i; Constraint *c=b->findMinLM(); if(c!=NULL && c->lm<0) { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"Split on constraint: "<<*c<split(b,l,r,c); @@ -145,19 +149,20 @@ void VPSC::solve() { } void IncVPSC::solve() { -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"solve_inc()..."<cost(); do { lastcost=cost; satisfy(); splitBlocks(); cost = bs->cost(); -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" cost="<insert(lb); } } -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" finished merges."<cleanup(); for(unsigned i=0;i::const_iterator i(bs->begin());i!=bs->end();i++) { Block *b = *i; b->wposn = b->desiredWeightedPosition(); b->posn = b->wposn / b->weight; } -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" moved blocks."<findMinLM(); if(v!=NULL && v->lm < -0.0000001) { -#ifdef RECTANGLE_OVERLAP_LOGGING - f<<" found split point: "<<*v<<" lm="<lm<cleanup(); } @@ -271,10 +279,10 @@ void IncVPSC::splitBlocks() { */ double IncVPSC::mostViolated(ConstraintList &l, Constraint* &v) { double minSlack = DBL_MAX; -#ifdef RECTANGLE_OVERLAP_LOGGING - ofstream f(LOGFILE,ios::app); - f<<"Looking for most violated..."<