From ec1ef78d51921cdb64d0078293fe2e19b161eca2 Mon Sep 17 00:00:00 2001 From: Ulrich Germann Date: Wed, 4 Nov 2015 18:12:49 +0000 Subject: [PATCH] Reduction in number of #ifdef HAVE_XMLRPC_C, as per Hieu's request. --- moses/ContextScope.h | 47 ++++----------------------- moses/ExportInterface.cpp | 13 +++----- moses/TranslationModel/UG/mmsapt.cpp | 7 ++-- moses/parameters/AllOptions.cpp | 1 - moses/parameters/AllOptions.h | 3 -- moses/parameters/CubePruningOptions.h | 2 -- moses/parameters/NBestOptions.h | 2 -- moses/parameters/OptionsBaseClass.cpp | 9 +++-- moses/parameters/OptionsBaseClass.h | 7 ++-- moses/parameters/ReportingOptions.cpp | 3 +- moses/parameters/ReportingOptions.h | 3 +- moses/parameters/SearchOptions.h | 2 -- moses/server/PackScores.cpp | 3 -- moses/server/PackScores.h | 10 ++---- moses/server/Server.cpp | 16 ++------- moses/server/Server.h | 6 ++-- moses/server/TranslationRequest.cpp | 15 +++++++-- moses/xmlrpc-c.h | 5 +++ run-regtests.sh | 7 +++- 19 files changed, 52 insertions(+), 109 deletions(-) create mode 100644 moses/xmlrpc-c.h diff --git a/moses/ContextScope.h b/moses/ContextScope.h index 4e1583a78..d98b96086 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -1,4 +1,4 @@ -// -*- c++ -*- +// -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*- // A class to store "local" information (such as task-specific caches). // The idea is for each translation task to have a scope, which stores // shared pointers to task-specific objects such as caches and priors. @@ -14,16 +14,12 @@ // for some reason, the xmlrpc_c headers must be included AFTER the // boost thread-related ones ... -#ifdef HAVE_XMLRPC_C -#include -#include -#include -#endif - +#include "xmlrpc-c.h" #include #include -// #include "thread_safe_container.h" +#include "TypeDef.h" +#include "Util.h" namespace Moses { @@ -40,25 +36,6 @@ protected: #endif SPTR const> m_context_weights; public: - // class write_access - // { - // boost::unique_lock m_lock; - // public: - - // write_access(boost::shared_mutex& lock) - // : m_lock(lock) - // { } - - // write_access(write_access& other) - // { - // swap(m_lock, other.m_lock); - // } - // }; - - // write_access lock() const - // { - // return write_access(m_lock); - // } template boost::shared_ptr const& @@ -109,22 +86,10 @@ public: m_scratchpad = other.m_scratchpad; } -#ifdef HAVE_XMLRPC_C SPTR const> - GetContextWeights(xmlrpc_c::value const* spec = NULL) { - if (spec && m_context_weights == NULL) { - boost::unique_lock lock(m_lock); - SPTR > M(new std::map); - - typedef std::map tmap; - tmap const tmp = static_cast(xmlrpc_c::value_struct(*spec)); - for(tmap::const_iterator m = tmp.begin(); m != tmp.end(); ++m) - (*M)[m->first] = xmlrpc_c::value_double(m->second); - m_context_weights = M; - } + GetContextWeights() { return m_context_weights; } -#endif bool SetContextWeights(std::string const& spec) { @@ -147,7 +112,9 @@ public: bool SetContextWeights(SPTR const> const& w) { if (m_context_weights) return false; +#ifdef WITH_THREADS boost::unique_lock lock(m_lock); +#endif m_context_weights = w; return true; } diff --git a/moses/ExportInterface.cpp b/moses/ExportInterface.cpp index 216eba335..f35efac15 100644 --- a/moses/ExportInterface.cpp +++ b/moses/ExportInterface.cpp @@ -60,13 +60,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #endif #include "ExportInterface.h" -#ifdef HAVE_XMLRPC_C -#include -#include -#include -#include "server/Server.h" -#endif - +#include "moses/server/Server.h" using namespace std; using namespace Moses; @@ -148,6 +142,9 @@ run_as_server() #ifdef HAVE_XMLRPC_C MosesServer::Server server(params); return server.run(); // actually: don't return. see Server::run() +#else + UTIL_THROW2("Moses was compiled without xmlrpc-c. " + << "No server functionality available."); #endif } @@ -229,10 +226,8 @@ batch_run() if (context_window) task->SetContextWindow(context_window); -#ifdef HAVE_XMLRPC_C if (context_weights != "" && !task->GetScope()->GetContextWeights()) task->GetScope()->SetContextWeights(context_weights); -#endif // Allow for (sentence-)context-specific processing prior to // decoding. This can be used, for example, for context-sensitive diff --git a/moses/TranslationModel/UG/mmsapt.cpp b/moses/TranslationModel/UG/mmsapt.cpp index 4fa53d0f4..b631c44a1 100644 --- a/moses/TranslationModel/UG/mmsapt.cpp +++ b/moses/TranslationModel/UG/mmsapt.cpp @@ -811,11 +811,9 @@ namespace Moses Mmsapt:: setup_bias(ttasksptr const& ttask) { -#ifdef HAVE_XMLRPC_C - - // VERBOSE(2,"Setting up bias at " << HERE << std::endl); SPTR const& scope = ttask->GetScope(); - SPTR context = scope->get(btfix.get(), true); + SPTR context; + context = scope->get(btfix.get(), true); if (context->bias) return; // bias weights specified with the session? @@ -853,7 +851,6 @@ namespace Moses context->bias_log = m_bias_log; context->bias->loglevel = m_bias_loglevel; } -#endif } void diff --git a/moses/parameters/AllOptions.cpp b/moses/parameters/AllOptions.cpp index 3f481e77d..eb8e502da 100644 --- a/moses/parameters/AllOptions.cpp +++ b/moses/parameters/AllOptions.cpp @@ -90,7 +90,6 @@ namespace Moses if (!output.update(param)) return false; return sanity_check(); } - #endif bool diff --git a/moses/parameters/AllOptions.h b/moses/parameters/AllOptions.h index c5f6e44e2..079099ef3 100644 --- a/moses/parameters/AllOptions.h +++ b/moses/parameters/AllOptions.h @@ -35,10 +35,7 @@ namespace Moses AllOptions() {} AllOptions(Parameter const& param); -#ifdef HAVE_XMLRPC_C bool update(std::mapconst& param); -#endif - bool NBestDistinct() const; }; diff --git a/moses/parameters/CubePruningOptions.h b/moses/parameters/CubePruningOptions.h index 961b1a479..9f85520ea 100644 --- a/moses/parameters/CubePruningOptions.h +++ b/moses/parameters/CubePruningOptions.h @@ -18,10 +18,8 @@ namespace Moses CubePruningOptions(Parameter const& param); CubePruningOptions() {}; -#ifdef HAVE_XMLRPC_C bool update(std::mapconst& params); -#endif }; } diff --git a/moses/parameters/NBestOptions.h b/moses/parameters/NBestOptions.h index 05da48508..0fe77dc3a 100644 --- a/moses/parameters/NBestOptions.h +++ b/moses/parameters/NBestOptions.h @@ -24,9 +24,7 @@ struct NBestOptions : public OptionsBaseClass bool init(Parameter const& param); -#ifdef HAVE_XMLRPC_C bool update(std::mapconst& param); -#endif }; diff --git a/moses/parameters/OptionsBaseClass.cpp b/moses/parameters/OptionsBaseClass.cpp index 0bb914417..148aa5d24 100644 --- a/moses/parameters/OptionsBaseClass.cpp +++ b/moses/parameters/OptionsBaseClass.cpp @@ -1,8 +1,7 @@ -// -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*- +// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*- #include "OptionsBaseClass.h" +namespace Moses { -namespace Moses -{ #ifdef HAVE_XMLRPC_C bool OptionsBaseClass:: @@ -10,7 +9,9 @@ namespace Moses { return true; } +#endif +#ifdef HAVE_XMLRPC_C bool OptionsBaseClass:: check(std::map const& param, @@ -19,7 +20,5 @@ namespace Moses std::map::const_iterator m; return (param.find(key) != param.end()); } - #endif - } diff --git a/moses/parameters/OptionsBaseClass.h b/moses/parameters/OptionsBaseClass.h index 55713a174..71f9fa77a 100644 --- a/moses/parameters/OptionsBaseClass.h +++ b/moses/parameters/OptionsBaseClass.h @@ -1,8 +1,6 @@ // -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*- #pragma once -#ifdef HAVE_XMLRPC_C -#include -#endif +#include "moses/xmlrpc-c.h" #include #include namespace Moses @@ -12,10 +10,9 @@ namespace Moses #ifdef HAVE_XMLRPC_C virtual bool update(std::mapconst& params); - +#endif bool check(std::map const& param, std::string const key); -#endif }; } diff --git a/moses/parameters/ReportingOptions.cpp b/moses/parameters/ReportingOptions.cpp index fa06eb06f..95f52770b 100644 --- a/moses/parameters/ReportingOptions.cpp +++ b/moses/parameters/ReportingOptions.cpp @@ -68,10 +68,11 @@ namespace Moses { #ifdef HAVE_XMLRPC_C bool ReportingOptions:: - update(std::mapconst& param) + update(std::mapconst& param) { ReportAllFactors = check(param, "report-all-factors"); return true; } #endif + } diff --git a/moses/parameters/ReportingOptions.h b/moses/parameters/ReportingOptions.h index 0c4c2ac58..eb6b29959 100644 --- a/moses/parameters/ReportingOptions.h +++ b/moses/parameters/ReportingOptions.h @@ -52,9 +52,8 @@ namespace Moses } #ifdef HAVE_XMLRPC_C - bool update(std::mapconst& param); + bool update(std::mapconst& param); #endif - }; } diff --git a/moses/parameters/SearchOptions.h b/moses/parameters/SearchOptions.h index 2b2f39a65..875678cc9 100644 --- a/moses/parameters/SearchOptions.h +++ b/moses/parameters/SearchOptions.h @@ -43,10 +43,8 @@ namespace Moses return early_discarding_threshold != -std::numeric_limits::infinity(); } -#ifdef HAVE_XMLRPC_C bool update(std::mapconst& params); -#endif }; diff --git a/moses/server/PackScores.cpp b/moses/server/PackScores.cpp index db5710132..4ec6109d3 100644 --- a/moses/server/PackScores.cpp +++ b/moses/server/PackScores.cpp @@ -1,5 +1,4 @@ // -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*- -#ifdef HAVE_XMLRPC_C #include "PackScores.h" #include "moses/FF/StatefulFeatureFunction.h" #include "moses/FF/StatelessFeatureFunction.h" @@ -44,5 +43,3 @@ PackScores(ScoreComponentCollection const& S) return xmlrpc_c::value_struct(M); } } -#endif - diff --git a/moses/server/PackScores.h b/moses/server/PackScores.h index 98c57059d..5d875bc3b 100644 --- a/moses/server/PackScores.h +++ b/moses/server/PackScores.h @@ -1,16 +1,10 @@ // -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*- -#ifdef HAVE_XMLRPC_C #pragma once #include #include "moses/FF/FeatureFunction.h" #include "moses/ScoreComponentCollection.h" namespace Moses { - -xmlrpc_c::value -PackScores(ScoreComponentCollection const& S); - + xmlrpc_c::value + PackScores(ScoreComponentCollection const& S); } - - -#endif diff --git a/moses/server/Server.cpp b/moses/server/Server.cpp index 8f8c7d6a9..b5814e969 100644 --- a/moses/server/Server.cpp +++ b/moses/server/Server.cpp @@ -1,11 +1,9 @@ // -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*- #include "Server.h" - namespace MosesServer { Server:: Server(Moses::Parameter& params) -#ifdef HAVE_XMLRPC_C : m_server_options(params), m_updater(new Updater), m_optimizer(new Optimizer), @@ -17,15 +15,11 @@ namespace MosesServer m_registry.addMethod("optimize", m_optimizer); m_registry.addMethod("close_session", m_close_session); } -#else - { } -#endif int Server:: run() { -#ifdef HAVE_XMLRPC_C xmlrpc_c::serverAbyss myAbyssServer (xmlrpc_c::serverAbyss::constrOpt() .registryP(&m_registry) @@ -42,12 +36,8 @@ namespace MosesServer } else myAbyssServer.run(); - std::cerr << "xmlrpc_c::serverAbyss.run() returned but should not." << std::endl; - // #pragma message("BUILDING MOSES WITH SERVER SUPPORT") -#else - // #pragma message("BUILDING MOSES WITHOUT SERVER SUPPORT") - std::cerr << "Moses was compiled without server support." << std::endl; -#endif + std::cerr << "xmlrpc_c::serverAbyss.run() returned but it should not." + << std::endl; return 1; } @@ -71,6 +61,4 @@ namespace MosesServer { return m_session_cache.erase(session_id); } - - } diff --git a/moses/server/Server.h b/moses/server/Server.h index 81fbf2953..4afbf5d91 100644 --- a/moses/server/Server.h +++ b/moses/server/Server.h @@ -1,11 +1,12 @@ // -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*- #pragma once -#ifdef HAVE_XMLRPC_C #include "moses/TypeDef.h" + #ifdef WITH_THREADS #include #include "moses/ThreadPool.h" #endif + #include #include #include @@ -14,7 +15,6 @@ #include "Updater.h" #include "CloseSession.h" #include "Session.h" -#endif #include "moses/parameters/ServerOptions.h" namespace MosesServer @@ -23,13 +23,11 @@ namespace MosesServer { Moses::ServerOptions m_server_options; SessionCache m_session_cache; -#ifdef HAVE_XMLRPC_C xmlrpc_c::registry m_registry; xmlrpc_c::methodPtr const m_updater; xmlrpc_c::methodPtr const m_optimizer; xmlrpc_c::methodPtr const m_translator; xmlrpc_c::methodPtr const m_close_session; -#endif public: Server(Moses::Parameter& params); diff --git a/moses/server/TranslationRequest.cpp b/moses/server/TranslationRequest.cpp index 5f7841ce0..83463dcae 100644 --- a/moses/server/TranslationRequest.cpp +++ b/moses/server/TranslationRequest.cpp @@ -35,6 +35,17 @@ create(Translator* translator, xmlrpc_c::paramList const& paramList, return ret; } +void +SetContextWeights(Moses::ContextScope& s, xmlrpc_c::value const& w) +{ + SPTR > M(new std::map); + typedef std::map tmap; + tmap const tmp = static_cast(xmlrpc_c::value_struct(w)); + for(tmap::const_iterator m = tmp.begin(); m != tmp.end(); ++m) + (*M)[m->first] = xmlrpc_c::value_double(m->second); + s.SetContextWeights(M); +} + void TranslationRequest:: Run() @@ -55,8 +66,8 @@ Run() // settings within the session scope param_t::const_iterator si = params.find("context-weights"); - if (si != params.end()) m_scope->GetContextWeights(&si->second); - + if (si != params.end()) SetContextWeights(*m_scope, si->second); + Moses::StaticData const& SD = Moses::StaticData::Instance(); //Make sure alternative paths are retained, if necessary diff --git a/moses/xmlrpc-c.h b/moses/xmlrpc-c.h new file mode 100644 index 000000000..6f85a189c --- /dev/null +++ b/moses/xmlrpc-c.h @@ -0,0 +1,5 @@ +#ifdef HAVE_XMLRPC_C +#include +#else +namespace xmlrpc_c { class value; } +#endif diff --git a/run-regtests.sh b/run-regtests.sh index e5466223b..8b64eac22 100755 --- a/run-regtests.sh +++ b/run-regtests.sh @@ -6,4 +6,9 @@ set -e -o pipefail git submodule init git submodule update regtest -./bjam -j$(nproc) --with-irstlm=./opt --with-boost=./opt --with-cmph=./opt --with-xmlrpc-c=./opt --with-regtest=./regtest $@ + +# test compilation without xmlrpc-c +./bjam -j$(nproc) --with-irstlm=./opt --with-boost=./opt --with-cmph=./opt --no-xmlrpc-c --with-regtest=./regtest -a -q $@ || exit $? + +# test compilation with xmlrpc-c +./bjam -j$(nproc) --with-irstlm=./opt --with-boost=./opt --with-cmph=./opt --with-xmlrpc-c=./opt --with-regtest=./regtest -a -q $@