blob: f46fee9750c49ef604f2545bfeb79f59e1e23eb7 [file] [log] [blame]
Gunes Bayir64a4c442023-07-27 22:59:22 +01001/*
2 * Copyright (c) 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
Gunes Bayir60ead4d2023-08-10 09:42:21 +010025#ifndef CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H
26#define CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H
Gunes Bayir64a4c442023-07-27 22:59:22 +010027
Gunes Bayirc4117a32023-08-07 16:52:33 +010028#include "ckw/TensorSampler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Gunes Bayirc4117a32023-08-07 16:52:33 +010030#include "src/Tensor3dMapper.h"
31
Gunes Bayir64a4c442023-07-27 22:59:22 +010032#include <cstdint>
Gunes Bayirc4117a32023-08-07 16:52:33 +010033#include <memory>
Gunes Bayir64a4c442023-07-27 22:59:22 +010034#include <string>
35
36namespace ckw
37{
38
39// Forward Declarations
Gunes Bayir60ead4d2023-08-10 09:42:21 +010040class CLTile;
41class CLKernelWriter;
Gunes Bayirc4117a32023-08-07 16:52:33 +010042class ITensor;
43class TensorSampler;
Gunes Bayir64a4c442023-07-27 22:59:22 +010044enum class MemoryOperation;
45
Gunes Bayir60ead4d2023-08-10 09:42:21 +010046/** Base class OpenCL memory operation helper classes
Gunes Bayir64a4c442023-07-27 22:59:22 +010047 * that helps writing code for memory operations like load/store.
48 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010049class ICLMemoryOpHelper
Gunes Bayir64a4c442023-07-27 22:59:22 +010050{
51public:
52 /** Constructor
53 *
Gunes Bayirc4117a32023-08-07 16:52:33 +010054 * @param[in] writer @ref ckw::CLKernelWriter object to write the code
55 * @param[in] tensor @ref ckw::ITensor object to perform the memory operation on
56 * @param[in] sampler @ref ckw::TensorSampler object that tells how to sample a tensor
57 * @param[in] op The memory operation to be done (e.g. Load/Store)
Gunes Bayir64a4c442023-07-27 22:59:22 +010058 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010059 ICLMemoryOpHelper(CLKernelWriter *writer, ITensor *tensor, TensorSampler *sampler, MemoryOperation op)
60 : _writer(writer), _tensor(tensor), _sampler(sampler), _op(op)
Gunes Bayir64a4c442023-07-27 22:59:22 +010061 {
Gunes Bayirc4117a32023-08-07 16:52:33 +010062 _mapper = std::make_unique<Tensor3dMapper>(tensor, sampler->format());
Gunes Bayir64a4c442023-07-27 22:59:22 +010063 }
64
65 /** Copy constructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010066 ICLMemoryOpHelper(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010067
68 /** Assignment operator overload */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010069 ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010070
71 /** Destructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010072 virtual ~ICLMemoryOpHelper() = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010073
74 /** Initialization method that takes a 3D tensor's x, z dimensions and
75 * the batch offset as a tile object, and initializes the code inside
76 * the writer object.
77 *
78 * @param[in] dst tile object to perform the memory operation on
79 * @param[in] x tile object that describes the x-coordinate of the tensor involved
80 * @param[in] z tile object that describes the z-coordinate of the tensor involved
81 * @param[in] b tile object that describes the batch offset of the tensor involved
82 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010083 virtual void initialize(const CLTile *dst, const CLTile *x, const CLTile *z, const CLTile *b) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010084
85 /** Method that writes the actual code to the writer that performs the mentioned memory
86 * operation on the tile initialized. It writes the code for a specific row given in the
87 * arguments.
88 *
Gunes Bayir60ead4d2023-08-10 09:42:21 +010089 * @param[in] row_id row id
90 * @param[in] coord_y y-coordinate as string
Gunes Bayir64a4c442023-07-27 22:59:22 +010091 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010092 virtual void write_row(int32_t row_id, const std::string &coord_y) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010093
94 /** Method that finalizes the code in the writer object. This part is usually for taking
95 * care of finalizing anything that's been initialized inside @ref IMemoryHelper::initialize()
96 * such as matching compound statements, checking certain boundary conditions etc. No inputs
97 * and/or outputs, only the writer object is affected.
98 */
99 virtual void finalize() = 0;
100
101protected:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 CLKernelWriter *_writer{nullptr};
103 ITensor *_tensor{nullptr};
104 TensorSampler *_sampler{nullptr};
105 MemoryOperation _op;
106 std::unique_ptr<Tensor3dMapper> _mapper{nullptr};
107 const CLTile *_dst{nullptr};
108 int32_t _ls_width_full{0};
109 std::string _coord_x{};
110 std::string _coord_z{};
111 std::string _coord_b{};
Gunes Bayir64a4c442023-07-27 22:59:22 +0100112};
113} // namespace ckw
114
Gunes Bayir60ead4d2023-08-10 09:42:21 +0100115#endif /* CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H */