Skip to content

Commit f3f0046

Browse files
authored
Merge pull request #572 from sebproell/yaml-large-sections
Only communicate small sections when reading yaml
2 parents 3c9f5ae + e5ff8e1 commit f3f0046

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/core/io/src/4C_io_input_file.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ namespace Core::IO
3333
namespace
3434
{
3535
std::string to_string(const ryml::csubstr str) { return std::string(str.data(), str.size()); };
36+
37+
// Copy the content of the source node to the destination node. Useful to copy a node from one
38+
// tree to another.
39+
void deep_copy(ryml::NodeRef src, ryml::NodeRef dst)
40+
{
41+
dst.set_type(src.type());
42+
43+
if (src.has_key())
44+
{
45+
dst.set_key(src.key());
46+
}
47+
if (src.has_val())
48+
{
49+
dst.set_val(src.val());
50+
}
51+
// Recursively copy children
52+
for (size_t i = 0; i < src.num_children(); ++i)
53+
{
54+
c4::yml::NodeRef src_child = src.child(i);
55+
c4::yml::NodeRef dst_child = dst.append_child();
56+
deep_copy(src_child, dst_child);
57+
}
58+
};
59+
3660
} // namespace
3761

3862
namespace Internal
@@ -728,8 +752,29 @@ namespace Core::IO
728752
{
729753
if (Core::Communication::my_mpi_rank(pimpl_->comm_) == 0)
730754
{
755+
ryml::Tree tree_with_small_sections = init_yaml_tree_with_exceptions();
756+
tree_with_small_sections.rootref() |= ryml::MAP;
757+
// Go through the tree and drop the huge sections from the tree.
758+
for (auto file_node : pimpl_->yaml_tree_.rootref())
759+
{
760+
auto new_file_node = tree_with_small_sections.rootref().append_child();
761+
new_file_node.set_type(file_node.type());
762+
new_file_node.set_key(file_node.key());
763+
764+
for (auto section_node : file_node.children())
765+
{
766+
if (section_node.is_map() ||
767+
(section_node.is_seq() && section_node.num_children() < huge_section_threshold))
768+
{
769+
// Copy the node to the new tree.
770+
auto new_section_node = new_file_node.append_child();
771+
deep_copy(section_node, new_section_node);
772+
}
773+
}
774+
}
775+
731776
std::stringstream ss;
732-
ss << pimpl_->yaml_tree_;
777+
ss << tree_with_small_sections;
733778
std::string yaml_str = ss.str();
734779
Core::Communication::broadcast(yaml_str, /*root*/ 0, pimpl_->comm_);
735780
}

0 commit comments

Comments
 (0)