11 DMLC_REGISTRY_FILE_TAG(split);
13 int count_tu_nodes(ASTNode* node) {
14 int accum = (
dynamic_cast<TranslationUnitNode*
>(node)) ? 1 : 0;
15 for (ASTNode* child : node->children) {
16 accum += count_tu_nodes(child);
21 void ASTBuilder::Split(
int parallel_comp) {
22 if (parallel_comp <= 0) {
23 LOG(INFO) <<
"Parallel compilation disabled; all member trees will be " 24 <<
"dumped to a single source file. This may increase " 25 <<
"compilation time and memory usage.";
28 LOG(INFO) <<
"Parallel compilation enabled; member trees will be " 29 <<
"divided into " << parallel_comp <<
" translation units.";
30 CHECK_EQ(this->main_node->children.size(), 1);
31 ASTNode* top_ac_node = this->main_node->children[0];
32 CHECK(dynamic_cast<AccumulatorContextNode*>(top_ac_node));
35 std::vector<ASTNode*> tree_head;
36 for (ASTNode* node : top_ac_node->children) {
37 CHECK(dynamic_cast<ConditionNode*>(node) || dynamic_cast<OutputNode*>(node)
38 || dynamic_cast<CodeFolderNode*>(node));
39 tree_head.push_back(node);
44 const int ntree =
static_cast<int>(tree_head.size());
45 const int nunit = parallel_comp;
46 const int unit_size = (ntree + nunit - 1) / nunit;
47 std::vector<ASTNode*> tu_list;
48 const int current_num_tu = count_tu_nodes(this->main_node);
49 for (
int unit_id = 0; unit_id < nunit; ++unit_id) {
50 const int tree_begin = unit_id * unit_size;
51 const int tree_end = std::min((unit_id + 1) * unit_size, ntree);
52 if (tree_begin < tree_end) {
53 TranslationUnitNode* tu
54 = AddNode<TranslationUnitNode>(top_ac_node, current_num_tu + unit_id);
55 tu_list.push_back(tu);
56 AccumulatorContextNode* ac = AddNode<AccumulatorContextNode>(tu);
57 tu->children.push_back(ac);
58 for (
int tree_id = tree_begin; tree_id < tree_end; ++tree_id) {
59 ASTNode* tree_head_node = tree_head[tree_id];
60 tree_head_node->parent = ac;
61 ac->children.push_back(tree_head_node);
65 top_ac_node->children = tu_list;