/// Removes the least recently used element from the cache void remove_lru() const{ cachelock.lock(); KeyType keytoerase = lruage.back().key; // is the key I am invalidating in the cache? typename cache_type::iterator i = cache.find(keytoerase); if (i != cache.end()) { // drop it from the lru list delete i->second; cache.erase(i); } cachelock.unlock(); }
inline cusparseHandle_t cusparse_handle(const command_queue &q) { typedef std::shared_ptr<std::remove_pointer<cusparseHandle_t>::type> smart_handle; typedef vex::detail::object_cache<vex::detail::index_by_context, smart_handle> cache_type; static cache_type cache; auto h = cache.find(q); if (h == cache.end()) { select_context(q); cusparseHandle_t handle; cuda_check( cusparseCreate(&handle) ); cuda_check( cusparseSetStream(handle, q.raw()) ); h = cache.insert(q, smart_handle(handle, detail::deleter())); } return h->second.get(); }
/// Updates the cache with this new value void update_cache(const KeyType &key, const ValueType &val) const{ cachelock.lock(); typename cache_type::iterator i = cache.find(key); // create a new entry if (i == cache.end()) { cachelock.unlock(); // if we are out of room, remove the lru entry if (cache.size() >= maxcache) remove_lru(); cachelock.lock(); // insert the element, remember the iterator so we can push it // straight to the LRU list std::pair<typename cache_type::iterator, bool> ret = cache.insert(std::make_pair(key, new lru_entry_type(key, val))); if (ret.second) lruage.push_front(*(ret.first->second)); } else { // modify entry in place i->second->value = val; // swap to front of list //boost::swap_nodes(lru_list_type::s_iterator_to(i->second), lruage.begin()); lruage.erase(lru_list_type::s_iterator_to(*(i->second))); lruage.push_front(*(i->second)); } cachelock.unlock(); }