Skip to content

Conversation

@Seco1024
Copy link
Collaborator

主要更動

  1. CUDA 實作
  • 拔掉 OpenMP + SIMD
  • 針對 batch_search() 實現 CUDA 搜尋版本,總共有四個 kernel function
    • Coarse Distance Computation
    • Top-nprobe Selection
    • Inverted List Scanning -> 最主要的優化在這邊
    • Final Merge & Reranking
  • GPU Memory Management
    • 實作 GpuCentroidsManager, GpuDataManager, GpuInvertedListsManager
    • 分別對照到 centroids, vectors, posting list 的記憶體管理
  • 整合到原本的 IVF Interface(src/IVFFlatIndex.cpp)
  • 詳細實作邏輯可以參考 /doc 下的 cuda.md
  1. Benchmark tools
  • 把 search() 改成 batch_search() 才能最大化 ANNS 的平行化效能
  • CPU & GPU 的 QPS measurement 要能夠 align
  1. Profiling tools
  • 測四大搜尋階段(可參考 discussion)的時間占比

編譯下 make cuda 就可以編譯出 GPU 版本的 IVF search

@Seco1024
Copy link
Collaborator Author

優化重點

  1. Memory Coalescing
    - Row-major layout 連續記憶體存取
    - Threads 以 stride 方式掃描資料
  2. Shared Memory 重用
    - Query vectors 載入 shared memory 避免重複讀取
    - Block-level merge 使用 shared memory 做候選收集
  3. Register-level Top-k
    - Thread-local top-k 完全在 registers 中運行(最多 128 個)
    - 使用 insertion sort(對小 k 很高效)
  4. Zero CPU-GPU Transfer Overhead
    - Index 資料常駐 GPU,只傳輸 queries 和 results
    - Batch processing 攤銷傳輸成本

效能問題
k=100 下 shared memory 不夠!!
要優化的話可以思考看看上面已經實作的優化重點是否有地方可以拔掉,和 shared memory 的實作 tradeoff 一下。

Copy link
Owner

@5000user5000 5000user5000 left a comment

Choose a reason for hiding this comment

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

LGTM,我會直接 merge 這個 PR

看起來很不錯,也有 cuda.md 能夠幫助理解實作內容,之後對於做報告應該會有幫助。
另外看起來 openMP+SIMD 應該還能運行,只是在 make cuda 版本就只有 cuda 避免干擾
數據上 cuda 也有比純 CPU 快,但 k=100 會爆 shared memory
這點我這禮拜會嘗試看看如何處理,並會開一個新的 PR 更新
如果沒辦法的話,由於時間問題,就直接開始做投影片準備報告
並且在報告可以說明此問題,以及之後可以用甚麼方式解決

@5000user5000 5000user5000 merged commit 8ca8e1c into 5000user5000:main Nov 25, 2025
1 check passed
@5000user5000 5000user5000 mentioned this pull request Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants