Skip to content

Polymorphic injectors, external currents, and diagnostics#377

Open
JamesMcClung wants to merge 26 commits intopsc-code:mainfrom
JamesMcClung:pr/polymorphism
Open

Polymorphic injectors, external currents, and diagnostics#377
JamesMcClung wants to merge 26 commits intopsc-code:mainfrom
JamesMcClung:pr/polymorphism

Conversation

@JamesMcClung
Copy link
Collaborator

This is the first of (hopefully) several PRs aiming to simplify the process of making a PSC integrator, while also making it more flexible.

As the title says, this PR makes injectors, external currents, and diagnostics inherit from a base class, and the PSC integrator has lists of pointers to each of those base classes.

Benefits of this approach:

  1. Remove the need for amalgamative types like CompositeInjector and DiagnosticsDefault
  2. Remove the need for empty types like ExternalCurrentsNone and InjectParticlesNone
  3. Opting out of e.g. diagnostics requires 0 code, simplifying tests
  4. No need to lock-in specific types for these objects in PscConfig types

Downsides of this approach:

  1. Virtual functions can't be templated, so the classes themselves must be templated by the function parameters (MfieldsState and Mparticles). This would be a problem if these objects a) needed to accept different types of arguments, and b) were expensive to create. Neither (a) nor (b) is currently the case.
  2. Virtual functions can't be inlined (and they require a vtable lookup, but that's relatively negligible), so this approach shouldn't be applied in hot spots.

It seems to me that every component of the PSC integrator could be polymorphic instead of templated, and that is one of the long-term goals of this PR chain.

Copy link
Contributor

@germasch germasch left a comment

Choose a reason for hiding this comment

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

Overall, I definitely like the clean up that comes by adding things after the constructor, and specifically having a list of external currents / injectors, etc, that makes it cleaner (including the default case of nothing) and more powerful.

I do have one little issue with storing the raw pointers / leaking the objects (not that the leaking as such matters, but I don't think it's too hard to prevent that).

What I'm somewhat less sure of is whether there's really much gained by having polymorphism here, in particular since essentially all the base type does is specify a given call signature, which could be done via std::function<> directly (if one were to rename the methods back to operator(), which I think I'd still prefer). I don't know, I haven't fully thought this through, in particular questions of ownership / context / copying (or avoiding them), so that's something we can discuss later, after this is merged.

Comment on lines 88 to 92
void inject(Mparticles& mprts, MfieldsState& mflds) override
{
(*this)(mprts, mflds);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I may learn more as I go through more commits, but at this point I'm wondering, why not stick with operator()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would say explicit method names helps with discoverability: one can search for implementations of named methods, but not for operator(). That's also a benefit of using polymorphism rather than templates or outright type erasure (à la std::function).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Following my own logic, I should get rid of the lambda wrappers and force users to make their own subclasses. That would be better future-proofed against more methods, too (such as requiring subclasses to provide logging-related information).

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I'm not entirely clear on my own thinking here, though I think it's nice isolating users from having to do their own subclasses -- though admittedly lambda functions are likely an even more unfamiliar feature to a casual user...

Anyway, I guess what's there currently (class from lambda) is fine for the time being.

Comment on lines 476 to 477
std::vector<InjectorBase<Mparticles, MfieldsState>*> injectors;

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I read somewhere that ideally modern C++ shouldn't use explicit pointers at all. The question here is, what guarantees that the objects pointed to don't go away?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kinda felt like using fancy pointer wrappers would be more performative than actually beneficial, since the psc integrator may as well be a singleton, and an entire case revolves around calling integrate() once. Thus the leaking :^)
But I'll go back and do the right thing...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, what would be the best option, if not unique_ptr?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As I'm working on switching to unique_ptrs, I'm finding it annoying to have to dynamically allocate all the components. I'm leaning back towards keeping raw pointers, since it's nicer to just stack-allocate things.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I haven't looked at the various places where this is being used, the ones I saw initially had a new anyway. I think transferring ownership is cleaner. I suppose one could have add_injector() or whatever dynamically copy construct the thing, which would hide the new, but at the expense of needing the injectors to be copy-constructible.

The other alternative I can think of is to just keep a reference, ie., punt on the ownership question -- the one thing that's nice about that is that it guarantees non-null, but generally speaking, I think clear ownership rules are preferable. And I can kinda think of test cases where one might want to have to integrators going at the same time for some kind of cross checking, where it'd generally be nice to not assume singleton behavior, though it's kinda far fetched.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The news were there to avoid having to stack-allocate on a separate line, which references can't do. I realize that isn't a good justification for raw pointers. I'll try to find a better solution that achieves clear ownership without introducing excessive boilerplate.

Comment on lines 149 to 151
psc.injectors.push_back(
new BoundaryInjector<ParticleGenerator, typename PscConfig::PushParticles>(
ParticleGenerator(1, 1), grid));
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this answers the question about the injectors not going away, essentially because they're being leaked ;) Obviously not a big issue as such, but I think unique_ptr may be the better option here.

Another thing I don't fully like is that the injectors are added after the fact, which kinda means that the integrator isn't fully set up after being constructed. But I think it's acceptable here, in that an empty list of integrators is a valid state.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I would have to agree that, as we currently do, passing N objects to the constructor makes that rather ugly...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The partially-constructed state of the integrator makes me uncomfortable, too... and I was only planning to make it worse over the next few weeks. This seems like a perfect place to use the builder pattern, but it might be a while before that end state is reached.

@JamesMcClung
Copy link
Collaborator Author

Some other things I'd like to build into these components are:

  • A name, used for logging.
  • Parameters like interval and a public should_run(int timestep) method

Copy link
Contributor

@germasch germasch left a comment

Choose a reason for hiding this comment

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

👍

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.

2 participants