blob: c4c7ee21079703b4d255f4caf4e45f4e21dfc208 [file] [log] [blame]
Pablo Tellob7c308a2019-01-22 12:59:26 +00001/*
2 * Copyright (c) 2019 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
25#include "arm_compute/runtime/CL/CLHelpers.h"
26
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/Error.h"
Pablo Tellodb8485a2019-09-24 11:03:47 +010029#include "arm_compute/runtime/CL/CLRuntimeContext.h"
Pablo Tellob7c308a2019-01-22 12:59:26 +000030
31namespace
32{
33#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
34void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data)
35{
Michalis Spyrou6bff1952019-10-02 17:22:11 +010036 ARM_COMPUTE_UNUSED(complete, user_data);
Pablo Tellob7c308a2019-01-22 12:59:26 +000037 printf("%.*s", len, buffer);
38}
39#endif /* defined(ARM_COMPUTE_ASSERTS_ENABLED) */
40
41/** This initialises the properties vector with the configuration to be used when creating the opencl context
42 *
43 * @param[in] platform The opencl platform used to create the context
44 * @param[in] device The opencl device to be used to create the context
45 * @param[in] prop An array of properties to be initialised
46 *
47 * @note In debug builds, this function will enable cl_arm_printf if it's supported.
48 *
49 * @return A pointer to the context properties which can be used to create an opencl context
50 */
51
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010052void initialise_context_properties(const cl::Platform &platform, const cl::Device &device, std::array<cl_context_properties, 7> &prop)
Pablo Tellob7c308a2019-01-22 12:59:26 +000053{
54 ARM_COMPUTE_UNUSED(device);
55#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
56 // Query devices in the context for cl_arm_printf support
57 if(arm_compute::device_supports_extension(device, "cl_arm_printf"))
58 {
59 // Create a cl_context with a printf_callback and user specified buffer size.
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010060 std::array<cl_context_properties, 7> properties_printf =
Pablo Tellob7c308a2019-01-22 12:59:26 +000061 {
62 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
63 // Enable a printf callback function for this context.
64 CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
65 // Request a minimum printf buffer size of 4MB for devices in the
66 // context that support this extension.
67 CL_PRINTF_BUFFERSIZE_ARM, 0x1000,
68 0
69 };
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010070 prop = properties_printf;
Pablo Tellob7c308a2019-01-22 12:59:26 +000071 }
72 else
73#endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
74 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010075 std::array<cl_context_properties, 3> properties =
Pablo Tellob7c308a2019-01-22 12:59:26 +000076 {
77 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
78 0
79 };
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010080 std::copy(properties.begin(), properties.end(), prop.begin());
Pablo Tellob7c308a2019-01-22 12:59:26 +000081 };
82}
83} //namespace
84
85namespace arm_compute
86{
87std::tuple<cl::Context, cl::Device, cl_int>
88create_opencl_context_and_device()
89{
90 ARM_COMPUTE_ERROR_ON(!opencl_is_available());
91 std::vector<cl::Platform> platforms;
92 cl::Platform::get(&platforms);
93 ARM_COMPUTE_ERROR_ON_MSG(platforms.size() == 0, "Couldn't find any OpenCL platform");
94 cl::Platform p = platforms[0];
95 cl::Device device;
96 std::vector<cl::Device> platform_devices;
97 p.getDevices(CL_DEVICE_TYPE_DEFAULT, &platform_devices);
98 ARM_COMPUTE_ERROR_ON_MSG(platform_devices.size() == 0, "Couldn't find any OpenCL device");
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010099 device = platform_devices[0];
100 cl_int err = CL_SUCCESS;
101 std::array<cl_context_properties, 7> properties = { 0, 0, 0, 0, 0, 0, 0 };
Pablo Tellob7c308a2019-01-22 12:59:26 +0000102 initialise_context_properties(p, device, properties);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100103 cl::Context cl_context = cl::Context(device, properties.data(), nullptr, nullptr, &err);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000104 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
105 return std::make_tuple(cl_context, device, err);
106}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100107
108void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush)
109{
110 ARM_COMPUTE_ERROR_ON_NULLPTR(kernel);
111 if(ctx)
112 {
113 ARM_COMPUTE_ERROR_ON(ctx->gpu_scheduler() == nullptr);
114 ctx->gpu_scheduler()->enqueue(*kernel, flush);
115 }
116 else
117 {
118 CLScheduler::get().enqueue(*kernel, flush);
119 }
120}
121
Pablo Tellob7c308a2019-01-22 12:59:26 +0000122} // namespace arm_compute