diff --git a/selfdrive/camerad/main.cc b/selfdrive/camerad/main.cc index cc1b7a6dda..8038b82322 100644 --- a/selfdrive/camerad/main.cc +++ b/selfdrive/camerad/main.cc @@ -1010,19 +1010,7 @@ cl_program build_pool_program(VisionState *s, void cl_init(VisionState *s) { int err; - cl_platform_id platform_id = NULL; - cl_uint num_devices; - cl_uint num_platforms; - - err = clGetPlatformIDs(1, &platform_id, &num_platforms); - assert(err == 0); - err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, - &s->device_id, &num_devices); - assert(err == 0); - - cl_print_info(platform_id, s->device_id); - printf("\n"); - + s->device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT); s->context = clCreateContext(NULL, 1, &s->device_id, NULL, NULL, &err); assert(err == 0); } diff --git a/selfdrive/camerad/test/yuv_bench/yuv_bench.cc b/selfdrive/camerad/test/yuv_bench/yuv_bench.cc index 22e71c4128..62860ea7cc 100644 --- a/selfdrive/camerad/test/yuv_bench/yuv_bench.cc +++ b/selfdrive/camerad/test/yuv_bench/yuv_bench.cc @@ -38,27 +38,8 @@ int main() { // init cl - /* Get Platform and Device Info */ - cl_platform_id platform_id = NULL; - cl_uint num_platforms_unused; - int err = clGetPlatformIDs(1, &platform_id, &num_platforms_unused); - if (err != 0) { - fprintf(stderr, "cl error: %d\n", err); - } - assert(err == 0); - - cl_device_id device_id = NULL; - cl_uint num_devices_unused; - err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, - &num_devices_unused); - if (err != 0) { - fprintf(stderr, "cl error: %d\n", err); - } - assert(err == 0); - - cl_print_info(platform_id, device_id); - printf("\n"); - + int err; + cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT); cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); assert(err == 0); diff --git a/selfdrive/camerad/transforms/rgb_to_yuv_test.cc b/selfdrive/camerad/transforms/rgb_to_yuv_test.cc index c8b8751058..9d68e5b9ef 100644 --- a/selfdrive/camerad/transforms/rgb_to_yuv_test.cc +++ b/selfdrive/camerad/transforms/rgb_to_yuv_test.cc @@ -42,14 +42,7 @@ static inline double millis_since_boot() { void cl_init(cl_device_id &device_id, cl_context &context) { int err; - cl_platform_id platform_id = NULL; - cl_uint num_devices; - cl_uint num_platforms; - - err = clGetPlatformIDs(1, &platform_id, &num_platforms); - err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, - &device_id, &num_devices); - cl_print_info(platform_id, device_id); + device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT); context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); } diff --git a/selfdrive/common/clutil.c b/selfdrive/common/clutil.c index 9a80c9067b..00d63cdcac 100644 --- a/selfdrive/common/clutil.c +++ b/selfdrive/common/clutil.c @@ -36,6 +36,44 @@ void clu_init(void) { #endif } +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; + int err = clGetPlatformIDs(0, NULL, &num_platforms); + assert(err == 0); + cl_platform_id* platform_ids = malloc(sizeof(cl_platform_id) * num_platforms); + err = clGetPlatformIDs(num_platforms, platform_ids, NULL); + assert(err == 0); + + char cBuffer[1024]; + for (size_t i = 0; i < num_platforms; i++) { + err = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL); + assert(err == 0); + printf("platform[%zu] CL_PLATFORM_NAME: %s\n", i, cBuffer); + + cl_uint num_devices; + err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices); + if (err != 0 || !num_devices) { + continue; + } + // Get first device + err = clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL); + assert(err == 0); + cl_print_info(platform_ids[i], device_id); + opencl_platform_found = true; + break; + } + free(platform_ids); + + if (!opencl_platform_found) { + printf("No valid openCL platform found\n"); + assert(opencl_platform_found); + } + return device_id; +} + cl_program cl_create_program_from_file(cl_context ctx, const char* path) { char* src_buf = read_file(path, NULL); assert(src_buf); diff --git a/selfdrive/common/clutil.h b/selfdrive/common/clutil.h index b87961eacd..abbda8c806 100644 --- a/selfdrive/common/clutil.h +++ b/selfdrive/common/clutil.h @@ -17,6 +17,7 @@ extern "C" { void clu_init(void); +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); void cl_print_build_errors(cl_program program, cl_device_id device); diff --git a/selfdrive/modeld/modeld.cc b/selfdrive/modeld/modeld.cc index 65b702dbd2..3df4078a7e 100644 --- a/selfdrive/modeld/modeld.cc +++ b/selfdrive/modeld/modeld.cc @@ -6,6 +6,7 @@ #include "common/visionbuf.h" #include "common/visionipc.h" #include "common/swaglog.h" +#include "common/clutil.h" #include "models/driving.h" #include "messaging.hpp" @@ -96,58 +97,12 @@ int main(int argc, char **argv) { #endif // cl init - cl_device_id device_id; - cl_context context; - cl_command_queue q; - { - cl_uint num_platforms; - err = clGetPlatformIDs(0, NULL, &num_platforms); - assert(err == 0); - - cl_platform_id * platform_ids = new cl_platform_id[num_platforms]; - err = clGetPlatformIDs(num_platforms, platform_ids, NULL); - assert(err == 0); - - LOGD("got %d opencl platform(s)", num_platforms); - - char cBuffer[1024]; - bool opencl_platform_found = false; - - for (size_t i = 0; i < num_platforms; i++){ - err = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL); - assert(err == 0); - LOGD("platform[%zu] CL_PLATFORM_NAME: %s", i, cBuffer); - - cl_uint num_devices; - err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices); - if (err != 0|| !num_devices){ - continue; - } - - // Get first device - err = clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL); - assert(err == 0); - - context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); - assert(err == 0); - - q = clCreateCommandQueue(context, device_id, 0, &err); - assert(err == 0); - - opencl_platform_found = true; - break; - } - - delete[] platform_ids; - - if (!opencl_platform_found){ - LOGE("No valid openCL platform found"); - assert(opencl_platform_found); - } - + cl_device_id device_id = cl_get_device_id(device_type); + cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); + assert(err == 0); - LOGD("opencl init complete"); - } + cl_command_queue q = clCreateCommandQueue(context, device_id, 0, &err); + assert(err == 0); // init the models ModelState model; diff --git a/selfdrive/modeld/thneed/debug/main.cc b/selfdrive/modeld/thneed/debug/main.cc index 85934e22c7..a59fd91f3c 100644 --- a/selfdrive/modeld/thneed/debug/main.cc +++ b/selfdrive/modeld/thneed/debug/main.cc @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d967ffcacdd6c1262ca9769e464976cf772320be1bf1cae2b3c8cd8717cfb7a0 -size 24541 +oid sha256:b6fcf547efd5bd6d6a740d9b7415e7dacd6624910eb3447e1a2063b0bfb6fba8 +size 24327 diff --git a/selfdrive/modeld/visiontest.c b/selfdrive/modeld/visiontest.c index 325e257e86..2ce68ad9c1 100644 --- a/selfdrive/modeld/visiontest.c +++ b/selfdrive/modeld/visiontest.c @@ -33,28 +33,8 @@ typedef struct { void initialize_opencl(VisionTest* visiontest) { // init cl - /* Get Platform and Device Info */ - cl_platform_id platform_ids[16] = {0}; - cl_uint num_platforms; - int err = clGetPlatformIDs(16, platform_ids, &num_platforms); - if (err != 0) { - fprintf(stderr, "cl error: %d\n", err); - } - assert(err == 0); - - // try to find a CPU device - cl_device_id device_id = NULL; - for (int i=0; icontext = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); assert(err == 0);