blob: a5b679ac0333031ca90a5d6ba72c739f180f9e18 [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"
Gunes Bayir2b9fa592024-01-17 16:07:03 +000031#include "src/TileView.h"
Gunes Bayirc4117a32023-08-07 16:52:33 +010032
Gunes Bayir64a4c442023-07-27 22:59:22 +010033#include <cstdint>
Gunes Bayirc4117a32023-08-07 16:52:33 +010034#include <memory>
Gunes Bayir64a4c442023-07-27 22:59:22 +010035#include <string>
36
37namespace ckw
38{
39
40// Forward Declarations
Gunes Bayir60ead4d2023-08-10 09:42:21 +010041class CLTile;
42class CLKernelWriter;
Gunes Bayirc4117a32023-08-07 16:52:33 +010043class ITensor;
44class TensorSampler;
Gunes Bayir64a4c442023-07-27 22:59:22 +010045enum class MemoryOperation;
46
Gunes Bayir60ead4d2023-08-10 09:42:21 +010047/** Base class OpenCL memory operation helper classes
Gunes Bayir64a4c442023-07-27 22:59:22 +010048 * that helps writing code for memory operations like load/store.
49 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010050class ICLMemoryOpHelper
Gunes Bayir64a4c442023-07-27 22:59:22 +010051{
52public:
53 /** Constructor
54 *
Gunes Bayirc4117a32023-08-07 16:52:33 +010055 * @param[in] writer @ref ckw::CLKernelWriter object to write the code
56 * @param[in] tensor @ref ckw::ITensor object to perform the memory operation on
57 * @param[in] sampler @ref ckw::TensorSampler object that tells how to sample a tensor
58 * @param[in] op The memory operation to be done (e.g. Load/Store)
Gunes Bayir2b9fa592024-01-17 16:07:03 +000059 * @param[in] dst The tile to perform the memory operation on
Gunes Bayir64a4c442023-07-27 22:59:22 +010060 */
Gunes Bayir2b9fa592024-01-17 16:07:03 +000061 ICLMemoryOpHelper(CLKernelWriter *writer,
62 ITensor *tensor,
63 TensorSampler *sampler,
64 MemoryOperation op,
65 const TileView<CLTile> &dst)
66 : _writer(writer), _tensor(tensor), _sampler(sampler), _op(op), _dst(dst)
Gunes Bayir64a4c442023-07-27 22:59:22 +010067 {
Gunes Bayir2b9fa592024-01-17 16:07:03 +000068 _mapper = std::make_unique<Tensor3dMapper>(tensor, sampler->format());
69 _ls_width_full = _dst.width();
Gunes Bayir64a4c442023-07-27 22:59:22 +010070 }
71
72 /** Copy constructor */
Gunes Bayir2b9fa592024-01-17 16:07:03 +000073 ICLMemoryOpHelper(const ICLMemoryOpHelper &) = delete;
Gunes Bayir64a4c442023-07-27 22:59:22 +010074
75 /** Assignment operator overload */
Gunes Bayir2b9fa592024-01-17 16:07:03 +000076 ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = delete;
Gunes Bayir64a4c442023-07-27 22:59:22 +010077
78 /** Destructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010079 virtual ~ICLMemoryOpHelper() = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010080
81 /** Initialization method that takes a 3D tensor's x, z dimensions and
82 * the batch offset as a tile object, and initializes the code inside
83 * the writer object.
84 *
Gunes Bayir64a4c442023-07-27 22:59:22 +010085 * @param[in] x tile object that describes the x-coordinate of the tensor involved
86 * @param[in] z tile object that describes the z-coordinate of the tensor involved
87 * @param[in] b tile object that describes the batch offset of the tensor involved
88 */
Gunes Bayir2b9fa592024-01-17 16:07:03 +000089 virtual void initialize(const CLTile *x, const CLTile *z, const CLTile *b) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010090
91 /** Method that writes the actual code to the writer that performs the mentioned memory
92 * operation on the tile initialized. It writes the code for a specific row given in the
93 * arguments.
94 *
Gunes Bayir60ead4d2023-08-10 09:42:21 +010095 * @param[in] row_id row id
96 * @param[in] coord_y y-coordinate as string
Gunes Bayir64a4c442023-07-27 22:59:22 +010097 */
Gunes Bayirc4117a32023-08-07 16:52:33 +010098 virtual void write_row(int32_t row_id, const std::string &coord_y) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010099
100 /** Method that finalizes the code in the writer object. This part is usually for taking
101 * care of finalizing anything that's been initialized inside @ref IMemoryHelper::initialize()
102 * such as matching compound statements, checking certain boundary conditions etc. No inputs
103 * and/or outputs, only the writer object is affected.
104 */
105 virtual void finalize() = 0;
106
107protected:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 CLKernelWriter *_writer{nullptr};
109 ITensor *_tensor{nullptr};
110 TensorSampler *_sampler{nullptr};
111 MemoryOperation _op;
112 std::unique_ptr<Tensor3dMapper> _mapper{nullptr};
Gunes Bayir2b9fa592024-01-17 16:07:03 +0000113 TileView<CLTile> _dst{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 int32_t _ls_width_full{0};
115 std::string _coord_x{};
116 std::string _coord_z{};
117 std::string _coord_b{};
Gunes Bayir64a4c442023-07-27 22:59:22 +0100118};
119} // namespace ckw
120
Gunes Bayir2b9fa592024-01-17 16:07:03 +0000121#endif // CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H