node.cc 1.68 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

tqchen committed
20 21 22 23
/*!
 * \file node.cc
 * \brief Graph node data structure.
 */
tqchen committed
24
#include <nnvm/node.h>
tqchen committed
25

tqchen committed
26
namespace nnvm {
tqchen committed
27 28 29 30 31 32

Node::~Node() {
  if (inputs.size() != 0) {
    // explicit deletion via DFS
    // this is used to avoid stackoverflow caused by chain of deletions
    std::vector<Node*> stack{this};
33
    std::vector<ObjectPtr> to_delete;
tqchen committed
34 35 36 37 38 39 40 41 42 43 44
    while (!stack.empty()) {
      Node* n = stack.back();
      stack.pop_back();
      for (NodeEntry& e : n->inputs) {
        if (e.node.unique()) {
          stack.push_back(e.node.get());
          to_delete.emplace_back(std::move(e.node));
        } else {
          e.node.reset();
        }
      }
45
      for (ObjectPtr& sp : n->control_deps) {
46 47
        if (sp.unique()) {
          stack.push_back(sp.get());
48
          to_delete.emplace_back(std::move(sp));
49 50 51 52
        } else {
          sp.reset();
        }
      }
tqchen committed
53 54 55 56 57
      n->inputs.clear();
    }
  }
}

tqchen committed
58
}  // namespace nnvm