More fiddling with TargetPhraseCollectionCache.

This commit is contained in:
Ulrich Germann 2015-10-17 16:37:15 +01:00
parent cdcafd817b
commit a41027fb1d
2 changed files with 84 additions and 12 deletions

View File

@ -12,31 +12,96 @@ namespace Moses
m_capacity = capacity;
}
bool
sancheck(TPCollWrapper const* first, TPCollWrapper const* last, size_t count)
{
if (first == NULL)
{
UTIL_THROW_IF2(last != NULL || count != 0, "queue error");
return true;
}
size_t s = 0;
for (TPCollWrapper const* x = first; x; x = x->next)
{
std::cerr << ++s << "/" << count << " "
<< first << " "
<< x->prev << " " << x << " " << x->next << " "
<< last << std::endl;
}
std::cerr << std::string(80,'-') << std::endl;
// while (x != last && s < count)
// {
// UTIL_THROW_IF2(x->next == NULL, "queue error");
// x = x->next;
// ++s;
// std::cerr << x << " " << s << "/" << count << std::endl;
// }
// std::cerr << x << " " << s << "/" << count << std::endl;
// UTIL_THROW_IF2(x != last, "queue error");
// UTIL_THROW_IF2(s != count, "queue error");
// x = last; s = 1;
// while (x != first && s++ < count)
// {
// UTIL_THROW_IF2(x->prev == NULL, "queue error");
// x = x->prev;
// }
// UTIL_THROW_IF2(x != first, "queue error");
// UTIL_THROW_IF2(s != count, "queue error");
return true;
}
/// remove a TPC from the "doomed" queue
void
TPCollCache
::remove_from_queue(TPCollWrapper* x)
{
// caller must lock!
if (m_doomed_first != x && x->prev == NULL)
{ // not in the queue
UTIL_THROW_IF2(x->next, "queue error");
return;
}
sancheck(m_doomed_first, m_doomed_last, m_doomed_count);
std::cerr << "Removing " << x << std::endl;
if (m_doomed_first == x)
m_doomed_first = x->next;
else (x->prev->next) = x->next;
if (m_doomed_last == x)
else x->prev->next = x->next;
if (m_doomed_last == x)
m_doomed_last = x->prev;
else x->next->prev = x->prev;
x->next = x->prev = NULL;
--m_doomed_count;
// sancheck(m_doomed_first, m_doomed_last, m_doomed_count);
}
void
TPCollCache
::add_to_queue(TPCollWrapper* x)
{
// sancheck(m_doomed_first, m_doomed_last, m_doomed_count);
// caller must lock!
x->prev = m_doomed_last;
if (!m_doomed_first) m_doomed_first = x;
(m_doomed_last ? m_doomed_last->next : m_doomed_last) = x;
++m_doomed_count;
if (!m_doomed_first)
m_doomed_first = x;
if (m_doomed_last) m_doomed_last->next = x;
m_doomed_last = x;
++m_doomed_count;
// sancheck(m_doomed_first, m_doomed_last, m_doomed_count);
}
TPCollWrapper*
@ -53,6 +118,7 @@ namespace Moses
std::pair<cache_t::iterator,bool> foo = m_cache.insert(e);
if (foo.second) foo.first->second = new TPCollWrapper(key, revision);
m = foo.first;
// ++m->second->refCount;
}
else
{
@ -73,9 +139,10 @@ namespace Moses
void
TPCollCache
::release(TPCollWrapper* ptr)
::release(TPCollWrapper const* ptr)
{
if (!ptr) return;
std::cerr << "Releasing " << ptr->key << " (" << ptr->refCount << ")" << std::endl;
if (--ptr->refCount == 0)
{
boost::unique_lock<boost::shared_mutex> lock(m_lock);
@ -93,14 +160,17 @@ namespace Moses
// boost::upgrade_to_unique_lock<boost::shared_mutex> xlock(lock);
m_cache.erase(m);
}
delete x;
std::cerr << "Deleting " << x->key << " " << x->refCount << std::endl;
// delete x;
}
add_to_queue(ptr);
add_to_queue(const_cast<TPCollWrapper*>(ptr));
}
} // TPCollCache::release(...)
TPCollWrapper::
TPCollWrapper(size_t revision_, uint64_t key_)
TPCollWrapper(uint64_t key_, size_t revision_)
: refCount(0), prev(NULL), next(NULL)
, revision(revision_), key(key_)
{ }

View File

@ -15,7 +15,9 @@ namespace Moses
: public TargetPhraseCollection
{
friend class TPCollCache;
boost::atomic<uint32_t> refCount; // reference count
friend class Mmsapt;
mutable boost::atomic<uint32_t> refCount; // reference count
public:
TPCollWrapper* prev; // ... in queue of TPCollWrappers used recently
TPCollWrapper* next; // ... in queue of TPCollWrappers used recently
public:
@ -27,7 +29,7 @@ namespace Moses
#else
timeval tstamp; // last use
#endif
TPCollWrapper(size_t r, uint64_t const k);
TPCollWrapper(uint64_t const key, size_t const rev);
~TPCollWrapper();
};
@ -53,7 +55,7 @@ namespace Moses
get(uint64_t key, size_t revision);
void
release(TPCollWrapper* tpc);
release(TPCollWrapper const* tpc);
};