You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2019/11/18 20:38:13 UTC

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type

zhiics commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r347596111
 
 

 ##########
 File path: include/tvm/runtime/container.h
 ##########
 @@ -0,0 +1,230 @@
+/*
+ * 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 tvm/runtime/container.h
+ * \brief Common POD(plain old data) container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_H_
+#define TVM_RUNTIME_CONTAINER_H_
+#include <dmlc/logging.h>
+#include <tvm/runtime/object.h>
+#include <initializer_list>
+#include <type_traits>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+
+/**
+ * \brief Base template for classes with array like memory layout.
+ *
+ *        It provides general methods to access the memory. The memory
+ *        layout is ArrayType + [ElemType]. The alignment of ArrayType
+ *        and ElemType is handled by the memory allocator.
+ *
+ * \tparam ArrayType
+ * \tparam ElemType
+ */
+template <typename ArrayType, typename ElemType>
+class InplaceArrayBase {
+ public:
+  /**
+   * \brief Initialize the elements in the array.
+   */
+  void Init() {
+    CHECK_EQ(sizeof(ArrayType) % alignof(ElemType), 0);
+    for (size_t i = 0; i < Self()->size(); ++i) {
+      void* field_ptr = AddressOf(i);
+      new (field_ptr) ElemType();
+    }
+  }
+
+  /**
+   * \brief Initialize the elements in the array.
+   *
+   * \tparam Iterator Iterator type of the array.
+   * \param begin The begin iterator.
+   * \param end The end iterator.
+   */
+  template <typename Iterator>
+  void Init(Iterator begin, Iterator end) {
+    CHECK_EQ(sizeof(ArrayType) % alignof(ElemType), 0);
+    ArrayType* self = Self();
+    size_t num_elems = std::distance(begin, end);
+    if (num_elems != self->size()) {
+      LOG(FATAL)
+          << "Number of initializer values does not match number of elements\n";
+    }
+    auto it = begin;
+    for (size_t i = 0; i < num_elems; ++i) {
+      void* field_ptr = AddressOf(i);
+      new (field_ptr) ElemType(*it);
+      ++it;
+    }
+  }
+
+  /**
+   * \brief Initialize the elements in the array.
+   *
+   * \param init The initializer list of elements.
+   */
+  void Init(std::initializer_list<ElemType> init) {
+    CHECK_EQ(sizeof(ArrayType) % alignof(ElemType), 0);
+    Init(init.begin(), init.end());
+  }
+
+  /*!
+   * \brief Access element at index
+   * \param idx The index of the element.
+   * \return Const reference to ElemType at the index.
+   */
+  const ElemType& operator[](size_t idx) const {
+    size_t size = Self()->size();
 
 Review comment:
   Just call the below one directly?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services