Skip to content

Commit

Permalink
ANN_BENCH: Fix segfault in CAGRA wrapper when moving its graph
Browse files Browse the repository at this point in the history
When the graph in CAGRA wrapper needs to be relocated to a different memory resource, the old one is destructed before the data is copied. This leads to a segfault during copy.
The fix is to retain the old graph pointer until the data is copied.
  • Loading branch information
achirkin authored Feb 27, 2025
1 parent 28a15e6 commit 84475d0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cpp/bench/ann/src/cuvs/cuvs_cagra_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,16 @@ void cuvs_cagra<T, IdxT>::set_search_param(const search_param_base& param,
RAFT_LOG_DEBUG("moving graph to new memory space: %s", allocator_to_string(graph_mem_).c_str());
// We create a new graph and copy to it from existing graph
auto mr = get_mr(graph_mem_);
*graph_ = raft::make_device_mdarray<IdxT, int64_t>(
handle_, mr, raft::make_extents<int64_t>(index_->graph().extent(0), index_->graph_degree()));

raft::copy(graph_->data_handle(),
index_->graph().data_handle(),
index_->graph().size(),
// Create a new graph, then copy, and __only then__ replace the shared pointer.
// We shouldn't use index_->graph() here, because it may be just a view of graph_.
auto new_graph = raft::make_device_mdarray<IdxT, int64_t>(handle_, mr, graph_->extents());
raft::copy(new_graph.data_handle(),
graph_->data_handle(),
graph_->size(),
raft::resource::get_cuda_stream(handle_));
raft::resource::sync_stream(handle_);
*graph_ = std::move(new_graph);

// NB: update_graph() only stores a view in the index. We need to keep the graph object alive.
index_->update_graph(handle_, make_const_mdspan(graph_->view()));
Expand Down

0 comments on commit 84475d0

Please sign in to comment.