Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, 2023 Arm Limited. |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [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 | |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 25 | #ifndef ACL_SRC_CORE_CL_CLUTILS_H |
| 26 | #define ACL_SRC_CORE_CL_CLUTILS_H |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 27 | |
| 28 | #include "arm_compute/core/CL/OpenCL.h" |
| 29 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 30 | #include <map> |
| 31 | |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 32 | namespace arm_compute |
| 33 | { |
| 34 | class TensorShape; |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 35 | class CLBuildOptions; |
| 36 | class ITensorInfo; |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 37 | class ICLTensor; |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 38 | enum class DataType; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 39 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 40 | /** OpenCL Image2D types */ |
| 41 | enum class CLImage2DType |
| 42 | { |
| 43 | ReadOnly, |
| 44 | WriteOnly |
| 45 | }; |
| 46 | |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 47 | /** Create a cl::Image2D object from a tensor |
| 48 | * |
| 49 | * @param[in] tensor Tensor from which to construct Image 2D object |
| 50 | * @param[in] image_type Image 2D type (@ref CLImage2DType) |
| 51 | * |
| 52 | * @return cl::Image2D object |
| 53 | */ |
| 54 | cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type); |
| 55 | |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 56 | /** Create a cl::Image2D object from an OpenCL buffer |
| 57 | * |
| 58 | * @note The following conditions are required to create a OpenCL image object from OpenCL buffer, |
| 59 | * -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension |
| 60 | * -# The stride Y for the input1 should satisfy the OpenCL pitch alignment requirement |
| 61 | * -# input width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4) |
| 62 | * -# input height should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT |
| 63 | * |
| 64 | * It is user responsibility to ensure the above conditions are satisfied since no checks are performed within this function |
| 65 | * |
| 66 | * @param[in] ctx cl::Context object |
| 67 | * @param[in] buffer cl::Buffer object from which the OpenCL image2d object is created |
| 68 | * @param[in] shape2d 2D tensor shape |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 69 | * @param[in] data_type DataType to use. Only supported: F32,F16 |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 70 | * @param[in] image_row_pitch Image row pitch (a.k.a. stride Y) to be used in the image2d object |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 71 | * @param[in] image_type Image 2D type (@ref CLImage2DType) |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 72 | * |
| 73 | * @return cl::Image2D object |
| 74 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 75 | cl::Image2D create_image2d_from_buffer(const cl::Context &ctx, |
| 76 | const cl::Buffer &buffer, |
| 77 | const TensorShape &shape2d, |
| 78 | DataType data_type, |
| 79 | size_t image_row_pitch, |
| 80 | CLImage2DType image_type); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 81 | |
| 82 | /** Check for CL error code and throw exception accordingly. |
| 83 | * |
| 84 | * @param[in] function_name The name of the CL function being called. |
| 85 | * @param[in] error_code The error returned by the CL function. |
| 86 | */ |
| 87 | void handle_cl_error(const std::string &function_name, cl_int error_code); |
| 88 | |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 89 | } // namespace arm_compute |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 90 | |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 91 | #endif // ACL_SRC_CORE_CL_CLUTILS_H |