micro_section_allocator.h 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * \file micro_section_allocator.h
 */
#ifndef TVM_RUNTIME_MICRO_MICRO_SECTION_ALLOCATOR_H_
#define TVM_RUNTIME_MICRO_MICRO_SECTION_ALLOCATOR_H_

#include <unordered_map>
#include "micro_common.h"

namespace tvm {
namespace runtime {

/*!
 * \brief allocator for an on-device memory section
 */
class MicroSectionAllocator {
 public:
  /*!
   * \brief constructor that specifies section boundaries
   * \param region location and size of the section on the device
   */
41 42
  explicit MicroSectionAllocator(DevMemRegion region, size_t word_size)
    : start_addr_(region.start),
43
      size_(0),
44 45 46 47 48 49
      capacity_(region.size),
      word_size_(word_size) {
      CHECK_EQ(start_addr_.value().val64 % word_size, 0)
        << "micro section start not aligned to " << word_size << " bytes";
      CHECK_EQ(capacity_ % word_size, 0)
        << "micro section end not aligned to " << word_size << " bytes";
50 51 52 53 54 55 56 57 58 59 60 61
    }

  /*!
   * \brief destructor
   */
  ~MicroSectionAllocator() {}

  /*!
   * \brief memory allocator
   * \param size size of allocated memory in bytes
   * \return pointer to allocated memory region in section, nullptr if out of space
   */
62 63
  DevPtr Allocate(size_t size) {
    size_ = UpperAlignValue(size_, word_size_);
64 65
    CHECK(size_ + size < capacity_)
        << "cannot alloc " << size << " bytes in section with start_addr " <<
66 67
        start_addr_.cast_to<void*>();
    DevPtr alloc_addr = start_addr_ + size_;
68
    size_ += size;
69 70
    alloc_map_[alloc_addr.value().val64] = size;
    return alloc_addr;
71 72 73 74 75 76 77
  }

  /*!
   * \brief free prior allocation from section
   * \param offs offset to allocated memory
   * \note simple allocator scheme, more complex versions will be implemented later
   */
78 79 80 81
  void Free(DevPtr addr) {
    CHECK(alloc_map_.find(addr.value().val64) != alloc_map_.end())
      << "freed pointer was never allocated";
    alloc_map_.erase(addr.value().val64);
82 83 84 85 86 87 88 89
    if (alloc_map_.empty()) {
      size_ = 0;
    }
  }

  /*!
   * \brief start offset of the memory region managed by this allocator
   */
90
  DevPtr start_addr() const { return start_addr_; }
91 92

  /*!
93
   * \brief current end addr of the space being used in this memory region
94
   */
95
  DevPtr curr_end_addr() const { return start_addr_ + size_; }
96 97

  /*!
98
   * \brief end addr of the memory region managed by this allocator
99
   */
100
  DevPtr max_addr() const { return start_addr_ + capacity_; }
101 102 103 104 105 106 107 108 109 110 111 112 113

  /*!
   * \brief size of the section
   */
  size_t size() const { return size_; }

  /*!
   * \brief capacity of the section
   */
  size_t capacity() const { return capacity_; }

 private:
  /*! \brief start address of the section */
114
  DevPtr start_addr_;
115 116 117 118
  /*! \brief current size of the section */
  size_t size_;
  /*! \brief total storage capacity of the section */
  size_t capacity_;
119 120
  /*! \brief number of bytes in a word on the target device */
  size_t word_size_;
121
  /*! \brief allocation map for allocation sizes */
122
  std::unordered_map<uint64_t, size_t> alloc_map_;
123 124 125 126 127
};

}  // namespace runtime
}  // namespace tvm
#endif  // TVM_RUNTIME_MICRO_MICRO_SECTION_ALLOCATOR_H_