NexGenAnalytics / Trilinos

Primary repository for the Trilinos Project
https://trilinos.org/
Other
0 stars 2 forks source link

Test yaml-cpp parser #219

Open cwschilly opened 10 months ago

cwschilly commented 10 months ago

This PR implements an optional yaml-cpp parser.

This PR should not be merged; its source code changes have been added to Trilinos PR #12599.

Current status

I am testing these by running MiniEM with the following input files:

This table represents the testing status. We expect all tests to pass except yaml-cpp Off x new yaml.

yaml-cpp On yaml-cpp Off
new yaml :heavy_check_mark: :x:
old yaml :heavy_check_mark: :heavy_check_mark:
xml :heavy_check_mark: :heavy_check_mark:

The difference between the old yaml and new yaml is this block:

  Wishlist:
    List of lists:
      - [1, 2, 3]
      - [1, 2, 3, 4]
    List of lists of lists:
      - [[1,2,3], [4,5,6], [7,8,9]]
      - [[9,7,8], [6,5,4], [3,2,1]]
    Line continuation:
      - [1, 2, 3, 4, 5, 6,
        7, 8, 9, 10]
# allow unicode characters in comments: ±

General Notes

  1. Many of the additions in Teuchos_YamlParser.cpp came from this commit, which removed yaml-cpp from Trilinos in 2017. I mainly "undid" these changes, with some other additions and the introduction of #ifdef directives.
  2. It is worth noting that many of the reasons that the above commit gave for removing yaml-cpp are still valid.
  3. yaml-cpp is only used to read in yaml (ie any functions that write out yaml are untouched)
  4. This PR adds internal CI to run the tests in the above table. These workflows, build scripts, and dockerfiles would not be included in the final PR to Trilinos.

Regarding Teuchos::Array

The original yaml-cpp parser (referenced above) could only support 1-D or 2-D Teuchos::Arrays. This meant that ragged arrays and higher dimensional arrays could not be parsed.

In the event of the user having a simple 1- or 2-D array in the yaml, the parser will create either a standard Teuchos::Array or a Teuchos::TwoDArray.

For the other cases, I've defined functions (getYaml2DRaggedArray and getYaml3DArray) that return Teuchos::Arrays of Teuchos::Arrays (of Teuchos::Arrays, in the 3D case).

Regarding CMake

  1. This PR adds yaml-cpp as a LIB_OPTIONAL_TPL in the TeuchosCore package.
  2. It also creates the HAVE_TEUCHOSCORE_YAMLCPP macro, which allows for #ifdef statements.

Regarding Changes to Source Code

In order to use a yaml input file, some changes were required in the MiniEM source code.

  1. Specifically, line 107 of MiniEM_ClosureModel_Factory_impl.hpp now reads:

    // unsigned int seed = plist.get<unsigned int>("seed");
    unsigned int seed = static_cast<unsigned int>(plist.get<int>("seed"));

    Originally, seed was read directly in as an unsigned int, with the xml input files specifying its type. Since yaml does not allow for types besides int, double, and string, I had to change the code to accept an int and then cast it as an unsigned int.

  2. This meant I had to change all of the xml input files to pass an int as well.

    <!-- <Parameter name="seed" type="unsigned int" value="0"/> -->
    <Parameter name="seed" type="int" value="0"/>
  3. The other change to source code is self-explanatory. BlockPrec/main.cpp now contains the option to parse yaml inputs:

    if (input_file.substr(input_file.size()-4) == std::string(".xml")) {
    Teuchos::updateParametersFromXmlFileAndBroadcast(input_file, input_params.ptr(), *comm);
    } else {
    Teuchos::updateParametersFromYamlFileAndBroadcast(input_file, input_params.ptr(), *comm);
    }