00001
00002 #pragma once
00003 #ifndef NO_MOSES
00004 namespace sapt {
00005
00006 template<typename Token>
00007 SPTR<pstats>
00008 Bitext<Token>::
00009 lookup(ttasksptr const& ttask, iter const& phrase, int max_sample) const
00010 {
00011 SPTR<pstats> ret = prep2(ttask, phrase, max_sample);
00012 UTIL_THROW_IF2(!ret, "Got NULL pointer where I expected a valid pointer.");
00013
00014
00015 if (m_num_workers <= 1)
00016 {
00017 boost::unique_lock<boost::shared_mutex> guard(m_lock);
00018 typename agenda::worker(*this->ag)();
00019 }
00020 else
00021 {
00022 boost::unique_lock<boost::mutex> lock(ret->lock);
00023 while (ret->in_progress)
00024 ret->ready.wait(lock);
00025 }
00026 return ret;
00027 }
00028
00029
00030 template<typename Token>
00031 void
00032 Bitext<Token>::
00033 prep(ttasksptr const& ttask, iter const& phrase, bool const track_sids) const
00034 {
00035 prep2(ttask, phrase, track_sids, m_default_sample_size);
00036 }
00037
00038
00039
00040
00041
00042
00043 template<typename Token>
00044 SPTR<pstats>
00045 Bitext<Token>
00046 ::prep2
00047 ( ttasksptr const& ttask, iter const& phrase, bool const track_sids,
00048 int max_sample) const
00049 {
00050 if (max_sample < 0) max_sample = m_default_sample_size;
00051 SPTR<SamplingBias> bias;
00052 SPTR<Moses::ContextScope> scope = ttask->GetScope();
00053 SPTR<ContextForQuery> context = scope->get<ContextForQuery>(this);
00054 if (context) bias = context->bias;
00055 SPTR<pstats::cache_t> cache;
00056
00057
00058
00059 if (max_sample == int(m_default_sample_size)
00060 && phrase.approxOccurrenceCount() > m_pstats_cache_threshold)
00061 {
00062 cache = (phrase.root == I1.get()
00063 ? (bias ? context->cache1 : m_cache1)
00064 : (bias ? context->cache2 : m_cache2));
00065 }
00066 SPTR<pstats> ret;
00067 SPTR<pstats> const* cached;
00068
00069 if (cache && (cached = cache->get(phrase.getPid(), ret)) && *cached)
00070 return *cached;
00071 boost::unique_lock<boost::shared_mutex> guard(m_lock);
00072 if (!ag)
00073 {
00074 ag.reset(new agenda(*this));
00075 if (m_num_workers > 1)
00076 ag->add_workers(m_num_workers);
00077 }
00078 ret = ag->add_job(this, phrase, max_sample, bias, track_sids);
00079 if (cache) cache->set(phrase.getPid(),ret);
00080 UTIL_THROW_IF2(ret == NULL, "Couldn't schedule sampling job.");
00081 return ret;
00082 }
00083
00084
00085
00086 }
00087 #endif