Compute Library
 23.08
CLHelpers.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 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 
26 
28 #include "arm_compute/core/Error.h"
31 
32 namespace
33 {
34 #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
35 void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data)
36 {
37  ARM_COMPUTE_UNUSED(complete, user_data);
38  printf("%.*s", len, buffer);
39 }
40 #endif /* defined(ARM_COMPUTE_ASSERTS_ENABLED) */
41 
42 /** This initialises the properties vector with the configuration to be used when creating the opencl context
43  *
44  * @param[in] platform The opencl platform used to create the context
45  * @param[in] device The opencl device to be used to create the context
46  * @param[in] prop An array of properties to be initialised
47  *
48  * @note In debug builds, this function will enable cl_arm_printf if it's supported.
49  *
50  * @return A pointer to the context properties which can be used to create an opencl context
51  */
52 
53 void initialise_context_properties(const cl::Platform &platform, const cl::Device &device, std::array<cl_context_properties, 7> &prop)
54 {
55  ARM_COMPUTE_UNUSED(device);
56 #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
57  // Query devices in the context for cl_arm_printf support
58  if(arm_compute::device_supports_extension(device, "cl_arm_printf"))
59  {
60  // Create a cl_context with a printf_callback and user specified buffer size.
61  std::array<cl_context_properties, 7> properties_printf =
62  {
63  CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
64  // Enable a printf callback function for this context.
65  CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
66  // Request a minimum printf buffer size of 4MB for devices in the
67  // context that support this extension.
68  CL_PRINTF_BUFFERSIZE_ARM, 0x1000,
69  0
70  };
71  prop = properties_printf;
72  }
73  else
74 #endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
75  {
76  std::array<cl_context_properties, 3> properties =
77  {
78  CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
79  0
80  };
81  std::copy(properties.begin(), properties.end(), prop.begin());
82  };
83 }
84 } //namespace
85 
86 namespace arm_compute
87 {
88 cl::Platform select_preferable_platform(CLBackendType cl_backend_type)
89 {
90  std::vector<cl::Platform> platforms;
91  cl::Platform::get(&platforms);
92  ARM_COMPUTE_ERROR_ON_MSG(platforms.size() == 0, "Couldn't find any OpenCL platform");
93 
94  cl::Platform selected_platform{ nullptr };
95 
96  // If the user has selected the Native platform, return the first available.
97  switch(cl_backend_type)
98  {
100  selected_platform = platforms[0];
101  break;
102  case CLBackendType::Clvk:
103  for(auto p : platforms)
104  {
105  std::string res = p.getInfo<CL_PLATFORM_NAME>();
106  if(res.find("clvk") != std::string::npos)
107  {
108  selected_platform = p;
109  break;
110  }
111  }
112  break;
113  default:
114  ARM_COMPUTE_ERROR("Unsupported backend type");
115  }
116 
117  if(!selected_platform())
118  {
119  ARM_COMPUTE_ERROR("No valid platform found");
120  }
121 
122  return selected_platform;
123 }
124 
125 std::tuple<cl::Context, cl::Device, cl_int>
127 {
129  cl::Platform p = select_preferable_platform(cl_backend_type);
130  cl::Device device;
131  std::vector<cl::Device> platform_devices;
132  p.getDevices(CL_DEVICE_TYPE_DEFAULT, &platform_devices);
133  ARM_COMPUTE_ERROR_ON_MSG(platform_devices.size() == 0, "Couldn't find any OpenCL device");
134  device = platform_devices[0];
135  cl_int err = CL_SUCCESS;
136  std::array<cl_context_properties, 7> properties = { 0, 0, 0, 0, 0, 0, 0 };
137  initialise_context_properties(p, device, properties);
138  cl::Context cl_context = cl::Context(device, properties.data(), nullptr, nullptr, &err);
139  ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
140  return std::make_tuple(cl_context, device, err);
141 }
142 
143 void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush)
144 {
146  if(ctx)
147  {
148  ARM_COMPUTE_ERROR_ON(ctx->gpu_scheduler() == nullptr);
149  ctx->gpu_scheduler()->enqueue(*kernel, flush);
150  }
151  else
152  {
153  CLScheduler::get().enqueue(*kernel, flush);
154  }
155 }
156 
157 } // namespace arm_compute
arm_compute::opencl_is_available
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:203
arm_compute::create_opencl_context_and_device
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device(CLBackendType cl_backend_type)
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:126
arm_compute::CLScheduler::enqueue
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:205
CLRuntimeContext.h
arm_compute::schedule_kernel_on_ctx
void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush=true)
Schedules a kernel using the context if not nullptr else uses the legacy scheduling flow.
Definition: CLHelpers.cpp:143
arm_compute::CLRuntimeContext::gpu_scheduler
CLScheduler * gpu_scheduler()
Definition: CLRuntimeContext.cpp:56
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:353
Error.h
arm_compute::CLBackendType::Clvk
@ Clvk
CLVK backend.
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:457
arm_compute::test::validation::reference::copy
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
CLHelpers.h
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::ICLKernel
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:67
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:103
arm_compute::CLBackendType
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:55
arm_compute::CLBackendType::Native
@ Native
OpenCL native backend.
arm_compute::device_supports_extension
bool device_supports_extension(const cl::Device &device, const char *extension_name)
Helper function to check whether a given extension is supported.
Definition: CLHelpers.cpp:283
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::select_preferable_platform
cl::Platform select_preferable_platform(CLBackendType cl_backend_type)
This function selects the OpenCL platform based on the backend type.
Definition: CLHelpers.cpp:88
Validate.h
CLHelpers.h
arm_compute::CLRuntimeContext
Runtime context.
Definition: CLRuntimeContext.h:38