00001
00002 #include <boost/thread/locks.hpp>
00003 #include "ug_bitext_pstats.h"
00004
00005 namespace sapt
00006 {
00007
00008 #if UG_BITEXT_TRACK_ACTIVE_THREADS
00009 ThreadSafeCounter pstats::active;
00010 #endif
00011
00012 pstats::
00013 pstats(bool const track_sids) : raw_cnt(0), sample_cnt(0), good(0), sum_pairs(0), in_progress(0), track_sids(track_sids)
00014 {
00015 for (int i = 0; i <= LRModel::NONE; ++i)
00016 ofwd[i] = obwd[i] = 0;
00017 }
00018
00019 pstats::
00020 ~pstats()
00021 {
00022 #if UG_BITEXT_TRACK_ACTIVE_THREADS
00023
00024 try { --active; } catch (...) {}
00025 #endif
00026 }
00027
00028 void
00029 pstats::
00030 register_worker()
00031 {
00032 this->lock.lock();
00033 ++this->in_progress;
00034 this->lock.unlock();
00035 }
00036
00037 void
00038 pstats::
00039 release()
00040 {
00041 this->lock.lock();
00042 if (this->in_progress-- == 1)
00043 this->ready.notify_all();
00044 this->lock.unlock();
00045 }
00046
00047 void
00048 pstats
00049 ::count_sample(int const docid, size_t const num_pairs,
00050 int const po_fwd, int const po_bwd)
00051 {
00052 boost::lock_guard<boost::mutex> guard(lock);
00053 ++sample_cnt;
00054 if (num_pairs == 0) return;
00055 ++good;
00056 sum_pairs += num_pairs;
00057 ++ofwd[po_fwd];
00058 ++obwd[po_bwd];
00059 if (docid >= 0)
00060 {
00061
00062 ++indoc[docid];
00063 }
00064 }
00065
00066 size_t
00067 pstats::
00068 add(uint64_t pid, float const w, float const b,
00069 std::vector<unsigned char> const& a,
00070 uint32_t const cnt2,
00071 uint32_t fwd_o,
00072 uint32_t bwd_o, int const docid, uint32_t const sid)
00073 {
00074 boost::lock_guard<boost::mutex> guard(this->lock);
00075 jstats& entry = this->trg[pid];
00076 size_t ret = entry.add(w, b, a, cnt2, fwd_o, bwd_o, docid, sid, track_sids);
00077 if (this->good < entry.rcnt())
00078 {
00079 UTIL_THROW(util::Exception, "more joint counts than good counts:"
00080 << entry.rcnt() << "/" << this->good << "!");
00081 }
00082 return ret;
00083 }
00084
00085 void
00086 pstats::
00087 wait() const
00088 {
00089 boost::unique_lock<boost::mutex> lock(this->lock);
00090 while (this->in_progress)
00091 this->ready.wait(lock);
00092 }
00093
00094 }
00095