Skip to content
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

fix(lmp): apply NEIGHMASK to neighbor list #4269

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Oct 28, 2024

Fix #4250.

See lammps/lammps#581 (comment) for an explanation of NEIGHMASK.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new function to set a mask for neighbor lists, enhancing configurability.
    • Added a method to the InputNlist structure for setting the mask.
    • Enhanced ComputeDeeptensorAtom and FixDPLR classes to utilize neighbor list masks in computations.
  • Bug Fixes

    • Improved validation of bonded pairs in the FixDPLR class with enhanced error handling.
  • Documentation

    • Updated documentation for new methods and functionalities related to neighbor list management.

Fix deepmodeling#4250.

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request introduce a new function DP_NlistSetMask to manage neighbor list masks, increment the API version from 22 to 23, and add a corresponding method set_mask(int mask) in the InputNlist structure. The modifications enhance neighbor list management without altering existing functionalities. Additionally, updates are made to various classes to integrate the new mask-setting functionality, ensuring that neighbor lists can be filtered based on specified mask values.

Changes

File Path Change Summary
source/api_c/include/c_api.h - API version updated from 22 to 23.
- Added extern void DP_NlistSetMask(DP_Nlist* nl, int mask);
source/api_c/include/deepmd.hpp - Added void set_mask(int mask) to InputNlist structure.
source/api_c/src/c_api.cc - Added void DP_NlistSetMask(DP_Nlist* nl, int mask) implementation.
source/common/include/neighbor_list.h - Added int mask = 0xFFFFFFFF; to InputNlist.
- Added void set_mask(int mask_) to InputNlist.
source/lmp/compute_deeptensor_atom.cpp - Added lmp_list.set_mask(NEIGHMASK); in compute_peratom method.
source/lmp/fix_dplr.cpp - Updated pre_force, post_force, and get_valid_pairs methods with new logic.
source/lmp/pair_deepmd.cpp - Updated compute method and added void deepmd_compat::InputNlist::set_mask(int mask).
source/api_cc/src/common.cc - Updated deepmd::NeighborListData::copy_from_nlist method to include bitwise AND operation.

Assessment against linked issues

