Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ravv usage thread-safe #381

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ public CompressedVectors createCompressedVectors(Object[] compressedVectors) {

@Override
public CompressedVectors encodeAll(RandomAccessVectorValues ravv, ForkJoinPool simdExecutor) {
var ravvCopy = ravv.threadLocalSupplier();
var cv = simdExecutor.submit(() -> IntStream.range(0, ravv.size())
.parallel()
.mapToObj(i -> {
var vector = ravv.getVector(i);
return vector == null
var localRavv = ravvCopy.get();
VectorFloat<?> v = localRavv.getVector(i);
return v == null
? new long[compressedVectorSize() / Long.BYTES]
: encode(vector);
})
: encode(v);
})
.toArray(long[][]::new))
.join();
return new ImmutableBQVectors(this, cv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ public static ImmutablePQVectors encodeAndBuild(ProductQuantization pq, int vect
// Encode the vectors in parallel into the compressed data chunks
// The changes are concurrent, but because they are coordinated and do not overlap, we can use parallel streams
// and then we are guaranteed safe publication because we join the thread after completion.
var ravvCopy = ravv.threadLocalSupplier();
simdExecutor.submit(() -> IntStream.range(0, ravv.size())
.parallel()
.forEach(ordinal -> {
// Retrieve the slice and mutate it.
var localRavv = ravvCopy.get();
var slice = PQVectors.get(chunks, ordinal, vectorsPerChunk, pq.getSubspaceCount());
var vector = ravv.getVector(ordinal);
var vector = localRavv.getVector(ordinal);
if (vector != null)
pq.encodeTo(vector, slice);
else
Expand Down
Loading