mosesdecoder/contrib/other-builds/moses2/System.cpp

129 lines
3.2 KiB
C++
Raw Normal View History

2015-10-23 20:33:12 +03:00
/*
2015-10-26 00:20:55 +03:00
* System.cpp
2015-10-23 20:33:12 +03:00
*
* Created on: 23 Oct 2015
* Author: hieu
*/
2015-10-27 18:46:37 +03:00
#include <string>
#include <iostream>
#include <boost/foreach.hpp>
2015-11-10 15:46:26 +03:00
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
2015-10-26 00:20:55 +03:00
#include "System.h"
2015-11-03 16:24:39 +03:00
#include "FF/FeatureFunction.h"
2015-11-04 16:09:53 +03:00
#include "TranslationModel/UnknownWordPenalty.h"
2015-11-11 19:23:49 +03:00
#include "legacy/Util2.h"
2015-10-27 18:46:37 +03:00
#include "util/exception.hh"
using namespace std;
2015-10-23 20:33:12 +03:00
2015-11-11 20:31:05 +03:00
System::System(const Parameter &paramsArg)
2015-11-05 14:19:37 +03:00
:params(paramsArg)
,featureFunctions(*this)
2015-10-27 18:46:37 +03:00
{
2015-11-10 15:46:26 +03:00
ini_performance_options();
2015-11-13 13:40:55 +03:00
params.SetParameter(stackSize, "stack", DEFAULT_MAX_HYPOSTACK_SIZE);
2015-11-06 19:39:30 +03:00
params.SetParameter(maxDistortion, "distortion-limit", -1);
params.SetParameter(maxPhraseLength, "max-phrase-length",
2015-11-13 13:40:55 +03:00
DEFAULT_MAX_PHRASE_LENGTH);
2015-11-16 19:47:49 +03:00
params.SetParameter(searchAlgorithm, "search-algorithm", Normal);
2015-11-04 20:23:04 +03:00
2015-11-05 14:19:37 +03:00
featureFunctions.Create();
2015-10-27 19:54:15 +03:00
LoadWeights();
2015-11-05 14:19:37 +03:00
featureFunctions.Load();
2015-10-31 05:45:01 +03:00
LoadMappings();
2015-10-27 18:46:37 +03:00
}
System::~System() {
}
2015-10-27 19:54:15 +03:00
void System::LoadWeights()
2015-10-24 14:39:15 +03:00
{
2015-11-11 20:31:05 +03:00
const PARAM_VEC *vec = params.GetParam("weight");
2015-11-05 14:19:37 +03:00
UTIL_THROW_IF2(vec == NULL, "Must have [weight] section");
2015-10-27 18:46:37 +03:00
2015-11-05 14:19:37 +03:00
weights.Init(featureFunctions);
BOOST_FOREACH(const std::string &line, *vec) {
weights.CreateFromString(featureFunctions, line);
2015-10-27 18:46:37 +03:00
}
2015-10-27 19:54:15 +03:00
}
2015-10-27 18:46:37 +03:00
2015-10-31 05:45:01 +03:00
void System::LoadMappings()
{
2015-11-13 13:40:55 +03:00
const PARAM_VEC *vec = params.GetParam("mapping");
2015-11-05 14:19:37 +03:00
UTIL_THROW_IF2(vec == NULL, "Must have [mapping] section");
2015-10-31 05:45:01 +03:00
2015-11-05 14:19:37 +03:00
BOOST_FOREACH(const std::string &line, *vec) {
2015-11-12 02:28:18 +03:00
vector<string> toks = Tokenize(line);
2015-11-06 19:09:44 +03:00
assert( (toks.size() == 2 && toks[0] == "T") || (toks.size() == 3 && toks[1] == "T") );
size_t ptInd;
if (toks.size() == 2) {
2015-11-12 02:29:58 +03:00
ptInd = Scan<size_t>(toks[1]);
2015-11-06 19:09:44 +03:00
}
else {
2015-11-12 02:29:58 +03:00
ptInd = Scan<size_t>(toks[2]);
2015-11-06 19:09:44 +03:00
}
2015-11-05 14:19:37 +03:00
const PhraseTable *pt = featureFunctions.GetPhraseTablesExcludeUnknownWordPenalty(ptInd);
mappings.push_back(pt);
2015-10-31 05:45:01 +03:00
}
2015-10-27 18:46:37 +03:00
2015-10-31 05:45:01 +03:00
// unk pt
2015-11-05 14:19:37 +03:00
const UnknownWordPenalty &unkWP = dynamic_cast<const UnknownWordPenalty&>(featureFunctions.FindFeatureFunction("UnknownWordPenalty0"));
mappings.push_back(&unkWP);
2015-10-31 05:45:01 +03:00
}
2015-10-24 15:19:42 +03:00
2015-11-10 14:28:17 +03:00
MemPool &System::GetManagerPool() const
{
MemPool *pool;
pool = m_managerPool.get();
if (pool == NULL) {
pool = new MemPool;
m_managerPool.reset(pool);
}
assert(pool);
return *pool;
}
2015-11-10 15:46:26 +03:00
void
System
::ini_performance_options()
{
2015-11-13 13:40:55 +03:00
const PARAM_VEC *paramsVec;
2015-11-10 15:46:26 +03:00
// 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 {
2015-11-12 02:29:58 +03:00
numThreads = Scan<int>(paramsVec->at(0));
2015-11-10 15:46:26 +03:00
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;
}