blob: 449d7d5a6ce4d9dc69b68e8c850d90a6ef710ecc [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"
29
30namespace
31{
32#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
33void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data)
34{
35 printf("%.*s", len, buffer);
36}
37#endif /* defined(ARM_COMPUTE_ASSERTS_ENABLED) */
38
39/** This initialises the properties vector with the configuration to be used when creating the opencl context
40 *
41 * @param[in] platform The opencl platform used to create the context
42 * @param[in] device The opencl device to be used to create the context
43 * @param[in] prop An array of properties to be initialised
44 *
45 * @note In debug builds, this function will enable cl_arm_printf if it's supported.
46 *
47 * @return A pointer to the context properties which can be used to create an opencl context
48 */
49
50void initialise_context_properties(const cl::Platform &platform, const cl::Device &device, cl_context_properties prop[7])
51{
52 ARM_COMPUTE_UNUSED(device);
53#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
54 // Query devices in the context for cl_arm_printf support
55 if(arm_compute::device_supports_extension(device, "cl_arm_printf"))
56 {
57 // Create a cl_context with a printf_callback and user specified buffer size.
58 cl_context_properties properties_printf[] =
59 {
60 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
61 // Enable a printf callback function for this context.
62 CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
63 // Request a minimum printf buffer size of 4MB for devices in the
64 // context that support this extension.
65 CL_PRINTF_BUFFERSIZE_ARM, 0x1000,
66 0
67 };
68 std::copy_n(prop, 7, properties_printf);
69 }
70 else
71#endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
72 {
73 cl_context_properties properties[] =
74 {
75 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()),
76 0
77 };
78 std::copy_n(prop, 3, properties);
79 };
80}
81} //namespace
82
83namespace arm_compute
84{
85std::tuple<cl::Context, cl::Device, cl_int>
86create_opencl_context_and_device()
87{
88 ARM_COMPUTE_ERROR_ON(!opencl_is_available());
89 std::vector<cl::Platform> platforms;
90 cl::Platform::get(&platforms);
91 ARM_COMPUTE_ERROR_ON_MSG(platforms.size() == 0, "Couldn't find any OpenCL platform");
92 cl::Platform p = platforms[0];
93 cl::Device device;
94 std::vector<cl::Device> platform_devices;
95 p.getDevices(CL_DEVICE_TYPE_DEFAULT, &platform_devices);
96 ARM_COMPUTE_ERROR_ON_MSG(platform_devices.size() == 0, "Couldn't find any OpenCL device");
97 device = platform_devices[0];
98 cl_int err = CL_SUCCESS;
99 cl_context_properties properties[7] = { 0, 0, 0, 0, 0, 0, 0 };
100 initialise_context_properties(p, device, properties);
101 cl::Context cl_context = cl::Context(device, properties, nullptr, nullptr, &err);
102 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
103 return std::make_tuple(cl_context, device, err);
104}
105} // namespace arm_compute