Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ug_bitext_pstats.cc « mm « UG « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e603def96158a58628983456e960a334309129d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2  -*-
#include <boost/thread/locks.hpp>
#include "ug_bitext_pstats.h"

namespace sapt
{

#if UG_BITEXT_TRACK_ACTIVE_THREADS
  ThreadSafeCounter pstats::active;
#endif

  pstats::
  pstats() : raw_cnt(0), sample_cnt(0), good(0), sum_pairs(0), in_progress(0)
  {
    for (int i = 0; i <= LRModel::NONE; ++i)
      ofwd[i] = obwd[i] = 0;
  }

  pstats::
  ~pstats()
  {
#if UG_BITEXT_TRACK_ACTIVE_THREADS
    // counter may not exist any more at destruction time, so try ... catch
    try { --active; } catch (...) {}
#endif
  }

  void
  pstats::
  register_worker()
  {
    this->lock.lock();
    ++this->in_progress;
    this->lock.unlock();
  }

  void
  pstats::
  release()
  {
    this->lock.lock();
    if (this->in_progress-- == 1) // last one - >we're done
      this->ready.notify_all();
    this->lock.unlock();
  }

  void
  pstats
  ::count_sample(int const docid, size_t const num_pairs,
		 int const po_fwd, int const po_bwd)
  {
    boost::lock_guard<boost::mutex> guard(lock);
    ++sample_cnt;
    if (num_pairs == 0) return;
    ++good;
    sum_pairs += num_pairs;
    ++ofwd[po_fwd];
    ++obwd[po_bwd];
    if (docid >= 0)
      {
	// while (int(indoc.size()) <= docid) indoc.push_back(0);
	++indoc[docid];
      }
  }

  size_t
  pstats::
  add(uint64_t pid, float const w, float const b,
      std::vector<unsigned char> const& a,
      uint32_t const cnt2,
      uint32_t fwd_o,
      uint32_t bwd_o, int const docid, int const sid)
  {
    boost::lock_guard<boost::mutex> guard(this->lock);
    jstats& entry = this->trg[pid];
    size_t ret = entry.add(w, b, a, cnt2, fwd_o, bwd_o, docid, sid);
    if (this->good < entry.rcnt())
      {
        UTIL_THROW(util::Exception, "more joint counts than good counts:"
                   << entry.rcnt() << "/" << this->good << "!");
      }
    return ret;
  }

  void 
  pstats::
  wait() const
  {
    boost::unique_lock<boost::mutex> lock(this->lock);
    while (this->in_progress)
      this->ready.wait(lock);
  }

} // end of namespace sapt