blob: 008d147fa22b23c97ede60ca30f42bcca49edd50 [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
28#include <cstdint>
29#include <string>
30
31namespace ckw
32{
33
34// Forward Declarations
Gunes Bayir60ead4d2023-08-10 09:42:21 +010035class CLTile;
36class CLKernelWriter;
Gunes Bayir64a4c442023-07-27 22:59:22 +010037class Tensor3dMapper;
38enum class MemoryOperation;
39
Gunes Bayir60ead4d2023-08-10 09:42:21 +010040/** Base class OpenCL memory operation helper classes
Gunes Bayir64a4c442023-07-27 22:59:22 +010041 * that helps writing code for memory operations like load/store.
42 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010043class ICLMemoryOpHelper
Gunes Bayir64a4c442023-07-27 22:59:22 +010044{
45public:
46 /** Constructor
47 *
Gunes Bayir60ead4d2023-08-10 09:42:21 +010048 * @param[in] x @ref ckw::CLKernelWriter object to write the code
Gunes Bayir64a4c442023-07-27 22:59:22 +010049 * @param[in] mapper @ref ckw::Tensor3dMapper object that tells how to map the Nd tensor to 3d
50 * @param[in] op The memory operation to be done (e.g. Load/Store)
51 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010052 ICLMemoryOpHelper(CLKernelWriter *x, Tensor3dMapper *mapper, MemoryOperation op)
Gunes Bayir64a4c442023-07-27 22:59:22 +010053 : _writer(x), _mapper(mapper), _op(op)
54 {
55 }
56
57 /** Copy constructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010058 ICLMemoryOpHelper(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010059
60 /** Assignment operator overload */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010061 ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010062
63 /** Destructor */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010064 virtual ~ICLMemoryOpHelper() = default;
Gunes Bayir64a4c442023-07-27 22:59:22 +010065
66 /** Initialization method that takes a 3D tensor's x, z dimensions and
67 * the batch offset as a tile object, and initializes the code inside
68 * the writer object.
69 *
70 * @param[in] dst tile object to perform the memory operation on
71 * @param[in] x tile object that describes the x-coordinate of the tensor involved
72 * @param[in] z tile object that describes the z-coordinate of the tensor involved
73 * @param[in] b tile object that describes the batch offset of the tensor involved
74 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010075 virtual void initialize(CLTile *dst, CLTile *x, CLTile *z, CLTile *b) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010076
77 /** Method that writes the actual code to the writer that performs the mentioned memory
78 * operation on the tile initialized. It writes the code for a specific row given in the
79 * arguments.
80 *
Gunes Bayir60ead4d2023-08-10 09:42:21 +010081 * @param[in] row_id row id
82 * @param[in] coord_y y-coordinate as string
Gunes Bayir64a4c442023-07-27 22:59:22 +010083 */
Gunes Bayir60ead4d2023-08-10 09:42:21 +010084 virtual void write(int32_t row_id, const std::string &coord_y) = 0;
Gunes Bayir64a4c442023-07-27 22:59:22 +010085
86 /** Method that finalizes the code in the writer object. This part is usually for taking
87 * care of finalizing anything that's been initialized inside @ref IMemoryHelper::initialize()
88 * such as matching compound statements, checking certain boundary conditions etc. No inputs
89 * and/or outputs, only the writer object is affected.
90 */
91 virtual void finalize() = 0;
92
93protected:
Gunes Bayir60ead4d2023-08-10 09:42:21 +010094 CLKernelWriter *_writer;
95 Tensor3dMapper *_mapper;
96 MemoryOperation _op;
97 CLTile *_dst{ nullptr };
98 int32_t _ls_width_full{ 0 };
99 std::string _coord_x{};
100 std::string _coord_z{};
101 std::string _coord_b{};
Gunes Bayir64a4c442023-07-27 22:59:22 +0100102};
103} // namespace ckw
104
Gunes Bayir60ead4d2023-08-10 09:42:21 +0100105#endif /* CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H */