Skip to content

Cleanup more pointers#1225

Merged
pcarruscag merged 6 commits intodevelopfrom
cleanup_more_pointers
Mar 9, 2021
Merged

Cleanup more pointers#1225
pcarruscag merged 6 commits intodevelopfrom
cleanup_more_pointers

Conversation

@pcarruscag
Copy link
Member

Proposed Changes

Pointers are pure evil.

PR Checklist

  • I am submitting my contribution to the develop branch.
  • My contribution generates no new compiler warnings (try with the '-Wall -Wextra -Wno-unused-parameter -Wno-empty-body' compiler flags, or simply --warnlevel=2 when using meson).
  • My contribution is commented and consistent with SU2 style.
  • I have added a test case that demonstrates my contribution, if necessary.
  • I have updated appropriate documentation (Tutorials, Docs Page, config_template.cpp) , if necessary.

@lgtm-com
Copy link

lgtm-com bot commented Mar 7, 2021

This pull request introduces 2 alerts and fixes 6 when merging 690c98c into b6b2e55 - view on LGTM.com

new alerts:

  • 2 for Multiplication result converted to larger type

fixed alerts:

  • 6 for Comparison of narrow type with wide type in loop condition

@lgtm-com
Copy link

lgtm-com bot commented Mar 7, 2021

This pull request introduces 2 alerts and fixes 6 when merging e35a5e2 into b6b2e55 - view on LGTM.com

new alerts:

  • 2 for Multiplication result converted to larger type

fixed alerts:

  • 6 for Comparison of narrow type with wide type in loop condition

@pcarruscag
Copy link
Member Author

Why is it good C++ to use containers (instead of raw arrays):

  • Book keeping: No need for verbose allocations / de-allocations.
  • Information: The size is carried with the container, the type of data, the allocation strategy (e.g. alignment), etc.
  • Correctness: A const method can change the elements of a raw array, because in such cases const does not qualify the data, only the address (pointer) of that data. On the other hand, a const method will imply const containers, on which only const methods can be called, and so on.
  • Performance 1: A nested array (**), or nested container (vector) is NOT a matrix, it is a list of arrays, consequently there is more construction / destruction overhead (more allocations) uncertain performance at runtime, because inner arrays can end up arbitrarily far from each other, you end up needing copies to talk with e.g. blas or mpi...
  • Versatility: Above all a container encapsulates data structure details (e.g. if something is a vector or list or map or row-major matrix or column-major matrix or sparse matrix) behind an interface. Which then lets you change the details separately from the code that uses the data. Whenever you pass an array by pointer you are locking yourself to ONE very particular organization of data in memory.
  • Performance 2: This encapsulation is not insulation, it is completely transparent to the compiler because any half decent container type will be defined inline. Furthermore... If the compiler sees two pointers it has absolutely no idea if they are pointing to the same thing, or worse if they partially overlap. To legally optimize such code compilers need to introduce a ton of checks and fallback code, on the other hand, defining operations on containers which you know never to overlap provides a clean way to avoid compiler-paranoia.

@pcarruscag
Copy link
Member Author

"But wait there's more"

  • Debugging, profiling, logging: The access methods of containers can do bound-checking for debugging, they can be instrumented to measure how much data is used, or to log every imaginable type of information. Raw arrays will have you run to valgrind or address sanitizers and then good luck if the only example where you are able to reproduce a bug is too large and you need to wait one hour per test.

@lgtm-com
Copy link

lgtm-com bot commented Mar 9, 2021

This pull request fixes 4 alerts when merging 2d0b349 into 0a219b1 - view on LGTM.com

fixed alerts:

  • 4 for Comparison of narrow type with wide type in loop condition

@pcarruscag pcarruscag mentioned this pull request Mar 9, 2021
5 tasks
@lgtm-com
Copy link

lgtm-com bot commented Mar 9, 2021

This pull request fixes 4 alerts when merging d4653a1 into 0a219b1 - view on LGTM.com

fixed alerts:

  • 4 for Comparison of narrow type with wide type in loop condition

Copy link
Contributor

@TobiKattmann TobiKattmann left a comment

Choose a reason for hiding this comment

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

LGTM 💐 and thanks for the passionate plea for using containers instead of pointers. The jury withdraws for its final deliberations 🧑‍⚖️ . Jokes aside, it is really good to get some insights
In order to nag about sth: I would appreciate a few more words in the PR description beyond bullying language features (Not sure what the pointer union has to say about that). Like using config->GetSolid_Wall instead of individual listings in several spaces; RMS_RES & BGS_RES vars now in containers for all(?) solvers (and all necessary work around that). Just some (more) highlights
(And have fun furthermore with deleting 'em all :) )

if ((config->GetMarker_All_KindBC(iMarker) == HEAT_FLUX) ||
(config->GetMarker_All_KindBC(iMarker) == ISOTHERMAL) ||
(config->GetMarker_All_KindBC(iMarker) == EULER_WALL) )
if (config->GetSolid_Wall(iMarker))
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 good catch (here and elsewhere). I have to admit I don't really understand what this method is doing after briefly looking at it, and grepping the repo indicates that is is unused ...

SlidingStateNodes.resize(nMarker);

for (iMarker = 0; iMarker < nMarker; iMarker++) {
for (unsigned long iMarker = 0; iMarker < nMarker; iMarker++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

any particular reason not to use auto iMarker = 0ul. asking out of curiosity

Copy link
Member Author

Choose a reason for hiding this comment

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

hmmmm ah yes, I cut and pasted it from the declaration at the top of the function :)

su2activematrix Point_Max_Coord; /*!< \brief Vector with pointers to the coords of the maximal residual for each variable. */
su2activematrix Point_Max_Coord_BGS; /*!< \brief Vector with pointers to the coords of the maximal residual for each variable. */

/*--- Variables that need to go. ---*/
Copy link
Contributor

Choose a reason for hiding this comment

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

:) The pointer-killer already found his next victims. Only a matter of time until he strikes again ...

inline void SetResToZero() {
SU2_OMP_MASTER {
for (auto& r : Residual_RMS) r = 0;
for (auto& r : Residual_Max) r = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Range-based for loops are a thing in c++ since 11 but I rarely ever used them, whilst in python I use them all the time. I really need to pay more attention when I can use them

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a type in graph_toolbox.hpp called DummyGridColor with which one can write:

for (const auto iPoint : DummyGridColor<>(nPoint)) {
  // awesome code
}

Should have given it a better name like idk... Range 🤦
Anyway having that const auto iPoint is real useful when we go for them record breaking nested loops 👍

Comment on lines -90 to +92
su2double *Residual_RMS, /*!< \brief Vector with the mean residual for each variable. */
*Residual_Max, /*!< \brief Vector with the maximal residual for each variable. */
*Residual, /*!< \brief Auxiliary nVar vector. */
vector<su2double> Residual_RMS; /*!< \brief Vector with the mean residual for each variable. */
vector<su2double> Residual_Max; /*!< \brief Vector with the maximal residual for each variable. */
vector<su2double> Residual_BGS; /*!< \brief Vector with the mean residual for each variable for BGS subiterations. */
Copy link
Member Author

Choose a reason for hiding this comment

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

Summary of what this PR is about.

@pcarruscag pcarruscag merged commit b7f6d4f into develop Mar 9, 2021
@pcarruscag pcarruscag deleted the cleanup_more_pointers branch March 9, 2021 20:40
@maxaehle maxaehle mentioned this pull request Apr 7, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants