@@ -309,18 +309,32 @@ namespace Core::IO
309309 std::all_of (section_name.begin () + 5 , section_name.end (),
310310 [](const char c) { return std::isdigit (c); });
311311 }
312+
313+ bool is_legacy_section (const std::string& section_name) const
314+ {
315+ return std::ranges::any_of (
316+ legacy_section_names_, [&](const auto & name) { return name == section_name; });
317+ }
318+
319+ InputFile::FragmentIteratorRange in_section (const std::string& section_name) const
320+ {
321+ static const std::vector<InputFile::Fragment> empty;
322+
323+ if (!content_by_section_.contains (section_name))
324+ {
325+ return std::views::all (empty);
326+ }
327+
328+ // Take a const reference to the section content to match the return type.
329+ const auto & lines = content_by_section_.at (section_name).fragments ;
330+ return std::views::all (lines);
331+ }
312332 };
313333
314334 } // namespace Internal
315335
316336 namespace
317337 {
318- /* *
319- * Sections that contain at least this number of entries are considered huge and are only
320- * available on rank 0.
321- */
322- constexpr std::size_t huge_section_threshold = 10'000 ;
323-
324338 // ! The different ways we want to handle sections in the input file.
325339 enum class SectionType
326340 {
@@ -720,31 +734,28 @@ namespace Core::IO
720734 // Communicate the dat content
721735 if (Core::Communication::my_mpi_rank (pimpl_->comm_ ) == 0 )
722736 {
723- // Temporarily move the sections that are not huge into a separate map.
724- std::unordered_map<std::string, Internal::SectionContent> non_huge_sections ;
737+ // Temporarily move the sections that we want to broadcast into a separate map.
738+ std::unordered_map<std::string, Internal::SectionContent> non_legacy_sections ;
725739
726740 for (auto && [section_name, content] : pimpl_->content_by_section_ )
727741 {
728- if (std::holds_alternative<Internal::SectionContent::DatContent>(content.content ))
742+ if (std::holds_alternative<Internal::SectionContent::DatContent>(content.content ) &&
743+ !pimpl_->is_legacy_section (section_name))
729744 {
730- if (content.as_dat ().lines .size () < huge_section_threshold)
731- {
732- non_huge_sections[section_name] = std::move (content);
733- }
745+ non_legacy_sections[section_name] = std::move (content);
734746 }
735747 }
736748
737- Core::Communication::broadcast (non_huge_sections , 0 , pimpl_->comm_ );
749+ Core::Communication::broadcast (non_legacy_sections , 0 , pimpl_->comm_ );
738750
739- // Move the non-huge sections back into the main map.
740- for (auto && [section_name, content] : non_huge_sections)
751+ for (auto && [section_name, content] : non_legacy_sections)
741752 {
742753 pimpl_->content_by_section_ [section_name] = std::move (content);
743754 }
744755 }
745756 else
746757 {
747- // Other ranks receive the non-huge sections.
758+ // Other ranks receive the non-legacy sections.
748759 Core::Communication::broadcast (pimpl_->content_by_section_ , 0 , pimpl_->comm_ );
749760 }
750761
@@ -754,7 +765,7 @@ namespace Core::IO
754765 {
755766 ryml::Tree tree_with_small_sections = init_yaml_tree_with_exceptions ();
756767 tree_with_small_sections.rootref () |= ryml::MAP;
757- // Go through the tree and drop the huge sections from the tree.
768+ // Go through the tree and drop the legacy sections from the tree.
758769 for (auto file_node : pimpl_->yaml_tree_ .rootref ())
759770 {
760771 auto new_file_node = tree_with_small_sections.rootref ().append_child ();
@@ -763,8 +774,7 @@ namespace Core::IO
763774
764775 for (auto section_node : file_node.children ())
765776 {
766- if (section_node.is_map () ||
767- (section_node.is_seq () && section_node.num_children () < huge_section_threshold))
777+ if (!pimpl_->is_legacy_section (to_string (section_node.key ())))
768778 {
769779 // Copy the node to the new tree.
770780 auto new_section_node = new_file_node.append_child ();
@@ -773,10 +783,8 @@ namespace Core::IO
773783 }
774784 }
775785
776- std::stringstream ss;
777- ss << tree_with_small_sections;
778- std::string yaml_str = ss.str ();
779- Core::Communication::broadcast (yaml_str, /* root*/ 0 , pimpl_->comm_ );
786+ auto serialized_tree = ryml::emitrs_yaml<std::string>(tree_with_small_sections);
787+ Core::Communication::broadcast (serialized_tree, /* root*/ 0 , pimpl_->comm_ );
780788 }
781789 else
782790 {
@@ -836,48 +844,15 @@ namespace Core::IO
836844 }
837845 }
838846
839- InputFile::FragmentIteratorRange InputFile::in_section (const std::string& section_name) const
840- {
841- static const std::vector<Fragment> empty;
842-
843- // Early return in case the section does not exist at all.
844- const bool known_somewhere = has_section (section_name);
845- if (!known_somewhere)
846- {
847- return std::views::all (empty);
848- }
849-
850- const bool locally_known = pimpl_->content_by_section_ .contains (section_name);
851- const bool known_everywhere = Core::Communication::all_reduce<bool >(
852- locally_known, [](const bool & r, const bool & in) { return r && in; }, pimpl_->comm_ );
853-
854- if (known_everywhere)
855- {
856- // Take a const reference to the section content to match the return type.
857- const auto & lines = pimpl_->content_by_section_ .at (section_name).fragments ;
858- return std::views::all (lines);
859- }
860- else
861- // Distribute the content of the section to all ranks.
862- {
863- FOUR_C_ASSERT ((!locally_known && (Core::Communication::my_mpi_rank (pimpl_->comm_ ) > 0 )) ||
864- (locally_known && (Core::Communication::my_mpi_rank (pimpl_->comm_ ) == 0 )),
865- " Implementation error: section should be known on rank 0 and unknown on others." );
866-
867- auto & content = pimpl_->content_by_section_ [section_name];
868- Core::Communication::broadcast (content, 0 , pimpl_->comm_ );
869- content.set_up_fragments ();
870-
871- const auto & lines = pimpl_->content_by_section_ .at (section_name).fragments ;
872- return std::views::all (lines);
873- }
874- }
875-
876-
877847
878848 InputFile::FragmentIteratorRange InputFile::in_section_rank_0_only (
879849 const std::string& section_name) const
880850 {
851+ FOUR_C_ASSERT_ALWAYS (pimpl_->is_legacy_section (section_name),
852+ " You tried to process section '{}' on rank 0 only, but this feature is meant for special "
853+ " legacy sections. Please use match_section() instead." ,
854+ section_name);
855+
881856 if (Core::Communication::my_mpi_rank (pimpl_->comm_ ) == 0 &&
882857 pimpl_->content_by_section_ .contains (section_name))
883858 {
@@ -952,7 +927,7 @@ namespace Core::IO
952927 if (list_spec)
953928 {
954929 std::vector<InputParameterContainer> list_entries;
955- for (const auto & line : in_section (section_name))
930+ for (const auto & line : pimpl_-> in_section (section_name))
956931 {
957932 Core::IO::ValueParser parser{line.get_as_dat_style_string (),
958933 {.user_scope_message =
@@ -977,7 +952,7 @@ namespace Core::IO
977952 // Create a group in the dat file format by starting with the section name.
978953 std::stringstream flattened_dat;
979954 flattened_dat << std::quoted (section_name) << " " ;
980- for (const auto & line : in_section (section_name))
955+ for (const auto & line : pimpl_-> in_section (section_name))
981956 {
982957 std::string line_str (line.get_as_dat_style_string ());
983958 // Split the line into key-value according to the dat file format. Quote keys and values
@@ -1084,7 +1059,7 @@ namespace Core::IO
10841059 auto section = tree.rootref ().append_child ();
10851060 section << ryml::key (section_name);
10861061 section |= ryml::SEQ;
1087- for (const auto & line : in_section (section_name))
1062+ for (const auto & line : pimpl_-> in_section (section_name))
10881063 {
10891064 section.append_child () = ryml::csubstr (
10901065 line.get_as_dat_style_string ().data (), line.get_as_dat_style_string ().size ());
0 commit comments