diff --git a/cpp/include/raft/neighbors/detail/cagra/graph_core.cuh b/cpp/include/raft/neighbors/detail/cagra/graph_core.cuh index 8fcba38883..192850cf2d 100644 --- a/cpp/include/raft/neighbors/detail/cagra/graph_core.cuh +++ b/cpp/include/raft/neighbors/detail/cagra/graph_core.cuh @@ -528,8 +528,9 @@ void optimize(raft::resources const& res, constexpr int _omp_chunk = 1024; #pragma omp parallel for schedule(dynamic, _omp_chunk) for (uint64_t j = 0; j < graph_size; j++) { - for (uint64_t _k = 0; _k < rev_graph_count.data_handle()[j]; _k++) { - uint64_t k = rev_graph_count.data_handle()[j] - 1 - _k; + uint64_t k = std::min(rev_graph_count.data_handle()[j], output_graph_degree); + while (k) { + k--; uint64_t i = rev_graph.data_handle()[k + (output_graph_degree * j)]; uint64_t pos =