Skip to content

[WIP] Number of threads in KMeans should not be bigger than number of chunks - #17210

Merged
adrinjalali merged 3 commits into
scikit-learn:masterfrom
jeremiedbb:fix-kmeans-threads-small-data
May 15, 2020
Merged

adrinjalali merged 3 commits into
scikit-learn:masterfrom
jeremiedbb:fix-kmeans-threads-small-data

Conversation

@jeremiedbb

@jeremiedbb jeremiedbb commented May 13, 2020 •

Copy link
Copy Markdown
Member

related to #17208

When the number of chunks is smaller than the number of cores (i.e. very small datasets), KMeans launches as many threads as there are cores anyway. It should use n_chunks threads instead.

@adrinjalali

Copy link
Copy Markdown
Member

curious, how much of a gain is this? and should it go in 0.23.1?

@jnothman jnothman added this to the 0.23.1 milestone May 14, 2020
@glemaitre

Copy link
Copy Markdown
Member

curious, how much of a gain is this? and should it go in 0.23.1?

It is a regression in #17208. It seems x10 on very small dataset.

@adrinjalali adrinjalali 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.

Then if you've tested and this is fixing the issue, I'm happy. I don't think we can easily write a test for it.

@jeremiedbb

Copy link
Copy Markdown
Member Author

It's an attempt to fix 17208, but it's still wip. I can reproduce the slowdown on my laptop and this pr fixes it but it seems to not work for the person who opened the issue. I need to investigate further with him

@rth

rth commented May 14, 2020

Copy link
Copy Markdown
Member

The fix sounds like a good improvement in any case. Though he has 4 cores, so I would have imagined spawning 8 threads shouldn't be too costly performance wise?

@jeremiedbb

jeremiedbb commented May 14, 2020 •

Copy link
Copy Markdown
Member Author

The fix sounds like a good improvement in any case

I agree

Though he has 4 cores, so I would have imagined spawning 8 threads shouldn't be too costly performance wise?

In the reproducible snippet, there are only 150 samples, which means there will only be one chunk. On my laptop with 4 cores, it spawns 4 threads and it makes a huge diff. The thing is that it only concerns very small datasets for which the whole fit time is ~ 0.005 sec. So I guess that the overhead of thread creation become non negligible.

@rth

rth commented May 14, 2020

Copy link
Copy Markdown
Member

Let's merge this as is? As if necessary you could create a new PR with more improvements? Please add a what's new entry.

BTW, can this affect other parts of the code that do parallel chunking where we could also apply this fix ?

@jeremiedbb

Copy link
Copy Markdown
Member Author

BTW, can this affect other parts of the code that do parallel chunking where we could also apply this fix ?

Not that I can think about.

@jeremiedbb

Copy link
Copy Markdown
Member Author

Let's merge this as is?

It can't hurt and It's still an improvement

@jeremiedbb

Copy link
Copy Markdown
Member Author

Some profiling showed that it's threadpoolctl that takes 90% of the time on these very small problems. It's called at each iteration. I'll make a pr to move it outside of the loop.

@adrinjalali

Copy link
Copy Markdown
Member

I guess @rth is also happy to have this merged. Merging, hopefully the other PR improving the threadpoolctl overhead would get in quickly too.

@adrinjalali
adrinjalali merged commit 90d00da into scikit-learn:master May 15, 2020
gio8tisu pushed a commit to gio8tisu/scikit-learn that referenced this pull request May 15, 2020
…hunks (scikit-learn#17210)

* num threads not bigger than num chunks

* what's new
adrinjalali pushed a commit to adrinjalali/scikit-learn that referenced this pull request May 18, 2020
…hunks (scikit-learn#17210)

* num threads not bigger than num chunks

* what's new
adrinjalali pushed a commit that referenced this pull request May 19, 2020
…hunks (#17210)

* num threads not bigger than num chunks

* what's new
viclafargue pushed a commit to viclafargue/scikit-learn that referenced this pull request Jun 26, 2020
…hunks (scikit-learn#17210)

* num threads not bigger than num chunks

* what's new
@jeremiedbb
jeremiedbb deleted the fix-kmeans-threads-small-data branch September 3, 2026 16:08
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.

5 participants