Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 1 | /* |
| 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 Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 25 | #ifndef CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H |
| 26 | #define CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 27 | |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 28 | #include "ckw/TensorSampler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 30 | #include "src/Tensor3dMapper.h" |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 31 | #include "src/TileView.h" |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 32 | |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 33 | #include <cstdint> |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 34 | #include <memory> |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 35 | #include <string> |
| 36 | |
| 37 | namespace ckw |
| 38 | { |
| 39 | |
| 40 | // Forward Declarations |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 41 | class CLTile; |
| 42 | class CLKernelWriter; |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 43 | class ITensor; |
| 44 | class TensorSampler; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 45 | enum class MemoryOperation; |
| 46 | |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 47 | /** Base class OpenCL memory operation helper classes |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 48 | * that helps writing code for memory operations like load/store. |
| 49 | */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 50 | class ICLMemoryOpHelper |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 51 | { |
| 52 | public: |
| 53 | /** Constructor |
| 54 | * |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 55 | * @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 Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 59 | * @param[in] dst The tile to perform the memory operation on |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 60 | */ |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 61 | 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 Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 67 | { |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 68 | _mapper = std::make_unique<Tensor3dMapper>(tensor, sampler->format()); |
| 69 | _ls_width_full = _dst.width(); |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** Copy constructor */ |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 73 | ICLMemoryOpHelper(const ICLMemoryOpHelper &) = delete; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 74 | |
| 75 | /** Assignment operator overload */ |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 76 | ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = delete; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 77 | |
| 78 | /** Destructor */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 79 | virtual ~ICLMemoryOpHelper() = default; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 80 | |
| 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 Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 85 | * @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 Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 89 | virtual void initialize(const CLTile *x, const CLTile *z, const CLTile *b) = 0; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 90 | |
| 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 Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 95 | * @param[in] row_id row id |
| 96 | * @param[in] coord_y y-coordinate as string |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 97 | */ |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame] | 98 | virtual void write_row(int32_t row_id, const std::string &coord_y) = 0; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 99 | |
| 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 | |
| 107 | protected: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | CLKernelWriter *_writer{nullptr}; |
| 109 | ITensor *_tensor{nullptr}; |
| 110 | TensorSampler *_sampler{nullptr}; |
| 111 | MemoryOperation _op; |
| 112 | std::unique_ptr<Tensor3dMapper> _mapper{nullptr}; |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 113 | TileView<CLTile> _dst{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | int32_t _ls_width_full{0}; |
| 115 | std::string _coord_x{}; |
| 116 | std::string _coord_z{}; |
| 117 | std::string _coord_b{}; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 118 | }; |
| 119 | } // namespace ckw |
| 120 | |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 121 | #endif // CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H |