File size: 3,575 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <memory>
#include <boost/pool/pool_alloc.hpp>
#include "Main.h"
#include "System.h"
#include "Phrase.h"
#include "TranslationTask.h"
#include "MemPoolAllocator.h"
#ifdef HAVE_XMLRPC_C
    #include "server/Server.h"
#endif // HAVE_XMLRPC_C

#include "legacy/InputFileStream.h"
#include "legacy/Parameter.h"
#include "legacy/ThreadPool.h"
#include "legacy/Timer.h"
#include "legacy/Util2.h"
#include "util/usage.hh"

using namespace std;

//extern size_t g_numHypos;

int main(int argc, char** argv)
{
  cerr << "Starting..." << endl;

  Moses2::Timer timer;
  timer.start();
  //Temp();

  Moses2::Parameter params;
  if (!params.LoadParam(argc, argv)) {
    return EXIT_FAILURE;
  }
  Moses2::System system(params);
  timer.check("Loaded");

  if (params.GetParam("show-weights")) {
    return EXIT_SUCCESS;
  }

  //cerr << "system.numThreads=" << system.options.server.numThreads << endl;
  Moses2::ThreadPool pool(system.options.server.numThreads, system.cpuAffinityOffset, system.cpuAffinityOffsetIncr);
  //cerr << "CREATED POOL" << endl;

  if (params.GetParam("server")) {
    std::cerr << "RUN SERVER" << std::endl;
    run_as_server(system);
  }
  else {
      std::cerr << "RUN BATCH" << std::endl;
      batch_run(params, system, pool);
  }

  cerr << "Decoding took " << timer.get_elapsed_time() << endl;
  //	cerr << "g_numHypos=" << g_numHypos << endl;
  cerr << "Finished" << endl;
  return EXIT_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////////////////////
void run_as_server(Moses2::System& system)
{
#ifdef HAVE_XMLRPC_C
	Moses2::Server server(system.options.server, system);
	server.run(system); // actually: don't return. see Server::run()
#else
  UTIL_THROW2("Moses2 was compiled without xmlrpc-c. "
              << "No server functionality available.");
#endif
}

////////////////////////////////////////////////////////////////////////////////////////////////
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));

        //cerr << "START pool.Submit()" << endl;
        pool.Submit(task);
        //task->Run();
        ++translationId;
    }

    pool.Stop(true);

    if (&inStream != &cin) {
        delete& inStream;
    }

    //util::PrintUsage(std::cerr);

}

////////////////////////////////////////////////////////////////////////////////////////////////
void Temp()
{
  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);

  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();
}