CONQUEST-release icon indicating copy to clipboard operation
CONQUEST-release copied to clipboard

Reduce code duplication in PAO_grid_transform_module

Open tkoskela opened this issue 2 years ago • 5 comments
trafficstars

The subroutines

https://github.com/OrderN/CONQUEST-release/blob/1378f0359b798362b84177bc4288e97ceff17824/src/PAO_grid_transform_module.f90#L98

and

https://github.com/OrderN/CONQUEST-release/blob/1378f0359b798362b84177bc4288e97ceff17824/src/PAO_grid_transform_module.f90#L270

Duplicate almost all of the code with only minor differences. It would be good for code stability to move the common code into a utility function, or perhaps merge the subroutines into one. Worth keeping in mind if they need refactoring for #198

tkoskela avatar Aug 16 '23 07:08 tkoskela

This is a good point. We could easily merge the two and add a flag to select PAO value or gradient to choose whether to call evaluate_pao or pao_elem_derivative_2 (though this latter name should be rationalised for clarity!)

davidbowler avatar Aug 21 '23 14:08 davidbowler

The simplest way is to put an if clause in the innermost loop. I've got a draft version of this in tk-reduce-pao-duplication. I'm a bit concerned about the performance implication of an if statement in the innermost loop. I would like to spend a bit of time to explore different options.

tkoskela avatar Aug 24 '23 09:08 tkoskela

Procedure pointers in Fortran 2003 could be useful here. According to a comment in this thread the performance gains can be dubious, but worth checking in my opinion. If there is no real performance gain, it becomes more of a question of which coding style we prefer. I don't have a strong opinion on which style is better, another if-layer in the loop nest or an obscure function interface.

Another reference in Intel docs

tkoskela avatar Aug 30 '23 09:08 tkoskela

I wonder if it's not easier to just use an abstract interface and pass the required subroutine as an argument, as described in this thread

davidbowler avatar Aug 30 '23 10:08 davidbowler

I wonder if it's not easier to just use an abstract interface and pass the required subroutine as an argument, as described in this thread

Yes, that does look easier! Thanks, I wasn't aware you could pass subroutines without pointers

tkoskela avatar Aug 31 '23 09:08 tkoskela