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

118 lines
3.0 KiB
C++
Raw Normal View History

2015-10-23 20:33:12 +03:00
#include <iostream>
2015-11-17 15:27:17 +03:00
#include <memory>
2015-12-31 01:51:50 +03:00
#include <boost/pool/pool_alloc.hpp>
2016-04-01 16:42:06 +03:00
#include "Main.h"
2015-10-26 00:20:55 +03:00
#include "System.h"
2015-10-24 01:19:31 +03:00
#include "Phrase.h"
2015-11-10 14:28:17 +03:00
#include "TranslationTask.h"
2015-12-26 03:59:43 +03:00
#include "MemPool.h"
2016-04-01 23:59:05 +03:00
#include "server/Server.h"
2015-11-13 02:18:30 +03:00
#include "legacy/InputFileStream.h"
2015-11-11 20:31:05 +03:00
#include "legacy/Parameter.h"
2015-11-13 00:58:07 +03:00
#include "legacy/ThreadPool.h"
2015-11-13 19:29:55 +03:00
#include "legacy/Timer.h"
2015-10-23 20:33:12 +03:00
using namespace std;
2015-11-10 19:27:28 +03:00
//extern size_t g_numHypos;
2015-11-06 14:51:14 +03:00
2015-10-24 21:54:16 +03:00
int main(int argc, char** argv)
2015-10-23 20:33:12 +03:00
{
cerr << "Starting..." << endl;
2016-04-28 18:51:42 +03:00
Moses2::Timer timer;
timer.start();
2015-12-29 21:44:42 +03:00
//Temp();
2015-11-17 15:27:17 +03:00
2015-12-10 23:49:30 +03:00
Moses2::Parameter params;
2015-10-27 18:46:37 +03:00
params.LoadParam(argc, argv);
2015-12-10 23:49:30 +03:00
Moses2::System system(params);
2016-05-08 23:48:17 +03:00
timer.check("Loaded");
2015-10-24 01:19:31 +03:00
2016-03-01 02:28:24 +03:00
//cerr << "system.numThreads=" << system.numThreads << endl;
2015-11-10 15:46:26 +03:00
2016-04-07 18:23:14 +03:00
Moses2::ThreadPool pool(system.options.server.numThreads, system.cpuAffinityOffset, system.cpuAffinityOffsetIncr);
2016-03-01 02:28:24 +03:00
//cerr << "CREATED POOL" << endl;
2015-11-10 14:28:17 +03:00
2016-04-01 16:56:03 +03:00
if (params.GetParam("server")) {
std::cerr << "RUN SERVER" << std::endl;
2016-04-01 21:02:29 +03:00
run_as_server(system);
2016-04-01 16:56:03 +03:00
}
else {
std::cerr << "RUN BATCH" << std::endl;
batch_run(params, system, pool);
}
2015-11-13 19:29:55 +03:00
cerr << "Decoding took " << timer.get_elapsed_time() << endl;
2015-11-10 19:27:28 +03:00
// cerr << "g_numHypos=" << g_numHypos << endl;
2015-10-23 20:33:12 +03:00
cerr << "Finished" << endl;
}
2015-11-17 15:27:17 +03:00
2016-04-01 16:56:03 +03:00
////////////////////////////////////////////////////////////////////////////////////////////////
2016-04-01 21:02:29 +03:00
void run_as_server(Moses2::System &system)
2016-04-01 16:56:03 +03:00
{
2016-04-10 22:01:21 +03:00
Moses2::Server server(system.options.server, system);
2016-04-01 21:02:29 +03:00
server.run(system); // actually: don't return. see Server::run()
2016-04-01 16:56:03 +03:00
}
2015-11-17 15:27:17 +03:00
////////////////////////////////////////////////////////////////////////////////////////////////
2016-04-01 16:42:06 +03:00
istream &GetInputStream(Moses2::Parameter &params)
{
const Moses2::PARAM_VEC *vec = params.GetParam("input-file");
if (vec && vec->size()) {
Moses2::InputFileStream *stream = new Moses2::InputFileStream(vec->at(0));
return *stream;
}
else {
return cin;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
void batch_run(Moses2::Parameter &params, Moses2::System &system, Moses2::ThreadPool &pool)
{
istream &inStream = GetInputStream(params);
long translationId = 0;
string line;
while (getline(inStream, line)) {
//cerr << "line=" << line << endl;
boost::shared_ptr<Moses2::TranslationTask> task(new Moses2::TranslationTask(system, line, translationId));
2015-11-17 15:27:17 +03:00
2016-04-01 16:42:06 +03:00
//cerr << "START pool.Submit()" << endl;
pool.Submit(task);
//task->Run();
++translationId;
}
pool.Stop(true);
if (&inStream != &cin) {
delete &inStream;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
2015-11-17 15:27:17 +03:00
void Temp()
{
2015-12-26 03:59:43 +03:00
Moses2::MemPool pool;
Moses2::MemPoolAllocator<int> a(pool);
boost::unordered_set<int, boost::hash<int>, std::equal_to<int>, Moses2::MemPoolAllocator<int> > s(a);
s.insert(3);
s.insert(4);
s.insert(3);
s.erase(3);
2015-12-31 01:51:50 +03:00
boost::pool_allocator<int> alloc;
std::vector<int, boost::pool_allocator<int> > v(alloc);
for (int i = 0; i < 1000; ++i)
v.push_back(i);
v.clear();
boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::
purge_memory();
abort();
2015-11-17 15:27:17 +03:00
}