Treelite
split.cc
Go to the documentation of this file.
1 
6 #include <dmlc/registry.h>
7 #include "./builder.h"
8 
9 namespace treelite {
10 namespace compiler {
11 
12 DMLC_REGISTRY_FILE_TAG(split);
13 
14 int count_tu_nodes(ASTNode* node) {
15  int accum = (dynamic_cast<TranslationUnitNode*>(node)) ? 1 : 0;
16  for (ASTNode* child : node->children) {
17  accum += count_tu_nodes(child);
18  }
19  return accum;
20 }
21 
22 void ASTBuilder::Split(int parallel_comp) {
23  if (parallel_comp <= 0) {
24  LOG(INFO) << "Parallel compilation disabled; all member trees will be "
25  << "dumped to a single source file. This may increase "
26  << "compilation time and memory usage.";
27  return;
28  }
29  LOG(INFO) << "Parallel compilation enabled; member trees will be "
30  << "divided into " << parallel_comp << " translation units.";
31  CHECK_EQ(this->main_node->children.size(), 1);
32  ASTNode* top_ac_node = this->main_node->children[0];
33  CHECK(dynamic_cast<AccumulatorContextNode*>(top_ac_node));
34 
35  /* tree_head[i] stores reference to head of tree i */
36  std::vector<ASTNode*> tree_head;
37  for (ASTNode* node : top_ac_node->children) {
38  CHECK(dynamic_cast<ConditionNode*>(node) || dynamic_cast<OutputNode*>(node)
39  || dynamic_cast<CodeFolderNode*>(node));
40  tree_head.push_back(node);
41  }
42  /* dynamic_cast<> is used here to check node types. This is to ensure
43  that we don't accidentally call Split() twice. */
44 
45  const int ntree = static_cast<int>(tree_head.size());
46  const int nunit = parallel_comp;
47  const int unit_size = (ntree + nunit - 1) / nunit;
48  std::vector<ASTNode*> tu_list; // list of translation units
49  const int current_num_tu = count_tu_nodes(this->main_node);
50  for (int unit_id = 0; unit_id < nunit; ++unit_id) {
51  const int tree_begin = unit_id * unit_size;
52  const int tree_end = std::min((unit_id + 1) * unit_size, ntree);
53  if (tree_begin < tree_end) {
54  TranslationUnitNode* tu
55  = AddNode<TranslationUnitNode>(top_ac_node, current_num_tu + unit_id);
56  tu_list.push_back(tu);
57  AccumulatorContextNode* ac = AddNode<AccumulatorContextNode>(tu);
58  tu->children.push_back(ac);
59  for (int tree_id = tree_begin; tree_id < tree_end; ++tree_id) {
60  ASTNode* tree_head_node = tree_head[tree_id];
61  tree_head_node->parent = ac;
62  ac->children.push_back(tree_head_node);
63  }
64  }
65  }
66  top_ac_node->children = tu_list;
67 }
68 
69 } // namespace compiler
70 } // namespace treelite
AST Builder class.