|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_ |
| 21 | +#define TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_ |
| 22 | + |
| 23 | +#include <tvm/runtime/c_runtime_api.h> |
| 24 | +#include <tvm/runtime/device_api.h> |
| 25 | +#include <tvm/runtime/logging.h> |
| 26 | +#include <tvm/runtime/ndarray.h> |
| 27 | +#include <tvm/runtime/packed_func.h> |
| 28 | + |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace runtime { |
| 33 | +namespace hexagon { |
| 34 | + |
| 35 | +class HexagonBuffer { |
| 36 | + public: |
| 37 | + /* \brief Allocate memory within hexagon accessible memory |
| 38 | + * scopes. |
| 39 | + * |
| 40 | + * \param ndim The number of dimensions of physical storage |
| 41 | + * to allocate. |
| 42 | + * |
| 43 | + * \param shape The shape of the ndarray for which to allocate |
| 44 | + * physical storage. |
| 45 | + * |
| 46 | + * \param dtype The data type of the physical storage. |
| 47 | + * |
| 48 | + * \param scope Optional storage scope indicating the memory |
| 49 | + * space in which to allocate. Defaults to global system |
| 50 | + * memory (DDR). |
| 51 | + */ |
| 52 | + HexagonBuffer(int ndim, const int64_t* shape, DLDataType dtype, Optional<String> scope); |
| 53 | + |
| 54 | + /* \brief Allocate memory within hexagon accessible memory |
| 55 | + * scopes. |
| 56 | + * |
| 57 | + * \param nbytes The number of bytes of flat physical storage |
| 58 | + * to allocate. |
| 59 | + * |
| 60 | + * \param alignment The byte alignment to be used when allocating. |
| 61 | + * |
| 62 | + * \param scope Optional storage scope indicating the memory |
| 63 | + * space in which to allocate. Defaults to global system |
| 64 | + * memory (DDR). |
| 65 | + */ |
| 66 | + HexagonBuffer(size_t nbytes, size_t alignment, Optional<String> scope); |
| 67 | + |
| 68 | + /* \brief Construct a hexagon buffer from externally allocated storage. |
| 69 | + * |
| 70 | + * \param data The externally allocated storage. |
| 71 | + * |
| 72 | + * \param scope Optional storage scope indicating the memory |
| 73 | + * space in the external allocation belongs. Assumes global system |
| 74 | + * memory if not provided. |
| 75 | + */ |
| 76 | + explicit HexagonBuffer(void* data, Optional<String> scope = Optional<String>()); |
| 77 | + |
| 78 | + //! \brief Destruction deallocates the underlying allocations. |
| 79 | + ~HexagonBuffer(); |
| 80 | + |
| 81 | + //! \brief Prevent copy construction of HexagonBuffers. |
| 82 | + HexagonBuffer(const HexagonBuffer&) = delete; |
| 83 | + |
| 84 | + //! \brief Prevent copy assignment with HexagonBuffers. |
| 85 | + HexagonBuffer& operator=(const HexagonBuffer&) = delete; |
| 86 | + |
| 87 | + //! \brief Allow move construction. |
| 88 | + HexagonBuffer(HexagonBuffer&&); |
| 89 | + |
| 90 | + //! \brief Allow move assignment. |
| 91 | + HexagonBuffer& operator=(HexagonBuffer&&); |
| 92 | + |
| 93 | + //! \brief Return pointer to allocation or allocations. |
| 94 | + void* GetPointer(); |
| 95 | + |
| 96 | + //! \brief Memory scopes managed by a Hexagon Buffer. |
| 97 | + enum class StorageScope { |
| 98 | + //! \brief System DDR corresponding to global storage. |
| 99 | + kDDR, |
| 100 | + /*! \brief Vector tightly coupled memory corresponding to |
| 101 | + * global.vtcm storage. |
| 102 | + */ |
| 103 | + kVTCM, |
| 104 | + }; |
| 105 | + |
| 106 | + //! \brief Return storage scope of underlying allocation. |
| 107 | + StorageScope GetStorageScope() const; |
| 108 | + |
| 109 | + private: |
| 110 | + //! \brief Assign a storage scope to the buffer. |
| 111 | + void SetStorageScope(Optional<String> scope); |
| 112 | + /*! \brief Array of allocations required by the buffer. |
| 113 | + * |
| 114 | + * For a 1d (flat) storage, a single contiguous allocation will |
| 115 | + * result. For 2d storage, (count, nbytes) = shape, which will |
| 116 | + * result in `count` discrete allocations. |
| 117 | + */ |
| 118 | + std::vector<void*> allocations_; |
| 119 | + /*! \brief Whether the allocation(s) present are managed |
| 120 | + * and should be deallocated upon destruction. |
| 121 | + */ |
| 122 | + bool managed_{true}; |
| 123 | + /*! \brief The underlying storage type in which the allocation |
| 124 | + * resides. |
| 125 | + */ |
| 126 | + StorageScope storage_scope_; |
| 127 | +}; |
| 128 | + |
| 129 | +HexagonBuffer* IsHexagonBuffer(DLTensor* tensor); |
| 130 | + |
| 131 | +} // namespace hexagon |
| 132 | +} // namespace runtime |
| 133 | +} // namespace tvm |
| 134 | + |
| 135 | +#endif // TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_ |
0 commit comments