Objective Addressed Explanation
Ensure stability when using deepmd with kspace and bond/angle information (#4250) The changes enhance neighbor list handling but do not directly address the segmentation fault issue.

Possibly related PRs

Suggested labels

OP, C++, LAMMPS

Suggested reviewers

  • iProzd
  • wanghan-iapcm

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
source/lib/include/neighbor_list.h (1)

98-101: Consider adding input validation and documentation.

While the setter implementation is correct, consider these improvements:

  1. Add parameter validation to prevent invalid mask values
  2. Enhance the documentation to explain:
    • The purpose and effect of the mask
    • The meaning of different bits
    • Common mask values and their use cases
   /**
    * @brief Set mask for this neighbor list.
+   * @param mask_ Bit mask to filter neighbor list entries
+   * @details The mask is applied using bitwise AND operation during neighbor list processing.
+   *          Default value 0xFFFFFFFF means no filtering.
    */
   void set_mask(int mask_) { mask = mask_; };
source/api_c/include/c_api.h (1)

71-79: Documentation could be more detailed for the mask parameter.

The function declaration and documentation follow the codebase standards. However, the documentation could be enhanced by describing the purpose and expected values of the mask parameter.

Consider expanding the documentation:

 /*
  * @brief Set mask for a neighbor list.
  *
- * @param nl Neighbor list.
- * @param mask mask.
+ * @param[in] nl Neighbor list.
+ * @param[in] mask Bit mask to filter neighbor list entries (e.g., NEIGHMASK in LAMMPS).
  * @since API version 23
  *
  **/
source/api_c/src/c_api.cc (1)

45-45: Add documentation for the new function.

The implementation looks good and follows the existing patterns. However, consider adding documentation to explain:

  • The purpose of the mask parameter
  • Expected values/range for the mask
  • Any side effects of setting the mask

Add documentation above the function:

+/**
+ * @brief Set a mask for the neighbor list
+ * @param[in] nl Pointer to the neighbor list
+ * @param[in] mask Bit mask to filter the neighbor list (e.g., NEIGHMASK)
+ */
void DP_NlistSetMask(DP_Nlist* nl, int mask) { nl->nl.set_mask(mask); }
source/api_c/include/deepmd.hpp (1)

614-617: LGTM! Consider enhancing the documentation.

The addition of the set_mask method is well-integrated and follows the codebase patterns. The implementation correctly wraps the C API call.

Consider enhancing the documentation to explain:

  • The purpose and effect of the mask
  • The expected mask values or format
  • Any relationship to LAMMPS' NEIGHMASK
/**
 * @brief Set mask for this neighbor list.
 * @param[in] mask The mask value to filter neighbor list entries.
 *            This is typically used with LAMMPS' NEIGHMASK to properly
 *            handle neighbor lists when bonds/angles are present.
 */
void set_mask(int mask) { DP_NlistSetMask(nl, mask); };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 04e1159 and 1542e57.

📒 Files selected for processing (8)
  • source/api_c/include/c_api.h (2 hunks)
  • source/api_c/include/deepmd.hpp (1 hunks)
  • source/api_c/src/c_api.cc (1 hunks)
  • source/api_cc/src/common.cc (1 hunks)
  • source/lib/include/neighbor_list.h (2 hunks)
  • source/lmp/compute_deeptensor_atom.cpp (1 hunks)
  • source/lmp/fix_dplr.cpp (1 hunks)
  • source/lmp/pair_deepmd.cpp (2 hunks)
🔇 Additional comments (10)
source/lmp/compute_deeptensor_atom.cpp (2)

139-139: LGTM! Strategic addition to prevent segmentation faults.

This change applies the NEIGHMASK to properly filter the neighbor list, which is crucial for preventing segmentation faults when using hybrid systems with bonds/angles and k-space calculations. The placement right before tensor computation ensures proper neighbor handling.


139-139: Verify consistent NEIGHMASK usage across the codebase.

Let's ensure this mask is consistently applied across all neighbor list implementations.

✅ Verification successful

Based on the search results, I can see that NEIGHMASK is consistently used with set_mask() across the codebase in the following files:

  1. source/lmp/fix_dplr.cpp
  2. source/lmp/compute_deeptensor_atom.cpp
  3. source/lmp/pair_deepmd.cpp

The usage pattern is consistent - NEIGHMASK is set on InputNlist objects before they are used for neighbor list operations. This is true for both regular and extended neighbor lists.

NEIGHMASK is consistently applied across all neighbor list implementations in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent NEIGHMASK usage in neighbor list handling

# Search for neighbor list mask usage
echo "=== Checking NEIGHMASK usage ==="
rg -A 2 "set_mask.*NEIGHMASK" 

# Search for InputNlist instantiations to verify mask setting
echo "=== Checking InputNlist usage ==="
rg -A 5 "InputNlist.*\(" 

Length of output: 73575

source/lib/include/neighbor_list.h (2)

45-46: LGTM: Mask initialization maintains backward compatibility.

The default value of 0xFFFFFFFF ensures all bits are set, meaning no filtering by default. This maintains backward compatibility with existing code while allowing for selective filtering when needed.


Line range hint 45-101: Verify the impact on memory layout and ABI compatibility.

The addition of a new member variable could affect the memory layout of the InputNlist struct. Since this struct is part of the public API and used in LAMMPS integration, we should verify:

  1. The impact on existing code that might depend on the struct's memory layout
  2. Whether this change requires coordination with LAMMPS maintainers
✅ Verification successful

The addition of the mask member variable is safe to proceed

Based on the analysis of the codebase:

  1. The InputNlist struct is used primarily for passing neighbor list information and doesn't involve any binary serialization, packing, or strict memory layout dependencies.
  2. In LAMMPS integration (pair_deepmd.cpp), the struct is used through its constructors and member functions, not through direct memory operations.
  3. The test files show the struct is used for its intended purpose of managing neighbor lists, with no reliance on its memory layout.
  4. The mask member is properly initialized in all constructors and accessed only through the set_mask method, maintaining encapsulation.

The change is safe as it:

  • Doesn't break the ABI as there's no evidence of direct memory operations or binary serialization
  • Maintains backward compatibility through proper initialization in all constructors
  • Is used consistently through member functions rather than direct memory access
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for direct struct memory operations that might be affected

# Look for potential memory operations on InputNlist
rg -l "memcpy.*InputNlist|malloc.*InputNlist|realloc.*InputNlist" 

# Look for size-dependent operations
rg -l "sizeof.*InputNlist"

# Look for serialization/deserialization code
rg -l "serialize.*InputNlist|deserialize.*InputNlist"

Length of output: 152


Script:

#!/bin/bash
# Let's try a broader search to understand the usage of InputNlist

# Look for files that use InputNlist
rg -l "InputNlist"

# Look for potential inheritance or composition
ast-grep --pattern 'class $_ : $$$public InputNlist$$$'

# Check for any pointer or reference usage of InputNlist
rg "InputNlist\s*[*&]" -l

# Look for any test files that might reveal usage patterns
fd "test.*\.(cpp|h|cc)" --exec rg -l "InputNlist" {}

Length of output: 5430


Script:

#!/bin/bash
# Let's examine how InputNlist is used in LAMMPS integration and key files

# Check LAMMPS integration files for struct usage patterns
rg -A 5 -B 5 "InputNlist" source/lmp/pair_deepmd.cpp source/lmp/pair_deepmd.h

# Look at the test files to understand memory handling
rg -A 5 -B 5 "InputNlist" source/lib/tests/test_neighbor_list.cc

# Check if there are any binary data handling or memory layout dependencies
rg "pack|unpack|binary|struct.*layout" source/lib/include/neighbor_list.h

Length of output: 5914

source/api_cc/src/common.cc (1)

244-246: LGTM! The neighbor list masking implementation looks correct.

The addition of the bitwise AND operation with inlist.mask properly filters the neighbor list entries, which is crucial for:

  1. Preventing segmentation faults by masking out invalid neighbor indices
  2. Supporting LAMMPS' NEIGHMASK functionality for proper handling of bond/angle information

This change effectively addresses the segmentation fault issue mentioned in PR #4250 that occurs when running LAMMPS with a combination of the DeepMD model, k-space, and bond/angle information.

source/lmp/pair_deepmd.cpp (2)

568-568: LGTM! Important fix for neighbor list filtering.

The addition of set_mask(NEIGHMASK) ensures proper filtering of the neighbor list to handle non-bonded interactions correctly. This is crucial for fixing the segmentation fault when using k-space with bonds/angles.


578-578: LGTM! Consistent neighbor list filtering for spin systems.

The addition of set_mask(NEIGHMASK) to the extended neighbor list ensures consistent filtering behavior for systems with spin interactions. This maintains proper handling of non-bonded interactions across all system types.

source/api_c/include/c_api.h (1)

15-15: LGTM: API version increment is appropriate.

The version bump from 22 to 23 correctly follows API versioning practices as we're adding a new function DP_NlistSetMask.

source/api_c/include/deepmd.hpp (1)

614-617: Verify the integration of the new mask functionality.

Let's verify the complete integration of this change:

✅ Verification successful

The set_mask functionality is properly integrated and used consistently

The verification shows:

  • The set_mask method is consistently used in LAMMPS integration files (fix_dplr.cpp, pair_deepmd.cpp, compute_deeptensor_atom.cpp)
  • All usages correctly pass the NEIGHMASK parameter
  • The implementation chain is complete:
    • C++ API in neighbor_list.h
    • C API wrapper in c_api.cc
    • High-level wrapper in deepmd.hpp
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage of the new set_mask method and API version changes

# Test 1: Find all potential callers of set_mask
echo "=== Potential callers of set_mask ==="
rg "set_mask" --type cpp

# Test 2: Verify API version increment
echo -e "\n=== API version changes ==="
rg "API_VERSION|VERSION_NUMBER" --type cpp

# Test 3: Check for any NEIGHMASK usage
echo -e "\n=== NEIGHMASK usage ==="
rg "NEIGHMASK" --type cpp

Length of output: 3089

source/lmp/fix_dplr.cpp (1)

466-466: Addition of neighbor list mask setting is appropriate

The line lmp_list.set_mask(NEIGHMASK); correctly sets the neighbor list mask, ensuring proper neighbor list configuration and resolving the segmentation fault issue described in issue #4250.

Copy link

codecov bot commented Oct 29, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.34%. Comparing base (04e1159) to head (1542e57).
Report is 3 commits behind head on devel.

Files with missing lines Patch % Lines
source/lmp/pair_deepmd.cpp 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4269   +/-   ##
=======================================
  Coverage   84.34%   84.34%           
=======================================
  Files         549      549           
  Lines       51464    51472    +8     
  Branches     3047     3055    +8     
=======================================
+ Hits        43406    43414    +8     
- Misses       7096     7098    +2     
+ Partials      962      960    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wanghan-iapcm
Copy link
Collaborator

@njzjz please cite the source of the solution in the issue description . thanks!

@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Oct 29, 2024
Merged via the queue into deepmodeling:devel with commit 95f0ed5 Oct 29, 2024
60 checks passed
iProzd added a commit to iProzd/deepmd-kit that referenced this pull request Nov 7, 2024
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.

Segmentation fault when running lammps with deepmd + kspace + bond/angle information
2 participants