Skip to content

Commit

Permalink
Reducing extra data copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan Chattopadhyaya committed Apr 18, 2024
1 parent 5c06d04 commit e8ffc55
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions cuda/src/CudaIndexJni.cu
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,21 @@ JNIEXPORT jint JNICALL Java_com_searchscale_lucene_vectorsearch_jni_CuVSIndexJni
std::cout<<"CUDA devices: "<<rmm::get_num_cuda_devices()<<std::endl;
rmm::mr::set_current_device_resource(&pool_mr);

// Copy the arrays from JNI to local variables.
// TODO: Instead of copying three times (JNI->array->hostmatrix->devicematrix),
// TODO: it might possible to do it once (JNI -> Device) for better efficiency.
// Copy the arrays from JNI to local variables.
long startTime = ms();
jsize numDocs = env->GetArrayLength(docIds);
std::vector<int> docs (numDocs);
env->GetIntArrayRegion( docIds, 0, numDocs, &docs[0] ); // TODO: This docid to index mapping should be persisted and used during search
std::vector<float> data(numVectors * dimension);
env->GetFloatArrayRegion( dataVectors, 0, numVectors * dimension, &data[0] );
auto datasetHost = raft::make_host_matrix<float, int64_t>(dev_resources, numVectors, dimension);
auto dataset = raft::make_device_matrix<float, int64_t>(dev_resources, numVectors, dimension);
int p = 0;
for(size_t i = 0; i < numDocs ; i ++) {
for(size_t j = 0; j < dimension; ++j) {
datasetHost(i, j) = data[p++]; // TODO: Is there a better SIMD friendly way to copy?
}
}
cudaStream_t stream = raft::resource::get_cuda_stream(dev_resources);
raft::copy(dataset.data_handle(), datasetHost.data_handle(), datasetHost.size(), stream);
raft::resource::sync_stream(dev_resources, stream);
auto extents = raft::make_extents<int64_t>(numVectors, dimension);
auto dataset = raft::make_mdspan<float, int64_t>(&data[0], extents);
std::cout<<"Data copying time (CPU to GPU): "<<(ms()-startTime)<<std::endl;


// Build the index
startTime = ms();
index_params.build_algo = raft::neighbors::cagra::graph_build_algo::NN_DESCENT;
auto ind = raft::neighbors::cagra::build<float, uint32_t>(dev_resources, index_params, raft::make_const_mdspan(dataset.view()));
auto ind = raft::neighbors::cagra::build<float, uint32_t>(dev_resources, index_params, raft::make_const_mdspan(dataset));
std::cout << "Cagra Index building time: " << (ms()-startTime) << std::endl;

// Serialize the index into a file
Expand Down

0 comments on commit e8ffc55

Please sign in to comment.