mxnet
container.h
Go to the documentation of this file.
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 
24 // Acknowledgement: This file originates from incubator-tvm
25 #ifndef MXNET_RUNTIME_CONTAINER_H_
26 #define MXNET_RUNTIME_CONTAINER_H_
27 #include <dmlc/logging.h>
28 #include <mxnet/runtime/memory.h>
29 #include <mxnet/runtime/object.h>
30 
31 #include <initializer_list>
32 #include <type_traits>
33 #include <utility>
34 #include <vector>
35 
36 namespace mxnet {
37 namespace runtime {
38 
39 class ADTBuilder;
79 template <typename ArrayType, typename ElemType>
81  public:
87  const ElemType& operator[](size_t idx) const {
88  size_t size = Self()->GetSize();
89  CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n";
90  return *(reinterpret_cast<ElemType*>(AddressOf(idx)));
91  }
92 
98  ElemType& operator[](size_t idx) {
99  size_t size = Self()->GetSize();
100  CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n";
101  return *(reinterpret_cast<ElemType*>(AddressOf(idx)));
102  }
103 
108  if (!(std::is_standard_layout<ElemType>::value &&
109  std::is_trivial<ElemType>::value)) {
110  size_t size = Self()->GetSize();
111  for (size_t i = 0; i < size; ++i) {
112  ElemType* fp = reinterpret_cast<ElemType*>(AddressOf(i));
113  fp->ElemType::~ElemType();
114  }
115  }
116  }
117 
118  protected:
119  friend class ADTBuilder;
130  template <typename... Args>
131  void EmplaceInit(size_t idx, Args&&... args) {
132  void* field_ptr = AddressOf(idx);
133  new (field_ptr) ElemType(std::forward<Args>(args)...);
134  }
135 
136  private:
142  inline ArrayType* Self() const {
143  return static_cast<ArrayType*>(const_cast<InplaceArrayBase*>(this));
144  }
145 
152  void* AddressOf(size_t idx) const {
153  static_assert(alignof(ArrayType) % alignof(ElemType) == 0 &&
154  sizeof(ArrayType) % alignof(ElemType) == 0,
155  "The size and alignment of ArrayType should respect "
156  "ElemType's alignment.");
157 
158  size_t kDataStart = sizeof(ArrayType);
159  ArrayType* self = Self();
160  char* data_start = reinterpret_cast<char*>(self) + kDataStart;
161  return data_start + idx * sizeof(ElemType);
162  }
163 };
164 
166 class ADTObj : public Object, public InplaceArrayBase<ADTObj, ObjectRef> {
167  public:
169  uint32_t tag;
171  uint32_t size{0};
172  // The fields of the structure follows directly in memory.
173 
174  static constexpr const uint32_t _type_index = TypeIndex::kMXNetADT;
175  static constexpr const char* _type_key = "MXNet.ADT";
177 
178  private:
182  size_t GetSize() const { return size; }
183 
191  template <typename Iterator>
192  void Init(Iterator begin, Iterator end) {
193  size_t num_elems = std::distance(begin, end);
194  this->size = 0;
195  auto it = begin;
196  for (size_t i = 0; i < num_elems; ++i) {
198  // Only increment size after the initialization succeeds
199  this->size++;
200  }
201  }
202 
203  friend class ADT;
205 };
206 
208 class ADT : public ObjectRef {
209  public:
216  ADT(uint32_t tag, std::vector<ObjectRef> fields)
217  : ADT(tag, fields.begin(), fields.end()){};
218 
226  template <typename Iterator>
227  ADT(uint32_t tag, Iterator begin, Iterator end) {
228  size_t num_elems = std::distance(begin, end);
229  auto ptr = make_inplace_array_object<ADTObj, ObjectRef>(num_elems);
230  ptr->tag = tag;
231  ptr->Init(begin, end);
232  data_ = std::move(ptr);
233  }
234 
241  ADT(uint32_t tag, std::initializer_list<ObjectRef> init)
242  : ADT(tag, init.begin(), init.end()){};
243 
250  const ObjectRef& operator[](size_t idx) const {
251  return operator->()->operator[](idx);
252  }
253 
257  size_t tag() const { return operator->()->tag; }
258 
262  size_t size() const { return operator->()->size; }
263 
271  template <typename... Args>
272  static ADT Tuple(Args&&... args) {
273  return ADT(0, std::forward<Args>(args)...);
274  }
275 
277 };
278 
279 } // namespace runtime
280 } // namespace mxnet
281 
282 #endif // MXNET_RUNTIME_CONTAINER_H_
ElemType & operator[](size_t idx)
Access element at index.
Definition: container.h:98
ADT(uint32_t tag, Iterator begin, Iterator end)
construct an ADT object reference.
Definition: container.h:227
#define MXNET_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:686
Runtime memory management.
namespace of mxnet
Definition: api_registry.h:33
Definition: object.h:56
~InplaceArrayBase()
Destroy the Inplace Array Base object.
Definition: container.h:107
static ADT Tuple(Args &&...args)
Construct a tuple object.
Definition: container.h:272
ADT(uint32_t tag, std::vector< ObjectRef > fields)
construct an ADT object reference.
Definition: container.h:216
An object representing a structure or enumeration.
Definition: container.h:166
Base class of all object reference.
Definition: object.h:499
uint32_t tag
The tag representing the constructor used.
Definition: container.h:169
const ObjectRef & operator[](size_t idx) const
Access element at index.
Definition: container.h:250
ADT(uint32_t tag, std::initializer_list< ObjectRef > init)
construct an ADT object reference.
Definition: container.h:241
base class of all object containers.
Definition: object.h:149
void EmplaceInit(size_t idx, Args &&...args)
Construct a value in place with the arguments.
Definition: container.h:131
size_t size() const
Return the number of fields.
Definition: container.h:262
size_t tag() const
Return the ADT tag.
Definition: container.h:257
A managed object in MXNet runtime.
const ElemType & operator[](size_t idx) const
Access element at index.
Definition: container.h:87
A builder class that helps to incrementally build ADT.
Definition: ffi_helper.h:106
Base template for classes with array like memory layout.
Definition: container.h:80
reference to algebraic data type objects.
Definition: container.h:208
#define MXNET_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:669