From 8b045094613b69afbd14b728afa429f3eae8c566 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Principal=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Mon, 3 Sep 2018 14:04:43 +0900 Subject: [PATCH] [tool] OpenCL info (#2549) This tool shows information from OpenCL : info of paltform, context and devices. Signed-off-by: Hyun Sik Yoon --- tools/CMakeLists.txt | 1 + tools/opencl_tool/CMakeLists.txt | 12 +++ tools/opencl_tool/src/opencl_info.cc | 154 +++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 tools/opencl_tool/CMakeLists.txt create mode 100644 tools/opencl_tool/src/opencl_info.cc diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 4b1c073..f57262f 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -6,3 +6,4 @@ endif() add_subdirectory(tflite_examples) add_subdirectory(nnapi_test) add_subdirectory(nnapi_quickcheck) +add_subdirectory(opencl_tool) diff --git a/tools/opencl_tool/CMakeLists.txt b/tools/opencl_tool/CMakeLists.txt new file mode 100644 index 0000000..66b9285 --- /dev/null +++ b/tools/opencl_tool/CMakeLists.txt @@ -0,0 +1,12 @@ +if(NOT ${TARGET_ARCH_BASE} STREQUAL "arm") + return() +endif(NOT ${TARGET_ARCH_BASE} STREQUAL "arm") + +list(APPEND OPENCL_INFO_SOURCE "src/opencl_info.cc") + +add_executable(opencl_info ${OPENCL_INFO_SOURCE}) +target_include_directories(opencl_info PUBLIC ${CMAKE_SOURCE_DIR}/externals/acl) +target_include_directories(opencl_info PUBLIC ${CMAKE_SOURCE_DIR}/externals/acl/include) +target_link_libraries(opencl_info arm_compute) + +install(TARGETS opencl_info DESTINATION bin) diff --git a/tools/opencl_tool/src/opencl_info.cc b/tools/opencl_tool/src/opencl_info.cc new file mode 100644 index 0000000..49673d1 --- /dev/null +++ b/tools/opencl_tool/src/opencl_info.cc @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/******************************************************************************* + * Copyright (c) 2008-2015 The Khronos Group Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and/or associated documentation files (the + * "Materials"), to deal in the Materials without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Materials, and to + * permit persons to whom the Materials are furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Materials. + * + * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. + ******************************************************************************/ + +#include "arm_compute/core/CL/OpenCL.h" + +#include +#include + +void printDeviceInfo(int n, cl::Device &device, cl::Device &default_device) +{ + bool is_default = (device() == default_device()); + std::cout << "\t\t\t#" << n << " Device: (id: " << device() << ") " + << (is_default ? " -> default" : "") << "\n"; + + const auto name = device.getInfo(); + std::cout << "\t\t\t\tName: " << name << "\n"; + + const auto compute_unit = device.getInfo(); + std::cout << "\t\t\t\tMax Compute Unit: " << compute_unit << "\n"; + + const auto max_work_item_size = device.getInfo(); + std::cout << "\t\t\t\tMax Work Item Size: ["; + for (auto size : max_work_item_size) + std::cout << size << ","; + std::cout << "]\n"; + + const auto max_work_group_size = device.getInfo(); + std::cout << "\t\t\t\tMax Work Grpup Size: " << max_work_group_size << "\n"; + + const auto max_clock_frequency = device.getInfo(); + std::cout << "\t\t\t\tMax Clock Frequency: " << max_clock_frequency << "\n"; +} + +void printContext(int n, cl::Platform &plat, int device_type, cl::Context &default_context) +{ + if (device_type == CL_DEVICE_TYPE_DEFAULT) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_DEFAULT"; + else if (device_type == CL_DEVICE_TYPE_GPU) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_GPU"; + else if (device_type == CL_DEVICE_TYPE_CPU) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_CPU"; + else if (device_type == CL_DEVICE_TYPE_ACCELERATOR) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_ACCELERATOR"; + else if (device_type == CL_DEVICE_TYPE_CUSTOM) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_CUSTOM"; + else if (device_type == CL_DEVICE_TYPE_ALL) + std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_ALL"; + + cl::Context context; + + try + { + cl_context_properties properties[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)plat(), 0}; + + cl_int default_error; + + context = cl::Context(device_type, properties, NULL, NULL, &default_error); + } + catch (cl::Error &err) // thrown when there is no Context for this platform + { + std::cout << "\t\t No Context Found\n"; + return; + } + + bool is_default = (context() == default_context()); + + std::cout << " (id: " << context() << ") " << (is_default ? " -> default" : "") << "\n"; + + const auto device_num = context.getInfo(); + std::cout << "\t\t\tDevice num: " << device_num << "\n"; + if (device_num == 0) + return; + + auto devices = context.getInfo(); + auto default_device = cl::Device::getDefault(); + + int d = 0; + for (auto device : devices) + printDeviceInfo(++d, device, default_device); +} + +void printPlatform(int n, cl::Platform &plat, cl::Platform &default_platform) +{ + bool is_default = (plat() == default_platform()); + + std::cout << "#" << n << ". Platform: (id: " << plat() << ") " + << (is_default ? " -> default" : "") << "\n"; + + cl::Context default_context = cl::Context::getDefault(); + std::cout << "\t" + << "default context: " << default_context() << "\n"; + + int x = 0; + printContext(++x, plat, CL_DEVICE_TYPE_DEFAULT, default_context); + printContext(++x, plat, CL_DEVICE_TYPE_GPU, default_context); + printContext(++x, plat, CL_DEVICE_TYPE_CPU, default_context); + printContext(++x, plat, CL_DEVICE_TYPE_ACCELERATOR, default_context); + printContext(++x, plat, CL_DEVICE_TYPE_CUSTOM, default_context); + printContext(++x, plat, CL_DEVICE_TYPE_ALL, default_context); +} + +int main(const int argc, char **argv) +{ + std::cout << "\nOpenCL Platform, Context, Device Info are as follows:\n\n"; + + std::vector platforms; + cl::Platform::get(&platforms); + + cl::Platform defaultPlatform = cl::Platform::getDefault(); + + int n = 0; + for (auto &p : platforms) + { + printPlatform(++n, p, defaultPlatform); + } + + return 0; +} -- 2.7.4