recycle stack

This commit is contained in:
Hieu Hoang 2015-12-11 15:11:16 +00:00
parent fd55279b09
commit b935767dc4
6 changed files with 58 additions and 13 deletions

View File

@ -25,6 +25,7 @@ namespace NSCubePruning
////////////////////////////////////////////////////////////////////////
Search::Search(Manager &mgr)
:Moses2::Search(mgr)
,m_stacks(mgr)
,m_queue(mgr.system.GetQueue())
,m_seenPositions(mgr.system.GetSeenPositions())
{

View File

@ -71,6 +71,11 @@ public:
void Add(const Hypothesis *hypo, Recycler<Hypothesis*> &hypoRecycle);
std::vector<const Hypothesis*> GetBestHypos(size_t num) const;
void Clear()
{
m_coll.clear();
}
protected:
Coll m_coll;

View File

@ -6,6 +6,8 @@
*/
#include "Stacks.h"
#include "../../System.h"
#include "../Manager.h"
using namespace std;
@ -15,24 +17,43 @@ namespace Moses2
namespace NSCubePruning
{
Stacks::Stacks() {
// TODO Auto-generated constructor stub
Stacks::Stacks(const Manager &mgr)
:m_mgr(mgr)
{
}
Stacks::~Stacks() {
Stacks::~Stacks()
{
Recycler<NSCubePruning::Stack*> &recycler = m_mgr.system.GetStackRecycler();
for (size_t i = 0; i < m_stacks.size(); ++i) {
recycler.Add(m_stacks[i]);
}
}
void Stacks::Init(size_t numStacks)
{
Recycler<NSCubePruning::Stack*> &recycler = m_mgr.system.GetStackRecycler();
m_stacks.resize(numStacks);
for (size_t i = 0; i < m_stacks.size(); ++i) {
if (recycler.IsEmpty()) {
m_stacks[i] = new Stack();
}
else {
Stack *stack = recycler.Get();
recycler.Pop();
stack->Clear();
m_stacks[i] = stack;
}
}
}
std::ostream& operator<<(std::ostream &out, const Stacks &obj)
{
for (size_t i = 0; i < obj.GetSize(); ++i) {
const Stack &stack = obj.m_stacks[i];
const Stack &stack = *obj.m_stacks[i];
out << stack.GetHypoSize() << " ";
}
@ -43,7 +64,7 @@ void Stacks::Add(const Hypothesis *hypo, Recycler<Hypothesis*> &hypoRecycle)
{
size_t numWordsCovered = hypo->GetBitmap().GetNumWordsCovered();
//cerr << "numWordsCovered=" << numWordsCovered << endl;
Stack &stack = m_stacks[numWordsCovered];
Stack &stack = *m_stacks[numWordsCovered];
stack.Add(hypo, hypoRecycle);
}

View File

@ -12,6 +12,7 @@
namespace Moses2
{
class Manager;
namespace NSCubePruning
{
@ -19,7 +20,7 @@ namespace NSCubePruning
class Stacks {
friend std::ostream& operator<<(std::ostream &, const Stacks &);
public:
Stacks();
Stacks(const Manager &mgr);
virtual ~Stacks();
void Init(size_t numStacks);
@ -28,15 +29,16 @@ public:
{ return m_stacks.size(); }
const Stack &Back() const
{ return m_stacks.back(); }
{ return *m_stacks.back(); }
Stack &operator[](size_t ind)
{ return m_stacks[ind]; }
{ return *m_stacks[ind]; }
void Add(const Hypothesis *hypo, Recycler<Hypothesis*> &hypoRecycle);
protected:
std::vector<Stack> m_stacks;
const Manager &m_mgr;
std::vector<Stack*> m_stacks;
};

View File

@ -160,6 +160,11 @@ ObjectPoolContiguous<Hypothesis*> &System::GetBatchForEval() const
return GetThreadSpecificObj(m_batchForEval);
}
Bitmaps &System::GetBitmaps() const
{
return GetThreadSpecificObj(m_bitmaps);
}
NSCubePruning::CubeEdge::Queue &System::GetQueue() const
{
return GetThreadSpecificObj(m_queue);
@ -170,10 +175,12 @@ NSCubePruning::CubeEdge::SeenPositions &System::GetSeenPositions() const
return GetThreadSpecificObj(m_seenPositions);
}
Bitmaps &System::GetBitmaps() const
Recycler<NSCubePruning::Stack*> &System::GetStackRecycler() const
{
return GetThreadSpecificObj(m_bitmaps);
return GetThreadSpecificObj(m_stack);
}
}

View File

@ -8,6 +8,7 @@
#pragma once
#include <vector>
#include <boost/thread/tss.hpp>
#include <boost/pool/object_pool.hpp>
#include "FF/FeatureFunctions.h"
#include "Weights.h"
#include "MemPool.h"
@ -20,6 +21,10 @@
namespace Moses2
{
namespace NSCubePruning
{
class Stack;
}
class FeatureFunction;
class StatefulFeatureFunction;
@ -57,9 +62,11 @@ public:
Recycler<Hypothesis*> &GetHypoRecycler() const;
ObjectPoolContiguous<Hypothesis*> &GetBatchForEval() const;
Bitmaps &GetBitmaps() const;
NSCubePruning::CubeEdge::Queue &GetQueue() const;
NSCubePruning::CubeEdge::SeenPositions &GetSeenPositions() const;
Bitmaps &GetBitmaps() const;
Recycler<NSCubePruning::Stack*> &GetStackRecycler() const;
protected:
mutable FactorCollection m_vocab;
@ -72,6 +79,8 @@ protected:
mutable boost::thread_specific_ptr<Bitmaps> m_bitmaps;
mutable boost::thread_specific_ptr< Recycler<NSCubePruning::Stack*> > m_stack;
void LoadWeights();
void LoadMappings();