-
Notifications
You must be signed in to change notification settings - Fork 58
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
Fix for concurrency bug #70
Conversation
C++ linting failing due to this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a question about the usage of the atomic. I'm not sure this will result in behavioral parity with the existing implementation
@@ -360,7 +360,7 @@ class TypedIndex : public Index { | |||
|
|||
int start = 0; | |||
if (!ep_added) { | |||
size_t id = ids.size() ? ids.at(0) : (currentLabel); | |||
size_t id = ids.size() ? ids.at(0) : (currentLabel.fetch_add(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is .fetch_add(1)
actually what we want throughout this change? Wouldn't that result in us incrementing currentLabel
multiple times where we actually only wanted to increment it once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, I'm reverting this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually not, my original implementation is correct. Basically here we assign an id, then here:
Line 384 in 33d68cd
start = 1; |
we set start = 1
so that when we loop via ParallelFor
we start from 1:
Line 392 in 33d68cd
ParallelFor(start, rows, numThreads, [&](size_t row, size_t threadId) { |
therefore we need currentLabel
already incremented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks for addressing the comment! This looks good to me, albeit race conditions are always a little tricky to verify fixes for
Description
When adding a point to an index without providing an id, we rely on
TypedIndex::currentLabel
to get one. But we are not doing it in a threadsafe way. This PR fixes that, solves #65Changes Made
C++
TypedIndex::currentLabel
is nowstd::atomic
and it's post-incremented atomically to get a new id when neededTesting
Checklist
Additional Comments