start of multithreading in moses2

This commit is contained in:
Hieu Hoang 2015-11-10 12:46:26 +00:00
parent c0d74aa1bd
commit 3040fe57b0
6 changed files with 59 additions and 8 deletions

View File

@ -33,16 +33,20 @@ int main(int argc, char** argv)
istream &inStream = GetInputStream(params);
Moses::ThreadPool pool(4);
cerr << "system.numThreads=" << system.numThreads << endl;
Moses::ThreadPool pool(system.numThreads);
string line;
while (getline(inStream, line)) {
boost::shared_ptr<TranslationTask> task(new TranslationTask(system, line));
//pool.Submit(task);
task->Run();
pool.Submit(task);
//task->Run();
}
pool.Stop(true);
if (inStream != cin) {
delete &inStream;
}

View File

@ -64,6 +64,8 @@ const Hypothesis *Manager::GetBestHypothesis() const
void Manager::Decode()
{
Init();
const Moses::Bitmap &initBitmap = m_bitmaps->GetInitialBitmap();
Hypothesis *initHypo = Hypothesis::Create(*this);
initHypo->Init(*m_initPhrase, m_initRange, initBitmap);

View File

@ -32,9 +32,6 @@ public:
,m_initRange(NOT_FOUND, NOT_FOUND)
{}
// must be run in same thread as Decode()
void Init();
virtual ~Manager();
MemPool &GetPool() const
@ -73,6 +70,9 @@ protected:
Stacks m_stacks;
SearchNormal *m_search;
// must be run in same thread as Decode()
void Init();
void CalcFutureScore();
};

View File

@ -7,6 +7,8 @@
#include <string>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include "System.h"
#include "FF/FeatureFunction.h"
#include "TranslationModel/UnknownWordPenalty.h"
@ -20,7 +22,8 @@ System::System(const Moses::Parameter &paramsArg)
,featureFunctions(*this)
{
params.SetParameter(stackSize, "stack", Moses::DEFAULT_MAX_HYPOSTACK_SIZE);
ini_performance_options();
params.SetParameter(stackSize, "stack", Moses::DEFAULT_MAX_HYPOSTACK_SIZE);
params.SetParameter(maxDistortion, "distortion-limit", -1);
params.SetParameter(maxPhraseLength, "max-phrase-length",
Moses::DEFAULT_MAX_PHRASE_LENGTH);
@ -95,3 +98,42 @@ Recycler<Hypothesis*> &System::GetHypoRecycle() const
return *pool;
}
void
System
::ini_performance_options()
{
const Moses::PARAM_VEC *paramsVec;
// m_parameter->SetParameter<size_t>(m_timeout_threshold, "time-out", -1);
// m_timeout = (GetTimeoutThreshold() == (size_t)-1) ? false : true;
numThreads = 1;
paramsVec = params.GetParam("threads");
if (paramsVec && paramsVec->size()) {
if (paramsVec->at(0) == "all") {
#ifdef WITH_THREADS
numThreads = boost::thread::hardware_concurrency();
if (!numThreads) {
std::cerr << "-threads all specified but Boost doesn't know how many cores there are";
throw;
}
#else
std::cerr << "-threads all specified but moses not built with thread support";
return false;
#endif
} else {
numThreads = Moses::Scan<int>(paramsVec->at(0));
if (numThreads < 1) {
std::cerr << "Specify at least one thread.";
throw;
}
#ifndef WITH_THREADS
if (numThreads > 1) {
std::cerr << "Error: Thread count of " << params->at(0)
<< " but moses not built with thread support";
throw
}
#endif
}
}
return;
}

View File

@ -39,11 +39,15 @@ public:
size_t stackSize;
int maxDistortion;
size_t maxPhraseLength;
int numThreads;
protected:
mutable boost::thread_specific_ptr<MemPool> m_managerPool;
mutable boost::thread_specific_ptr<Recycler<Hypothesis*> > m_hypoRecycle;
void LoadWeights();
void LoadMappings();
void ini_performance_options();
};

View File

@ -15,7 +15,6 @@ TranslationTask::~TranslationTask()
void TranslationTask::Run()
{
m_mgr->Init();
m_mgr->Decode();
const Hypothesis *bestHypo = m_mgr->GetBestHypothesis();