blob: f9dcfeac3a5ed9194f84b8183a8c43185ccd305e [file] [log] [blame]
Gian Marco Iodice781cba72020-06-19 16:56:57 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2020-2021, 2023 Arm Limited.
Gian Marco Iodice781cba72020-06-19 16:56:57 +01003 *
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 Sujak0d27b2e2023-08-24 14:01:20 +010025#ifndef ACL_SRC_CORE_CL_CLUTILS_H
26#define ACL_SRC_CORE_CL_CLUTILS_H
Gian Marco Iodice781cba72020-06-19 16:56:57 +010027
28#include "arm_compute/core/CL/OpenCL.h"
29
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include <map>
31
Gian Marco Iodice781cba72020-06-19 16:56:57 +010032namespace arm_compute
33{
34class TensorShape;
SiCongLi1af54162021-10-06 15:25:57 +010035class CLBuildOptions;
36class ITensorInfo;
Jakub Sujak8c49f162023-06-16 09:52:50 +010037class ICLTensor;
Matthew Bentham314d3e22023-06-23 10:53:52 +000038enum class DataType;
Gian Marco Iodice781cba72020-06-19 16:56:57 +010039
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +000040/** OpenCL Image2D types */
41enum class CLImage2DType
42{
43 ReadOnly,
44 WriteOnly
45};
46
Jakub Sujak8c49f162023-06-16 09:52:50 +010047/** 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 */
54cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type);
55
Gian Marco Iodice781cba72020-06-19 16:56:57 +010056/** 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 Iodice6f931342020-09-15 14:17:41 +010069 * @param[in] data_type DataType to use. Only supported: F32,F16
Gian Marco Iodice781cba72020-06-19 16:56:57 +010070 * @param[in] image_row_pitch Image row pitch (a.k.a. stride Y) to be used in the image2d object
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +000071 * @param[in] image_type Image 2D type (@ref CLImage2DType)
Gian Marco Iodice781cba72020-06-19 16:56:57 +010072 *
73 * @return cl::Image2D object
74 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075cl::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 Do500e10b2023-09-12 17:49:38 +010081
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 */
87void handle_cl_error(const std::string &function_name, cl_int error_code);
88
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010089} // namespace arm_compute
Gian Marco Iodice781cba72020-06-19 16:56:57 +010090
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010091#endif // ACL_SRC_CORE_CL_CLUTILS_H