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

Feature: Better Rust Concurrency support #482

Open
3 tasks done
jbrummack opened this issue Sep 3, 2024 · 1 comment
Open
3 tasks done

Feature: Better Rust Concurrency support #482

jbrummack opened this issue Sep 3, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@jbrummack
Copy link
Contributor

Describe what you are looking for

The documentation says somewhere that the Usearch-Index is threadsafe, would it be possible to make it Send+Sync?
For now I have to do this workaround:

struct SyncIndex {
    index: Index,
}
unsafe impl Send for SyncIndex {}
unsafe impl Sync for SyncIndex {}

I tried this with Rayon (index creation took 9 minutes instead of 96) and with Actix and it worked quite well with f32 in both.
I tested making Index conform to Send + Sync in a stashed fork and it also worked.
The workaround starts to not work as soon as more internal stuff comes to the surface like this:

let len = tensor.shape()[0];
(0..len).into_par_iter().for_each(|current_offset| {
        println!("{current_offset}");
        let calc_offset = current_offset * 128;
        let keypoint = &view[calc_offset..calc_offset + 128];
        let keypoint_b1 = b1x8::from_u8s(keypoint);
        index.add(current_offset as u64, keypoint_b1);
        bar.inc(1);
});

Which results in this:

error[E0277]: `*const cxx::void` cannot be shared between threads safely
   --> src/main.rs:73:43
    |
73  |           (0..len).into_par_iter().for_each(|current_offset| {
    |  __________________________________--------_^
    | |                                  |
    | |                                  required by a bound introduced by this call
74  | |             println!("{current_offset}");
75  | |             let calc_offset = current_offset * 128;
76  | |             let keypoint = &view[calc_offset..calc_offset + 128];
...   |
81  | |             bar.inc(1);
82  | |         });
    | |_________^ `*const cxx::void` cannot be shared between threads safely

PS: I tried to run OpenMP, however it looks like rust doesnt support OpenMP if im not mistaken

Can you contribute to the implementation?

  • I can contribute

Is your feature request specific to a certain interface?

Other bindings

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jbrummack jbrummack added the enhancement New feature or request label Sep 3, 2024
@ashvardanian
Copy link
Contributor

Sure! If you know a better way to expose concurrency to Rust - feel free to open a PR 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants