diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2016-04-27 16:02:13 +0200 |
---|---|---|
committer | Cedric Nugteren <web@cedricnugteren.nl> | 2016-04-27 16:02:13 +0200 |
commit | 82be8f211cbd50d2d75fe78d8af4a1da04a0582b (patch) | |
tree | 19b4dbb4478edb28b7afd99a11e5a1e470098a5d /include | |
parent | 44bdb60e834ef015ee4cb25a6f0eba2a092291f0 (diff) |
Moved all cache-related functions to a separate file; added a ClearCompiledProgramCache function to clear the cache
Diffstat (limited to 'include')
-rw-r--r-- | include/clblast.h | 7 | ||||
-rw-r--r-- | include/clblast_c.h | 6 | ||||
-rw-r--r-- | include/internal/cache.h | 72 | ||||
-rw-r--r-- | include/internal/routine.h | 37 |
4 files changed, 98 insertions, 24 deletions
diff --git a/include/clblast.h b/include/clblast.h index f73acb57..4a3ec9b6 100644 --- a/include/clblast.h +++ b/include/clblast.h @@ -539,6 +539,13 @@ StatusCode Trsm(const Layout layout, const Side side, const Triangle triangle, c cl_command_queue* queue, cl_event* event = nullptr); // ================================================================================================= + +// CLBlast stores binaries of compiled kernels into a cache in case the same kernel is used later on +// for the same device. This cache can be cleared to free up system memory or in case of debugging. +StatusCode ClearCompiledProgramCache(); + +// ================================================================================================= + } // namespace clblast // CLBLAST_CLBLAST_H_ diff --git a/include/clblast_c.h b/include/clblast_c.h index 8c0a0792..1ca300ca 100644 --- a/include/clblast_c.h +++ b/include/clblast_c.h @@ -1036,6 +1036,12 @@ StatusCode PUBLIC_API CLBlastZtrsm(const Layout layout, const Side side, const T // ================================================================================================= +// CLBlast stores binaries of compiled kernels into a cache in case the same kernel is used later on +// for the same device. This cache can be cleared to free up system memory or in case of debugging. +StatusCode PUBLIC_API CLBlastClearCompiledProgramCache(); + +// ================================================================================================= + #ifdef __cplusplus } // extern "C" #endif diff --git a/include/internal/cache.h b/include/internal/cache.h new file mode 100644 index 00000000..44fad68d --- /dev/null +++ b/include/internal/cache.h @@ -0,0 +1,72 @@ + +// ================================================================================================= +// 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. +// +// ================================================================================================= + +#ifndef CLBLAST_CACHE_H_ +#define CLBLAST_CACHE_H_ + +#include <string> +#include <vector> +#include <mutex> + +#include "internal/utilities.h" + +namespace clblast { +namespace cache { +// ================================================================================================= + +// The cache of compiled OpenCL programs, along with some meta-data +struct ProgramCache { + Program program; + std::string device_name; + Precision precision; + std::string routine_name_; + + // Finds out whether the properties match + bool MatchInCache(const std::string &ref_device, const Precision &ref_precision, + const std::string &ref_routine) { + return (device_name == ref_device && + precision == ref_precision && + routine_name_ == ref_routine); + } +}; + +// The actual cache, implemented as a vector of the above data-type, and its mutex +static std::vector<ProgramCache> program_cache_; +static std::mutex program_cache_mutex_; + +// ================================================================================================= + +// 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); + +// 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); + +// 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); + +// ================================================================================================= + +// Clears the cache of stored program binaries +StatusCode ClearCompiledProgramCache(); + +// ================================================================================================= +} // namespace cache +} // namespace clblast + +// CLBLAST_CACHE_H_ +#endif diff --git a/include/internal/routine.h b/include/internal/routine.h index b2b6f622..013769d8 100644 --- a/include/internal/routine.h +++ b/include/internal/routine.h @@ -18,8 +18,8 @@ #include <string> #include <vector> -#include <mutex> +#include "internal/cache.h" #include "internal/utilities.h" #include "internal/database.h" @@ -31,26 +31,6 @@ template <typename T> class Routine { public: - // The cache of compiled OpenCL programs, along with some meta-data - struct ProgramCache { - Program program; - std::string device_name; - Precision precision; - std::string routine_name_; - - // Finds out whether the properties match - bool MatchInCache(const std::string &ref_device, const Precision &ref_precision, - const std::string &ref_routine) { - return (device_name == ref_device && - precision == ref_precision && - routine_name_ == ref_routine); - } - }; - - // The actual cache, implemented as a vector of the above data-type, and its mutex - static std::vector<ProgramCache> program_cache_; - static std::mutex program_cache_mutex_; - // Helper functions which check for errors in the status code static constexpr bool ErrorIn(const StatusCode s) { return (s != StatusCode::kSuccess); } @@ -103,12 +83,21 @@ class Routine { const bool do_transpose, const bool do_conjugate, const bool upper = false, const bool lower = false, const bool diagonal_imag_zero = false); - + + // Stores a newly compiled program into the cache + void StoreProgramToCache(const Program& program) const { + return cache::StoreProgramToCache(program, device_name_, precision_, routine_name_); + } + // Queries the cache and retrieve either a matching program or a boolean whether a match exists. // The first assumes that the program is available in the cache and will throw an exception // otherwise. - const Program& GetProgramFromCache() const; - bool ProgramIsInCache() const; + const Program& GetProgramFromCache() const { + return cache::GetProgramFromCache(device_name_, precision_, routine_name_); + } + bool ProgramIsInCache() const { + return cache::ProgramIsInCache(device_name_, precision_, routine_name_); + } // Non-static variable for the precision. Note that the same variable (but static) might exist in // a derived class. |