Skip to content

flann/kmeans: Fix non-determinism of KMeans index - #14092

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
mattismyname:fix-kmeans
Mar 20, 2019
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
mattismyname:fix-kmeans

Conversation

@mattismyname

@mattismyname mattismyname commented Mar 18, 2019 •

Copy link
Copy Markdown

When running with >1 OpenCV thread, KMeans index generation was
non-deterministic. Issue is resolved by protecting each radius variable
with a mutex.

resolves #9179
relates #3458

@alalek

alalek commented Mar 19, 2019

Copy link
Copy Markdown
Member

Nice catch!

Using of multiple mutexes has huge overhead here.

I believe we can move out post-processing from parallel_for body (it is simple enough).
Lets calculate these values in parallel:

  • new_centroid
  • sq_dist
    for each indices_length
// pre-allocate buffers
std::vector<int> new_centroid(indices_length);
std::vector<DistanceType> sq_dist(indices_length);

...

KMeansDistanceComputer invoker(...);
parallel_for_(cv::Range(0, (int)indices_length), invoker);

... post processing loop: i=[0..indices_length) ...
- update `radiuses[new_centroid[i]]`
- update `belongs_to[i]` and `count[belongs_to[i]]`
- update `converged`

@alalek

alalek commented Mar 19, 2019

Copy link
Copy Markdown
Member

As a "bugfix" this patch should go into 3.4 branch first. We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

So, please:

  • change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
  • rebase your commits from master onto 3.4 branch. For example:
    git rebase -i --onto upstream/3.4 upstream/master
    (check list of your commits, save and quit (Esc + "wq" + Enter)
    where upstream is configured by following this GitHub guide and fetched (git fetch upstream).
  • push rebased commits into source branch of your fork (with --force option)

Note: no needs to re-open PR, apply changes "inplace".

@mattismyname
mattismyname changed the base branch from master to 3.4 March 19, 2019 23:02
@mattismyname

Copy link
Copy Markdown
Author

ok please take a look now.

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

Please take a look on comments below.

@alalek

alalek commented Mar 20, 2019

Copy link
Copy Markdown
Member

Perhaps this patch should resolve these sporadic failures: #9179

@mattismyname
mattismyname force-pushed the fix-kmeans branch 3 times, most recently from 6b3b022 to 80cadf5 Compare March 20, 2019 15:41
When running with >1 OpenCV thread, KMeans index generation was
non-deterministic because of a RWW race. Issue is resolved by removing
the offending logic from the parallel section.

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@opencv-pushbot
opencv-pushbot merged commit 74e3403 into opencv:3.4 Mar 20, 2019
@alalek alalek mentioned this pull request Mar 22, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants