Skip to content

Commit

Permalink
Merge pull request #451 from ddemidov/offline-cache-optimization
Browse files Browse the repository at this point in the history
Offline cache optimization
  • Loading branch information
kylelutz committed Apr 15, 2015
2 parents 72c75f1 + d465111 commit 08eb4e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
3 changes: 2 additions & 1 deletion include/boost/compute/detail/meta_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ class meta_kernel
std::string source = this->source();

// generate cache key
std::string cache_key = "__boost_meta_kernel_" + detail::sha1(source);
std::string cache_key = "__boost_meta_kernel_" +
static_cast<std::string>(detail::sha1(source));

// load program cache
boost::shared_ptr<program_cache> cache =
Expand Down
39 changes: 25 additions & 14 deletions include/boost/compute/detail/sha1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,31 @@ namespace boost {
namespace compute {
namespace detail {

// Returns SHA1 hash of the string parameter.
inline std::string sha1(const std::string &src) {
boost::uuids::detail::sha1 sha1;
sha1.process_bytes(src.c_str(), src.size());

unsigned int hash[5];
sha1.get_digest(hash);

std::ostringstream buf;
for(int i = 0; i < 5; ++i)
buf << std::hex << std::setfill('0') << std::setw(8) << hash[i];

return buf.str();
}
// Accumulates SHA1 hash of the passed strings.
class sha1 {
public:
sha1(const std::string &s = "") {
if (!s.empty()) this->process(s);
}

sha1& process(const std::string &s) {
h.process_bytes(s.c_str(), s.size());
return *this;
}

operator std::string() {
unsigned int digest[5];
h.get_digest(digest);

std::ostringstream buf;
for(int i = 0; i < 5; ++i)
buf << std::hex << std::setfill('0') << std::setw(8) << digest[i];

return buf.str();
}
private:
boost::uuids::detail::sha1 h;
};

} // end detail namespace
} // end compute namespace
Expand Down
23 changes: 10 additions & 13 deletions include/boost/compute/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,16 @@ class program
{
#ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE
// Get hash string for the kernel.
std::string hash;
{
device d(context.get_device());
platform p = d.platform();

std::ostringstream src;
src << "// " << p.name() << " v" << p.version() << "\n"
<< "// " << context.get_device().name() << "\n"
<< "// " << options << "\n\n"
<< source;

hash = detail::sha1(src.str());
}
device d = context.get_device();
platform p = d.platform();

detail::sha1 hash;
hash.process( p.name() )
.process( p.version() )
.process( d.name() )
.process( options )
.process( source )
;

// Try to get cached program binaries:
try {
Expand Down

0 comments on commit 08eb4e3

Please sign in to comment.