From 725c5e742f85f04d6e4142f555362b4698035e53 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Mon, 15 Jun 2020 15:58:12 -0700 Subject: [PATCH] Modeld: loop over platforms to find CPU/GPU (#1710) * Modeld: loop over platforms to find gpu * Remove prints * PC needs CPU * Keep default for qcom * Update error message old-commit-hash: 4ca8283d739ab31e2fdbd2aeb658cb4f0d2e1bcc --- selfdrive/modeld/modeld.cc | 63 ++++++++++++++++++++++++++------------ tools/webcam/README.md | 1 - 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/selfdrive/modeld/modeld.cc b/selfdrive/modeld/modeld.cc index 7206f5a9e8..c383d5cffd 100644 --- a/selfdrive/modeld/modeld.cc +++ b/selfdrive/modeld/modeld.cc @@ -84,40 +84,63 @@ int main(int argc, char **argv) { PubMaster pm({"model", "cameraOdometry"}); SubMaster sm({"pathPlan"}); +#ifdef QCOM + cl_device_type device_type = CL_DEVICE_TYPE_DEFAULT; +#else + cl_device_type device_type = CL_DEVICE_TYPE_CPU; +#endif + // cl init cl_device_id device_id; cl_context context; cl_command_queue q; { - // TODO: refactor this - cl_platform_id platform_id[2]; - cl_uint num_devices; cl_uint num_platforms; + err = clGetPlatformIDs(0, NULL, &num_platforms); + assert(err == 0); - err = clGetPlatformIDs(sizeof(platform_id)/sizeof(cl_platform_id), platform_id, &num_platforms); + cl_platform_id * platform_ids = new cl_platform_id[num_platforms]; + err = clGetPlatformIDs(num_platforms, platform_ids, NULL); assert(err == 0); - #ifdef QCOM - int clPlatform = 0; - #else - // don't use nvidia on pc, it's broken - // TODO: write this nicely - int clPlatform = num_platforms-1; - #endif + LOGD("got %d opencl platform(s)", num_platforms); char cBuffer[1024]; - clGetPlatformInfo(platform_id[clPlatform], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL); - LOG("got %d opencl platform(s), using %s", num_platforms, cBuffer); + bool opencl_platform_found = false; - err = clGetDeviceIDs(platform_id[clPlatform], CL_DEVICE_TYPE_DEFAULT, 1, - &device_id, &num_devices); - assert(err == 0); + 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); + } - context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err); - assert(err == 0); - q = clCreateCommandQueue(context, device_id, 0, &err); - assert(err == 0); LOGD("opencl init complete"); } diff --git a/tools/webcam/README.md b/tools/webcam/README.md index 9243a79061..c1e7d27d07 100644 --- a/tools/webcam/README.md +++ b/tools/webcam/README.md @@ -19,7 +19,6 @@ git clone https://github.com/commaai/openpilot.git - Add line "export PYTHONPATH=$HOME/openpilot" to your ~/.bashrc - Install tensorflow 2.2 and nvidia drivers: nvidia-xxx/cuda10.0/cudnn7.6.5 - Install [OpenCL Driver](http://registrationcenter-download.intel.com/akdlm/irc_nas/vcp/15532/l_opencl_p_18.1.0.015.tgz) -- (Note: the code assumes cl platforms order to be 0.GPU/1.CPU when running clinfo; if reverse, change the -1 to -2 in selfdrive/modeld/modeld.cc#L130; helping us refactor this mess is encouraged) - Install [OpenCV4](https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/) (ignore the Python part) ## Build openpilot for webcam