2015-11-04 21:12:49 +03:00
|
|
|
// -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
|
2015-03-28 17:43:21 +03:00
|
|
|
// 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.
|
2015-04-30 08:05:11 +03:00
|
|
|
// Since these objects are referenced via shared pointers, sopes can
|
2015-03-28 17:43:21 +03:00
|
|
|
// share information.
|
2015-04-30 08:05:11 +03:00
|
|
|
#pragma once
|
2015-03-28 17:43:21 +03:00
|
|
|
|
|
|
|
#ifdef WITH_THREADS
|
|
|
|
#include <boost/thread/shared_mutex.hpp>
|
|
|
|
#include <boost/thread/locks.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#endif
|
|
|
|
|
2015-10-22 02:00:41 +03:00
|
|
|
// for some reason, the xmlrpc_c headers must be included AFTER the
|
2015-10-21 02:56:29 +03:00
|
|
|
// boost thread-related ones ...
|
2015-11-04 21:12:49 +03:00
|
|
|
#include "xmlrpc-c.h"
|
2015-10-21 02:56:29 +03:00
|
|
|
|
2015-03-30 03:20:17 +03:00
|
|
|
#include <map>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2015-11-04 21:12:49 +03:00
|
|
|
#include "TypeDef.h"
|
|
|
|
#include "Util.h"
|
2015-03-28 17:43:21 +03:00
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2015-05-02 13:45:24 +03:00
|
|
|
class ContextScope
|
|
|
|
{
|
2015-05-10 12:19:26 +03:00
|
|
|
protected:
|
|
|
|
typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t;
|
|
|
|
typedef scratchpad_t::iterator iter_t;
|
|
|
|
typedef scratchpad_t::value_type entry_t;
|
|
|
|
typedef scratchpad_t::const_iterator const_iter_t;
|
|
|
|
scratchpad_t m_scratchpad;
|
2015-05-04 10:24:36 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-05-10 12:19:26 +03:00
|
|
|
mutable boost::shared_mutex m_lock;
|
2015-05-04 10:24:36 +03:00
|
|
|
#endif
|
2015-10-28 16:01:11 +03:00
|
|
|
SPTR<std::map<std::string,float> const> m_context_weights;
|
2015-05-10 12:19:26 +03:00
|
|
|
public:
|
2015-12-12 19:23:37 +03:00
|
|
|
typedef boost::shared_ptr<ContextScope> ptr;
|
2015-05-10 12:19:26 +03:00
|
|
|
template<typename T>
|
|
|
|
boost::shared_ptr<void> const&
|
|
|
|
set(void const* const key, boost::shared_ptr<T> const& val) {
|
2015-05-04 10:24:36 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-05-10 12:19:26 +03:00
|
|
|
boost::unique_lock<boost::shared_mutex> lock(m_lock);
|
2015-05-04 10:24:36 +03:00
|
|
|
#endif
|
2015-05-10 12:19:26 +03:00
|
|
|
return (m_scratchpad[key] = val);
|
|
|
|
}
|
2015-03-28 17:43:21 +03:00
|
|
|
|
2015-05-10 12:19:26 +03:00
|
|
|
template<typename T>
|
|
|
|
boost::shared_ptr<T> const
|
|
|
|
get(void const* key, bool CreateNewIfNecessary=false) {
|
2015-05-04 10:24:36 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-05-10 12:19:26 +03:00
|
|
|
using boost::shared_mutex;
|
|
|
|
using boost::upgrade_lock;
|
|
|
|
// T const* key = reinterpret_cast<T const*>(xkey);
|
|
|
|
upgrade_lock<shared_mutex> lock(m_lock);
|
2015-05-04 10:24:36 +03:00
|
|
|
#endif
|
2015-05-10 12:19:26 +03:00
|
|
|
iter_t m = m_scratchpad.find(key);
|
|
|
|
boost::shared_ptr< T > ret;
|
|
|
|
if (m != m_scratchpad.end()) {
|
|
|
|
if (m->second == NULL && CreateNewIfNecessary) {
|
2015-05-04 10:24:36 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-05-10 12:19:26 +03:00
|
|
|
boost::upgrade_to_unique_lock<shared_mutex> xlock(lock);
|
2015-05-04 10:24:36 +03:00
|
|
|
#endif
|
2015-05-10 12:19:26 +03:00
|
|
|
m->second.reset(new T);
|
|
|
|
}
|
|
|
|
ret = boost::static_pointer_cast< T >(m->second);
|
2015-03-30 03:20:17 +03:00
|
|
|
return ret;
|
2015-03-28 17:43:21 +03:00
|
|
|
}
|
2015-05-10 12:19:26 +03:00
|
|
|
if (!CreateNewIfNecessary) return ret;
|
|
|
|
#ifdef WITH_THREADS
|
|
|
|
boost::upgrade_to_unique_lock<shared_mutex> xlock(lock);
|
|
|
|
#endif
|
|
|
|
ret.reset(new T);
|
|
|
|
m_scratchpad[key] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2015-03-28 17:43:21 +03:00
|
|
|
|
2015-05-02 13:45:24 +03:00
|
|
|
ContextScope() { }
|
2015-03-30 03:20:17 +03:00
|
|
|
|
2015-05-10 12:19:26 +03:00
|
|
|
ContextScope(ContextScope const& other) {
|
2015-05-04 10:24:36 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-05-02 13:45:24 +03:00
|
|
|
boost::unique_lock<boost::shared_mutex> lock1(this->m_lock);
|
|
|
|
boost::unique_lock<boost::shared_mutex> lock2(other.m_lock);
|
2015-05-04 10:24:36 +03:00
|
|
|
#endif
|
2015-05-02 13:45:24 +03:00
|
|
|
m_scratchpad = other.m_scratchpad;
|
|
|
|
}
|
2015-03-28 17:43:21 +03:00
|
|
|
|
2015-10-28 16:01:11 +03:00
|
|
|
SPTR<std::map<std::string,float> const>
|
2015-11-04 21:12:49 +03:00
|
|
|
GetContextWeights() {
|
2015-10-21 02:56:29 +03:00
|
|
|
return m_context_weights;
|
|
|
|
}
|
2015-10-31 20:14:47 +03:00
|
|
|
|
2015-10-28 13:26:41 +03:00
|
|
|
bool
|
|
|
|
SetContextWeights(std::string const& spec) {
|
|
|
|
if (m_context_weights) return false;
|
|
|
|
boost::unique_lock<boost::shared_mutex> lock(m_lock);
|
2015-10-28 16:01:11 +03:00
|
|
|
SPTR<std::map<std::string,float> > M(new std::map<std::string, float>);
|
2015-10-31 20:14:47 +03:00
|
|
|
|
2015-10-28 13:26:41 +03:00
|
|
|
// TO DO; This needs to be done with StringPiece.find, not Tokenize
|
|
|
|
// PRIORITY: low
|
|
|
|
std::vector<std::string> tokens = Tokenize(spec,":");
|
2015-10-31 20:14:47 +03:00
|
|
|
for (std::vector<std::string>::iterator it = tokens.begin();
|
|
|
|
it != tokens.end(); it++) {
|
2015-10-28 13:26:41 +03:00
|
|
|
std::vector<std::string> key_and_value = Tokenize(*it, ",");
|
2015-10-28 16:01:11 +03:00
|
|
|
(*M)[key_and_value[0]] = atof(key_and_value[1].c_str());
|
2015-10-28 13:26:41 +03:00
|
|
|
}
|
2015-10-28 16:01:11 +03:00
|
|
|
m_context_weights = M;
|
2015-10-28 13:26:41 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-31 20:14:47 +03:00
|
|
|
|
2015-10-28 14:18:06 +03:00
|
|
|
bool
|
2015-10-28 16:01:11 +03:00
|
|
|
SetContextWeights(SPTR<std::map<std::string,float> const> const& w) {
|
2015-10-28 14:18:06 +03:00
|
|
|
if (m_context_weights) return false;
|
2015-11-04 21:12:49 +03:00
|
|
|
#ifdef WITH_THREADS
|
2015-10-28 14:18:06 +03:00
|
|
|
boost::unique_lock<boost::shared_mutex> lock(m_lock);
|
2015-11-04 21:12:49 +03:00
|
|
|
#endif
|
2015-10-28 14:18:06 +03:00
|
|
|
m_context_weights = w;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:45:24 +03:00
|
|
|
};
|
2015-03-28 17:43:21 +03:00
|
|
|
|
|
|
|
};
|