Skip to content

Make OpenBLAS's usage of OpenMP respect openblas_set_num_threads()#5808

Merged
martin-frbg merged 1 commit into
OpenMathLib:developfrom
nh2:issue-5806-openblas_set_num_threads-openmp
Jul 13, 2026
Merged

Make OpenBLAS's usage of OpenMP respect openblas_set_num_threads()#5808
martin-frbg merged 1 commit into
OpenMathLib:developfrom
nh2:issue-5806-openblas_set_num_threads-openmp

Conversation

@nh2

@nh2 nh2 commented May 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #5806.

Also

Reviewing by commit is recommended.

@itamarst

Copy link
Copy Markdown

Would be nice to get a fix merged, if someone has time to review.

@ngoldbaum

Copy link
Copy Markdown
Contributor

@frbg - @itamarst is working with me and @kumaraditya303 on improving support for free-threading in Python packages. He's identified the issue this PR in particular fixes as the cause of substantial CPU oversubscription in scikit-learn using the threading joblib backend. We'd appreciate your eyes on this and the original issue.

@martin-frbg

Copy link
Copy Markdown
Collaborator

Can you split this please ? openblas_set_num_threads() immediately getting reset to the OMP maximum is clearly unintended (though obviously it should be bounded by the OMP maximum).

Regarding the other other things in this PR - openblas_set_num_threads_local is intended to match mkl_set_num_threads_local (but may not yet be complete) - so nothing to do with forcing OMP parallel jobs inside an OMP_PARALLEL region (which IIRC would be invoking undefined behavior at best).
Refactoring num_cpu_avail() may be a good idea at some point, but it bears the risk of introducing new bugs and I have neither the time nor energy to review that now.

…ixes OpenMathLib#5806.

Until now, the code in `num_cpu_avail()`,

    if (blas_cpu_number != openmp_nthreads) {
      goto_set_num_threads(openmp_nthreads);
    }

would just always set the threads back to OpenMP's thread count.
@nh2 nh2 force-pushed the issue-5806-openblas_set_num_threads-openmp branch from d18385b to f8674a7 Compare July 12, 2026 04:13
@nh2 nh2 mentioned this pull request Jul 12, 2026
2 tasks
@nh2

nh2 commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

Can you split this please ?

Done. I moved the rest to PR #5907.

@martin-frbg

Copy link
Copy Markdown
Collaborator

Thank you

@martin-frbg martin-frbg added this to the 0.3.34 milestone Jul 13, 2026
@martin-frbg martin-frbg merged commit b668c9a into OpenMathLib:develop Jul 13, 2026
101 of 106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openblas_set_num_threads() is silently overridden when built with USE_OPENMP

4 participants