Compute Library
 21.02
CLUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 "arm_compute/core/Error.h"
25 #include "arm_compute/core/Types.h"
26 
27 #include "src/core/CL/CLUtils.h"
28 
29 cl::Image2D arm_compute::create_image2d_from_buffer(const cl::Context &ctx, const cl::Buffer &buffer, const TensorShape &shape2d, DataType data_type, size_t image_row_pitch)
30 {
31  cl_channel_type cl_data_type;
32 
33  switch(data_type)
34  {
35  case DataType::F32:
36  cl_data_type = CL_FLOAT;
37  break;
38  case DataType::F16:
39  cl_data_type = CL_HALF_FLOAT;
40  break;
41  default:
42  ARM_COMPUTE_ERROR("Data type not support with OpenCL image2d");
43  }
44 
45  cl_mem cl_image;
46  cl_int err = CL_SUCCESS;
47 
48  const cl_image_format format = { CL_RGBA, cl_data_type };
49 
50  cl_image_desc desc;
51  memset(&desc, 0, sizeof(desc));
52  desc.image_type = CL_MEM_OBJECT_IMAGE2D;
53  desc.mem_object = buffer();
54  desc.image_row_pitch = image_row_pitch;
55  desc.image_width = shape2d[0];
56  desc.image_height = shape2d[1];
57 
58  cl_image = clCreateImage(ctx(), CL_MEM_READ_ONLY, &format, &desc, nullptr, &err);
59 
60  ARM_COMPUTE_UNUSED(err);
61  ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Error during the creation of CL image from buffer");
62 
63  return cl::Image2D(cl_image);
64 }
Shape of a tensor.
Definition: TensorShape.h:39
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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:943
const DataType data_type
Definition: Im2Col.cpp:150
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
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)
Create a cl::Image2D object from an OpenCL buffer.
Definition: CLUtils.cpp:29
DataType
Available data types.
Definition: Types.h:77