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

Why &mut self for fn search in trait Index #11

Closed
ava57r opened this issue Oct 7, 2020 · 4 comments
Closed

Why &mut self for fn search in trait Index #11

ava57r opened this issue Oct 7, 2020 · 4 comments
Labels
question Further information is requested

Comments

@ava57r
Copy link
Collaborator

ava57r commented Oct 7, 2020

Hello.

Faiss binding required only *const FaissIndex
https://github.com/Enet4/faiss-rs/blob/master/faiss-sys/src/bindings.rs#L108

but here &mut self
https://github.com/Enet4/faiss-rs/blob/master/src/macros.rs#L71
https://github.com/Enet4/faiss-rs/blob/master/src/macros.rs#L76

@Enet4 Enet4 added the question Further information is requested label Oct 7, 2020
@Enet4
Copy link
Owner

Enet4 commented Oct 7, 2020

A mutable reference to the index is required to comply with the thread safety guarantees of index implementations. In particular, an index of an unknown implementation is not guaranteed to be thread safe, even for searching. While the native Faiss API (and by consequence the C API) are more lenient on this matter by letting this operation be performed over a const reference, Rust has stricter aliasing requirements which are leveraged here to prevent data races at compile-time.

In general, you are advised to follow the guidelines for concurrent index searching in the wiki page above. Since they are already backed by multiple threads internally, calling search from multiple threads can cripple performance. As a workaround, in the case of CPU-backed indexes, if you downcast the index to its implementation, then the interface on the ConcurrentIndex trait will become available, which lets you search through an immutable reference.

@ava57r
Copy link
Collaborator Author

ava57r commented Oct 7, 2020

Thanks.
I see.

https://docs.rs/faiss/0.9.0/faiss/index/trait.ConcurrentIndex.html
"... batched querying"

How can I split results for 2 different queries from SearchResult struct?

@Enet4
Copy link
Owner

Enet4 commented Oct 7, 2020

The API is currently a bit thin here, but the logic is the same as in the native API: in search and assign, each query maps to k search results. So to get to the ith result of the jth query, you can index the distance or index property by j * k + i.

@ava57r
Copy link
Collaborator Author

ava57r commented Oct 7, 2020

Thanks!
I am trying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants