From 259cb965384110feb2280e89c8adfb961c11f9fa Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Thu, 4 Aug 2016 18:49:19 +0100 Subject: [PATCH] stem leak by using mempool --- .../other-builds/moses2/SCFG/TrellisPath.cpp | 41 +++++++++++-------- .../other-builds/moses2/SCFG/TrellisPath.h | 11 ++--- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/contrib/other-builds/moses2/SCFG/TrellisPath.cpp b/contrib/other-builds/moses2/SCFG/TrellisPath.cpp index 0c52114d3..1bd0a5dcb 100644 --- a/contrib/other-builds/moses2/SCFG/TrellisPath.cpp +++ b/contrib/other-builds/moses2/SCFG/TrellisPath.cpp @@ -19,27 +19,30 @@ namespace Moses2 namespace SCFG { -TrellisNode::TrellisNode(const ArcLists &arcLists, const SCFG::Hypothesis &hypo) +TrellisNode::TrellisNode(MemPool &pool, const ArcLists &arcLists, const SCFG::Hypothesis &hypo) :arcList(arcLists.GetArcList(&hypo)) +,ind(0) +,m_prevNodes(pool) { UTIL_THROW_IF2(arcList.size() == 0, "Empty arclist"); - ind = 0; - CreateTail(arcLists, hypo); + CreateTail(pool, arcLists, hypo); } -TrellisNode::TrellisNode(const ArcLists &arcLists, const ArcList &varcList, size_t vind) +TrellisNode::TrellisNode(MemPool &pool, const ArcLists &arcLists, const ArcList &varcList, size_t vind) :arcList(varcList) - ,ind(vind) +,ind(vind) +,m_prevNodes(pool) { UTIL_THROW_IF2(vind >= arcList.size(), "arclist out of bound" << ind << " >= " << arcList.size()); const SCFG::Hypothesis &hypo = arcList[ind]->Cast(); - CreateTail(arcLists, hypo); + CreateTail(pool, arcLists, hypo); } -TrellisNode::TrellisNode(const ArcLists &arcLists, const TrellisNode &orig, const TrellisNode &nodeToChange) +TrellisNode::TrellisNode(MemPool &pool, const ArcLists &arcLists, const TrellisNode &orig, const TrellisNode &nodeToChange) :arcList(orig.arcList) - ,ind(orig.ind) +,ind(orig.ind) +,m_prevNodes(pool) { const TrellisNode::Children &origChildren = orig.GetChildren(); m_prevNodes.resize(origChildren.size()); @@ -49,25 +52,25 @@ TrellisNode::TrellisNode(const ArcLists &arcLists, const TrellisNode &orig, cons const TrellisNode *origChild = origChildren[i]; if (origChild != &nodeToChange) { // recurse - newChild = new TrellisNode(arcLists, *origChild, nodeToChange); + newChild = new (pool.Allocate()) TrellisNode(pool, arcLists, *origChild, nodeToChange); } else { size_t nextInd = nodeToChange.ind + 1; - newChild = new TrellisNode(arcLists, nodeToChange.arcList, nextInd); + newChild = new (pool.Allocate()) TrellisNode(pool, arcLists, nodeToChange.arcList, nextInd); } m_prevNodes[i] = newChild; } } -void TrellisNode::CreateTail(const ArcLists &arcLists, const SCFG::Hypothesis &hypo) +void TrellisNode::CreateTail(MemPool &pool, const ArcLists &arcLists, const SCFG::Hypothesis &hypo) { const Vector &prevHypos = hypo.GetPrevHypos(); m_prevNodes.resize(prevHypos.size(), NULL); for (size_t i = 0; i < hypo.GetPrevHypos().size(); ++i) { const SCFG::Hypothesis &prevHypo = *prevHypos[i]; - TrellisNode *prevNode = new TrellisNode(arcLists, prevHypo); + TrellisNode *prevNode = new (pool.Allocate()) TrellisNode(pool, arcLists, prevHypo); m_prevNodes[i] = prevNode; } } @@ -119,7 +122,7 @@ TrellisPath::TrellisPath(const SCFG::Manager &mgr, const SCFG::Hypothesis &hypo) // 1st m_scores = m_scores = new (pool.Allocate()) Scores(mgr.system, pool, mgr.system.featureFunctions.GetNumScores(), hypo.GetScores()); - m_node = new TrellisNode(mgr.arcLists, hypo); + m_node = new (pool.Allocate()) TrellisNode(pool, mgr.arcLists, hypo); m_prevNodeChanged = m_node; } @@ -139,13 +142,13 @@ TrellisPath::TrellisPath(const SCFG::Manager &mgr, const SCFG::TrellisPath &orig m_scores->PlusEquals(mgr.system, nextHypo.GetScores()); if (origPath.m_node == &nodeToChange) { - m_node = new TrellisNode(mgr.arcLists, nodeToChange.arcList, nodeToChange.ind + 1); + m_node = new (pool.Allocate()) TrellisNode(pool, mgr.arcLists, nodeToChange.arcList, nodeToChange.ind + 1); m_prevNodeChanged= m_node; } else { - // recursively copy nodes until we find the node that needs to change - m_node = new TrellisNode(mgr.arcLists, *origPath.m_node, nodeToChange); - m_prevNodeChanged= m_node; + // recursively copy nodes until we find the node that needs to change + m_node = new (pool.Allocate()) TrellisNode(pool, mgr.arcLists, *origPath.m_node, nodeToChange); + m_prevNodeChanged= m_node; } } @@ -169,8 +172,10 @@ SCORE TrellisPath::GetFutureScore() const void TrellisPath::CreateDeviantPaths(TrellisPaths &paths, const SCFG::Manager &mgr) const { if (m_prevNodeChanged->HasMore()) { + MemPool &pool = mgr.GetPool(); + //cerr << "BEGIN deviantPath" << endl; - SCFG::TrellisPath *deviantPath = new TrellisPath(mgr, *this, *m_prevNodeChanged); + SCFG::TrellisPath *deviantPath = new SCFG::TrellisPath(mgr, *this, *m_prevNodeChanged); //cerr << "END deviantPath" << endl; paths.Add(deviantPath); //cerr << "ADDED deviantPath" << endl; diff --git a/contrib/other-builds/moses2/SCFG/TrellisPath.h b/contrib/other-builds/moses2/SCFG/TrellisPath.h index 4ab0d864a..20ec3c1a1 100644 --- a/contrib/other-builds/moses2/SCFG/TrellisPath.h +++ b/contrib/other-builds/moses2/SCFG/TrellisPath.h @@ -7,6 +7,7 @@ #pragma once #include "../ArcLists.h" #include "../TypeDef.h" +#include "../Vector.h" namespace Moses2 { @@ -25,15 +26,15 @@ class Hypothesis; class TrellisNode { public: - typedef std::vector Children; + typedef Vector Children; const ArcList &arcList; size_t ind; - TrellisNode(const ArcLists &arcLists, const SCFG::Hypothesis &hypo); - TrellisNode(const ArcLists &arcLists, const ArcList &varcList, size_t vind); + TrellisNode(MemPool &pool, const ArcLists &arcLists, const SCFG::Hypothesis &hypo); + TrellisNode(MemPool &pool, const ArcLists &arcLists, const ArcList &varcList, size_t vind); - TrellisNode(const ArcLists &arcLists, const TrellisNode &orig, const TrellisNode &nodeToChange); + TrellisNode(MemPool &pool, const ArcLists &arcLists, const TrellisNode &orig, const TrellisNode &nodeToChange); const SCFG::Hypothesis &GetHypothesis() const; bool HasMore() const; @@ -45,7 +46,7 @@ public: protected: Children m_prevNodes; - void CreateTail(const ArcLists &arcLists, const SCFG::Hypothesis &hypo); + void CreateTail(MemPool &pool, const ArcLists &arcLists, const SCFG::Hypothesis &hypo); }; /////////////////////////////////////////////////////////////////////