bitmap in mempool

This commit is contained in:
Hieu Hoang 2016-01-14 11:49:42 +00:00
parent 0f7ce2e871
commit 747349b2ad
7 changed files with 31 additions and 53 deletions

View File

@ -38,6 +38,7 @@ Manager::~Manager() {
delete m_search; delete m_search;
delete m_estimatedScores; delete m_estimatedScores;
delete m_bitmaps;
GetPool().Reset(); GetPool().Reset();
GetHypoRecycle().Clear(); GetHypoRecycle().Clear();
@ -49,6 +50,7 @@ void Manager::Init()
m_pool = &system.GetManagerPool(); m_pool = &system.GetManagerPool();
m_systemPool = &system.GetSystemPool(); m_systemPool = &system.GetSystemPool();
m_hypoRecycle = &system.GetHypoRecycler(); m_hypoRecycle = &system.GetHypoRecycler();
m_bitmaps = new Bitmaps(GetPool());
m_initPhrase = new (GetPool().Allocate<TargetPhrase>()) TargetPhrase(GetPool(), system, 0); m_initPhrase = new (GetPool().Allocate<TargetPhrase>()) TargetPhrase(GetPool(), system, 0);
@ -69,7 +71,7 @@ void Manager::Init()
CalcFutureScore(); CalcFutureScore();
m_bitmaps.Init(m_input->GetSize(), vector<bool>(0)); m_bitmaps->Init(m_input->GetSize(), vector<bool>(0));
switch (system.searchAlgorithm) { switch (system.searchAlgorithm) {
case Normal: case Normal:

View File

@ -49,7 +49,7 @@ public:
{ return *m_hypoRecycle; } { return *m_hypoRecycle; }
Bitmaps &GetBitmaps() Bitmaps &GetBitmaps()
{ return m_bitmaps; } { return *m_bitmaps; }
const Sentence &GetInput() const const Sentence &GetInput() const
{ return *m_input; } { return *m_input; }
@ -75,7 +75,7 @@ protected:
long m_translationId; long m_translationId;
Sentence *m_input; Sentence *m_input;
InputPaths m_inputPaths; InputPaths m_inputPaths;
Bitmaps m_bitmaps; Bitmaps *m_bitmaps;
SquareMatrix *m_estimatedScores; SquareMatrix *m_estimatedScores;
TargetPhrase *m_initPhrase; TargetPhrase *m_initPhrase;

View File

@ -18,12 +18,11 @@ class Vector : public std::vector<T, MemPoolAllocator<T> >
typedef std::vector<T, MemPoolAllocator<T> > Parent; typedef std::vector<T, MemPoolAllocator<T> > Parent;
public: public:
Vector(MemPool &pool, size_t size = 0) Vector(MemPool &pool, size_t size = 0, const T &val = T())
:Parent(size, T(), MemPoolAllocator<T>(pool) ) :Parent(size, val, MemPoolAllocator<T>(pool) )
{ {
} }
protected: protected:
}; };

View File

@ -25,9 +25,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
namespace Moses2 namespace Moses2
{ {
Bitmap::Bitmap(size_t size, const std::vector<bool>& initializer) Bitmap::Bitmap(MemPool &pool, size_t size, const std::vector<bool>& initializer)
:m_bitmap(initializer.begin(), initializer.end()) :m_bitmap(pool, size, false)
{ {
for (size_t i = 0; i < initializer.size(); ++i) {
m_bitmap[i] = initializer[i];
}
// The initializer may not be of the same length. Change to the desired // The initializer may not be of the same length. Change to the desired
// length. If we need to add any elements, initialize them to false. // length. If we need to add any elements, initialize them to false.
@ -36,35 +39,21 @@ Bitmap::Bitmap(size_t size, const std::vector<bool>& initializer)
m_numWordsCovered = std::count(m_bitmap.begin(), m_bitmap.end(), true); m_numWordsCovered = std::count(m_bitmap.begin(), m_bitmap.end(), true);
// Find the first gap, and cache it. // Find the first gap, and cache it.
std::vector<char>::const_iterator first_gap = std::find( Vector<char>::const_iterator first_gap = std::find(
m_bitmap.begin(), m_bitmap.end(), false); m_bitmap.begin(), m_bitmap.end(), false);
m_firstGap = ( m_firstGap = (
(first_gap == m_bitmap.end()) ? (first_gap == m_bitmap.end()) ?
NOT_FOUND : first_gap - m_bitmap.begin()); NOT_FOUND : first_gap - m_bitmap.begin());
} }
//! Create Bitmap of length size and initialise. Bitmap::Bitmap(MemPool &pool, const Bitmap &copy, const Range &range)
Bitmap::Bitmap(size_t size) :m_bitmap(pool, copy.m_bitmap.size())
:m_bitmap(size, false)
,m_firstGap(0)
,m_numWordsCovered(0)
{
}
//! Deep copy.
Bitmap::Bitmap(const Bitmap &copy)
:m_bitmap(copy.m_bitmap)
,m_firstGap(copy.m_firstGap)
,m_numWordsCovered(copy.m_numWordsCovered)
{
}
Bitmap::Bitmap(const Bitmap &copy, const Range &range)
:m_bitmap(copy.m_bitmap)
,m_firstGap(copy.m_firstGap) ,m_firstGap(copy.m_firstGap)
,m_numWordsCovered(copy.m_numWordsCovered) ,m_numWordsCovered(copy.m_numWordsCovered)
{ {
for (size_t i = 0; i < m_bitmap.size(); ++i) {
m_bitmap[i] = copy.m_bitmap[i];
}
SetValueNonOverlap(range); SetValueNonOverlap(range);
} }

View File

@ -29,9 +29,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include "Range.h" #include "Range.h"
#include "../Vector.h"
namespace Moses2 namespace Moses2
{ {
class MemPool;
typedef unsigned long WordsBitmapID; typedef unsigned long WordsBitmapID;
@ -50,7 +52,7 @@ class Bitmap
{ {
friend std::ostream& operator<<(std::ostream& out, const Bitmap& bitmap); friend std::ostream& operator<<(std::ostream& out, const Bitmap& bitmap);
private: private:
std::vector<char> m_bitmap; //! Ticks of words in sentence that have been done. Vector<char> m_bitmap; //! Ticks of words in sentence that have been done.
size_t m_firstGap; //! Cached position of first gap, or NOT_FOUND. size_t m_firstGap; //! Cached position of first gap, or NOT_FOUND.
size_t m_numWordsCovered; size_t m_numWordsCovered;
@ -95,15 +97,9 @@ private:
public: public:
//! Create Bitmap of length size, and initialise with vector. //! Create Bitmap of length size, and initialise with vector.
explicit Bitmap(size_t size, const std::vector<bool>& initializer); explicit Bitmap(MemPool &pool, size_t size, const std::vector<bool>& initializer);
//! Create Bitmap of length size and initialise. explicit Bitmap(MemPool &pool, const Bitmap &copy, const Range &range);
explicit Bitmap(size_t size);
//! Deep copy.
explicit Bitmap(const Bitmap &copy);
explicit Bitmap(const Bitmap &copy, const Range &range);
//! Count of words translated. //! Count of words translated.
size_t GetNumWordsCovered() const { size_t GetNumWordsCovered() const {

View File

@ -7,31 +7,30 @@ using namespace std;
namespace Moses2 namespace Moses2
{ {
Bitmaps::Bitmaps() Bitmaps::Bitmaps(MemPool &pool)
:m_pool(pool)
{ {
} }
Bitmaps::~Bitmaps() Bitmaps::~Bitmaps()
{ {
Clear();
} }
void Bitmaps::Init(size_t inputSize, const std::vector<bool> &initSourceCompleted) void Bitmaps::Init(size_t inputSize, const std::vector<bool> &initSourceCompleted)
{ {
m_initBitmap = new Bitmap(inputSize, initSourceCompleted); m_initBitmap = new (m_pool.Allocate<Bitmap>()) Bitmap(m_pool, inputSize, initSourceCompleted);
m_coll[m_initBitmap]; m_coll[m_initBitmap];
} }
const Bitmap &Bitmaps::GetNextBitmap(const Bitmap &bm, const Range &range) const Bitmap &Bitmaps::GetNextBitmap(const Bitmap &bm, const Range &range)
{ {
Bitmap *newBM = new Bitmap(bm, range); Bitmap *newBM = new (m_pool.Allocate<Bitmap>()) Bitmap(m_pool, bm, range);
Coll::const_iterator iter = m_coll.find(newBM); Coll::const_iterator iter = m_coll.find(newBM);
if (iter == m_coll.end()) { if (iter == m_coll.end()) {
m_coll[newBM] = NextBitmaps(); m_coll[newBM] = NextBitmaps();
return *newBM; return *newBM;
} else { } else {
delete newBM;
return *iter->first; return *iter->first;
} }
} }
@ -56,14 +55,5 @@ const Bitmap &Bitmaps::GetBitmap(const Bitmap &bm, const Range &range)
return *newBM; return *newBM;
} }
void Bitmaps::Clear()
{
BOOST_FOREACH (const Coll::value_type& myPair, m_coll) {
const Bitmap *bm = myPair.first;
delete bm;
}
m_coll.clear();
}
} }

View File

@ -8,6 +8,7 @@
namespace Moses2 namespace Moses2
{ {
class MemPool;
class Bitmaps class Bitmaps
{ {
@ -17,9 +18,11 @@ class Bitmaps
Coll m_coll; Coll m_coll;
const Bitmap *m_initBitmap; const Bitmap *m_initBitmap;
MemPool &m_pool;
const Bitmap &GetNextBitmap(const Bitmap &bm, const Range &range); const Bitmap &GetNextBitmap(const Bitmap &bm, const Range &range);
public: public:
Bitmaps(); Bitmaps(MemPool &pool);
virtual ~Bitmaps(); virtual ~Bitmaps();
void Init(size_t inputSize, const std::vector<bool> &initSourceCompleted); void Init(size_t inputSize, const std::vector<bool> &initSourceCompleted);
@ -27,7 +30,6 @@ public:
return *m_initBitmap; return *m_initBitmap;
} }
const Bitmap &GetBitmap(const Bitmap &bm, const Range &range); const Bitmap &GetBitmap(const Bitmap &bm, const Range &range);
void Clear();
}; };
} }