M4Err ParseCommand(BitStream *bs, ODCommand **com, u32 *com_size) { u32 val, size, sizeHeader; u8 tag; M4Err err; ODCommand *newCom; if (!bs) return M4BadParam; *com_size = 0; //tag tag = BS_ReadInt(bs, 8); sizeHeader = 1; //size size = 0; do { val = BS_ReadInt(bs, 8); sizeHeader++; size <<= 7; size |= val & 0x7F; } while ( val & 0x80 ); *com_size = size; newCom = CreateCom(tag); if (! newCom) { *com = NULL; return M4OutOfMem; } newCom->tag = tag; err = ReadCom(bs, newCom, *com_size); //little trick to handle lazy bitstreams that encode //SizeOfInstance on a fix number of bytes //This nb of bytes is added in Read methods *com_size += sizeHeader - GetSizeFieldSize(*com_size); *com = newCom; if (err) { DelCom(newCom); *com = NULL; } return err; }
int TAGMFast::MLEGradAscent(const double& Thres, const int& MaxIter, const TStr PlotNm, const double StepAlpha, const double StepBeta) { time_t InitTime = time(NULL); TExeTm ExeTm, CheckTm; int iter = 0, PrevIter = 0; TIntFltPrV IterLV; TUNGraph::TNodeI UI; double PrevL = TFlt::Mn, CurL = 0.0; TIntV NIdxV(F.Len(), 0); for (int i = 0; i < F.Len(); i++) { NIdxV.Add(i); } IAssert(NIdxV.Len() == F.Len()); TIntFltH GradV; while(iter < MaxIter) { NIdxV.Shuffle(Rnd); for (int ui = 0; ui < F.Len(); ui++, iter++) { int u = NIdxV[ui]; // //find set of candidate c (we only need to consider c to which a neighbor of u belongs to) UI = G->GetNI(u); TIntSet CIDSet(5 * UI.GetDeg()); for (int e = 0; e < UI.GetDeg(); e++) { if (HOVIDSV[u].IsKey(UI.GetNbrNId(e))) { continue; } TIntFltH& NbhCIDH = F[UI.GetNbrNId(e)]; for (TIntFltH::TIter CI = NbhCIDH.BegI(); CI < NbhCIDH.EndI(); CI++) { CIDSet.AddKey(CI.GetKey()); } } for (TIntFltH::TIter CI = F[u].BegI(); CI < F[u].EndI(); CI++) { //remove the community membership which U does not share with its neighbors if (! CIDSet.IsKey(CI.GetKey())) { DelCom(u, CI.GetKey()); } } if (CIDSet.Empty()) { continue; } GradientForRow(u, GradV, CIDSet); if (Norm2(GradV) < 1e-4) { continue; } double LearnRate = GetStepSizeByLineSearch(u, GradV, GradV, StepAlpha, StepBeta); if (LearnRate == 0.0) { continue; } for (int ci = 0; ci < GradV.Len(); ci++) { int CID = GradV.GetKey(ci); double Change = LearnRate * GradV.GetDat(CID); double NewFuc = GetCom(u, CID) + Change; if (NewFuc <= 0.0) { DelCom(u, CID); } else { AddCom(u, CID, NewFuc); } } if (! PlotNm.Empty() && (iter + 1) % G->GetNodes() == 0) { IterLV.Add(TIntFltPr(iter, Likelihood(false))); } } printf("\r%d iterations (%f) [%lu sec]", iter, CurL, time(NULL) - InitTime); fflush(stdout); if (iter - PrevIter >= 2 * G->GetNodes() && iter > 10000) { PrevIter = iter; CurL = Likelihood(); if (PrevL > TFlt::Mn && ! PlotNm.Empty()) { printf("\r%d iterations, Likelihood: %f, Diff: %f", iter, CurL, CurL - PrevL); } fflush(stdout); if (CurL - PrevL <= Thres * fabs(PrevL)) { break; } else { PrevL = CurL; } } } printf("\n"); printf("MLE for Lambda completed with %d iterations(%s)\n", iter, ExeTm.GetTmStr()); if (! PlotNm.Empty()) { TGnuPlot::PlotValV(IterLV, PlotNm + ".likelihood_Q"); } return iter; }
void CodeGen::WatchPeepHole() { bool optim_done = false; TAsmCommand *a, *b, *temp; for ( int i = 0 ; ( i < 500 ) && !optim_done ; ++i ) { CreateOptPointers(); optim_done = true; while ( !Finished() ) { a = (*buffer)[opt1]; b = (*buffer)[opt2]; Op *aop1 = a->GetOp1(), *aop2 = a->GetOp2(), *bop1 = b->GetOp1(), *bop2 = b->GetOp2(); //push eax, pop eax; pop eax, push eax if ( a->IsPush() && b->IsPop() && EqualOperands( aop1 , bop1 ) ) { optim_done = false; DelCom(); DelCom(); continue; } if ( a->IsPush() && ( b->GetMnem() == ADD ) ) if ( bop1->GetReg() == esp ) { optim_done = false; if ( bop2->GetInt() == 4 ) { DelCom(); DelCom(); } if ( bop2->GetInt() > 4 ) { temp = new TAsmCommand( ADD , reg( esp ) , imm( bop2->GetInt() - 4 ) ); DelCom(); ReplaceCom( temp ); } continue; }; //push x, pop y, x or y is register if ( a->IsPush() && b->IsPop() && ( aop1->IsReg() || bop1->IsReg() ) ) { optim_done = false; temp = new TAsmCommand( MOV , CopyOp( bop1 ) , CopyOp( aop1 ) ); DelCom(); ReplaceCom( temp ); continue; } ///grouping togehter such adds and subs: //add reg, imm; sub reg, imm //add mem, imm; sub mem, imm if ( ( ( a->GetMnem() == ADD ) || ( a->GetMnem() == SUB ) ) && ( ( b->GetMnem() == ADD ) || ( b->GetMnem() == SUB ) ) ) { if ( aop2->IsImm() && bop2->IsImm() ) { if ( ( aop1->IsReg() && bop1->IsReg() && ( aop1->GetReg() == bop1->GetReg() ) ) || ( aop1->IsMem() && bop1->IsMem() && ( aop1->GetMem() == bop1->GetMem() ) ) ) { optim_done = false; int toadd = 0; if ( a->GetMnem() == ADD ) toadd += aop2->GetInt(); else toadd -= aop2->GetInt(); if ( b->GetMnem() == ADD ) toadd += bop2->GetInt(); else toadd -= bop2->GetInt(); if ( toadd < 0 ) temp = new TAsmCommand( SUB , CopyOp( aop1 ) , imm( -toadd ) ); else if ( toadd > 0 ) temp = new TAsmCommand( ADD , CopyOp( aop1 ) , imm( toadd ) ); else DelCom(); DelCom(); if ( toadd ) ReplaceCom( temp ); continue; } } } //add smth, 0 if ( a->GetMnem() == ADD ) if ( aop2->IsImm() ) if ( !aop2->GetInt() ) { DelCom(); continue; } //cmp smth, 0 if ( a->GetMnem() == CMP ) if ( aop2->IsImm() ) if ( !aop2->GetInt() ) { temp = new TAsmCommand( TEST , CopyOp( a->GetOp1() ) , CopyOp( a->GetOp1() ) ); ReplaceCom( temp ); continue; } //mov smth, 0 if ( a->GetMnem() == MOV ) if ( aop2->IsImm() && !aop2->GetInt() ) if ( aop1->IsReg() || aop1->IsMem() ) { temp = new TAsmCommand( XOR , CopyOp( a->GetOp1() ) , CopyOp( a->GetOp1() ) ); ReplaceCom( temp ); continue; } //try to exchange if ( i < 250 ) { if ( ( a->IsPop() && !b->IsPush() && !b->IsPop() ) || ( b->IsPush() && !a->IsPush() && !a->IsPop() ) ) { optim_done = false; //optim_done = false; if ( a->IsPop() ) { if ( AllowedToExchange( a , b ) ) { Exchange(); continue; } } else if ( AllowedToExchange( b , a ) ) { Exchange(); continue; } } } else { if ( ( a->IsPush() && !b->IsPush() && !b->IsPop() ) || ( b->IsPop() && !a->IsPush() && !a->IsPop() ) ) { optim_done = false; //optim_done = false; if ( a->IsPush() ) { if ( AllowedToExchange( a , b ) ) { Exchange(); continue; } } else if ( AllowedToExchange( b , a ) ) { Exchange(); continue; } } } NextOptPointers(); //see if not shorted } } }
/// Newton method: DEPRECATED int TAGMFast::MLENewton(const double& Thres, const int& MaxIter, const TStr PlotNm) { TExeTm ExeTm; int iter = 0, PrevIter = 0; TIntFltPrV IterLV; double PrevL = TFlt::Mn, CurL; TUNGraph::TNodeI UI; TIntV NIdxV; G->GetNIdV(NIdxV); int CID, UID, NewtonIter; double Fuc, PrevFuc, Grad, H; while(iter < MaxIter) { NIdxV.Shuffle(Rnd); for (int ui = 0; ui < F.Len(); ui++, iter++) { if (! PlotNm.Empty() && iter % G->GetNodes() == 0) { IterLV.Add(TIntFltPr(iter, Likelihood(false))); } UID = NIdxV[ui]; //find set of candidate c (we only need to consider c to which a neighbor of u belongs to) TIntSet CIDSet; UI = G->GetNI(UID); if (UI.GetDeg() == 0) { //if the node is isolated, clear its membership and skip if (! F[UID].Empty()) { F[UID].Clr(); } continue; } for (int e = 0; e < UI.GetDeg(); e++) { if (HOVIDSV[UID].IsKey(UI.GetNbrNId(e))) { continue; } TIntFltH& NbhCIDH = F[UI.GetNbrNId(e)]; for (TIntFltH::TIter CI = NbhCIDH.BegI(); CI < NbhCIDH.EndI(); CI++) { CIDSet.AddKey(CI.GetKey()); } } for (TIntFltH::TIter CI = F[UID].BegI(); CI < F[UID].EndI(); CI++) { //remove the community membership which U does not share with its neighbors if (! CIDSet.IsKey(CI.GetKey())) { DelCom(UID, CI.GetKey()); } } if (CIDSet.Empty()) { continue; } for (TIntSet::TIter CI = CIDSet.BegI(); CI < CIDSet.EndI(); CI++) { CID = CI.GetKey(); //optimize for UID, CID //compute constants TFltV AlphaKV(UI.GetDeg()); for (int e = 0; e < UI.GetDeg(); e++) { if (HOVIDSV[UID].IsKey(UI.GetNbrNId(e))) { continue; } AlphaKV[e] = (1 - PNoCom) * exp(- DotProduct(UID, UI.GetNbrNId(e)) + GetCom(UI.GetNbrNId(e), CID) * GetCom(UID, CID)); IAssertR(AlphaKV[e] <= 1.0, TStr::Fmt("AlphaKV=%f, %f, %f", AlphaKV[e].Val, PNoCom.Val, GetCom(UI.GetNbrNId(e), CID))); } Fuc = GetCom(UID, CID); PrevFuc = Fuc; Grad = GradientForOneVar(AlphaKV, UID, CID, Fuc), H = 0.0; if (Grad <= 1e-3 && Grad >= -0.1) { continue; } NewtonIter = 0; while (NewtonIter++ < 10) { Grad = GradientForOneVar(AlphaKV, UID, CID, Fuc), H = 0.0; H = HessianForOneVar(AlphaKV, UID, CID, Fuc); if (Fuc == 0.0 && Grad <= 0.0) { Grad = 0.0; } if (fabs(Grad) < 1e-3) { break; } if (H == 0.0) { Fuc = 0.0; break; } double NewtonStep = - Grad / H; if (NewtonStep < -0.5) { NewtonStep = - 0.5; } Fuc += NewtonStep; if (Fuc < 0.0) { Fuc = 0.0; } } if (Fuc == 0.0) { DelCom(UID, CID); } else { AddCom(UID, CID, Fuc); } } } if (iter - PrevIter >= 2 * G->GetNodes() && iter > 10000) { PrevIter = iter; CurL = Likelihood(); if (PrevL > TFlt::Mn && ! PlotNm.Empty()) { printf("\r%d iterations, Likelihood: %f, Diff: %f", iter, CurL, CurL - PrevL); } fflush(stdout); if (CurL - PrevL <= Thres * fabs(PrevL)) { break; } else { PrevL = CurL; } } } if (! PlotNm.Empty()) { printf("\nMLE for Lambda completed with %d iterations(%s)\n", iter, ExeTm.GetTmStr()); TGnuPlot::PlotValV(IterLV, PlotNm + ".likelihood_Q"); } return iter; }