|
| 1 | +/*! |
| 2 | + * Copyright (c) 2016 by Contributors |
| 3 | + * \file graph_algorithm.h |
| 4 | + * \brief This header contains graph algorithms on StaticGraph. |
| 5 | + * It is used compute informations such as whether two |
| 6 | + * operations can run in parallel, and helps allocation. |
| 7 | +*/ |
| 8 | +#ifndef NNVM_PASS_GRAPH_ALGORITHM_H_ |
| 9 | +#define NNVM_PASS_GRAPH_ALGORITHM_H_ |
| 10 | + |
| 11 | +#include <nnvm/graph.h> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace nnvm { |
| 15 | +namespace pass { |
| 16 | + |
| 17 | +/*! |
| 18 | + * \brief Find best path in the DAG, with reward defined |
| 19 | + * by sum of reward of each node along the path. |
| 20 | + * \param graph the original static graph. |
| 21 | + * \param topo_order topo order of the nodes in the graph. |
| 22 | + * \param node_reward the reward of each node. |
| 23 | + * \param path the output path of nodes. |
| 24 | + * \return the total reward of best path. |
| 25 | + */ |
| 26 | +inline uint32_t FindBestPath( |
| 27 | + const IndexedGraph& graph, |
| 28 | + const std::vector<uint32_t>& node_reward, |
| 29 | + std::vector<uint32_t>* path) { |
| 30 | + const uint32_t num_nodes = static_cast<uint32_t>(graph.num_nodes()); |
| 31 | + CHECK_EQ(num_nodes, node_reward.size()); |
| 32 | + |
| 33 | + std::vector<uint32_t> best_reward(node_reward.size(), 0); |
| 34 | + std::vector<uint32_t> next_node(node_reward.size(), num_nodes); |
| 35 | + uint32_t best_solution = 0, best_start_node = 0; |
| 36 | + |
| 37 | + // traverse in reverse topo order |
| 38 | + for (uint32_t i = static_cast<uint32_t>(graph.num_nodes()); i != 0; --i) { |
| 39 | + const uint32_t nid = i - 1; |
| 40 | + best_reward[nid] += node_reward[nid]; |
| 41 | + if (best_reward[nid] > best_solution) { |
| 42 | + best_solution = best_reward[nid]; |
| 43 | + best_start_node = nid; |
| 44 | + } |
| 45 | + for (const auto& e : graph[nid].inputs) { |
| 46 | + const uint32_t prev = e.node_id; |
| 47 | + if (best_reward[nid] > best_reward[prev]) { |
| 48 | + best_reward[prev] = best_reward[nid]; |
| 49 | + next_node[prev] = nid; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + path->clear(); |
| 54 | + uint32_t reward = 0; |
| 55 | + for (uint32_t nid = best_start_node; nid < num_nodes; nid = next_node[nid]) { |
| 56 | + path->push_back(nid); reward += node_reward[nid]; |
| 57 | + } |
| 58 | + CHECK_EQ(reward, best_solution); |
| 59 | + return best_solution; |
| 60 | +} |
| 61 | + |
| 62 | +/*! |
| 63 | + * \brief Color the nodes in the graph into index. |
| 64 | + * The coloring algorithm tries to assign node group |
| 65 | + * such that node in the same group cannot run in parallel. |
| 66 | + * |
| 67 | + * \param graph the original indexed graph. |
| 68 | + * \param node_importance The importance of the node |
| 69 | + * \param max_ncolor maximum number of colors allowed. |
| 70 | + * \param color the color index of each of the node. |
| 71 | + * \return the total number of colors. |
| 72 | + */ |
| 73 | +inline uint32_t ColorNodeGroup( |
| 74 | + const IndexedGraph &graph, |
| 75 | + std::vector<uint32_t> node_importance, |
| 76 | + uint32_t max_ncolor, |
| 77 | + std::vector<uint32_t> *color) { |
| 78 | + CHECK_NE(max_ncolor, 0); |
| 79 | + CHECK_EQ(graph.num_nodes(), node_importance.size()); |
| 80 | + |
| 81 | + color->clear(); |
| 82 | + color->resize(graph.num_nodes(), max_ncolor); |
| 83 | + uint32_t cindex; |
| 84 | + // greedy algorithm, every time |
| 85 | + // find a path with best reward and assign a new color |
| 86 | + // All the nodes in the path cannot run in parallel. |
| 87 | + for (cindex = 0; cindex < max_ncolor - 1; ++cindex) { |
| 88 | + std::vector<uint32_t> path; |
| 89 | + uint32_t reward = FindBestPath(graph, node_importance, &path); |
| 90 | + if (reward == 0) break; |
| 91 | + for (uint32_t nid : path) { |
| 92 | + if (node_importance[nid] != 0) { |
| 93 | + CHECK_EQ(color->at(nid), max_ncolor); |
| 94 | + color->at(nid) = cindex; |
| 95 | + // make the importance 0 after color is decided. |
| 96 | + node_importance[nid] = 0; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + // assign i for rest of the node |
| 101 | + for (uint32_t i = 0; i < graph.num_nodes(); ++i) { |
| 102 | + if (color->at(i) == max_ncolor) { |
| 103 | + color->at(i) = cindex; |
| 104 | + } |
| 105 | + } |
| 106 | + return cindex + 1; |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace pass |
| 110 | +} // namespace nnvm |
| 111 | + |
| 112 | +#endif // NNVM_PASS_GRAPH_ALGORITHM_H_ |
0 commit comments