Compute Library
 23.11
CLUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2023 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 #include "src/core/CL/CLUtils.h"
25 
32 
33 #include "support/StringSupport.h"
34 
35 namespace arm_compute
36 {
38 {
40 
41  const cl::Context &ctx = CLKernelLibrary::get().context();
42  const cl::Buffer &buffer = tensor->cl_buffer();
43  const ITensorInfo *info = tensor->info();
44  ARM_COMPUTE_ERROR_ON_MSG(info->lock_paddings(), "Tensor paddings must not be locked to allow extending paddings to "
45  "satisfy cl_image pitch alignment requirement");
46 
47  const size_t image_w{info->dimension(0) / 4};
48  const size_t image_h{info->tensor_shape().total_size() / info->dimension(0)};
49  const size_t max_image_w{CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>()};
50  const size_t max_image_h{CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>()};
51 
52  ARM_COMPUTE_UNUSED(max_image_w, max_image_h);
53  ARM_COMPUTE_ERROR_ON_MSG(image_w > max_image_w, "Image width exceeds maximum width for exporting to cl_image");
54  ARM_COMPUTE_ERROR_ON_MSG(image_h > max_image_h, "Image height exceeds maximum height for exporting to cl_image");
55 
56  const TensorShape shape2d(image_w, image_h);
57  const size_t image_row_pitch = info->strides_in_bytes()[1];
58 
59  return create_image2d_from_buffer(ctx, buffer, shape2d, info->data_type(), image_row_pitch, image_type);
60 }
61 
62 cl::Image2D create_image2d_from_buffer(const cl::Context &ctx,
63  const cl::Buffer &buffer,
64  const TensorShape &shape2d,
66  size_t image_row_pitch,
67  CLImage2DType image_type)
68 {
70  "The extension cl_khr_image2d_from_buffer is not supported on the target platform");
72  "Impossible to retrieve the cl_image pitch alignment");
73  ARM_COMPUTE_ERROR_ON_MSG(buffer.get() == nullptr, "Cannot create cl_image from empty cl_buffer");
74 
75  cl_channel_type cl_data_type;
76 
77  switch (data_type)
78  {
79  case DataType::F32:
80  cl_data_type = CL_FLOAT;
81  break;
82  case DataType::F16:
83  cl_data_type = CL_HALF_FLOAT;
84  break;
85  default:
86  ARM_COMPUTE_ERROR("Data type not support with OpenCL image2d");
87  }
88 
89  cl_mem cl_image;
90  cl_int err = CL_SUCCESS;
91 
92  const cl_image_format format = {CL_RGBA, cl_data_type};
93 
94  cl_image_desc desc;
95  memset(&desc, 0, sizeof(desc));
96  desc.image_type = CL_MEM_OBJECT_IMAGE2D;
97  desc.mem_object = buffer();
98  desc.image_row_pitch = image_row_pitch;
99  desc.image_width = shape2d[0];
100  desc.image_height = shape2d[1];
101 
102  switch (image_type)
103  {
105  cl_image = clCreateImage(ctx(), CL_MEM_READ_ONLY, &format, &desc, nullptr, &err);
106  break;
108  cl_image = clCreateImage(ctx(), CL_MEM_WRITE_ONLY, &format, &desc, nullptr, &err);
109  break;
110  default:
111  ARM_COMPUTE_ERROR("Unsupported CLImage2DType");
112  }
113 
114  ARM_COMPUTE_UNUSED(err);
115  ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Error during the creation of CL image from buffer");
116 
117  return cl::Image2D(cl_image);
118 }
119 
120 void handle_cl_error(const std::string &function_name, cl_int error_code)
121 {
122  if (error_code != CL_SUCCESS)
123  {
124  std::string error_message = function_name + " - Error code: " + std::to_string(error_code);
125  ARM_COMPUTE_ERROR(error_message.c_str());
126  }
127 }
128 
129 } // namespace arm_compute
arm_compute::CLKernelLibrary::context
cl::Context & context()
Accessor for the associated CL context.
Definition: CLKernelLibrary.cpp:69
StringSupport.h
ICLTensor.h
arm_compute::create_image2d_from_tensor
cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type)
Create a cl::Image2D object from a tensor.
Definition: CLUtils.cpp:37
arm_compute::CLImage2DType
CLImage2DType
OpenCL Image2D types.
Definition: CLUtils.h:41
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::CLImage2DType::WriteOnly
@ WriteOnly
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:41
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:354
arm_compute::image2d_from_buffer_supported
bool image2d_from_buffer_supported(const cl::Device &device)
Helper function to check whether the cl_khr_image2d_from_buffer extension is supported.
Definition: CLHelpers.cpp:377
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:41
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
StringUtils.h
CLUtils.h
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::CLImage2DType::ReadOnly
@ ReadOnly
arm_compute::handle_cl_error
void handle_cl_error(const std::string &function_name, cl_int error_code)
Check for CL error code and throw exception accordingly.
Definition: CLUtils.cpp:120
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
CLCompileContext.h
arm_compute::CLTensor::cl_buffer
const cl::Buffer & cl_buffer() const override
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
Definition: CLTensor.cpp:51
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:67
clCreateImage
cl_mem clCreateImage(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, const cl_image_desc *image_desc, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:1028
ActivationFunctionUtils.h
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:222
arm_compute::create_image2d_from_buffer
cl::Image2D create_image2d_from_buffer(const cl::Context &ctx, const cl::Buffer &buffer, const TensorShape &shape2d, DataType data_type, size_t image_row_pitch, CLImage2DType image_type)
Create a cl::Image2D object from an OpenCL buffer.
Definition: CLUtils.cpp:62
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::to_string
std::string to_string(const ClComponentElementwiseBinary::Attributes::ElementwiseOp &op)
Formatted output of the arm_compute::experimental::dynamic_fusion::ClComponentElementwiseBinary::Attr...
Definition: ElementwiseBinary.h:68
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::get_cl_image_pitch_alignment
size_t get_cl_image_pitch_alignment(const cl::Device &device)
Helper function to get the cl_image pitch alignment in pixels.
Definition: CLHelpers.cpp:382
arm_compute::CLTensor::info
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Validate.h
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:83
arm_compute::CLKernelLibrary::get_device
const cl::Device & get_device()
Gets the CL device for which the programs are created.
Definition: CLKernelLibrary.cpp:73