make binary pt threadable

This commit is contained in:
Hieu Hoang 2013-03-04 18:15:05 +00:00
parent 991ea4f9c1
commit cc2ec9bc3e
2 changed files with 56 additions and 26 deletions

View File

@ -28,65 +28,89 @@ PhraseDictionaryTreeAdaptor::
PhraseDictionaryTreeAdaptor(const std::string &line) PhraseDictionaryTreeAdaptor(const std::string &line)
: PhraseDictionary("PhraseDictionaryTreeAdaptor", line) : PhraseDictionary("PhraseDictionaryTreeAdaptor", line)
{ {
imp = new PDTAimp(this,m_numInputScores);
} }
PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor() PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor()
{ {
imp->CleanUp();
delete imp;
} }
bool PhraseDictionaryTreeAdaptor::InitDictionary() bool PhraseDictionaryTreeAdaptor::InitDictionary()
{ {
const StaticData &staticData = StaticData::Instance();
vector<float> weight = staticData.GetWeights(this);
const LMList &languageModels = staticData.GetLMList();
if(m_numScoreComponents!=weight.size()) {
std::stringstream strme;
strme << "ERROR: mismatch of number of scaling factors: "<<weight.size()
<<" "<<m_numScoreComponents<<"\n";
UserMessage::Add(strme.str());
return false;
}
imp->Create(m_input, m_output, m_filePath, weight, languageModels);
return true; return true;
} }
void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source) void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source)
{ {
imp->CleanUp(); const StaticData &staticData = StaticData::Instance();
PDTAimp *obj = new PDTAimp(this,m_numInputScores);
const LMList &languageModels = staticData.GetLMList();
vector<float> weight = staticData.GetWeights(this);
if(m_numScoreComponents!=weight.size()) {
std::stringstream strme;
strme << "ERROR: mismatch of number of scaling factors: "<<weight.size()
<<" "<<m_numScoreComponents<<"\n";
UserMessage::Add(strme.str());
abort();
}
obj->Create(m_input, m_output, m_filePath, weight, languageModels);
obj->CleanUp();
// caching only required for confusion net // caching only required for confusion net
if(ConfusionNet const* cn=dynamic_cast<ConfusionNet const*>(&source)) if(ConfusionNet const* cn=dynamic_cast<ConfusionNet const*>(&source))
imp->CacheSource(*cn); obj->CacheSource(*cn);
m_implementation.reset(obj);
}
void PhraseDictionaryTreeAdaptor::CleanUpAfterSentenceProcessing(InputType const& source)
{
PDTAimp *obj = GetImplementation();
obj->CleanUp();
} }
TargetPhraseCollection const* TargetPhraseCollection const*
PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const
{ {
return imp->GetTargetPhraseCollection(src); return GetImplementation()->GetTargetPhraseCollection(src);
} }
TargetPhraseCollection const* TargetPhraseCollection const*
PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(InputType const& src,WordsRange const &range) const PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(InputType const& src,WordsRange const &range) const
{ {
if(imp->m_rangeCache.empty()) { if(GetImplementation()->m_rangeCache.empty()) {
return imp->GetTargetPhraseCollection(src.GetSubString(range)); return GetImplementation()->GetTargetPhraseCollection(src.GetSubString(range));
} else { } else {
return imp->m_rangeCache[range.GetStartPos()][range.GetEndPos()]; return GetImplementation()->m_rangeCache[range.GetStartPos()][range.GetEndPos()];
} }
} }
void PhraseDictionaryTreeAdaptor::EnableCache() void PhraseDictionaryTreeAdaptor::EnableCache()
{ {
imp->useCache=1; GetImplementation()->useCache=1;
} }
void PhraseDictionaryTreeAdaptor::DisableCache() void PhraseDictionaryTreeAdaptor::DisableCache()
{ {
imp->useCache=0; GetImplementation()->useCache=0;
}
PDTAimp* PhraseDictionaryTreeAdaptor::GetImplementation()
{
PDTAimp* dict;
dict = m_implementation.get();
CHECK(dict);
return dict;
}
const PDTAimp* PhraseDictionaryTreeAdaptor::GetImplementation() const
{
PDTAimp* dict;
dict = m_implementation.get();
CHECK(dict);
return dict;
} }
} }

View File

@ -23,12 +23,17 @@ class InputType;
class PhraseDictionaryTreeAdaptor : public PhraseDictionary class PhraseDictionaryTreeAdaptor : public PhraseDictionary
{ {
typedef PhraseDictionary MyBase; typedef PhraseDictionary MyBase;
PDTAimp *imp;
boost::thread_specific_ptr<PDTAimp> m_implementation;
friend class PDTAimp; friend class PDTAimp;
PhraseDictionaryTreeAdaptor(); PhraseDictionaryTreeAdaptor();
PhraseDictionaryTreeAdaptor(const PhraseDictionaryTreeAdaptor&); PhraseDictionaryTreeAdaptor(const PhraseDictionaryTreeAdaptor&);
void operator=(const PhraseDictionaryTreeAdaptor&); void operator=(const PhraseDictionaryTreeAdaptor&);
PDTAimp* GetImplementation();
const PDTAimp* GetImplementation() const;
public: public:
PhraseDictionaryTreeAdaptor(const std::string &line); PhraseDictionaryTreeAdaptor(const std::string &line);
virtual ~PhraseDictionaryTreeAdaptor(); virtual ~PhraseDictionaryTreeAdaptor();
@ -51,6 +56,7 @@ public:
TargetPhraseCollection const* GetTargetPhraseCollection(InputType const& src,WordsRange const & srcRange) const; TargetPhraseCollection const* GetTargetPhraseCollection(InputType const& src,WordsRange const & srcRange) const;
virtual void InitializeForInput(InputType const& source); virtual void InitializeForInput(InputType const& source);
void CleanUpAfterSentenceProcessing(InputType const& source);
virtual ChartRuleLookupManager *CreateRuleLookupManager( virtual ChartRuleLookupManager *CreateRuleLookupManager(
const InputType &, const InputType &,