[tool] OpenCL info (#2549)
author윤현식/동작제어Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Mon, 3 Sep 2018 05:04:43 +0000 (14:04 +0900)
committer박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 3 Sep 2018 05:04:43 +0000 (14:04 +0900)
This tool shows information from OpenCL : info of paltform, context and devices.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
tools/CMakeLists.txt
tools/opencl_tool/CMakeLists.txt [new file with mode: 0644]
tools/opencl_tool/src/opencl_info.cc [new file with mode: 0644]

index 4b1c073..f57262f 100644 (file)
@@ -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 (file)
index 0000000..66b9285
--- /dev/null
@@ -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 (file)
index 0000000..49673d1
--- /dev/null
@@ -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 <iostream>
+#include <vector>
+
+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<CL_DEVICE_NAME>();
+  std::cout << "\t\t\t\tName: " << name << "\n";
+
+  const auto compute_unit = device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
+  std::cout << "\t\t\t\tMax Compute Unit: " << compute_unit << "\n";
+
+  const auto max_work_item_size = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
+  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<CL_DEVICE_MAX_WORK_GROUP_SIZE>();
+  std::cout << "\t\t\t\tMax Work Grpup Size: " << max_work_group_size << "\n";
+
+  const auto max_clock_frequency = device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>();
+  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<CL_CONTEXT_NUM_DEVICES>();
+  std::cout << "\t\t\tDevice num: " << device_num << "\n";
+  if (device_num == 0)
+    return;
+
+  auto devices = context.getInfo<CL_CONTEXT_DEVICES>();
+  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<cl::Platform> platforms;
+  cl::Platform::get(&platforms);
+
+  cl::Platform defaultPlatform = cl::Platform::getDefault();
+
+  int n = 0;
+  for (auto &p : platforms)
+  {
+    printPlatform(++n, p, defaultPlatform);
+  }
+
+  return 0;
+}