From 82be8f211cbd50d2d75fe78d8af4a1da04a0582b Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Wed, 27 Apr 2016 16:02:13 +0200 Subject: Moved all cache-related functions to a separate file; added a ClearCompiledProgramCache function to clear the cache --- src/cache.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/cache.cc (limited to 'src/cache.cc') diff --git a/src/cache.cc b/src/cache.cc new file mode 100644 index 00000000..beeb1b35 --- /dev/null +++ b/src/cache.cc @@ -0,0 +1,73 @@ + +// ================================================================================================= +// 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 +// +// This file implements the caching functionality of compiled binaries. +// +// ================================================================================================= + +#include +#include +#include + +#include "internal/cache.h" + +namespace clblast { +namespace cache { +// ================================================================================================= + +// Stores the compiled program in the cache +void StoreProgramToCache(const Program& program, const std::string &device_name, + const Precision &precision, const std::string &routine_name) { + program_cache_mutex_.lock(); + program_cache_.push_back({program, device_name, precision, routine_name}); + program_cache_mutex_.unlock(); +} + +// Queries the cache and retrieves a matching program. Assumes that the match is available, throws +// otherwise. +const Program& GetProgramFromCache(const std::string &device_name, const Precision &precision, + const std::string &routine_name) { + program_cache_mutex_.lock(); + for (auto &cached_program: program_cache_) { + if (cached_program.MatchInCache(device_name, 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 ProgramIsInCache(const std::string &device_name, const Precision &precision, + const std::string &routine_name) { + program_cache_mutex_.lock(); + for (auto &cached_program: program_cache_) { + if (cached_program.MatchInCache(device_name, precision, routine_name)) { + program_cache_mutex_.unlock(); + return true; + } + } + program_cache_mutex_.unlock(); + return false; +} + +// ================================================================================================= + +// Clears the cache of stored program binaries +StatusCode ClearCompiledProgramCache() { + program_cache_mutex_.lock(); + program_cache_.clear(); + program_cache_mutex_.unlock(); + return StatusCode::kSuccess; +} + +// ================================================================================================= +} // namespace cache +} // namespace clblast -- cgit v1.2.3