diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2016-06-28 22:32:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-28 22:32:25 +0200 |
commit | 7c13bacf129291e3e295ecb6e833788477085fa0 (patch) | |
tree | d114eeca418444d0b1c70cc9cce983de041235c9 /src/cache.cpp | |
parent | 181eb20bbf15cf11baaf6112b6965050c49dd543 (diff) | |
parent | 577f0ee1179014ece853af39d6f0ff0c87316eb3 (diff) |
Merge pull request #70 from CNugteren/development
Update to version 0.8.0
Diffstat (limited to 'src/cache.cpp')
-rw-r--r-- | src/cache.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/cache.cpp b/src/cache.cpp new file mode 100644 index 00000000..cd9055d0 --- /dev/null +++ b/src/cache.cpp @@ -0,0 +1,111 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren <www.cedricnugteren.nl> +// +// This file implements the caching functionality of compiled binaries and programs. +// +// ================================================================================================= + +#include <string> +#include <vector> +#include <mutex> + +#include "cache.hpp" + +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) { + 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) { + program_cache_mutex_.lock(); + program_cache_.push_back(ProgramCache{program, context.pointer(), precision, routine_name}); + program_cache_mutex_.unlock(); +} + +// 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) { + 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; + } + } + binary_cache_mutex_.unlock(); + throw std::runtime_error("Internal CLBlast error: 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) { + program_cache_mutex_.lock(); + for (auto &cached_program: program_cache_) { + if (cached_program.MatchInCache(context.pointer(), precision, routine_name)) { + program_cache_mutex_.unlock(); + return cached_program.program; + } + } + program_cache_mutex_.unlock(); + throw std::runtime_error("Internal CLBlast error: Expected program in cache, but found none."); +} + +// 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; + } + } + binary_cache_mutex_.unlock(); + return false; +} + +// 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.pointer(), precision, routine_name)) { + program_cache_mutex_.unlock(); + return true; + } + } + program_cache_mutex_.unlock(); + return false; +} + +// ================================================================================================= + +// Clears the cache of stored binaries and programs +StatusCode CacheClearAll() { + binary_cache_mutex_.lock(); + binary_cache_.clear(); + binary_cache_mutex_.unlock(); + program_cache_mutex_.lock(); + program_cache_.clear(); + program_cache_mutex_.unlock(); + return StatusCode::kSuccess; +} + +// ================================================================================================= +} // namespace clblast |