Skip to content

Conversation

@MelReyCG
Copy link
Contributor

@MelReyCG MelReyCG commented Nov 26, 2025

GEOS Code Rules

Aims at writing down the coding standards for GEOS, focusing on type safety, error handling, parallelism, and performance. Key principles include:

  • Type System
  • Memory Management
  • Error Handling
  • Parallelism
  • Performance
  • Memory / Safety
  • Architecture
  • Testing

These rules ensure code quality, consistency, and maintainability across the GEOS codebase.

Comment on lines 113 to 120
// 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();

Copy link
Contributor

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

Copy link
Contributor Author

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

@MelReyCG MelReyCG self-assigned this Nov 28, 2025
@MelReyCG MelReyCG changed the title Docs/rey/code rules docs: Code rules Nov 28, 2025
@MelReyCG MelReyCG changed the title docs: Code rules docs: Adding Code rules page Nov 28, 2025
@MelReyCG MelReyCG marked this pull request as ready for review November 28, 2025 09:12
Copy link
Contributor

@jafranc jafranc left a 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 😄 )

Comment on lines +126 to +139
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)
Copy link
Contributor

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.
Copy link
Contributor

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." );
Copy link
Contributor

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 ?

Copy link
Contributor Author

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``).
Copy link
Contributor

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 )
Copy link
Contributor

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 ?

Comment on lines +1013 to +1019
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()``
Copy link
Contributor

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.
Copy link
Contributor

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 ?

Comment on lines +1088 to +1108
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.

Copy link
Contributor

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 ?

Copy link
Contributor Author

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?

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 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

Copy link
Contributor Author

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

Copy link
Contributor

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 👍

Comment on lines +1212 to +1239
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();

Copy link
Contributor

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 ?

Comment on lines +1284 to +1286
arrayView1d< real64 > getView() { return m_data.toView(); }
arrayView1d< real64 const > getViewConst() const { return m_data.toViewConst(); }
};
Copy link
Contributor

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 ?

@MelReyCG
Copy link
Contributor Author

MelReyCG commented Dec 3, 2025

Thanks @jafranc for your feedback, I'll take them into account.

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.

4 participants