From 0128081bb41fd526df7882e0925f9b5f9051ef75 Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Tue, 29 Dec 2015 16:07:56 +0000 Subject: [PATCH] use allocator in stacks --- .../moses2/Search/CubePruning/Search.cpp | 2 +- .../moses2/Search/CubePruning/Stack.cpp | 40 +++++++++++-------- .../moses2/Search/CubePruning/Stack.h | 22 +++++----- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/contrib/other-builds/moses2/Search/CubePruning/Search.cpp b/contrib/other-builds/moses2/Search/CubePruning/Search.cpp index 20cbfa814..35f77e6e1 100644 --- a/contrib/other-builds/moses2/Search/CubePruning/Search.cpp +++ b/contrib/other-builds/moses2/Search/CubePruning/Search.cpp @@ -165,7 +165,7 @@ void Search::PostDecode(size_t stackInd) CubeEdges &edges = m_cubeEdges[numWords]; // sort hypo for a particular bitmap and hypoEndPos - CubeEdge::Hypotheses &sortedHypos = val.second.GetSortedAndPruneHypos(m_mgr); + CubeEdge::Hypotheses &sortedHypos = val.second->GetSortedAndPruneHypos(m_mgr); BOOST_FOREACH(const TargetPhrases *tps, path.targetPhrases) { if (tps && tps->GetSize()) { diff --git a/contrib/other-builds/moses2/Search/CubePruning/Stack.cpp b/contrib/other-builds/moses2/Search/CubePruning/Stack.cpp index 2ddeecd28..998598db1 100644 --- a/contrib/other-builds/moses2/Search/CubePruning/Stack.cpp +++ b/contrib/other-builds/moses2/Search/CubePruning/Stack.cpp @@ -19,15 +19,22 @@ namespace Moses2 namespace NSCubePruning { +MiniStack::MiniStack(const Manager &mgr) +:m_sortedHypos(NULL) +{ + MemPool &pool = mgr.GetPool(); + m_coll = new (pool.Allocate<_HCType>()) _HCType(MemPoolAllocator(pool)); +} + CubeEdge::Hypotheses &MiniStack::GetSortedAndPruneHypos(const Manager &mgr) const { if (m_sortedHypos == NULL) { // create sortedHypos first MemPool &pool = mgr.GetPool(); - m_sortedHypos = new (pool.Allocate< Vector >()) Vector(pool, m_coll.size()); + m_sortedHypos = new (pool.Allocate< Vector >()) Vector(pool, m_coll->size()); size_t ind = 0; - BOOST_FOREACH(const Hypothesis *hypo, m_coll) { + BOOST_FOREACH(const Hypothesis *hypo, *m_coll) { (*m_sortedHypos)[ind] = hypo; ++ind; } @@ -81,9 +88,10 @@ void MiniStack::SortAndPruneHypos(const Manager &mgr) const /////////////////////////////////////////////////////////////// Stack::Stack(const Manager &mgr) +:m_mgr(&mgr) { MemPool &pool = mgr.GetPool(); - m_coll = new (pool.Allocate()) Coll(MemPoolAllocator< std::pair >(pool)); + m_coll = new (pool.Allocate()) Coll(MemPoolAllocator< std::pair >(pool)); } Stack::~Stack() @@ -93,7 +101,7 @@ Stack::~Stack() void Stack::Add(const Hypothesis *hypo, Recycler &hypoRecycle) { - StackAdd added = Add(hypo); + StackAdd added = Add2(hypo); if (added.toBeDeleted) { hypoRecycle.Add(added.toBeDeleted); @@ -101,7 +109,7 @@ void Stack::Add(const Hypothesis *hypo, Recycler &hypoRecycle) } -StackAdd Stack::Add(const Hypothesis *hypo) +StackAdd Stack::Add2(const Hypothesis *hypo) { HypoCoverage key(&hypo->GetBitmap(), hypo->GetInputPath().range.GetEndPos()); MiniStack::_HCType &innerColl = GetMiniStack(key).GetColl(); @@ -139,7 +147,7 @@ std::vector Stack::GetBestHypos(size_t num) const { std::vector ret; BOOST_FOREACH(const Coll::value_type &val, *m_coll) { - const MiniStack::_HCType &hypos = val.second.GetColl(); + const MiniStack::_HCType &hypos = val.second->GetColl(); ret.insert(ret.end(), hypos.begin(), hypos.end()); } @@ -158,7 +166,7 @@ size_t Stack::GetHypoSize() const { size_t ret = 0; BOOST_FOREACH(const Coll::value_type &val, *m_coll) { - const MiniStack::_HCType &hypos = val.second.GetColl(); + const MiniStack::_HCType &hypos = val.second->GetColl(); ret += hypos.size(); } return ret; @@ -166,22 +174,20 @@ size_t Stack::GetHypoSize() const MiniStack &Stack::GetMiniStack(const HypoCoverage &key) { - /* - _HCType *ret; - Coll::iterator iter = m_coll.find(key); - if (iter == m_coll.end()) { - ret = new _HCType(); - m_coll[key] = ret; + MiniStack *ret; + Coll::iterator iter = m_coll->find(key); + if (iter == m_coll->end()) { + MemPool &pool = m_mgr->GetPool(); + ret = new (pool.Allocate()) MiniStack(*m_mgr); + (*m_coll)[key] = ret; } else { ret = iter->second; } return *ret; - */ - return (*m_coll)[key]; } -} +} // namespace NSCubePruning -} +} //namespace Moses2 diff --git a/contrib/other-builds/moses2/Search/CubePruning/Stack.h b/contrib/other-builds/moses2/Search/CubePruning/Stack.h index 6ebd32396..2c5e8de29 100644 --- a/contrib/other-builds/moses2/Search/CubePruning/Stack.h +++ b/contrib/other-builds/moses2/Search/CubePruning/Stack.h @@ -28,22 +28,22 @@ class MiniStack public: typedef boost::unordered_set, - UnorderedComparer + UnorderedComparer, + MemPoolAllocator > _HCType; - MiniStack() - {} + MiniStack(const Manager &mgr); _HCType &GetColl() - { return m_coll; } + { return *m_coll; } const _HCType &GetColl() const - { return m_coll; } + { return *m_coll; } CubeEdge::Hypotheses &GetSortedAndPruneHypos(const Manager &mgr) const; protected: - _HCType m_coll; + _HCType *m_coll; mutable CubeEdge::Hypotheses *m_sortedHypos; void SortAndPruneHypos(const Manager &mgr) const; @@ -59,13 +59,14 @@ public: typedef std::pair HypoCoverage; // bitmap and current endPos of hypos - typedef boost::unordered_map, std::equal_to, - MemPoolAllocator< std::pair > + MemPoolAllocator< std::pair > > Coll; + const Manager *m_mgr; Stack(const Manager &mgr); virtual ~Stack(); @@ -86,7 +87,8 @@ public: protected: Coll *m_coll; - StackAdd Add(const Hypothesis *hypo); + Stack(); // don't implement + StackAdd Add2(const Hypothesis *hypo); MiniStack &GetMiniStack(const HypoCoverage &key);