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

179 lines
4.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-12-10 23:49:30 +03:00
namespace Moses2
{
2016-03-31 23:00:16 +03:00
System::System(const Parameter &paramsArg) :
params(paramsArg), featureFunctions(*this)
2015-10-27 18:46:37 +03:00
{
2016-04-07 16:51:43 +03:00
options.init(paramsArg);
2016-03-31 23:00:16 +03:00
bestCollector.reset(new OutputCollector());
ini_performance_options();
params.SetParameter(cpuAffinityOffset, "cpu-affinity-offset", 0);
params.SetParameter(cpuAffinityOffsetIncr, "cpu-affinity-increment", 1);
reportSegmentation = (params.GetParam("report-segmentation-enriched") ? 2 :
params.GetParam("report-segmentation") ? 1 : 0);
params.SetParameter(outputHypoScore, "output-hypo-score", false);
const PARAM_VEC *section;
section = params.GetParam("n-best-list");
if (section) {
if (section->size() >= 2) {
nBestPath = section->at(0);
nbestSize = Scan<size_t>(section->at(1));
distinctNBest = (section->size() > 2 && section->at(2) == "distinct");
2015-12-01 02:03:33 +03:00
}
2016-03-31 23:00:16 +03:00
else {
throw "wrong format for switch -n-best-list file size [disinct]";
2016-03-17 20:54:25 +03:00
}
2016-03-31 23:00:16 +03:00
}
else {
nbestSize = 0;
}
// output collectors
if (nbestSize) {
nbestCollector.reset(new OutputCollector(nBestPath));
}
2016-03-17 20:54:25 +03:00
2016-03-31 23:00:16 +03:00
featureFunctions.Create();
LoadWeights();
cerr << "START featureFunctions.Load()" << endl;
featureFunctions.Load();
cerr << "START LoadMappings()" << endl;
LoadMappings();
cerr << "END LoadMappings()" << endl;
2015-10-27 18:46:37 +03:00
}
2016-03-20 14:40:39 +03:00
System::~System()
{
2015-10-27 18:46:37 +03:00
}
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);
2016-03-31 23:00:16 +03:00
BOOST_FOREACH(const std::string &line, *vec){
cerr << "line=" << line << endl;
weights.CreateFromString(featureFunctions, line);
}
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
2016-03-31 23:00:16 +03:00
BOOST_FOREACH(const std::string &line, *vec){
vector<string> toks = Tokenize(line);
assert( (toks.size() == 2 && toks[0] == "T") || (toks.size() == 3 && toks[1] == "T") );
size_t ptInd;
if (toks.size() == 2) {
ptInd = Scan<size_t>(toks[1]);
}
else {
ptInd = Scan<size_t>(toks[2]);
2015-10-31 05:45:01 +03:00
}
2016-03-31 23:00:16 +03:00
const PhraseTable *pt = featureFunctions.GetPhraseTablesExcludeUnknownWordPenalty(ptInd);
mappings.push_back(pt);
}
2015-10-27 18:46:37 +03:00
2016-03-31 23:00:16 +03:00
// unk pt
const UnknownWordPenalty *unkWP =
dynamic_cast<const UnknownWordPenalty*>(featureFunctions.FindFeatureFunction(
"UnknownWordPenalty0"));
2015-12-07 21:16:51 +03:00
if (unkWP) {
2016-03-31 23:00:16 +03:00
mappings.push_back(unkWP);
2015-12-07 21:16:51 +03:00
}
2015-10-31 05:45:01 +03:00
}
2015-10-24 15:19:42 +03:00
2016-01-03 00:44:44 +03:00
MemPool &System::GetSystemPool() const
{
MemPool &ret = GetThreadSpecificObj(m_systemPool);
return ret;
}
2015-11-10 14:28:17 +03:00
MemPool &System::GetManagerPool() const
{
MemPool &ret = GetThreadSpecificObj(m_managerPool);
return ret;
2015-11-10 14:28:17 +03:00
}
2016-03-31 23:00:16 +03:00
void System::ini_performance_options()
2015-11-10 15:46:26 +03:00
{
2016-03-31 23:00:16 +03:00
const 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 = 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;
2015-11-10 15:46:26 +03:00
}
2015-11-18 18:33:42 +03:00
FactorCollection &System::GetVocab() const
{
2016-03-31 23:00:16 +03:00
return m_vocab;
2015-11-18 18:33:42 +03:00
}
2015-11-20 03:49:48 +03:00
Recycler<HypothesisBase*> &System::GetHypoRecycler() const
{
2016-03-31 23:00:16 +03:00
return GetThreadSpecificObj(m_hypoRecycler);
}
2015-12-10 23:49:30 +03:00
}