|
|
@ -32,41 +32,10 @@ void clu_init(void) { |
|
|
|
unlink(CL_IDX_CACHE_FILE); |
|
|
|
unlink(CL_IDX_CACHE_FILE); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
// helper functions
|
|
|
|
|
|
|
|
|
|
|
|
cl_device_id cl_get_device_id(cl_device_type device_type) { |
|
|
|
std::string get_version_string(cl_platform_id platform) { |
|
|
|
bool opencl_platform_found = false; |
|
|
|
|
|
|
|
cl_device_id device_id = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cl_uint num_platforms = 0; |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformIDs(0, NULL, &num_platforms)); |
|
|
|
|
|
|
|
std::unique_ptr<cl_platform_id[]> platform_ids = std::make_unique<cl_platform_id[]>(num_platforms); |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformIDs(num_platforms, &platform_ids[0], NULL)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char cBuffer[1024]; |
|
|
|
|
|
|
|
for (size_t i = 0; i < num_platforms; i++) { |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL)); |
|
|
|
|
|
|
|
printf("platform[%zu] CL_PLATFORM_NAME: %s\n", i, cBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cl_uint num_devices; |
|
|
|
|
|
|
|
int err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices); |
|
|
|
|
|
|
|
if (err != 0 || !num_devices) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Get first device
|
|
|
|
|
|
|
|
CL_CHECK(clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL)); |
|
|
|
|
|
|
|
cl_print_info(platform_ids[i], device_id); |
|
|
|
|
|
|
|
opencl_platform_found = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!opencl_platform_found) { |
|
|
|
|
|
|
|
printf("No valid openCL platform found\n"); |
|
|
|
|
|
|
|
assert(opencl_platform_found); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return device_id; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::string get_version_string(cl_platform_id platform) { |
|
|
|
|
|
|
|
size_t size = 0; |
|
|
|
size_t size = 0; |
|
|
|
CL_CHECK(clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 0, NULL, &size)); |
|
|
|
CL_CHECK(clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 0, NULL, &size)); |
|
|
|
std::string version; |
|
|
|
std::string version; |
|
|
@ -138,7 +107,7 @@ void cl_print_build_errors(cl_program program, cl_device_id device) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifndef CLU_NO_CACHE |
|
|
|
#ifndef CLU_NO_CACHE |
|
|
|
static cl_program cached_program_from_hash(cl_context ctx, cl_device_id device_id, uint64_t hash) { |
|
|
|
cl_program cached_program_from_hash(cl_context ctx, cl_device_id device_id, uint64_t hash) { |
|
|
|
char cache_path[1024]; |
|
|
|
char cache_path[1024]; |
|
|
|
snprintf(cache_path, sizeof(cache_path), "/tmp/clcache/%016" PRIx64 ".clb", hash); |
|
|
|
snprintf(cache_path, sizeof(cache_path), "/tmp/clcache/%016" PRIx64 ".clb", hash); |
|
|
|
|
|
|
|
|
|
|
@ -156,7 +125,7 @@ static cl_program cached_program_from_hash(cl_context ctx, cl_device_id device_i |
|
|
|
return prg; |
|
|
|
return prg; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static std::vector<uint8_t> get_program_binary(cl_program prg) { |
|
|
|
std::vector<uint8_t> get_program_binary(cl_program prg) { |
|
|
|
cl_uint num_devices; |
|
|
|
cl_uint num_devices; |
|
|
|
CL_CHECK(clGetProgramInfo(prg, CL_PROGRAM_NUM_DEVICES, sizeof(num_devices), &num_devices, NULL)); |
|
|
|
CL_CHECK(clGetProgramInfo(prg, CL_PROGRAM_NUM_DEVICES, sizeof(num_devices), &num_devices, NULL)); |
|
|
|
assert(num_devices == 1); |
|
|
|
assert(num_devices == 1); |
|
|
@ -166,20 +135,55 @@ static std::vector<uint8_t> get_program_binary(cl_program prg) { |
|
|
|
assert(binary_size > 0); |
|
|
|
assert(binary_size > 0); |
|
|
|
|
|
|
|
|
|
|
|
std::vector<uint8_t> binary_buf(binary_size); |
|
|
|
std::vector<uint8_t> binary_buf(binary_size); |
|
|
|
uint8_t* bufs[1] = { &binary_buf[0], }; |
|
|
|
uint8_t* bufs[1] = { |
|
|
|
|
|
|
|
&binary_buf[0], |
|
|
|
|
|
|
|
}; |
|
|
|
CL_CHECK(clGetProgramInfo(prg, CL_PROGRAM_BINARIES, sizeof(bufs), &bufs, NULL)); |
|
|
|
CL_CHECK(clGetProgramInfo(prg, CL_PROGRAM_BINARIES, sizeof(bufs), &bufs, NULL)); |
|
|
|
|
|
|
|
|
|
|
|
return binary_buf; |
|
|
|
return binary_buf; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void add_index(uint64_t index_hash, uint64_t src_hash) { |
|
|
|
void add_index(uint64_t index_hash, uint64_t src_hash) { |
|
|
|
FILE* f = fopen(CL_IDX_CACHE_FILE, "a"); |
|
|
|
FILE* f = fopen(CL_IDX_CACHE_FILE, "a"); |
|
|
|
assert(f); |
|
|
|
assert(f); |
|
|
|
fprintf(f, "%016" PRIx64 " %016" PRIx64 "\n", index_hash, src_hash); |
|
|
|
fprintf(f, "%016" PRIx64 " %016" PRIx64 "\n", index_hash, src_hash); |
|
|
|
fclose(f); |
|
|
|
fclose(f); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cl_device_id cl_get_device_id(cl_device_type device_type) { |
|
|
|
|
|
|
|
bool opencl_platform_found = false; |
|
|
|
|
|
|
|
cl_device_id device_id = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cl_uint num_platforms = 0; |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformIDs(0, NULL, &num_platforms)); |
|
|
|
|
|
|
|
std::unique_ptr<cl_platform_id[]> platform_ids = std::make_unique<cl_platform_id[]>(num_platforms); |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformIDs(num_platforms, &platform_ids[0], NULL)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char cBuffer[1024]; |
|
|
|
|
|
|
|
for (size_t i = 0; i < num_platforms; i++) { |
|
|
|
|
|
|
|
CL_CHECK(clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL)); |
|
|
|
|
|
|
|
printf("platform[%zu] CL_PLATFORM_NAME: %s\n", i, cBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cl_uint num_devices; |
|
|
|
|
|
|
|
int err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices); |
|
|
|
|
|
|
|
if (err != 0 || !num_devices) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Get first device
|
|
|
|
|
|
|
|
CL_CHECK(clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL)); |
|
|
|
|
|
|
|
cl_print_info(platform_ids[i], device_id); |
|
|
|
|
|
|
|
opencl_platform_found = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!opencl_platform_found) { |
|
|
|
|
|
|
|
printf("No valid openCL platform found\n"); |
|
|
|
|
|
|
|
assert(opencl_platform_found); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return device_id; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cl_program cl_index_program_from_string(cl_context ctx, cl_device_id device_id, |
|
|
|
cl_program cl_index_program_from_string(cl_context ctx, cl_device_id device_id, |
|
|
|
const char* src, const char* args, |
|
|
|
const char* src, const char* args, |
|
|
|