00001
00002
00003
00004
00005 #include "moses/TranslationModel/UG/generic/threading/ug_thread_pool.h"
00006 #include "moses/thread_safe_container.h"
00007 #include "ug_bitext.h"
00008 #include "ug_lru_cache.h"
00009
00010 namespace Moses {
00011 namespace sapt {
00012
00013 template<typename Token>
00014 struct StatsCollector
00015 {
00016 typedef lru_cache::LRU_Cache< uint64_t, pstats > hcache_t;
00017 typedef ThreadSafeContainer<uint64_t, SPTR<pstats> > pcache_t;
00018 typedef map<uint64_t, SPTR<pstats> > lcache_t;
00019 SPTR<Bitext<Token> const> bitext;
00020 sampling_method method;
00021 size_t sample_size;
00022 SPTR<SamplingBias const> bias;
00023 hcache_t* hcache;
00024 pcache_t* pcache;
00025 size_t pcache_th;
00026 SPTR<lcache_t> lcache;
00027 ug::ThreadPool* tpool;
00028
00029 StatsCollector(SPTR<Bitext<Token> > xbitext,
00030 SPTR<SamplingBias> const xbias)
00031 : method(ranked_sampling)
00032 , sample_size(100)
00033 , bias(xbias)
00034 , hcache(NULL)
00035 , pcache(NULL)
00036 , pcache_th(10000)
00037 , tpool(NULL)
00038 {
00039 bitext = xbitext;
00040 }
00041
00042 void
00043 process(typename TSA<Token>::tree_iterator& m,
00044 typename TSA<Token>::tree_iterator& r)
00045 {
00046 if (!lcache) lcache.reset(new lcache_t);
00047 if (m.down())
00048 {
00049 do
00050 {
00051 if (!r.extend(m.getToken(-1)->id())) continue;
00052 this->process(m, r);
00053 uint64_t pid = r.getPid();
00054 SPTR<pstats> stats;
00055 if (hcache) stats = hcache->get(pid);
00056 if (!stats && pcache)
00057 {
00058 SPTR<pstats> const* foo = pcache->get(pid);
00059 if (foo) stats = *foo;
00060 }
00061 if (!stats)
00062 {
00063 BitextSampler<Token> s(bitext.get(), r, bias, sample_size, method);
00064 stats = s.stats();
00065 if (hcache) hcache->set(pid,stats);
00066 if (pcache && r.ca() >= pcache_th) pcache->set(pid,stats);
00067 if (tpool) tpool->add(s);
00068 else s();
00069 }
00070 (*lcache)[pid] = stats;
00071 r.up();
00072 }
00073 while (m.over());
00074 m.up();
00075 }
00076 }
00077 };
00078 }
00079 }
00080
00081 #if 0
00082 #endif
00083