Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 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" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLRuntimeContext.h" |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 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 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 37 | ARM_COMPUTE_UNUSED(complete, user_data); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 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 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 53 | void initialise_context_properties(const cl::Platform &platform, const cl::Device &device, std::array<cl_context_properties, 7> &prop) |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 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. |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 61 | std::array<cl_context_properties, 7> properties_printf = |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 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 | }; |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 71 | prop = properties_printf; |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 72 | } |
| 73 | else |
| 74 | #endif // defined(ARM_COMPUTE_ASSERTS_ENABLED) |
| 75 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 76 | std::array<cl_context_properties, 3> properties = |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 77 | { |
| 78 | CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform()), |
| 79 | 0 |
| 80 | }; |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 81 | std::copy(properties.begin(), properties.end(), prop.begin()); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | } //namespace |
| 85 | |
| 86 | namespace arm_compute |
| 87 | { |
| 88 | std::tuple<cl::Context, cl::Device, cl_int> |
| 89 | create_opencl_context_and_device() |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(!opencl_is_available()); |
| 92 | std::vector<cl::Platform> platforms; |
| 93 | cl::Platform::get(&platforms); |
| 94 | ARM_COMPUTE_ERROR_ON_MSG(platforms.size() == 0, "Couldn't find any OpenCL platform"); |
| 95 | cl::Platform p = platforms[0]; |
| 96 | cl::Device device; |
| 97 | std::vector<cl::Device> platform_devices; |
| 98 | p.getDevices(CL_DEVICE_TYPE_DEFAULT, &platform_devices); |
| 99 | ARM_COMPUTE_ERROR_ON_MSG(platform_devices.size() == 0, "Couldn't find any OpenCL device"); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 100 | device = platform_devices[0]; |
| 101 | cl_int err = CL_SUCCESS; |
| 102 | std::array<cl_context_properties, 7> properties = { 0, 0, 0, 0, 0, 0, 0 }; |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 103 | initialise_context_properties(p, device, properties); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 104 | cl::Context cl_context = cl::Context(device, properties.data(), nullptr, nullptr, &err); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context"); |
| 106 | return std::make_tuple(cl_context, device, err); |
| 107 | } |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 108 | |
| 109 | void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(kernel); |
| 112 | if(ctx) |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON(ctx->gpu_scheduler() == nullptr); |
| 115 | ctx->gpu_scheduler()->enqueue(*kernel, flush); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | CLScheduler::get().enqueue(*kernel, flush); |
| 120 | } |
| 121 | } |
| 122 | |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 123 | } // namespace arm_compute |