Skip to content

Use namespace for global variables 'mesh' and 'dump' - #1470

Merged
bendudson merged 43 commits into
nextfrom
namespace_globals
Feb 5, 2019
Merged

Use namespace for global variables 'mesh' and 'dump'#1470
bendudson merged 43 commits into
nextfrom
namespace_globals

Conversation

@johnomotani

@johnomotani johnomotani commented Dec 19, 2018

Copy link
Copy Markdown
Contributor
  • Create the namespace bout::globals containing Mesh* mesh and Datafile dump.
  • In bout.hxx include statement using namespace bout::globals so no changes are needed to user code.
  • Remove references to global mesh pointer in several places in the library code.
  • The global pointer (renamed to bout::globals::mesh) is still used in many places as a default argument or if nullptr is passed as a Mesh*.
  • The last places depending on bout::globals::mesh are: the solver code (see Solver depends on global mesh #1471); FieldData (which should be refactored, possibly merging FieldData and Field, see also Make Field more powerful #897).
  • SlepcSolver uses bout::globals::dump directly because it needs to write output without calling BoutMonitor. Seems acceptable to leave this as a special case because SlepcSolver needs a different output scheme as an eigenvalue rather than initial-value solver.
  • The only changes to the tests are that the unit tests need to include using namespace bout::globals because they use the global mesh.
  • There is an unrelated change to test-petsc_laplace, just getting it to work and removing the binary grid file.

This implements the suggestion in #1468.

@johnomotani johnomotani added the proposal A code/feature outline proposal label Dec 19, 2018
Comment thread include/field.hxx Outdated
Comment thread include/field.hxx Outdated
Comment thread include/field_data.hxx Outdated
Comment thread src/invert/laplace/impls/cyclic/cyclic_laplace.hxx Outdated
Comment thread src/mesh/boundary_standard.cxx
Comment thread src/mesh/difops.cxx Outdated
Comment thread src/physics/gyro_average.cxx
Comment thread src/physics/sourcex.cxx Outdated
Comment thread src/solver/impls/arkode/arkode.cxx Outdated
@johnomotani
johnomotani force-pushed the namespace_globals branch 4 times, most recently from 9d18cf3 to b145852 Compare December 20, 2018 10:36
bendudson
bendudson previously approved these changes Dec 23, 2018
@bendudson

Copy link
Copy Markdown
Contributor

Sorry missed that this is going into forward_declares rather than next. Can it go into next then merge next into forward_declares?

@johnomotani

Copy link
Copy Markdown
Contributor Author

The namespace_globals branch includes a bunch of commits from forward_declares. It uses at least a few of the changes, which we could 'rebase' away, but if #1464 is nearly ready it would be easier not to:

  • including physicsmodel.hxx in solver.cxx instead of solver.hxx
  • moving Field::getMesh() implementation from .hxx to .cxx

@d7919

d7919 commented Dec 30, 2018

Copy link
Copy Markdown
Member

#1464 was kind of an experiment to see if there were any immediate gains -- I think there are some (both in terms of compilation time as well as flexibility) but the commit history might be a bit noisier than required as I approached this in a roundabout way. @ZedThree has looked into tools that might be able to automate some of these include changes (although the compilation time gains would not have been achieved with this -- it required some insight to see how to make a gain).

@johnomotani

Copy link
Copy Markdown
Contributor Author

Righto, in that case it does make sense to rebase this on next. Will do now...

@johnomotani
johnomotani changed the base branch from forward_declares to next January 2, 2019 13:11
@johnomotani

Copy link
Copy Markdown
Contributor Author

I've rebased this PR on next instead of forward_declares now, and tidied up the commit history a little bit. I think the PR should be ready to merge now, unless anyone has more comments?

Comment thread include/bout/solver.hxx Outdated
Comment thread include/bout.hxx Outdated
Comment thread include/bout/solver.hxx
Comment thread src/physics/physicsmodel.cxx Outdated

@ZedThree ZedThree left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A super minor comment: in some places you've done:

Mesh* mesh = f.getMesh()

and in others

Mesh* localmesh = f.getMesh();

Is it worth picking one and being consistent (answer might be no!)? I only mention this as we're touching all these places

Comment thread include/invert_parderiv.hxx Outdated
Comment thread src/bout++.cxx Outdated
Comment thread src/solver/impls/petsc/petsc.cxx Outdated
Comment thread src/solver/impls/power/power.hxx
@ZedThree ZedThree added this to the BOUT-4.3 milestone Jan 2, 2019
johnomotani and others added 7 commits January 2, 2019 21:35
Forward declare class Mesh instead. Requires explicitly including
mesh.hxx in several other files, plus a few other extra explicit
includes.
Handle the nullptr case in the initializer list instead.
nullptr is handled by the base Laplacian class constructor and converted
to bout::globals::mesh.
Use bout::globals::mesh in Coordinates, and test_coordinates
Forward declare Field2D, Field3D, Vector2D and Vector3D in datafile.hxx
to avoid circular dependencies due to including datafile.hxx in
globals.hxx.
@johnomotani

Copy link
Copy Markdown
Contributor Author

@ZedThree I think I've addressed all your comments except the one about Mesh* naming consistency.

BTW I made the changes as a rebase because there are already rather a lot of commits in this PR. Do we have a style preference for (a) 'fixing' existing commits or (b) adding new commits with small changes/fixes to a PR?

Update SlepcSolver to use 'bout::globals::dump' instead of 'dump'.
BOUTMAIN is defined in physicsmodel.hxx, so a file using it will usually
include 'using namespace bout::globals' from physicsmodel.hxx, but in
case the 'using' statement is explicitly excluded using the
BOUT_NO_USING_NAMESPACE_BOUTGLOBALS macro or if in future the 'using'
statement is removed, use the full name 'bout::globals::dump' instead of
'dump'.
@ZedThree

ZedThree commented Jan 3, 2019

Copy link
Copy Markdown
Member

Thanks! I'll look through them now.

BTW I made the changes as a rebase because there are already rather a lot of commits in this PR. Do we have a style preference for (a) 'fixing' existing commits or (b) adding new commits with small changes/fixes to a PR?

I prefer (b) as it's then possible to review just those commits, although if it's something like a typo in the last commit and I catch it early, I sometimes amend and force-push.

if(eigenValOnly){
simtime=reEigBout;
dump.write();
bout::globals::dump.write();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not for this PR, but it might be nice for SlepcSolver to have it's own output file (which is bout::globals::dump by default)?

ZedThree
ZedThree previously approved these changes Jan 3, 2019
@ZedThree

ZedThree commented Jan 3, 2019

Copy link
Copy Markdown
Member

Looks good to me, but @d7919 had a bunch of comments too -- are you happy those are resolved?

Comment thread include/invert_laplace.hxx Outdated
* @param[in] opt The options section to use. By default "laplace" will be used
*/
static Laplacian *create(Options *opt = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh *mesh_in = mesh);
static Laplacian *create(Options *opt = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh *mesh_in = bout::globals::mesh);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should mesh_in really be nullptr here? Presumably this is passed to the constructor noted above (or equivalent) which can then choose what to do if it receives nullptr?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed -- nullptr is a better default, it's what we do in other bits of the code, as well as giving us some space to potential remove the global mesh entirely.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can't just pass nullptr here because LaplaceFactory uses mesh_in to check whether we can use a serial solver

if(mesh_in->firstX() && mesh_in->lastX()) {
// Can use serial algorithm

I think this was originally to allow the default Laplace solver to switch between serial_tri and spt depending on whether a serial or parallel solver was needed, but now the default is cyclic in either case. I think it would be better now to allow any Laplace solver to be created, and move the check into the constructor of the serial solvers. Does that sound reasonable, and should I do it here or create a separate PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If LaplaceFactory gets a nullptr, it could just fetch the global mesh though?

LaplaceFactory should get rewritten anyway (e.g. using GenericFactory), so I would leave that to another PR

Comment thread src/mesh/parallel/fci.cxx Outdated
@d7919

d7919 commented Jan 29, 2019

Copy link
Copy Markdown
Member

Have you checked that we can compile and run an appropriately updated example without using the using statements?

@ZedThree

ZedThree commented Feb 1, 2019

Copy link
Copy Markdown
Member

Might be wise to merge in next now 4.2.1 is in and verify everything still works

Avoids potential issues if order of FCIMap member declarations changes
in the future.
@johnomotani

Copy link
Copy Markdown
Contributor Author

Have you checked that we can compile and run an appropriately updated example without using the using statements?

@d7919 The integrated tests run without needing any update. Examples and user code should not need updating either. I've just checked examples/blob2d to be sure.

and also for LaplaceFactory::createLaplacian.

LaplaceFactory uses bout::globals::mesh if the input 'Mesh* mesh_in' is
nullptr.
@bendudson
bendudson merged commit 51ecd76 into next Feb 5, 2019
@bendudson
bendudson deleted the namespace_globals branch February 5, 2019 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proposal A code/feature outline proposal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants