ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
opencl_info.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*******************************************************************************
18 * Copyright (c) 2008-2015 The Khronos Group Inc.
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a
21 * copy of this software and/or associated documentation files (the
22 * "Materials"), to deal in the Materials without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Materials, and to
25 * permit persons to whom the Materials are furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included
29 * in all copies or substantial portions of the Materials.
30 *
31 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
36 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
37 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
38 ******************************************************************************/
39
40#include "arm_compute/core/CL/OpenCL.h"
41
42#include <iostream>
43#include <vector>
44
45void printDeviceInfo(int n, cl::Device &device)
46{
47 std::cout << "\t\t\t#" << n << " Device: (id: " << device() << ")\n";
48
49 const auto name = device.getInfo<CL_DEVICE_NAME>();
50 std::cout << "\t\t\t\tName: " << name << "\n";
51
52 const auto compute_unit = device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
53 std::cout << "\t\t\t\tMax Compute Unit: " << compute_unit << "\n";
54
55 const auto max_work_item_size = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
56 std::cout << "\t\t\t\tMax Work Item Size: [";
57 for (auto size : max_work_item_size)
58 std::cout << size << ",";
59 std::cout << "]\n";
60
61 const auto max_work_group_size = device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>();
62 std::cout << "\t\t\t\tMax Work Grpup Size: " << max_work_group_size << "\n";
63
64 const auto max_clock_frequency = device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>();
65 std::cout << "\t\t\t\tMax Clock Frequency: " << max_clock_frequency << "\n";
66}
67
68void printContext(int n, cl::Platform &plat, int device_type)
69{
70 if (device_type == CL_DEVICE_TYPE_DEFAULT)
71 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_DEFAULT";
72 else if (device_type == CL_DEVICE_TYPE_GPU)
73 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_GPU";
74 else if (device_type == CL_DEVICE_TYPE_CPU)
75 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_CPU";
76 else if (device_type == CL_DEVICE_TYPE_ACCELERATOR)
77 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_ACCELERATOR";
78 else if (device_type == CL_DEVICE_TYPE_CUSTOM)
79 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_CUSTOM";
80 else if (device_type == CL_DEVICE_TYPE_ALL)
81 std::cout << "\t #" << n << " context when CL_DEVICE_TYPE_ALL";
82
83 cl::Context context;
84
85 try
86 {
87 cl_context_properties properties[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)plat(), 0};
88
89 cl_int default_error;
90
91 context = cl::Context(device_type, properties, NULL, NULL, &default_error);
92 }
93 catch (cl::Error &err) // thrown when there is no Context for this platform
94 {
95 std::cout << "\t\t No Context Found\n";
96 return;
97 }
98
99 std::cout << " (id: " << context() << ")\n";
100
101 const auto device_num = context.getInfo<CL_CONTEXT_NUM_DEVICES>();
102 std::cout << "\t\t\tDevice num: " << device_num << "\n";
103 if (device_num == 0)
104 return;
105
106 auto devices = context.getInfo<CL_CONTEXT_DEVICES>();
107
108 int d = 0;
109 for (auto device : devices)
110 printDeviceInfo(++d, device);
111}
112
113void printPlatform(int n, cl::Platform &plat)
114{
115 std::cout << "#" << n << ". Platform: (id: " << plat() << ")\n";
116
117 cl::Context default_context;
118
119 int x = 0;
120 printContext(++x, plat, CL_DEVICE_TYPE_DEFAULT);
121 printContext(++x, plat, CL_DEVICE_TYPE_GPU);
122 printContext(++x, plat, CL_DEVICE_TYPE_CPU);
123 printContext(++x, plat, CL_DEVICE_TYPE_ACCELERATOR);
124 printContext(++x, plat, CL_DEVICE_TYPE_CUSTOM);
125 printContext(++x, plat, CL_DEVICE_TYPE_ALL);
126}
127
128int main(const int argc, char **argv)
129{
130 try
131 {
132 std::cout << "\nOpenCL Platform, Context, Device Info are as follows:\n\n";
133 {
134 std::vector<cl::Platform> platforms;
135 cl::Platform::get(&platforms);
136
137 int n = 0;
138 for (auto &p : platforms)
139 {
140 printPlatform(++n, p);
141 }
142 }
143
144 std::cout << "\n\nShowing default platfom, context, and device:\n"
145 << "(Note: this may throw an error depending on system or compiler)\n";
146 {
147 auto platform = cl::Platform::getDefault();
148 std::cout << "* default platform (id: " << platform() << ")\n";
149 auto context = cl::Context::getDefault();
150 std::cout << "* default context (id: " << context() << ")\n";
151 auto device = cl::Device::getDefault();
152 printDeviceInfo(0, device);
153 }
154 }
155 catch (cl::Error &err)
156 {
157 std::cerr << "cl::Error was thrown: Please refer to the info below:\n"
158 << "\terror code: " << err.err() << ", meaning: " << err.what() << std::endl;
159 }
160 catch (std::system_error &err)
161 {
162 std::cerr << "std::system_error was thrown: Please refer to the info below:\n"
163 << "\terror code: " << err.code() << ", meaning: " << err.what() << std::endl;
164 }
165
166 return 0;
167}
int main(void)
int32_t size[5]
Definition Slice.cpp:35
void printPlatform(int n, cl::Platform &plat)
void printContext(int n, cl::Platform &plat, int device_type)
void printDeviceInfo(int n, cl::Device &device)
Configuration p