#ifndef CLUTIL_H #define CLUTIL_H #include #include #include #ifdef __APPLE__ #include #else #include #endif #ifdef __cplusplus extern "C" { #endif void clu_init(void); 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); void cl_print_build_errors(cl_program program, cl_device_id device); void cl_print_build_errors(cl_program program, cl_device_id device); cl_program cl_cached_program_from_hash(cl_context ctx, cl_device_id device_id, uint64_t hash); cl_program cl_cached_program_from_string(cl_context ctx, cl_device_id device_id, const char* src, const char* args, uint64_t *out_hash); cl_program cl_cached_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args, uint64_t *out_hash); 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* cl_get_error_string(int err); static inline int cl_check_error(int err) { if (err != 0) { fprintf(stderr, "%s\n", cl_get_error_string(err)); exit(1); } 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_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 #ifdef __cplusplus } #endif #endif