diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-12 13:56:42 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-12 13:56:42 +0200 |
commit | 244971287003d079e46193b8a209c28955f90134 (patch) | |
tree | 8beaae6b12b913acb213fc7f2415fd63886192f9 /mteval | |
parent | 5250fd67a4b8f242068cff87f0a6a4211f8b0fcf (diff) | |
parent | b66e838ed52decc0be1eb5817b2a77c3840db2c5 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'mteval')
-rw-r--r-- | mteval/Makefile.am | 2 | ||||
-rw-r--r-- | mteval/external_scorer.cc | 33 | ||||
-rw-r--r-- | mteval/ns_ext.cc | 43 |
3 files changed, 70 insertions, 8 deletions
diff --git a/mteval/Makefile.am b/mteval/Makefile.am index 681e798e..c833eb01 100644 --- a/mteval/Makefile.am +++ b/mteval/Makefile.am @@ -46,4 +46,4 @@ mbr_kbest_LDADD = libmteval.a ../utils/libutils.a scorer_test_SOURCES = scorer_test.cc scorer_test_LDADD = libmteval.a ../utils/libutils.a $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) -AM_CPPFLAGS = -DTEST_DATA=\"$(top_srcdir)/mteval/test_data\" -DBOOST_TEST_DYN_LINK -W -Wall -Wno-sign-compare -I$(top_srcdir) -I$(top_srcdir)/utils +AM_CPPFLAGS = -DTEST_DATA=\"$(top_srcdir)/mteval/test_data\" -DBOOST_TEST_DYN_LINK -W -Wall -Wno-sign-compare -I$(top_srcdir) -I$(top_srcdir)/utils -I$(top_srcdir)/klm diff --git a/mteval/external_scorer.cc b/mteval/external_scorer.cc index 1c09c2a1..054c279e 100644 --- a/mteval/external_scorer.cc +++ b/mteval/external_scorer.cc @@ -9,22 +9,35 @@ #include <cassert> #include "stringlib.h" +#include "filelib.h" #include "tdict.h" using namespace std; +extern const char* meteor_jar_path; +extern void setup_child_process_handler(); + map<string, boost::shared_ptr<ScoreServer> > ScoreServerManager::servers_; class METEORServer : public ScoreServer { public: - METEORServer() : ScoreServer("java -Xmx1024m -jar /usr0/cdyer/meteor/meteor-1.3.jar - - -mira -lower -t tune -l en") {} + METEORServer(const string& cmd) : ScoreServer(cmd) {} }; ScoreServer* ScoreServerManager::Instance(const string& score_type) { boost::shared_ptr<ScoreServer>& s = servers_[score_type]; if (!s) { if (score_type == "meteor") { - s.reset(new METEORServer); +#if HAVE_METEOR + if (!FileExists(meteor_jar_path)) { + cerr << meteor_jar_path << " not found!\n"; + abort(); + } + s.reset(new METEORServer(string("java -Xmx1536m -jar ") + meteor_jar_path + " - - -mira -lower -t tune -l en")); +#else + cerr << "cdec was not built with the --with-meteor option." << endl; + abort(); +#endif } else { cerr << "Don't know how to create score server for type '" << score_type << "'\n"; abort(); @@ -34,6 +47,7 @@ ScoreServer* ScoreServerManager::Instance(const string& score_type) { } ScoreServer::ScoreServer(const string& cmd) { + setup_child_process_handler(); cerr << "Invoking " << cmd << " ..." << endl; if (pipe(p2c) < 0) { perror("pipe"); exit(1); } if (pipe(c2p) < 0) { perror("pipe"); exit(1); } @@ -49,10 +63,10 @@ ScoreServer::ScoreServer(const string& cmd) { cerr << "Exec'ing from child " << cmd << endl; vector<string> vargs; SplitOnWhitespace(cmd, &vargs); - const char** cargv = static_cast<const char**>(malloc(sizeof(const char*) * vargs.size())); - for (unsigned i = 1; i < vargs.size(); ++i) cargv[i-1] = vargs[i].c_str(); - cargv[vargs.size() - 1] = NULL; - execvp(vargs[0].c_str(), (char* const*)cargv); + const char** cargv = static_cast<const char**>(malloc(sizeof(const char*) * (vargs.size() + 1))); + for (unsigned i = 0; i < vargs.size(); i++) cargv[i] = vargs[i].c_str(); + cargv[vargs.size()] = NULL; + execvp(*cargv, (char* const*)cargv); } else { // parent close(c2p[1]); close(p2c[0]); @@ -64,6 +78,7 @@ ScoreServer::ScoreServer(const string& cmd) { } ScoreServer::~ScoreServer() { + cerr << "ScoreServer::~ScoreServer()\n"; // TODO close stuff, join stuff } @@ -138,7 +153,11 @@ struct ExternalScore : public ScoreBase<ExternalScore> { assert(!"not implemented"); // no idea } void PlusEquals(const Score& delta, const float scale) { - assert(!"not implemented"); // don't even know what this is + if (static_cast<const ExternalScore&>(delta).score_server) score_server = static_cast<const ExternalScore&>(delta).score_server; + if (fields.size() != static_cast<const ExternalScore&>(delta).fields.size()) + fields.resize(max(fields.size(), static_cast<const ExternalScore&>(delta).fields.size())); + for (unsigned i = 0; i < static_cast<const ExternalScore&>(delta).fields.size(); ++i) + fields[i] += static_cast<const ExternalScore&>(delta).fields[i] * scale; } void PlusEquals(const Score& delta) { if (static_cast<const ExternalScore&>(delta).score_server) score_server = static_cast<const ExternalScore&>(delta).score_server; diff --git a/mteval/ns_ext.cc b/mteval/ns_ext.cc index 1e7e2bc1..efe48afe 100644 --- a/mteval/ns_ext.cc +++ b/mteval/ns_ext.cc @@ -4,15 +4,57 @@ #include <cstdlib> #include <cstring> #include <unistd.h> +#include <signal.h> +#include <sys/wait.h> #include <sstream> #include <iostream> #include <cassert> +#include "filelib.h" #include "stringlib.h" #include "tdict.h" using namespace std; +static volatile bool child_need_init = true; + +void metric_child_signal_handler(int signo) { + int status = 0; + string cmd; + { + ReadFile rf("/proc/self/cmdline"); + if (rf) getline(*rf.stream(), cmd); + } + for (unsigned i = 0; i < cmd.size(); ++i) + if (cmd[i] == 0) cmd[i] = ' '; + cerr << "Received SIGCHLD(" << signo << ")\n"; + if (cmd.size()) + cerr << " Parent command line: " << cmd << endl; + else + cerr << " Parent command line not available!\n"; + // reap zombies + bool should_exit = false; + while (waitpid(-1, &status, WNOHANG) > 0) { + cerr << " Child status: " << status << (status ? " [FAILURE]" : " [OK]") << endl; + if (status) should_exit = true; + } + if (should_exit) { + cerr << "Exiting on account of non-zero child exit code...\n"; + exit(1); + } +} + +void setup_child_process_handler() { + if (child_need_init == true) { + child_need_init = false; + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_NOCLDSTOP; + sa.sa_handler = metric_child_signal_handler; + sigaction(SIGCHLD, &sa, NULL); + } +} + struct NScoreServer { NScoreServer(const std::string& cmd); ~NScoreServer(); @@ -27,6 +69,7 @@ struct NScoreServer { }; NScoreServer::NScoreServer(const string& cmd) { + setup_child_process_handler(); cerr << "Invoking " << cmd << " ..." << endl; if (pipe(p2c) < 0) { perror("pipe"); exit(1); } if (pipe(c2p) < 0) { perror("pipe"); exit(1); } |