blob: 7f363431e89ebdf7453939697d2762d41fc97d7f [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"
29#include "src/Tensor3dMapper.h"
30
Gunes Bayir64a4c442023-07-27 22:59:22 +010031#include <cstdint>
Gunes Bayirc4117a32023-08-07 16:52:33 +010032#include <memory>
Gunes Bayir64a4c442023-07-27 22:59:22 +010033#include <string>
34
35namespace ckw
36{
37
38// Forward Declarations
Gunes Bayir60ead4d2023-08-10 09:42:21 +010039class CLTile;
40class CLKernelWriter;
Gunes Bayirc4117a32023-08-07 16:52:33 +010041class ITensor;
42class TensorSampler;
Gunes Bayir64a4c442023-07-27 22:59:22 +010043enum class MemoryOperation;
44
Gunes Bayir60ead4d2023-08-10 09:42:21 +010045/** Base class OpenCL memory operation helper classes
Gunes Bayir64a4c442023-07-27 22:59:22 +010046 * that helps writing code for memory operations like load/store.
47 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010048class ICLMemoryOpHelper
Gunes Bayir64a4c442023-07-27 22:59:22 +010049{
50public:
51 /** Constructor
52 *
Gunes Bayirc4117a32023-08-07 16:52:33 +010053 * @param[in] writer @ref ckw::CLKernelWriter object to write the code
54 * @param[in] tensor @ref ckw::ITensor object to perform the memory operation on
55 * @param[in] sampler @ref ckw::TensorSampler object that tells how to sample a tensor
56 * @param[in] op The memory operation to be done (e.g. Load/Store)
Gunes Bayir64a4c442023-07-27 22:59:22 +010057 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010058 ICLMemoryOpHelper(CLKernelWriter *writer, ITensor *tensor, TensorSampler *sampler, MemoryOperation op)
59 : _writer(writer), _tensor(tensor), _sampler(sampler), _op(op)
Gunes Bayir64a4c442023-07-27 22:59:22 +010060 {
Gunes Bayirc4117a32023-08-07 16:52:33 +010061 _mapper = std::make_unique<Tensor3dMapper>(tensor, sampler->format());
Gunes Bayir64a4c442023-07-27 22:59:22 +010062 }
63
64 /** Copy constructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010065 ICLMemoryOpHelper(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010066
67 /** Assignment operator overload */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010068 ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010069
70 /** Destructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010071 virtual ~ICLMemoryOpHelper() = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010072
73 /** Initialization method that takes a 3D tensor's x, z dimensions and
74 * the batch offset as a tile object, and initializes the code inside
75 * the writer object.
76 *
77 * @param[in] dst tile object to perform the memory operation on
78 * @param[in] x tile object that describes the x-coordinate of the tensor involved
79 * @param[in] z tile object that describes the z-coordinate of the tensor involved
80 * @param[in] b tile object that describes the batch offset of the tensor involved
81 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010082 virtual void initialize(const CLTile *dst, const CLTile *x, const CLTile *z, const CLTile *b) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010083
84 /** Method that writes the actual code to the writer that performs the mentioned memory
85 * operation on the tile initialized. It writes the code for a specific row given in the
86 * arguments.
87 *
Gunes Bayir60ead4d2023-08-10 09:42:21 +010088 * @param[in] row_id row id
89 * @param[in] coord_y y-coordinate as string
Gunes Bayir64a4c442023-07-27 22:59:22 +010090 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010091 virtual void write_row(int32_t row_id, const std::string &coord_y) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010092
93 /** Method that finalizes the code in the writer object. This part is usually for taking
94 * care of finalizing anything that's been initialized inside @ref IMemoryHelper::initialize()
95 * such as matching compound statements, checking certain boundary conditions etc. No inputs
96 * and/or outputs, only the writer object is affected.
97 */
98 virtual void finalize() = 0;
99
100protected:
Gunes Bayirc4117a32023-08-07 16:52:33 +0100101 CLKernelWriter *_writer{ nullptr };
102 ITensor *_tensor{ nullptr };
103 TensorSampler *_sampler{ nullptr };
104 MemoryOperation _op;
105 std::unique_ptr<Tensor3dMapper> _mapper{ nullptr };
106 const CLTile *_dst{ nullptr };
107 int32_t _ls_width_full{ 0 };
108 std::string _coord_x{};
109 std::string _coord_z{};
110 std::string _coord_b{};
Gunes Bayir64a4c442023-07-27 22:59:22 +0100111};
112} // namespace ckw
113
Gunes Bayir60ead4d2023-08-10 09:42:21 +0100114#endif /* CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H */