xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/CLSubTensor.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2019 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 #ifndef ARM_COMPUTE_CLSUBTENSOR_H
25 #define ARM_COMPUTE_CLSUBTENSOR_H
26 
27 #include "arm_compute/core/SubTensorInfo.h"
28 #include "arm_compute/runtime/CL/CLTensor.h"
29 
30 #include <cstdint>
31 
32 namespace arm_compute
33 {
34 class ITensorInfo;
35 
36 /** Basic implementation of the OpenCL sub-tensor interface */
37 class CLSubTensor : public ICLTensor
38 {
39 public:
40     /** Default Constructor */
41     CLSubTensor();
42     /** Constructor
43      *
44      * @param[in] parent        Parent tensor
45      * @param[in] tensor_shape  Shape of the subtensor
46      * @param[in] coords        Coordinates of the first subtensor element inside the parent tensor.
47      * @param[in] extend_parent (Optional) Extend parent with subtensor shape if subtensor indexes out of bounds
48      */
49     CLSubTensor(ICLTensor *parent, const TensorShape &tensor_shape, const Coordinates &coords, bool extend_parent = false);
50     /** Destructor: free the tensor's memory */
51     ~CLSubTensor() = default;
52     /** Restrict instances of this class to be copy constructed */
53     CLSubTensor(const CLSubTensor &) = delete;
54     /** Restrict instances of this class to be copied */
55     CLSubTensor &operator=(const CLSubTensor &) = delete;
56     /** Allow instances of this class to be move constructed */
57     CLSubTensor(CLSubTensor &&) = default;
58     /** Allow instances of this class to be moved */
59     CLSubTensor &operator=(CLSubTensor &&) = default;
60 
61     /** Enqueue a map operation of the allocated buffer.
62      *
63      * @note Mapping a subtensor will lead to the mapping of the whole parent tensor for now.
64      *
65      * @param[in] blocking If true, then the mapping will be ready to use by the time
66      *                     this method returns, else it is the caller's responsibility
67      *                     to flush the queue and wait for the mapping operation to have completed.
68      */
69     void map(bool blocking = true);
70     using ICLTensor::map;
71     /** Enqueue an unmap operation of the allocated and mapped buffer.
72      *
73      * @note Unmapping a subtensor will lead to the unmapping of the whole parent tensor for now.
74      *
75      * @note This method simply enqueues the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
76      *       the memory is accessed by the device.
77      */
78     void unmap();
79     using ICLTensor::unmap;
80 
81     /** Return the parent tensor of the subtensor
82      *
83      * @return Parent tensor
84      */
85     ICLTensor *parent();
86 
87     // Inherited methods overridden:
88     ITensorInfo      *info() const override;
89     ITensorInfo      *info() override;
90     const cl::Buffer &cl_buffer() const override;
91     CLQuantization    quantization() const override;
92 
93 protected:
94     // Inherited methods overridden:
95     uint8_t *do_map(cl::CommandQueue &q, bool blocking) override;
96     void do_unmap(cl::CommandQueue &q) override;
97 
98 private:
99     ICLTensor            *_parent;
100     mutable SubTensorInfo _info;
101 };
102 }
103 #endif /*ARM_COMPUTE_CLSUBTENSOR_H */
104