Skip to content

Conversation

IvanPleshkov
Copy link
Contributor

This PR adds a vulkan memory barrier. Memory barrier is a sync primitive that adds guarantees about execution order. After this barrier, all changes must be available for downloading without race conditions or unflushed caches.

Copy link
Contributor

coderabbitai bot commented Feb 25, 2025

📝 Walkthrough

Walkthrough

The changes introduce synchronization barriers into GPU command flows. A new method, barrier_buffers, is added to the GPU context to enforce buffer visibility after GPU dispatch commands. This new method is integrated into both basic tests and GPU insertion operations to ensure that buffer writes are properly synchronized before subsequent operations. Additionally, new accessor methods are added to GPU-related structures to safely expose buffer references.

Changes

File(s) Change Summary
lib/gpu/.../basic_test.rs, lib/gpu/.../context.rs, lib/segment/.../gpu_insert_context.rs Added GPU buffer synchronization: introduced new barrier call (barrier_buffers) after dispatch commands in tests and insert operations; updated control flow to ensure GPU buffer writes are visible before subsequent commands.
lib/segment/.../gpu_links.rs, lib/segment/.../gpu_visited_flags.rs Added new public accessor methods (links_buffer and visited_flags_buffer) to expose internal GPU buffers for external usage while preserving ownership semantics using Arc.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Basic GPU Test
    participant Context as GPU Context
    participant Vulkan as Vulkan Device
    participant Buffer as Storage Buffer

    Test->>Context: Dispatch GPU computation
    Test->>Context: Invoke barrier_buffers([storage_buffer])
    Context->>Vulkan: Execute cmd_pipeline_barrier for storage_buffer
    Test->>Context: Continue with context.run()
Loading
sequenceDiagram
    participant Insert as GPU Insert Context
    participant Context as GPU Context
    participant Vulkan as Vulkan Device
    participant Buffers as GPU Buffers

    Insert->>Context: Dispatch GPU computation (greedy_search/run_insert_vector)
    Insert->>Context: Invoke barrier_buffers([...buffers])
    Context->>Vulkan: Execute cmd_pipeline_barrier for provided buffers
    Insert->>Context: Continue with GPU operations
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

🧹 Nitpick comments (3)
lib/gpu/src/context.rs (3)

127-131: Avoid potential duplication in self.resources.
Pushing each buffer into self.resources might re-insert the same buffer multiple times if barrier_buffers is called repeatedly with the same resource. While not incorrect, consider checking whether the buffer is already tracked to avoid duplicates.


150-160: Consider narrowing the pipeline stage flags for performance.
Using ALL_COMMANDS as the destination stage is safe but can be unnecessarily broad. If you know the next pipeline stage specifically (e.g., only transfer or compute stages), refining dst_stage_mask can improve performance.

- vk::PipelineStageFlags::ALL_COMMANDS,
+ vk::PipelineStageFlags::TRANSFER | vk::PipelineStageFlags::COMPUTE_SHADER,

115-163: Add dedicated tests for barrier_buffers.
While this method can rely on downstream wait_finish calls, explicit tests verifying correct buffer synchronization across multiple commands or queue operations would help ensure correctness and detect regressions.

Do you want me to open a follow-up PR or generate a test snippet for buffer synchronization scenarios?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e76cd28 and dbc66b9.

📒 Files selected for processing (5)
  • lib/gpu/src/basic_test.rs (1 hunks)
  • lib/gpu/src/context.rs (1 hunks)
  • lib/segment/src/index/hnsw_index/gpu/gpu_insert_context.rs (2 hunks)
  • lib/segment/src/index/hnsw_index/gpu/gpu_links.rs (1 hunks)
  • lib/segment/src/index/hnsw_index/gpu/gpu_visited_flags.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Basic TLS/HTTPS tests
  • GitHub Check: test-snapshot-operations-s3-minio
  • GitHub Check: test-shard-snapshot-api-s3-minio
  • GitHub Check: test-low-resources
  • GitHub Check: test-consistency
  • GitHub Check: test-consensus-compose
  • GitHub Check: test (macos-latest)
  • GitHub Check: test
  • GitHub Check: test (windows-latest)
  • GitHub Check: test
  • GitHub Check: test-consensus
  • GitHub Check: test
  • GitHub Check: test (ubuntu-latest)
🔇 Additional comments (7)
lib/segment/src/index/hnsw_index/gpu/gpu_links.rs (1)

166-168: New public method to expose the GPU links buffer
This getter correctly returns a clone of the Arc<gpu::Buffer>, allowing safe external access with no apparent pitfalls.

lib/segment/src/index/hnsw_index/gpu/gpu_visited_flags.rs (1)

141-143: New public method to expose the visited flags buffer
This addition mirrors the approach used in GpuLinks, returning an Arc<gpu::Buffer> for consistent external access.

lib/gpu/src/basic_test.rs (1)

140-141: Barrier ensures buffer synchronization
The barrier step before proceeding to the next command provides the requisite memory ordering guarantees for subsequent GPU operations.

lib/segment/src/index/hnsw_index/gpu/gpu_insert_context.rs (2)

326-328: Added barrier for responses buffer in greedy search
Placing a barrier after dispatch ensures completed writes to responses_buffer before any subsequent operations.


394-401: Barrier for multiple buffers in insert vector flow
Synchronizing responses_buffer, insert_atomics_buffer, links_buffer, and visited_flags_buffer comprehensively ensures data consistency across GPU operations.

lib/gpu/src/context.rs (2)

115-122: Good addition of buffer synchronization and documentation.
The doc comments clearly explain the reasoning behind adding barrier_buffers. This method both complements and provides faster in-graph synchronization than a full wait_finish().


141-146: Verify correctness of source/destination access masks.
At present, src_access_mask is set to SHADER_WRITE, and dst_access_mask includes both transfer and shader flags. This covers a wide range of usage scenarios but may be too permissive if the buffers are only read afterwards. Consider refining them or confirming all potential subsequent usage paths.

Would you like to cross-check usage in other modules to confirm correctness?

@IvanPleshkov IvanPleshkov merged commit c2acb3b into dev Feb 25, 2025
19 of 20 checks passed
@IvanPleshkov IvanPleshkov deleted the gpu-add-memory-barriers branch February 25, 2025 13:59
timvisee pushed a commit that referenced this pull request Mar 21, 2025
* gpu add memory barriers

* add comments
@timvisee timvisee mentioned this pull request Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants