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" |
| 29 | #include "src/Tensor3dMapper.h" |
| 30 | |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 31 | #include <cstdint> |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 32 | #include <memory> |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 33 | #include <string> |
| 34 | |
| 35 | namespace ckw |
| 36 | { |
| 37 | |
| 38 | // Forward Declarations |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 39 | class CLTile; |
| 40 | class CLKernelWriter; |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 41 | class ITensor; |
| 42 | class TensorSampler; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 43 | enum class MemoryOperation; |
| 44 | |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 45 | /** Base class OpenCL memory operation helper classes |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 46 | * that helps writing code for memory operations like load/store. |
| 47 | */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 48 | class ICLMemoryOpHelper |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | /** Constructor |
| 52 | * |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 53 | * @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 Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 57 | */ |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 58 | ICLMemoryOpHelper(CLKernelWriter *writer, ITensor *tensor, TensorSampler *sampler, MemoryOperation op) |
| 59 | : _writer(writer), _tensor(tensor), _sampler(sampler), _op(op) |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 60 | { |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 61 | _mapper = std::make_unique<Tensor3dMapper>(tensor, sampler->format()); |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** Copy constructor */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 65 | ICLMemoryOpHelper(const ICLMemoryOpHelper &) = default; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 66 | |
| 67 | /** Assignment operator overload */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 68 | ICLMemoryOpHelper &operator=(const ICLMemoryOpHelper &) = default; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 69 | |
| 70 | /** Destructor */ |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 71 | virtual ~ICLMemoryOpHelper() = default; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 72 | |
| 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 Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 82 | virtual void initialize(const CLTile *dst, const CLTile *x, const CLTile *z, const CLTile *b) = 0; |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 83 | |
| 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 Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 88 | * @param[in] row_id row id |
| 89 | * @param[in] coord_y y-coordinate as string |
Gunes Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 90 | */ |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 91 | 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] | 92 | |
| 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 | |
| 100 | protected: |
Gunes Bayir | c4117a3 | 2023-08-07 16:52:33 +0100 | [diff] [blame^] | 101 | 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 Bayir | 64a4c44 | 2023-07-27 22:59:22 +0100 | [diff] [blame] | 111 | }; |
| 112 | } // namespace ckw |
| 113 | |
Gunes Bayir | 60ead4d | 2023-08-10 09:42:21 +0100 | [diff] [blame] | 114 | #endif /* CKW_SRC_CL_HELPERS_ICLMEMORYOPHELPER_H */ |