Skip to content

Conversation

@magnesj
Copy link
Owner

@magnesj magnesj commented Jul 29, 2024

This PR need more work when the other PRs are merged to dev.

PR Type

Enhancement, Tests


Description

  • Added RiaScopeTimer class for measuring time spent in a scope and logging the duration.
  • Introduced debugTimer method in RiaLogging to log the duration of a code block.
  • Integrated RiaScopeTimer into fault data computation in RimEclipseCase.
  • Optimized fault calculation in RigMainGrid by avoiding redundant calculations and using std::map.
  • Added OpenMP parallelization for fault face addition in RigMainGrid.
  • Replaced pointDistance with pointDistanceSquared for performance improvement in RigMainGrid.

Changes walkthrough 📝

Relevant files
Enhancement
RiaLogging.cpp
Add `RiaScopeTimer` for scope timing and logging                 

ApplicationLibCode/Application/Tools/RiaLogging.cpp

  • Added RiaScopeTimer class for measuring time spent in a scope.
  • Introduced debugTimer method to log the duration of a code block.
  • +34/-0   
    RimEclipseCase.cpp
    Integrate `RiaScopeTimer` into fault data computation       

    ApplicationLibCode/ProjectDataModel/RimEclipseCase.cpp

  • Integrated RiaScopeTimer to measure the duration of fault
    calculations.
  • Simplified conditional checks for fault data computation.
  • +7/-7     
    RigMainGrid.cpp
    Optimize fault calculation and add parallelization             

    ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp

  • Added StructGridDefines namespace with utility functions for face
    indices.
  • Optimized fault calculation by avoiding redundant calculations and
    using std::map.
  • Introduced OpenMP parallelization for fault face addition.
  • Replaced pointDistance with pointDistanceSquared for performance
    improvement.
  • +111/-36
    RiaLogging.h
    Declare `RiaScopeTimer` and `debugTimer` method                   

    ApplicationLibCode/Application/Tools/RiaLogging.h

  • Declared RiaScopeTimer class for timing code execution.
  • Added debugTimer method to RiaLogging class.
  • +28/-1   
    RigMainGrid.h
    Update `addUnNamedFaultFaces` method signature                     

    ApplicationLibCode/ReservoirDataModel/RigMainGrid.h

  • Updated addUnNamedFaultFaces method signature to include face map
    parameter.
  • +9/-8     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @qodo-code-review
    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Performance Concern
    The use of #pragma omp parallel for in the method calculateFaults at line 565 introduces parallel processing. However, the critical sections within this loop (lines 694-698) might negate the benefits of parallelization due to frequent locking and unlocking. Consider optimizing the management of shared resources or reducing the scope of critical sections.

    Code Complexity
    The method addUnNamedFaultFaces has significantly increased in complexity and length, making it harder to maintain and understand. Consider refactoring to improve modularity, such as breaking down the method into smaller, more focused sub-methods.

    @qodo-code-review
    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add error handling for map access to prevent exceptions

    Consider checking the validity of faceMap entries before accessing them with at(),
    to prevent potential exceptions if a face type is not found.

    ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp [658]

    -auto faceNodeIndices = faceMap.at(face);
    +auto it = faceMap.find(face);
    +if (it == faceMap.end()) return; // or handle error
    +auto faceNodeIndices = it->second;
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding error handling for map access is crucial to prevent potential exceptions, which could lead to crashes or undefined behavior. This is a significant improvement for robustness.

    9
    Thread-safety
    Ensure thread safety in the destructor during duration calculation and logging

    To ensure that the duration calculation in the destructor is thread-safe, consider
    protecting the calculation and logging with a mutex, especially since
    RiaLogging::debug might not be thread-safe.

    ApplicationLibCode/Application/Tools/RiaLogging.cpp [432-439]

    +std::mutex durationMutex;
    +std::lock_guard<std::mutex> lock(durationMutex);
     auto end = std::chrono::high_resolution_clock::now();
     std::chrono::duration<double> duration = end - m_startTime;
    -auto text = m_message + QString(" (duration : %1 seconds)").arg(duration.count());
    +QString text;
    +QTextStream(&text) << m_message << " (duration : " << duration.count() << " seconds)";
     RiaLogging::debug(text);
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Ensuring thread safety in the destructor is important, especially if RiaLogging::debug is not thread-safe. This suggestion addresses a potential concurrency issue effectively.

    8
    Performance
    Improve string formatting efficiency in the destructor

    Replace the direct use of QString::arg with a more efficient string formatting
    method such as QString::asprintf or using QTextStream for better performance,
    especially in a destructor where efficiency is critical.

    ApplicationLibCode/Application/Tools/RiaLogging.cpp [437]

    -auto text = m_message + QString(" (duration : %1 seconds)").arg(duration.count());
    +QString text;
    +QTextStream(&text) << m_message << " (duration : " << duration.count() << " seconds)";
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion to use QTextStream for string formatting is valid and can improve performance, especially in a destructor. However, the performance gain might be minimal in this context.

    7
    Best practice
    Use structured binding for map iteration to enhance code readability

    Use a structured binding for cleaner and more readable code when iterating over the
    cubeFaceIndices() map.

    ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp [515-517]

    -for (const auto& [key, value] : cubeFaceIndices())
    +for (auto& [key, value] : cubeFaceIndices())
     {
         faultFaceToFaceIdxs[key] = value;
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using structured binding for map iteration is a best practice that improves code readability. However, it is a minor improvement and does not affect functionality.

    5

    @magnesj magnesj marked this pull request as draft August 1, 2024 13:12
    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