From 115bdb456a068c6f4cce751758e3b969578e4504 Mon Sep 17 00:00:00 2001 From: deanlee Date: Thu, 10 Dec 2020 05:36:58 +0000 Subject: [PATCH] std::hash --- selfdrive/common/clutil.cc | 68 +++++--------------------------------- selfdrive/common/clutil.h | 41 +++-------------------- 2 files changed, 13 insertions(+), 96 deletions(-) diff --git a/selfdrive/common/clutil.cc b/selfdrive/common/clutil.cc index 4c861b0652..4438960a98 100644 --- a/selfdrive/common/clutil.cc +++ b/selfdrive/common/clutil.cc @@ -19,16 +19,9 @@ #include "clutil.h" -typedef struct CLUProgramIndex { - uint64_t index_hash; - const uint8_t* bin_data; - const uint8_t* bin_end; -} CLUProgramIndex; - #ifdef CLU_NO_SRC #include "clcache_bins.h" #else -static const CLUProgramIndex clu_index[] = {}; #endif #define CL_IDX_CACHE_FILE "/tmp/clcache/index.cli" @@ -152,31 +145,6 @@ void cl_print_build_errors(cl_program program, cl_device_id device) { free(log); } -uint64_t clu_index_hash(const char* s) { - size_t sl = strlen(s); - assert(sl < 128); - uint64_t x = 0; - for (int i=127; i>=0; i--) { - x *= 65599ULL; - x += (uint8_t)s[i> 32); -} - -uint64_t clu_fnv_hash(const uint8_t *data, size_t len) { - /* 64 bit Fowler/Noll/Vo FNV-1a hash code */ - uint64_t hval = 0xcbf29ce484222325ULL; - const uint8_t *dp = data; - const uint8_t *de = data + len; - while (dp < de) { - hval ^= (uint64_t) *dp++; - hval += (hval << 1) + (hval << 4) + (hval << 5) + - (hval << 7) + (hval << 8) + (hval << 40); - } - - return hval; -} - cl_program cl_cached_program_from_hash(cl_context ctx, cl_device_id device_id, uint64_t hash) { char cache_path[1024]; snprintf(cache_path, sizeof(cache_path), "/tmp/clcache/%016" PRIx64 ".clb", hash); @@ -223,7 +191,7 @@ cl_program cl_cached_program_from_string(cl_context ctx, cl_device_id device_id, std::string platform_version = get_version_string(platform); std::string hash_buf = util::string_format("%s%c%s%c%s", platform_version.c_str(), 1, src, 1, args); - uint64_t hash = clu_fnv_hash((uint8_t*)&hash_buf[0], hash_buf.length()); + size_t hash = std::hash{}(hash_buf); cl_program prg = NULL; #ifndef CLU_NO_CACHE @@ -270,44 +238,24 @@ static void add_index(uint64_t index_hash, uint64_t src_hash) { } #endif -cl_program cl_program_from_index(cl_context ctx, cl_device_id device_id, uint64_t index_hash) { - int i; - for (i=0; i= ARRAYSIZE(clu_index)) { - assert(false); - } - - size_t bin_size = clu_index[i].bin_end - clu_index[i].bin_data; - const uint8_t *bin_data = clu_index[i].bin_data; - - cl_program prg = CL_CHECK_ERR(clCreateProgramWithBinary(ctx, 1, &device_id, &bin_size, (const uint8_t**)&bin_data, NULL, &err)); - - CL_CHECK(clBuildProgram(prg, 1, &device_id, NULL, NULL, NULL)); - - return prg; -} - cl_program cl_index_program_from_string(cl_context ctx, cl_device_id device_id, const char* src, const char* args, - uint64_t index_hash) { - uint64_t src_hash = 0; + const char* file, int line, const char* function) { + size_t src_hash = 0; cl_program ret = cl_cached_program_from_string(ctx, device_id, src, args, &src_hash); #ifndef CLU_NO_CACHE - add_index(index_hash, src_hash); + std::string str = util::string_format("%s%d%s%s", file, line, function, args); + add_index(std::hash{}(str), src_hash); #endif return ret; } -cl_program cl_index_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args, - uint64_t index_hash) { +cl_program cl_index_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args) { uint64_t src_hash = 0; cl_program ret = cl_cached_program_from_file(ctx, device_id, path, args, &src_hash); #ifndef CLU_NO_CACHE - add_index(index_hash, src_hash); + std::string str = util::string_format("%s%s", path, args); + add_index(std::hash{}(str), src_hash); #endif return ret; } diff --git a/selfdrive/common/clutil.h b/selfdrive/common/clutil.h index ec277605c9..fb77890d9b 100644 --- a/selfdrive/common/clutil.h +++ b/selfdrive/common/clutil.h @@ -33,16 +33,10 @@ cl_device_id cl_get_device_id(cl_device_type device_type); cl_program cl_create_program_from_file(cl_context ctx, const char* path); void cl_print_info(cl_platform_id platform, cl_device_id device); -cl_program cl_program_from_index(cl_context ctx, cl_device_id device_id, uint64_t index_hash); - cl_program cl_index_program_from_string(cl_context ctx, cl_device_id device_id, const char* src, const char* args, - uint64_t index_hash); -cl_program cl_index_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args, - uint64_t index_hash); - -uint64_t clu_index_hash(const char *s); -uint64_t clu_fnv_hash(const uint8_t *data, size_t len); + const char *file, int line, const char *function); +cl_program cl_index_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args); const char* cl_get_error_string(int err); @@ -54,35 +48,10 @@ static inline int cl_check_error(int err) { return err; } - -// // string hash macro. compiler, I'm so sorry. -#define CLU_H1(s,i,x) (x*65599ULL+(uint8_t)s[(i)>32))) - -#define CLU_STRINGIFY(x) #x -#define CLU_STRINGIFY2(x) CLU_STRINGIFY(x) -#define CLU_LINESTR CLU_STRINGIFY2(__LINE__) - -#ifdef CLU_NO_SRC - - #define CLU_LOAD_FROM_STRING(ctx, device_id, src, args) \ - cl_program_from_index(ctx, device_id, CLU_HASH("\1" __FILE__ "\1" CLU_LINESTR) ^ clu_fnv_hash((const uint8_t*)__func__, strlen(__func__)) ^ clu_fnv_hash((const uint8_t*)args, strlen(args))) +#define CLU_LOAD_FROM_STRING(ctx, device_id, src, args) \ + cl_index_program_from_string(ctx, device_id, src, args, __FILE__, __LINE__, __func__); #define CLU_LOAD_FROM_FILE(ctx, device_id, path, args) \ - cl_program_from_index(ctx, device_id, CLU_HASH("\2" path) ^ clu_fnv_hash((const uint8_t*)args, strlen(args))) - -#else - - #define CLU_LOAD_FROM_STRING(ctx, device_id, src, args) \ - cl_index_program_from_string(ctx, device_id, src, args, clu_index_hash("\1" __FILE__ "\1" CLU_LINESTR) ^ clu_fnv_hash((const uint8_t*)__func__, strlen(__func__)) ^ clu_fnv_hash((const uint8_t*)args, strlen(args))) - #define CLU_LOAD_FROM_FILE(ctx, device_id, path, args) \ - cl_index_program_from_file(ctx, device_id, path, args, clu_index_hash("\2" path) ^ clu_fnv_hash((const uint8_t*)args, strlen(args))) - -#endif + cl_index_program_from_file(ctx, device_id, path, args); #ifdef __cplusplus }