diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2016-05-01 12:56:08 +0200 |
---|---|---|
committer | Cedric Nugteren <web@cedricnugteren.nl> | 2016-05-01 12:56:08 +0200 |
commit | 9602c150aa3b7f0a392207bef8cbb6048b1da891 (patch) | |
tree | b9b6f2c2300d77427ee6121151efa00a11c60859 /include/internal | |
parent | e113ff0852d21ecb898b3b192145b70cad3f338a (diff) |
Added a program cache (per-context) next to the per-device binary cache
Diffstat (limited to 'include/internal')
-rw-r--r-- | include/internal/cache.h | 38 | ||||
-rw-r--r-- | include/internal/clpp11.h | 4 | ||||
-rw-r--r-- | include/internal/routine.h | 27 |
3 files changed, 53 insertions, 16 deletions
diff --git a/include/internal/cache.h b/include/internal/cache.h index fa33b78f..4a11b70f 100644 --- a/include/internal/cache.h +++ b/include/internal/cache.h @@ -7,7 +7,7 @@ // Author(s): // Cedric Nugteren <www.cedricnugteren.nl> // -// This file implements the caching functionality of compiled binaries. +// This file implements the caching functionality of compiled binaries and programs. // // ================================================================================================= @@ -46,18 +46,46 @@ static std::mutex binary_cache_mutex_; // ================================================================================================= -// Stores the compiled binary in the cache -void StoreBinaryToCache(const std::string& binary, const std::string &device_name, +// The cache of compiled OpenCL programs, along with some meta-data +struct ProgramCache { + Program program; + ContextPointer context_ptr; + Precision precision; + std::string routine_name_; + + // Finds out whether the properties match + bool MatchInCache(const ContextPointer ref_context, const Precision &ref_precision, + const std::string &ref_routine) { + return (context_ptr == ref_context && + 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 binary or program in the cache +void StoreBinaryToCache(const std::string &binary, const std::string &device_name, const Precision &precision, const std::string &routine_name); +void StoreProgramToCache(const Program &program, const Context &context, + const Precision &precision, const std::string &routine_name); -// Queries the cache and retrieves a matching binary. Assumes that the match is available, throws -// otherwise. +// Queries the cache and retrieves a matching binary or program. 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); +const Program& GetProgramFromCache(const Context &context, const Precision &precision, + const std::string &routine_name); // 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); +bool ProgramIsInCache(const Context &context, const Precision &precision, + const std::string &routine_name); // ================================================================================================= diff --git a/include/internal/clpp11.h b/include/internal/clpp11.h index b865ab1e..e70f9000 100644 --- a/include/internal/clpp11.h +++ b/include/internal/clpp11.h @@ -269,10 +269,14 @@ class Context { // Accessor to the private data-member const cl_context& operator()() const { return *context_; } + cl_context* pointer() const { return &(*context_); } private: std::shared_ptr<cl_context> context_; }; +// Pointer to an OpenCL context +using ContextPointer = cl_context*; + // ================================================================================================= // Enumeration of build statuses of the run-time compilation process diff --git a/include/internal/routine.h b/include/internal/routine.h index 32be6012..f2f236ac 100644 --- a/include/internal/routine.h +++ b/include/internal/routine.h @@ -84,24 +84,29 @@ class Routine { const bool upper = false, const bool lower = false, const bool diagonal_imag_zero = false); - // Stores a newly compiled binary into the cache + // Stores a newly compiled binary/program into the cache void StoreBinaryToCache(const std::string& binary) const { - return cache::StoreBinaryToCache(binary, device_name_, precision_, routine_name_); + cache::StoreBinaryToCache(binary, device_name_, precision_, routine_name_); + } + void StoreProgramToCache(const Program& program) const { + cache::StoreProgramToCache(program, context_, 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. + // Queries the cache and retrieve either a matching binary/program or a boolean whether a match + // exists. The first assumes that the binary/program is available in the cache and will throw an + // exception otherwise. + std::string GetBinaryFromCache() const { + return cache::GetBinaryFromCache(device_name_, precision_, routine_name_); + } Program GetProgramFromCache() const { - auto& binary = cache::GetBinaryFromCache(device_name_, precision_, routine_name_); - auto program = Program(device_, context_, binary); - auto options = std::vector<std::string>(); - program.Build(device_, options); - return program; + return cache::GetProgramFromCache(context_, precision_, routine_name_); } - bool ProgramIsInCache() const { + bool BinaryIsInCache() const { return cache::BinaryIsInCache(device_name_, precision_, routine_name_); } + bool ProgramIsInCache() const { + return cache::ProgramIsInCache(context_, precision_, routine_name_); + } // Non-static variable for the precision. Note that the same variable (but static) might exist in // a derived class. |