using namespace group help functions

pull/2744/head
deanlee 5 years ago
parent 7f6dd0e5b1
commit 5fe8d77ae4
  1. 84
      selfdrive/common/clutil.cc
  2. 11
      selfdrive/common/clutil.h

@ -32,41 +32,10 @@ void clu_init(void) {
unlink(CL_IDX_CACHE_FILE);
#endif
}
namespace {
// helper functions
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;
}
static std::string get_version_string(cl_platform_id platform) {
std::string get_version_string(cl_platform_id platform) {
size_t size = 0;
CL_CHECK(clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 0, NULL, &size));
std::string version;
@ -138,7 +107,7 @@ void cl_print_build_errors(cl_program program, cl_device_id device) {
}
#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];
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;
}
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_CHECK(clGetProgramInfo(prg, CL_PROGRAM_NUM_DEVICES, sizeof(num_devices), &num_devices, NULL));
assert(num_devices == 1);
@ -166,20 +135,55 @@ static std::vector<uint8_t> get_program_binary(cl_program prg) {
assert(binary_size > 0);
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));
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");
assert(f);
fprintf(f, "%016" PRIx64 " %016" PRIx64 "\n", index_hash, src_hash);
fclose(f);
}
#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,
const char* src, const char* args,

@ -30,24 +30,13 @@ extern "C" {
void clu_init(void);
cl_device_id cl_get_device_id(cl_device_type device_type);
void cl_print_info(cl_platform_id platform, cl_device_id device);
cl_program cl_index_program_from_string(cl_context ctx, cl_device_id device_id,
const char* src, const char* args,
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* file, int line, const char* function);
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;
}
#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) \

Loading…
Cancel
Save