• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_CLDEVICE_H
25 #define ARM_COMPUTE_CLDEVICE_H
26 
27 #include "arm_compute/core/CL/CLHelpers.h"
28 #include "arm_compute/core/CL/CLTypes.h"
29 #include "arm_compute/core/GPUTarget.h"
30 #include "arm_compute/core/IDevice.h"
31 
32 #include <set>
33 #include <string>
34 
35 namespace arm_compute
36 {
37 /**  OpenCL device type class
38  *
39  *   Initializes and stores all the information about a cl device,
40  *   working mainly as a cache mechanism.
41  * */
42 class CLDevice : public IDevice
43 {
44 public:
45     /** Default Constructor */
CLDevice()46     CLDevice()
47         : _device(cl::Device()), _options()
48     {
49     }
50 
51     /** Constructor
52      *
53      * @param[in] cl_device OpenCL device
54      */
CLDevice(const cl::Device & cl_device)55     CLDevice(const cl::Device &cl_device)
56         : _device(), _options()
57     {
58         _device = cl_device;
59 
60         // Get device target
61         std::string device_name = _device.getInfo<CL_DEVICE_NAME>();
62         _options.gpu_target     = get_target_from_name(device_name);
63 
64         // Fill extensions
65         std::string extensions = _device.getInfo<CL_DEVICE_EXTENSIONS>();
66 
67         std::istringstream iss(extensions);
68         for(std::string s; iss >> s;)
69         {
70             _options.extensions.insert(s);
71         }
72 
73         // SW workaround for G76
74         if(_options.gpu_target == GPUTarget::G76)
75         {
76             _options.extensions.insert("cl_arm_integer_dot_product_int8");
77         }
78 
79         // Get device version
80         _options.version = get_cl_version(_device);
81 
82         // Get compute units
83         _options.compute_units = _device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
84 
85         // Get device version
86         _options.device_version = _device.getInfo<CL_DEVICE_VERSION>();
87     }
88 
89     /** Returns the GPU target of the cl device
90      *
91      * @return The GPU target
92      */
target()93     const GPUTarget &target() const
94     {
95         return _options.gpu_target;
96     }
97 
98     /** Returns the number of compute units available
99      *
100      * @return Number of compute units
101      */
compute_units()102     size_t compute_units() const
103     {
104         return _options.compute_units;
105     }
106 
107     /** Returns the underlying cl device object
108      *
109      * @return A cl device
110      */
cl_device()111     const cl::Device &cl_device() const
112     {
113         return _device;
114     }
115 
116     /** Returns the device's CL version
117      *
118      * @return CLVersion of the device
119      */
version()120     CLVersion version() const
121     {
122         return _options.version;
123     }
124 
125     /** Returns the device version as a string
126      *
127      * @return CLVersion of the device
128      */
device_version()129     std::string device_version() const
130     {
131         return _options.device_version;
132     }
133 
134     // Inherrited methods
type()135     DeviceType type() const override
136     {
137         return DeviceType::CL;
138     }
139 
supported(const std::string & extension)140     bool supported(const std::string &extension) const override
141     {
142         return _options.extensions.count(extension) != 0;
143     }
144 
145 private:
146     cl::Device             _device;  /**< OpenCL device. */
147     struct CLDeviceOptions _options; /**< OpenCL device options */
148 };
149 
150 } // namespace arm_compute
151 
152 #endif /* ARM_COMPUTE_CLDEVICE_H */
153