-threads all option

git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@4252 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
heafield 2011-09-22 22:29:56 +00:00
parent ddbfee788c
commit 19879a8b1e
4 changed files with 49 additions and 38 deletions

View File

@ -217,24 +217,6 @@ int main(int argc, char* argv[])
return EXIT_FAILURE;
}
// create threadpool, if necessary
int threadcount = (parameter.GetParam("threads").size() > 0) ?
Scan<size_t>(parameter.GetParam("threads")[0]) : 1;
#ifdef WITH_THREADS
if (threadcount < 1) {
cerr << "Error: Need to specify a positive number of threads" << endl;
exit(1);
}
ThreadPool pool(threadcount);
#else
if (threadcount > 1) {
cerr << "Error: Thread count of " << threadcount
<< " but moses not built with thread support" << endl;
exit(1);
}
#endif
const StaticData &staticData = StaticData::Instance();
if (!StaticData::LoadDataStatic(&parameter))
return EXIT_FAILURE;
@ -268,6 +250,10 @@ int main(int argc, char* argv[])
if (ioWrapper == NULL)
return EXIT_FAILURE;
#ifdef WITH_THREADS
ThreadPool pool(staticData.ThreadCount());
#endif
// read each sentence & decode
InputType *source=0;
while(ReadInput(*ioWrapper,staticData.GetInputType(),source)) {

View File

@ -336,25 +336,6 @@ int main(int argc, char** argv)
}
// create threadpool, if using multi-threaded decoding
// note: multi-threading is done on sentence-level,
// each thread translates one sentence
int threadcount = (params->GetParam("threads").size() > 0) ?
Scan<size_t>(params->GetParam("threads")[0]) : 1;
#ifdef WITH_THREADS
if (threadcount < 1) {
cerr << "Error: Need to specify a positive number of threads" << endl;
exit(1);
}
ThreadPool pool(threadcount);
#else
if (threadcount > 1) {
cerr << "Error: Thread count of " << threadcount << " but moses not built with thread support" << endl;
exit(1);
}
#endif
// initialize all "global" variables, which are stored in StaticData
// note: this also loads models such as the language model, etc.
if (!StaticData::LoadDataStatic(params)) {
@ -370,6 +351,7 @@ int main(int argc, char** argv)
// shorthand for accessing information in StaticData
const StaticData& staticData = StaticData::Instance();
// set up read/writing class
IOWrapper* ioWrapper = GetIODevice(staticData);
if (!ioWrapper) {
@ -441,6 +423,10 @@ int main(int argc, char** argv)
alignmentInfoCollector.reset(new OutputCollector(ioWrapper->GetAlignmentOutputStream()));
}
#ifdef WITH_THREADS
ThreadPool pool(staticData.ThreadCount());
#endif
// main loop over set of input sentences
InputType* source = NULL;
size_t lineCount = 0;
@ -457,7 +443,7 @@ int main(int argc, char** argv)
alignmentInfoCollector.get() );
// execute task
#ifdef WITH_THREADS
pool.Submit(task);
pool.Submit(task);
#else
task->Run();
#endif

View File

@ -45,6 +45,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "SyntacticLanguageModel.h"
#endif
#ifdef WITH_THREADS
#include <boost/thread.hpp>
#endif
using namespace std;
namespace Moses
@ -387,6 +391,35 @@ bool StaticData::LoadData(Parameter *parameter)
m_lmcache_cleanup_threshold = (m_parameter->GetParam("clean-lm-cache").size() > 0) ?
Scan<size_t>(m_parameter->GetParam("clean-lm-cache")[0]) : 1;
m_threadCount = 1;
const std::vector<std::string> &threadInfo = m_parameter->GetParam("threads");
if (!threadInfo.empty()) {
if (threadInfo[0] == "all") {
#ifdef WITH_THREADS
m_threadCount = boost::thread::hardware_concurrency();
if (!m_threadCount) {
UserMessage::Add("-threads all specified but Boost doesn't know how many cores there are");
return false;
}
#else
UserMessage::Add("-threads all specified but moses not built with thread support");
return false;
#endif
} else {
m_threadCount = Scan<int>(threadInfo[0]);
if (m_threadCount < 1) {
UserMessage::Add("Specify at least one thread.");
return false;
}
#ifndef WITH_THREADS
if (m_threadCount > 1) {
UserMessage::Add(std::string("Error: Thread count of ") + threadInfo[0] + " but moses not built with thread support");
return false;
}
#endif
}
}
// Read in constraint decoding file, if provided
if(m_parameter->GetParam("constraint").size()) {
if (m_parameter->GetParam("search-algorithm").size() > 0

View File

@ -208,6 +208,8 @@ protected:
UnknownLHSList m_unknownLHS;
WordAlignmentSort m_wordAlignmentSort;
int m_threadCount;
StaticData();
void LoadPhraseBasedParameters();
@ -595,6 +597,10 @@ public:
WordAlignmentSort GetWordAlignmentSort() const {
return m_wordAlignmentSort;
}
int ThreadCount() const {
return m_threadCount;
}
};
}