Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline cache optimization #451

Merged
merged 1 commit into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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