blob: 365ecb06c488506a21a9ddb8a2e514b1584f7a3c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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_CLHELPERS_H__
25#define __ARM_COMPUTE_CLHELPERS_H__
26
27#include "arm_compute/core/CL/OpenCL.h"
28#include "arm_compute/core/Helpers.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010029#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31#include <string>
32
33namespace arm_compute
34{
35enum class DataType;
36enum class GPUTarget;
37
38/** Enable operation operations on GPUTarget enumerations */
39template <>
40struct enable_bitwise_ops<arm_compute::GPUTarget>
41{
42 static constexpr bool value = true;
43};
44
45/** Max vector width of an OpenCL vector */
Anthony Barbier7068f992017-10-26 15:23:08 +010046static constexpr unsigned int max_cl_vector_width = 16;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48/** Translates a tensor data type to the appropriate OpenCL type.
49 *
50 * @param[in] dt @ref DataType to be translated to OpenCL type.
51 *
52 * @return The string specifying the OpenCL type to be used.
53 */
54std::string get_cl_type_from_data_type(const DataType &dt);
55
SiCong Lic51b72f2017-07-28 14:46:20 +010056/** Get the size of a data type in number of bits.
57 *
58 * @param[in] dt @ref DataType.
59 *
60 * @return Number of bits in the data type specified.
61 */
62std::string get_data_size_from_data_type(const DataType &dt);
63
Georgios Pinitasac4e8732017-07-05 17:02:25 +010064/** Translates fixed point tensor data type to the underlying OpenCL type.
65 *
66 * @param[in] dt @ref DataType to be translated to OpenCL type.
67 *
68 * @return The string specifying the underlying OpenCL type to be used.
69 */
70std::string get_underlying_cl_type_from_data_type(const DataType &dt);
71
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072/** Translates a given gpu device target to string.
73 *
74 * @param[in] target Given gpu target.
75 *
76 * @return The string describing the target.
77 */
78const std::string &string_from_target(GPUTarget target);
79
80/** Helper function to create and return a unique_ptr pointed to a CL kernel object
81 * It also calls the kernel's configuration.
82 *
83 * @param[in] args All the arguments that need pass to kernel's configuration.
84 *
85 * @return A unique pointer pointed to a CL kernel object
86 */
87template <typename Kernel, typename... T>
88std::unique_ptr<Kernel> create_configure_kernel(T &&... args)
89{
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010090 std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 k->configure(std::forward<T>(args)...);
92 return k;
93}
94
95/** Helper function to create and return a unique_ptr pointed to a CL kernel object
96 *
97 * @return A unique pointer pointed to a CL kernel object
98 */
99template <typename Kernel>
100std::unique_ptr<Kernel> create_kernel()
101{
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100102 std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 return k;
104}
105
106/** Helper function to get the GPU target from CL device
107 *
108 * @param[in] device A CL device
109 *
110 * @return the GPU target
111 */
112GPUTarget get_target_from_device(cl::Device &device);
113
114/** Helper function to get the GPU arch
115 *
116 * @param[in] target GPU target
117 *
118 * @return the GPU target which shows the arch
119 */
120GPUTarget get_arch_from_target(GPUTarget target);
steniu0134702472017-07-11 09:22:58 +0100121
122/** Helper function to get the highest OpenCL version supported
123 *
124 * @param[in] device A CL device
125 *
126 * @return the highest OpenCL version supported
127 */
128CLVersion get_cl_version(const cl::Device &device);
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100129/** Helper function to check whether the cl_khr_fp16 extension is supported
130 *
131 * @param[in] device A CL device
132 *
133 * @return True if the extension is supported
134 */
135bool fp16_support(const cl::Device &device);
steniu0134702472017-07-11 09:22:58 +0100136/** Helper function to check whether the arm_non_uniform_work_group_size extension is supported
137 *
138 * @param[in] device A CL device
139 *
140 * @return True if the extension is supported
141 */
142bool non_uniform_workgroup_support(const cl::Device &device);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143}
Anthony Barbierac69aa12017-07-03 17:39:37 +0100144#endif /* __ARM_COMPUTE_CLHELPERS_H__ */