pull/2744/head
deanlee 5 years ago
parent dff1d146ec
commit 115bdb456a
  1. 68
      selfdrive/common/clutil.cc
  2. 41
      selfdrive/common/clutil.h

@ -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<sl ? sl-1-i : sl];
}
return x ^ (x >> 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<std::string>{}(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); i++) {
if (clu_index[i].index_hash == index_hash) {
break;
}
}
if (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<std::string>{}(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<std::string>{}(str), src_hash);
#endif
return ret;
}

@ -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)<strlen(s)?strlen(s)-1-(i):strlen(s)])
#define CLU_H4(s,i,x) CLU_H1(s,i,CLU_H1(s,i+1,CLU_H1(s,i+2,CLU_H1(s,i+3,x))))
#define CLU_H16(s,i,x) CLU_H4(s,i,CLU_H4(s,i+4,CLU_H4(s,i+8,CLU_H4(s,i+12,x))))
#define CLU_H64(s,i,x) CLU_H16(s,i,CLU_H16(s,i+16,CLU_H16(s,i+32,CLU_H16(s,i+48,x))))
// #define CLU_H256(s,i,x) CLU_H64(s,i,CLU_H64(s,i+64,CLU_H64(s,i+128,CLU_H64(s,i+192,x))))
#define CLU_H128(s,i,x) CLU_H64(s,i,CLU_H64(s,i+64,x))
#define CLU_HASH(s) ((uint64_t)(CLU_H128(s,0,0)^(CLU_H128(s,0,0)>>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
}

Loading…
Cancel
Save