1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2019-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust
25*c217d954SCole Faust #include "arm_compute/runtime/CL/CLHelpers.h"
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h"
28*c217d954SCole Faust #include "arm_compute/core/Error.h"
29*c217d954SCole Faust #include "arm_compute/core/Validate.h"
30*c217d954SCole Faust #include "arm_compute/runtime/CL/CLRuntimeContext.h"
31*c217d954SCole Faust
32*c217d954SCole Faust namespace
33*c217d954SCole Faust {
34*c217d954SCole Faust #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
printf_callback(const char * buffer,unsigned int len,size_t complete,void * user_data)35*c217d954SCole Faust void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data)
36*c217d954SCole Faust {
37*c217d954SCole Faust ARM_COMPUTE_UNUSED(complete, user_data);
38*c217d954SCole Faust printf("%.*s", len, buffer);
39*c217d954SCole Faust }
40*c217d954SCole Faust #endif /* defined(ARM_COMPUTE_ASSERTS_ENABLED) */
41*c217d954SCole Faust
42*c217d954SCole Faust /** This initialises the properties vector with the configuration to be used when creating the opencl context
43*c217d954SCole Faust *
44*c217d954SCole Faust * @param[in] platform The opencl platform used to create the context
45*c217d954SCole Faust * @param[in] device The opencl device to be used to create the context
46*c217d954SCole Faust * @param[in] prop An array of properties to be initialised
47*c217d954SCole Faust *
48*c217d954SCole Faust * @note In debug builds, this function will enable cl_arm_printf if it's supported.
49*c217d954SCole Faust *
50*c217d954SCole Faust * @return A pointer to the context properties which can be used to create an opencl context
51*c217d954SCole Faust */
52*c217d954SCole Faust
initialise_context_properties(const cl::Platform & platform,const cl::Device & device,std::array<cl_context_properties,7> & prop)53*c217d954SCole Faust void initialise_context_properties(const cl::Platform &platform, const cl::Device &device, std::array<cl_context_properties, 7> &prop)
54*c217d954SCole Faust {
55*c217d954SCole Faust ARM_COMPUTE_UNUSED(device);
56*c217d954SCole Faust #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
57*c217d954SCole Faust // Query devices in the context for cl_arm_printf support
58*c217d954SCole Faust if(arm_compute::device_supports_extension(device, "cl_arm_printf"))
59*c217d954SCole Faust {
60*c217d954SCole Faust // Create a cl_context with a printf_callback and user specified buffer size.
61*c217d954SCole Faust std::array<cl_context_properties, 7> properties_printf =
62*c217d954SCole Faust {
63*c217d954SCole Faust CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
64*c217d954SCole Faust // Enable a printf callback function for this context.
65*c217d954SCole Faust CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
66*c217d954SCole Faust // Request a minimum printf buffer size of 4MB for devices in the
67*c217d954SCole Faust // context that support this extension.
68*c217d954SCole Faust CL_PRINTF_BUFFERSIZE_ARM, 0x1000,
69*c217d954SCole Faust 0
70*c217d954SCole Faust };
71*c217d954SCole Faust prop = properties_printf;
72*c217d954SCole Faust }
73*c217d954SCole Faust else
74*c217d954SCole Faust #endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
75*c217d954SCole Faust {
76*c217d954SCole Faust std::array<cl_context_properties, 3> properties =
77*c217d954SCole Faust {
78*c217d954SCole Faust CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
79*c217d954SCole Faust 0
80*c217d954SCole Faust };
81*c217d954SCole Faust std::copy(properties.begin(), properties.end(), prop.begin());
82*c217d954SCole Faust };
83*c217d954SCole Faust }
84*c217d954SCole Faust } //namespace
85*c217d954SCole Faust
86*c217d954SCole Faust namespace arm_compute
87*c217d954SCole Faust {
select_preferable_platform(CLBackendType cl_backend_type)88*c217d954SCole Faust cl::Platform select_preferable_platform(CLBackendType cl_backend_type)
89*c217d954SCole Faust {
90*c217d954SCole Faust std::vector<cl::Platform> platforms;
91*c217d954SCole Faust cl::Platform::get(&platforms);
92*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(platforms.size() == 0, "Couldn't find any OpenCL platform");
93*c217d954SCole Faust
94*c217d954SCole Faust cl::Platform selected_platform{ nullptr };
95*c217d954SCole Faust
96*c217d954SCole Faust // If the user has selected the Native platform, return the first available.
97*c217d954SCole Faust switch(cl_backend_type)
98*c217d954SCole Faust {
99*c217d954SCole Faust case CLBackendType::Native:
100*c217d954SCole Faust selected_platform = platforms[0];
101*c217d954SCole Faust break;
102*c217d954SCole Faust case CLBackendType::Clvk:
103*c217d954SCole Faust for(auto p : platforms)
104*c217d954SCole Faust {
105*c217d954SCole Faust std::string res = p.getInfo<CL_PLATFORM_NAME>();
106*c217d954SCole Faust if(res.find("clvk") != std::string::npos)
107*c217d954SCole Faust {
108*c217d954SCole Faust selected_platform = p;
109*c217d954SCole Faust break;
110*c217d954SCole Faust }
111*c217d954SCole Faust }
112*c217d954SCole Faust break;
113*c217d954SCole Faust default:
114*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported backend type");
115*c217d954SCole Faust }
116*c217d954SCole Faust
117*c217d954SCole Faust if(!selected_platform())
118*c217d954SCole Faust {
119*c217d954SCole Faust ARM_COMPUTE_ERROR("No valid platform found");
120*c217d954SCole Faust }
121*c217d954SCole Faust
122*c217d954SCole Faust return selected_platform;
123*c217d954SCole Faust }
124*c217d954SCole Faust
125*c217d954SCole Faust std::tuple<cl::Context, cl::Device, cl_int>
create_opencl_context_and_device(CLBackendType cl_backend_type)126*c217d954SCole Faust create_opencl_context_and_device(CLBackendType cl_backend_type)
127*c217d954SCole Faust {
128*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!opencl_is_available());
129*c217d954SCole Faust cl::Platform p = select_preferable_platform(cl_backend_type);
130*c217d954SCole Faust cl::Device device;
131*c217d954SCole Faust std::vector<cl::Device> platform_devices;
132*c217d954SCole Faust p.getDevices(CL_DEVICE_TYPE_DEFAULT, &platform_devices);
133*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(platform_devices.size() == 0, "Couldn't find any OpenCL device");
134*c217d954SCole Faust device = platform_devices[0];
135*c217d954SCole Faust cl_int err = CL_SUCCESS;
136*c217d954SCole Faust std::array<cl_context_properties, 7> properties = { 0, 0, 0, 0, 0, 0, 0 };
137*c217d954SCole Faust initialise_context_properties(p, device, properties);
138*c217d954SCole Faust cl::Context cl_context = cl::Context(device, properties.data(), nullptr, nullptr, &err);
139*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
140*c217d954SCole Faust return std::make_tuple(cl_context, device, err);
141*c217d954SCole Faust }
142*c217d954SCole Faust
schedule_kernel_on_ctx(CLRuntimeContext * ctx,ICLKernel * kernel,bool flush)143*c217d954SCole Faust void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush)
144*c217d954SCole Faust {
145*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(kernel);
146*c217d954SCole Faust if(ctx)
147*c217d954SCole Faust {
148*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(ctx->gpu_scheduler() == nullptr);
149*c217d954SCole Faust ctx->gpu_scheduler()->enqueue(*kernel, flush);
150*c217d954SCole Faust }
151*c217d954SCole Faust else
152*c217d954SCole Faust {
153*c217d954SCole Faust CLScheduler::get().enqueue(*kernel, flush);
154*c217d954SCole Faust }
155*c217d954SCole Faust }
156*c217d954SCole Faust
157*c217d954SCole Faust } // namespace arm_compute
158