Skip to content

Impact of active wait policy of OMP implementations in scikit-learn #34764

Open
Shreevable/scikit-learn
#3

Description

@cakedev0

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

About active wait in OpenMP

To my knowledge, there are two possible OpenMP implementations in scikit-learn:

  • GNU OpenMP (libgomp): libgomp is the default one (i.e. the one packaged in PyPI build,
    and the one fetched by default in conda-forge). Its active wait behavior is controlled
    by GOMP_SPINCOUNT (an integer).
  • LLVM OpenMP (libomp), for which active wait behavior is controlled by KMP_BLOCKTIME (a time in ms or us).
    It has a sibling: libiomp5 from Intel. They seem to resemble a lot as libomp is a fork of libiomp.

GOMP_SPINCOUNT and KMP_BLOCKTIME have the same role: control how long threads remain
actively waiting/spinning after the end of a parallel region.

These environment variables can dramatically hurt or help performance depending on the workload: if threads are still actively waiting when the next parallel region starts, entering it is much cheaper. Conversely, active waiting can pollute other threads/backends (e.g. OpenBLAS pthreads).

Inconveniently, libgomp reads GOMP_SPINCOUNT only once at initialization time unlike OMP_NUM_THREADS, which can be adjusted at runtime per workload.
libomp on the other hand, provides kmp_set_blocktime that seems to work like omp_set_num_threads (=> could
probably be used in a local context manager).

Also, in their recent versions, both libraries share a common policy for heterogeneous cores*: when such cores are detected, they set GOMP_SPINCOUNT=1/KMP_BLOCKTIME=0.
It was not the case a couple of years ago (at least for libgomp), which is why the libgomp bundled inside the scikit-learn PyPI wheel has GOMP_SPINCOUNT=300000 (the default), see issue #34437 about why the PyPI wheel bundles an old-ish libgomp.
Otherwise the default values are:

  • GOMP_SPINCOUNT=300000
  • KMP_BLOCKTIME=200ms

Ok, so now that I said all that, what are the affected workloads in scikit-learn?
To my knowledge, there are mainly two: k-means and HGB.

Impact on Kmeans

See the dedicated issue: #17334

But in short:

In Kmeans fit, we alternate OMP regions with BLAS regions.
So if:

  • BLAS is OpenBLAS pthreads (true for PyPi wheel)
  • OpenMP is libgomp, the active wait is enabled (GOMP_SPINCOUNT=300000)
  • (repeated) small fits
    Then you'll observe a dramatic slowdown (compared to other setups), the smaller the datasets
    the worse it'll get. The example from the issue is 10-20x slowdown.

Note that the active wait of libomp is implemented differently and doesn't pollute
the openBLAS pthreads (even with KMP_BLOCKTIME=200ms).

Impact on HGB

In HGB, we have many small OMP regions (around 10k for a typical fit, independently of the dataset size).

Hence, the overhead of handling threads can easily dominate, in that case active wait helps a lot
as it reduces this overhead.

Note that it doesn't solve the problem entirely (pranges still have an overhead,
even if threads are already awake and actively waiting), HGB still scales counter-productively
for small datasets, or deep trees, or large number of threads, ... But the GOMP_SPINCOUNT=1/KMP_BLOCKTIME=0
effect is dramatic, in my benchmarks it makes HGB fit 5-10x slower on a wide variety of standard small to medium-big datasets. This slowdown is observed by laptop users installing scikit-learn from conda-forge.

Scaling of HGB has its own dedicated issue:


*heterogeneous cores / hybrid CPUs are basically the norm for modern laptops nowadays.

Activity

  1. added
    Needs Triage
    on Aug 17, 2026
  2. added
    Needs Decision
    and removed
    Needs Triage
    on Aug 18, 2026
  3. github-actions commented on Aug 18, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  4. betatim commented on Aug 18, 2026

    @betatim
    Member

    Could you put a short sentence or two about what you want to happen. Is this a discussion item or are there options for something we can do that need deciding?

  5. ogrisel commented on Aug 19, 2026

    @ogrisel
    Member

    I think we can to change HGB parallel region to switch to serial mode when input dimensions are dimmed too small to benefit from parallelism. This should help reduce the OpenMP overhead, and therefore reduce the need for the active wait setting.

  6. cakedev0 commented on Aug 19, 2026

    @cakedev0
    MemberAuthor

    Sorry, I first write down my current understanding and share it (with others and with future me), because I think it explains clearly some otherwise mysterious slowdowns.

    In terms of proposed solutions, what I see for now:

    1) Switching to libomp

    If it was risk-less, switching to libomp in the PyPI wheel for linux and using kmp_set_blocktime would be a great solution:
    For HGB we could use kmp_set_blocktime to make sure active wait is enabled
    For k-means, as mentioned in the issue, libomp active wait doesn’t negatively affect openBLAS pthreads.

    But as far as I understand after some discussion with AI, it's a fairly risky change because of interactions it could have with other libraries (such as pytorch, xgboost or lightbm).

    Note that the openMP implementation in the PyPI wheel is not libgomp for non-linux OS:

    • macOS: libomp
    • Windows: vcomp. I learned the existence of this today, and I haven’t found documentation about the active wait behavior. I'm not even sure “active wait” is a relevant concept on windows, but measuring the overhead included by parallel regions would be informative.

    2) Documentation/warnings

    We could detect and warn people when e.g. active wait is disabled and they fit a HGB model. Or when we detect many small-enough k-means fits (a bit harder to detect).

    3) [mitigation for HGB] Reduce the number of parallel regions

    Not sure it's really possible... Trees need to be built sequentially, each node's workload depends on its parent. So at least one parallel region per total number of nodes (32 x 100 for default params), it's already too many… And my quick attempt at reducing the number of parallel regions inside a node was a failure (I think Cython doesn’t provide necessary OMP tooling for that). Edit: it's more that HGB code is structurally (and inherently) a bad candidate for that.

    4) [mitigation for HGB] Switching to sequential mode for small workloads

    Before each prange, make a small “back of the envelope” calculation to decide whether it’s worth switching to a sequential range instead.

    5) [mitigation for HGB] Better initial sizing of the threadspool

    Given the current parallelization strategy, limiting the number of threads based on the number of features seems to help a lot in some cases.

    6) [mitigation for HGB] Adapt 4 & 5 to active wait

    Detect the absence of active wait before a fit of HGB (ideally through threadpoolctl, I guess this kind of logic belongs there), and adapt policies 4 and 5 accordingly (more switches to sequential mode, less initial number of threads).


    Most of the proposed solutions here are focused on HGB as I think it’s the most critical impact.

    I will soon have some experiments results for 4, 5 and 6. I got some for 4 and 5 already and it's promising, but no-active wait still hurts runtime badly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Decision

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions