2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
// =================================================================================================
|
|
|
|
// 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>
|
|
|
|
//
|
2016-05-01 12:56:08 +02:00
|
|
|
// This file implements the caching functionality of compiled binaries and programs.
|
2016-04-27 16:02:13 +02:00
|
|
|
//
|
|
|
|
// =================================================================================================
|
|
|
|
|
|
|
|
#ifndef CLBLAST_CACHE_H_
|
|
|
|
#define CLBLAST_CACHE_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
|
2016-10-22 15:36:48 +02:00
|
|
|
#include "utilities/utilities.hpp"
|
2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
namespace clblast {
|
|
|
|
// =================================================================================================
|
|
|
|
|
2016-04-28 21:14:17 +02:00
|
|
|
// The cache of compiled OpenCL binaries, along with some meta-data
|
|
|
|
struct BinaryCache {
|
|
|
|
std::string binary;
|
2016-04-27 16:02:13 +02:00
|
|
|
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
|
2016-04-28 21:14:17 +02:00
|
|
|
static std::vector<BinaryCache> binary_cache_;
|
|
|
|
static std::mutex binary_cache_mutex_;
|
2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
// =================================================================================================
|
|
|
|
|
2016-05-01 12:56:08 +02:00
|
|
|
// The cache of compiled OpenCL programs, along with some meta-data
|
|
|
|
struct ProgramCache {
|
|
|
|
Program program;
|
2016-07-10 11:24:36 +02:00
|
|
|
cl_context context;
|
2016-05-01 12:56:08 +02:00
|
|
|
Precision precision;
|
|
|
|
std::string routine_name_;
|
|
|
|
|
|
|
|
// Finds out whether the properties match
|
2016-07-10 11:24:36 +02:00
|
|
|
bool MatchInCache(const cl_context ref_context, const Precision &ref_precision,
|
2016-05-01 12:56:08 +02:00
|
|
|
const std::string &ref_routine) {
|
2016-07-10 11:24:36 +02:00
|
|
|
return (context == ref_context &&
|
2016-05-01 12:56:08 +02:00
|
|
|
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,
|
2016-04-28 21:14:17 +02:00
|
|
|
const Precision &precision, const std::string &routine_name);
|
2016-05-01 12:56:08 +02:00
|
|
|
void StoreProgramToCache(const Program &program, const Context &context,
|
|
|
|
const Precision &precision, const std::string &routine_name);
|
2016-04-27 16:02:13 +02:00
|
|
|
|
2016-05-01 12:56:08 +02:00
|
|
|
// Queries the cache and retrieves a matching binary or program. Assumes that the match is
|
|
|
|
// available, throws otherwise.
|
2016-04-28 21:14:17 +02:00
|
|
|
const std::string& GetBinaryFromCache(const std::string &device_name, const Precision &precision,
|
|
|
|
const std::string &routine_name);
|
2016-05-01 12:56:08 +02:00
|
|
|
const Program& GetProgramFromCache(const Context &context, const Precision &precision,
|
|
|
|
const std::string &routine_name);
|
2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
// Queries the cache to see whether or not the compiled kernel is already there
|
2016-04-28 21:14:17 +02:00
|
|
|
bool BinaryIsInCache(const std::string &device_name, const Precision &precision,
|
|
|
|
const std::string &routine_name);
|
2016-05-01 12:56:08 +02:00
|
|
|
bool ProgramIsInCache(const Context &context, const Precision &precision,
|
|
|
|
const std::string &routine_name);
|
2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
// =================================================================================================
|
|
|
|
|
2016-04-28 21:14:17 +02:00
|
|
|
// Clears the cache of stored binaries
|
2016-10-22 04:14:19 +02:00
|
|
|
void CacheClearAll();
|
2016-04-27 16:02:13 +02:00
|
|
|
|
|
|
|
// =================================================================================================
|
|
|
|
} // namespace clblast
|
|
|
|
|
|
|
|
// CLBLAST_CACHE_H_
|
|
|
|
#endif
|