-
Notifications
You must be signed in to change notification settings - Fork 97
docs: Adding Code rules page #3915
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
base: develop
Are you sure you want to change the base?
Conversation
| // BAD - Do not use std::vector | ||
| std::vector<real64> values; | ||
|
|
||
| // GOOD - Use GEOS arrays | ||
| array1d<real64> values; | ||
| arrayView1d<real64> valuesView = values.toView(); | ||
| arrayView1d<real64 const> constView = values.toViewConst(); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand with this code the benefits of GEOS array over std array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added precisions to the comments
jafranc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great ! Maybe there is the work of 2 docs there (or more 😄 )
| Use GEOS tensor types for geometric and mechanical calculations: | ||
|
|
||
| .. list-table:: GEOS Tensor Types | ||
| :header-rows: 1 | ||
| :widths: 30 70 | ||
|
|
||
| * - Type | ||
| - Description | ||
| * - ``R1Tensor`` | ||
| - 3D vector (real64) | ||
| * - ``R1Tensor32`` | ||
| - 3D vector (real32) | ||
| * - ``R2SymTensor`` | ||
| - Symmetric 6-component tensor (Voigt notation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we can expand a bit on why tensor are in use and not their vector counter part
| map<string, real64> orderedMap; | ||
|
|
||
| The following standard library components are allowed: | ||
| - ``std::pair``, ``std::tuple`` for temporary structures, but beware of memory allocation, sometimes struct are to prefer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe an example on this specific point can help user
| setApplyDefaultValue( 1.0e-6 ). | ||
| setDescription( "Initial time step value. This parameter controls " | ||
| "the starting time increment for the simulation. " | ||
| "Valid range: (0, 1e-3]. See User Guide Chapter 5." ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For later, should we think of adding an optional setRange to Wrappers ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally agree, along with "unit" type. This is the direction I want to promote.
| - **Use GoogleTest framework**. | ||
| - In GEOS, the goal of unit test is never to take position on the validity of physical results (this is the point of integrated-tests). | ||
| - Place tests in ``src/coreComponents/<component>/tests/``. | ||
| - Test GPU code paths if applicable (use ``#ifdef GEOS_USE_DEVICE``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would look nice in the example too
| wrappedStats.stats().m_productionRate ); | ||
|
|
||
| // statistics CSV output (only if enabled and from rank 0) | ||
| if( m_writeCSV > 0 && MpiWrapper::commRank() == 0 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not wrap this in a MACRO and advise to use it ?
| Additional Validation Guidelines | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| - Use ``setInputFlag()`` to mark parameters as ``REQUIRED`` or ``OPTIONAL`` | ||
| - Use ``setApplyDefaultValue()`` to provide sensible defaults | ||
| - Use ``setRTTypeName()`` for runtime type validation (e.g., ``rtTypes::CustomTypes::positive``) | ||
| - Document valid ranges in ``setDescription()`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be in the Wrapper section ?
| Caliper Integration | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Use ``GEOS_MARK_FUNCTION`` and ``Timer`` for performance tracking for the main computation functions / scopes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and GEOS_CALIPER_MARK_SCOPE() then ?
| Memory Management Rules | ||
| --------------------------------- | ||
|
|
||
| Generalities | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| - **Minimize dynamic memory allocation** as much as possible, particularly in performance-critical code, | ||
| - For frequently allocated/deallocated objects, **consider memory pools**, | ||
| - For containers with known size, **reserve capacity upfront**. | ||
|
|
||
| Be Const / Constexpr | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| **Use ``const`` and ``constexpr`` when possible**, enabling: | ||
|
|
||
| - **Compiler optimization,** enables better code generation by giving constraints to the code, | ||
| - **Code safety,** prevents accidental modification for constant contexts, | ||
| - **Show code intention,** make code more readable. | ||
|
|
||
| Also, **Mark methods const if the method is not designed to modify** the object state. | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, it sounds like very high level advises, maybe better to relocate in another doc ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your feeling, and I think the same, strictly forbid useless mutability is a choice, thus a rule.
As it is a rule, it can give strict guidelines when reviewing code.
It is indeed a quite common one, but not enforced in all C++ applications.
I could put the reasons of rules in a foldable block to improve document readability.
I stay open to your feedback, do still you think that it should be moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the document could be two documents or main parts.
- One high level C++ do/don't we choose and want to enforce (not sure it is doable).
- One other that is more Domain Specific because GEOS's is using RAJA+CHAI and macros, and this is design/interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of restructuring as follow:
- Code Rules
- Coding Practices
- memory
- perf
- validation / precision
- architecture
- GEOS Features
- typing
- documentation
- testing
- logging
- error managment
- parallelism
- perf
- physics-specific
- Coding Practices
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this'd be a good splitting 👍
| Provide Views to Arrays | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| - When possible, prefer provide views to arrays (to const data if possible). | ||
| - This **must** be done especially for RAJA kernels, and views must be **captured by value**. | ||
|
|
||
| The rule is generalizable to ``string_view`` for strings, but not applicable in GPU context. | ||
|
|
||
| Why Use Views? | ||
|
|
||
| - **No memory allocation:** Views are lightweight references | ||
| - **Mutability correctness:** Can provide ``const`` read-only access to inner data | ||
| - **GPU compatibility:** Views work seamlessly on device | ||
|
|
||
| .. dropdown:: Example: Views for arrays | ||
| :icon: code | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| // BAD - Creates a copy | ||
| array1d< real64 > copy = originalArray; | ||
|
|
||
| // GOOD - Creates a view (lightweight) | ||
| arrayView1d< real64 > view = originalArray.toView(); | ||
|
|
||
| // GOOD - Const view for read-only access | ||
| arrayView1d< real64 const > constView = originalArray.toViewConst(); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be moved above for more visibility ?
| arrayView1d< real64 > getView() { return m_data.toView(); } | ||
| arrayView1d< real64 const > getViewConst() const { return m_data.toViewConst(); } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
detail but with class all will be private then ... struct ?
|
Thanks @jafranc for your feedback, I'll take them into account. |
GEOS Code Rules
Aims at writing down the coding standards for GEOS, focusing on type safety, error handling, parallelism, and performance. Key principles include:
These rules ensure code quality, consistency, and maintainability across the GEOS codebase.