summaryrefslogtreecommitdiff
path: root/src/cache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache.cpp')
-rw-r--r--src/cache.cpp136
1 files changed, 53 insertions, 83 deletions
diff --git a/src/cache.cpp b/src/cache.cpp
index 6786eaa2..2b91abf1 100644
--- a/src/cache.cpp
+++ b/src/cache.cpp
@@ -20,103 +20,73 @@
namespace clblast {
// =================================================================================================
-// Stores the compiled binary or IR in the cache
-void StoreBinaryToCache(const std::string &binary, const std::string &device_name,
- const Precision &precision, const std::string &routine_name) {
- #ifdef VERBOSE
- printf("[DEBUG] Storing binary in cache\n");
- #endif
- binary_cache_mutex_.lock();
- binary_cache_.push_back(BinaryCache{binary, device_name, precision, routine_name});
- binary_cache_mutex_.unlock();
-}
-
-// Stores the compiled program in the cache
-void StoreProgramToCache(const Program &program, const Context &context,
- const Precision &precision, const std::string &routine_name) {
- #ifdef VERBOSE
- printf("[DEBUG] Storing program in cache\n");
- #endif
- program_cache_mutex_.lock();
- program_cache_.push_back(ProgramCache{program, context(), precision, routine_name});
- program_cache_mutex_.unlock();
-}
+template <typename Key, typename Value>
+template <typename U>
+Value Cache<Key, Value>::Get(const U &key, bool *in_cache) const {
+ std::lock_guard<std::mutex> lock(cache_mutex_);
-// Queries the cache and retrieves a matching binary. Assumes that the match is available, throws
-// otherwise.
-const std::string& GetBinaryFromCache(const std::string &device_name, const Precision &precision,
- const std::string &routine_name) {
- #ifdef VERBOSE
- printf("[DEBUG] Retrieving binary from cache\n");
- #endif
- binary_cache_mutex_.lock();
- for (auto &cached_binary: binary_cache_) {
- if (cached_binary.MatchInCache(device_name, precision, routine_name)) {
- binary_cache_mutex_.unlock();
- return cached_binary.binary;
+#if __cplusplus >= 201402L
+ // generalized std::map::find() of C++14
+ auto it = cache_.find(key);
+#else
+ // O(n) lookup in a vector
+ auto it = std::find_if(cache_.begin(), cache_.end(), [&] (const std::pair<Key, Value> &pair) {
+ return pair.first == key;
+ });
+#endif
+ if (it == cache_.end()) {
+ if (in_cache) {
+ *in_cache = false;
}
+ return Value();
}
- binary_cache_mutex_.unlock();
- throw LogicError("GetBinaryFromCache: Expected binary in cache, but found none");
-}
-// Queries the cache and retrieves a matching program. Assumes that the match is available, throws
-// otherwise.
-const Program& GetProgramFromCache(const Context &context, const Precision &precision,
- const std::string &routine_name) {
- #ifdef VERBOSE
- printf("[DEBUG] Retrieving program from cache\n");
- #endif
- program_cache_mutex_.lock();
- for (auto &cached_program: program_cache_) {
- if (cached_program.MatchInCache(context(), precision, routine_name)) {
- program_cache_mutex_.unlock();
- return cached_program.program;
- }
+ if (in_cache) {
+ *in_cache = true;
}
- program_cache_mutex_.unlock();
- throw LogicError("GetProgramFromCache: Expected program in cache, but found none");
+ return it->second;
}
-// Queries the cache to see whether or not the compiled kernel is already there
-bool BinaryIsInCache(const std::string &device_name, const Precision &precision,
- const std::string &routine_name) {
- binary_cache_mutex_.lock();
- for (auto &cached_binary: binary_cache_) {
- if (cached_binary.MatchInCache(device_name, precision, routine_name)) {
- binary_cache_mutex_.unlock();
- return true;
- }
+template <typename Key, typename Value>
+void Cache<Key, Value>::Store(Key &&key, Value &&value) {
+ std::lock_guard<std::mutex> lock(cache_mutex_);
+
+#if __cplusplus >= 201402L
+ // emplace() into a map
+ auto r = cache_.emplace(std::move(key), std::move(value));
+ if (!r.second) {
+ throw LogicError("Cache::Store: object already in cache");
}
- binary_cache_mutex_.unlock();
- return false;
+#else
+ // emplace_back() into a vector
+ cache_.emplace_back(std::move(key), std::move(value));
+#endif
}
-// Queries the cache to see whether or not the compiled kernel is already there
-bool ProgramIsInCache(const Context &context, const Precision &precision,
- const std::string &routine_name) {
- program_cache_mutex_.lock();
- for (auto &cached_program: program_cache_) {
- if (cached_program.MatchInCache(context(), precision, routine_name)) {
- program_cache_mutex_.unlock();
- return true;
- }
- }
- program_cache_mutex_.unlock();
- return false;
+template <typename Key, typename Value>
+void Cache<Key, Value>::Invalidate() {
+ std::lock_guard<std::mutex> lock(cache_mutex_);
+
+ cache_.clear();
+}
+
+template <typename Key, typename Value>
+Cache<Key, Value> &Cache<Key, Value>::Instance() {
+ return instance_;
}
+template <typename Key, typename Value>
+Cache<Key, Value> Cache<Key, Value>::instance_;
+
// =================================================================================================
-// Clears the cache of stored binaries and programs
-void CacheClearAll() {
- binary_cache_mutex_.lock();
- binary_cache_.clear();
- binary_cache_mutex_.unlock();
- program_cache_mutex_.lock();
- program_cache_.clear();
- program_cache_mutex_.unlock();
-}
+template class Cache<BinaryKey, std::string>;
+template std::string BinaryCache::Get(const BinaryKeyRef &, bool *) const;
+
+// =================================================================================================
+
+template class Cache<ProgramKey, Program>;
+template Program ProgramCache::Get(const ProgramKeyRef &, bool *) const;
// =================================================================================================
} // namespace clblast