Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-line help for input options for factory created types #2483

Merged
merged 3 commits into from
Jan 6, 2022

Conversation

ZedThree
Copy link
Member

@ZedThree ZedThree commented Jan 6, 2022

Replaces #2336 with a much simpler implementation. It does a very similar thing: pass an empty Options to a type to see what options it expects, and the associated default values and docstrings, and then print all that information.

#2336 tries to avoid actually constructing types, using a companion template class that just reads Options, which has some distinct advantages: we only need an Options to construct this companion class, so we don't need to set anything else up; we don't need to worry about any preconditions; we don't do any unnecessary work.

However, it does also have some glaring downsides: it requires either lots of duplication or ugly indirection; inheritance is painful to deal with; if default values depended on e.g. Mesh values, we have to be able to defer setting the real default value somehow.

So what we do here is just create a real object instead. Much simpler.

We need to make sure all the factories can take the explicit name of the type we want plus an Options, but this is not very much code. We also need to set up a global Mesh, but making a very small one is pretty cheap.

I was worried that there were some types that can only be constructed with >2 processors, but that doesn't seem to be the case.

Also, all the Solvers set most of their options in their init functions, which makes them a little bit pointless:

$  ./blob2d --help-solver arkode
Input options for Solver 'arkode':

solver:is_nonsplit_model_diffusive = true               # type: bool, doc: If not a split operator, treat RHS as diffusive?
solver:mms = false              # type: bool
solver:mms_initialise = false           # type: bool
solver:monitor_timestep = false         # type: bool

If we're happy with the direction of this PR, I'll change types to read the Options in their constructors instead.

@ZedThree ZedThree added this to the BOUT-5.0 milestone Jan 6, 2022
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

@@ -78,13 +78,16 @@ public:

using Factory::create;
ReturnType create(Options* options, int y_offset = 0, Mesh* mesh = nullptr,
Region<Ind3D> region_in = {}) {
Region<Ind3D> region_in = {}) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the parameter region_in is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

Region<Ind3D> region_in = {}) const {
              ^
const        &

return Factory::create(options, y_offset, mesh, region_in);
}
ReturnType create(int y_offset = 0, Mesh* mesh = nullptr,
Region<Ind3D> region_in = {}) {
Region<Ind3D> region_in = {}) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the parameter region_in is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

Region<Ind3D> region_in = {}) const {
              ^
const        &

mesh_options["nz"] = 4;

// We might need a global mesh for some types, so best make one
bout::globals::mpi = new MpiWrapper();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: assigning newly created gsl::owner<> to non-owner MpiWrapper * [cppcoreguidelines-owning-memory]

bout::globals::mpi = new MpiWrapper();
^


if (current_arg == help_arg) {
if (i + 1 >= argc) {
throw BoutException(_("Usage is {} {} <name>\n"), argv[0], help_arg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

throw BoutException(_("Usage is {} {} <name>\n"), argv[0], help_arg);
                                                  ^

if (i + 1 >= argc) {
throw BoutException(_("Usage is {} {} <name>\n"), argv[0], help_arg);
}
printTypeOptions<Factory>(argv[i + 1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

printTypeOptions<Factory>(argv[i + 1]);
                          ^

johnomotani
johnomotani previously approved these changes Jan 6, 2022
Copy link
Contributor

@johnomotani johnomotani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

src/bout++.cxx Outdated Show resolved Hide resolved
@bendudson
Copy link
Contributor

Thanks @ZedThree (and happy New Year!). This looks like a great way to get this functionality in a simple way without lots of code changes.

The two-stage construction of the solvers is a bit odd, and moving everything into the constructor would be good. I think init was needed to resolve some kind of circular dependency when adding PETSc support around 2013, but perhaps there is a way to remove it now.

Co-authored-by: johnomotani <john.omotani@ukaea.uk>
Copy link
Contributor

@johnomotani johnomotani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ZedThree
Copy link
Member Author

ZedThree commented Jan 6, 2022

Happy new year to you too @bendudson @johnomotani !

I'll have a look at squashing the solver init functions into the ctors in a separate PR. Might look at doing so for Mesh too? I'm less sure that will work though.

The other QoL missing from this PR is that the arguments must be separated with a space: it would be nice to split on equals too, but then we need to do more parsing of the command line, and at that point I almost want to find a library.
I'll investigate this more if people make noise about it.

@ZedThree
Copy link
Member Author

ZedThree commented Jan 6, 2022

(Also, this PR is a factor 10 smaller than #2336 😶)

@ZedThree ZedThree changed the title Command-line help for options Command-line help for input options for factory created types Jan 6, 2022
@ZedThree ZedThree merged commit ffd9c6a into next Jan 6, 2022
@ZedThree ZedThree deleted the command-line-help-options-simple-ctors branch January 6, 2022 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants