You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Slowdown when using openblas-pthreads alongside openmp based parallel code #3187
I have a code which mixes BLAS calls (gemm) and OpenMP based parallel loops (they are not nested). When OpenBLAS is built using OpenMP everything is fine but when OpenBLAS is built with pthreads there's a huge slowdown. Below is a reproducible example (sorry it's from python/cython)
On my laptop (2 physical cores), when I use a sequential loop in (*), it runs in 0.26s. When I use a parallel loop it runs in 2.6s (10x slower). This is with OpenBLAS 0.3.12 built with pthreads. This conda env allows to reproduce conda create -n tmp -c conda-forge python numpy cython ipython.
However, if I use OpenBLAS built with OpenMP, it runs in 0.26s with and without prange. This is with OpenBLAS 0.3.9 built with OpenMP. This conda env allows to reproduce conda create -n tmp -c conda-forge python numpy cython ipython blas[build=openblas] libopenblas=0.3.9.
omp_get_max_threads() returns 4 (I have 2 physical cores + hyperthreading). Do you think OpenBLAS is using too many threads ?
It only occurs when I use an OpenMP loop, so it means there's an interaction between OpenBLAS built with pthreads and OpenMP.
2020-07-17
Conda-forge is building openblas with both pthreads and openmp on Linux
The main change is that openblas will use pthreads for threading by default on Linux instead of the previous openmp default. The openmp builds can be recovered by installing libopenblas=*=*openmp*.
Is it really libgomp? Note that we are using libomp (from LLVM) with openblas openmp build because libgomp is not fork safe. If you are loading libgomp.so.3 from conda-forge, it's a symlink to libomp, but if you are loading libgomp.so.3 from the system it'll be the real libgomp and therefore you'll have two instances of openmp loaded which is not advisable.
My main concern is that the issue happens with OpenBLAS built with pthreads (not when built with OpenMP) !
When I execute my snippet above, libopenblas is loaded because numpy is imported and libgomp is loaded because of the prange loop. It can be confirmed with
If I replace prange by range, libgomp disappears from the list and if I don't import numpy libopenblas disappears.
It also confirms that I have only 1 OpenMP runtime loaded (threadpoolctl takes symlinks into account).
Also, for this specific environment (conda create -n tmp -c conda-forge python numpy cython ipython) it's libgomp and not libomp which is installed by conda-forge
Thanks for the info. That's to be expected though. When running openmp, you create a few threads and then each thread calls blas which in turn creates more threads. In a multi-threaded environment, it's safest to just do openblas_set_num_threads(1) before calling blas.
It's not an oversubscription issue here. They are not nested. I first call gemm and then call a function which executes a parallel loop (with no blas inside).
OpenBLAS assumes each thread has CPU at its hands with all outermost caches. 30x slowdoen means multiple threads are stuck on same CPU and actually get to spill to the main memory instead of cache.
You can set OPENBLAS_NUM_THREADS=1 running samples right now, before building OMP version to address oversubscription issue. pthread version has no idea of OpenMP spinnnging full room of threads, and spins up own in each thread.
EDIT: hyperthreads do not add any value to HPC.You can disable them to get single-threaded stuff end faster, parallel will have half double-speed cores essentially, i.e same timing as with HT
Sorry I'm not sure to understand your answer. I agree that HT is useless in HPC most of the time but it does not seem to be the only issue here since the program is fast when OpenBLAS is built with pthreads and I run the loop in sequential mode. The issue only appears when I also run the loop in parallel with OpenMP. I recall that they are not nested but run one after the other.
I post a pure C reproducible code here, hope it will make my concerns clearer.
#include<stdlib.h>#include<stdio.h>#include<omp.h>#include"cblas.h"doublef2(double*X, intn){
doublev=0.0;
#pragma omp parallel for reduction(+:v)
for(inti=0; i<n; i++){
v+=X[i];
}
returnv;
}
intmain(intargc, char**argv){
intm=10000,
n=10,
k=100;
double*A= (double*) malloc(m*k*sizeof(double)),
*B= (double*) malloc(n*k*sizeof(double)),
*C= (double*) malloc(m*n*sizeof(double));
for(inti=0; i<m*k; i++){
A[i] =0.1;
}
for(inti=0; i<n*k; i++){
B[i] =1.2;
}
doublev=0.0;
for(inti=0; i<1000; i++){
// BLAS callcblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, n, k, 1.0, A, k, B, k, 0.0, C, n); // C <- A @ B.T// Followed by parallel loopv+=f2(C, m*n);
}
free(A), free(B), free(C);
return0;
}
Here a the timings on a 40 cores machine (20 physical + HT).
openblas pthreads
openblas openmp
sequential loop
1.22s
0.62s
parallel loop
21s
0.51s
Focusing on the pthreads + parallel loop case:
If I set OPENBLAS_NUM_THREADS=20 and OMP_NUM_THREADS=20 the result becomes 0.34s which confirms that HT is detrimental but I don't understand what is the difference with the openblas openmp + sequential loop case.
If I set OPENBLAS_NUM_THREADS=21 and OMP_NUM_THREADS=21, the result is 0.5s, i.e. 50% slower than above, which I would not expect since it's only 1 more thread.
If I set OPENBLAS_NUM_THREADS=22 and OMP_NUM_THREADS=22, the result is 5s ! I don't think the only issue here is basic oversubscription.
That just forces each new unnecessary OpenBLAS pthread swarm onto a single core,slightly better at data locality than completely unbound but still bad.
40-cores machine might be 2 sockets with CoD each, i.e 4 sockets. Fitting task into one CoD node (10 threads) will make it fall on a single cache (uncore) complex and perfrom optimally, you can manage 4 of those via OMP, a bit of a hack but works most of the time.
PS you think 50x slowdown is bad.... 8-socket systems with subpar data locality do like 500x down....
That just forces each new unnecessary OpenBLAS pthread swarm onto a single core,slightly better at data locality than completely unbound but still bad.
OpenBLAS pthread behaviour should not be affected by what openmp does since the openblas calls are from the main thread and an unrelated work happens in the openmp threads.
Reacted by Jérémie du Boisberranger and Olivier Grisel
OMP placement policy actually sets CPU affinity for OMP threads, so all the following pthreads cannot escape that. There is no hidden side-step API that nobody else uses.
@jeremiedbb maybe there is a way to introspect the CPU affinity of new pthreads started before and after calling the OMP f2 function in your minimal reproducer? That would help us confirm whether or not this is the cause of the problem.
I introspected the affinities for both OpenMP and OpenBLAS threadpools and it turns out that no affinity constraint is set (openblas is built with NO_AFFINITY). Here's the output of sched_getaffinity (I also checked the /proc/thread-self/status file and the results are the same) from the snippet:
So the affinity seems to not be the reason of the bad interaction between openblas-pthreads and openmp. However I found that when the openmp loop ends, the threads are still waiting for computation in an active way (OMP_WAIT_POLICY), which consumes resources and prevent openblas to start computations right away. By default, openmp makes waiting threads spin for a while.
Unfortunately, setting OMP_WAIT_POLICY=passive does not really improve the performances on a machine with many cores for some reason that I don't understand yet. The best solution I found so far is to set the num threads for both to half the number of threads, besides building openblas with openmp of course.
I guess this is a wont fix from the OpenBLAS side. OpenMP programs do not interact well with other libraries managing their own threadpool. Feel free to close the issue if you think there's no more to add about that.
Still I wonder if there is the same kind of wait policy in openblas.
@isuruf I think this issue is a good reason to always try to use an openblas built with openmp for the scikit-learn builds on conda-forge (I noticed it was not always the case).
@jeremiedbb OpenBLAS does have a similar wait policy for its threads, governed by the value of THREAD_TIMEOUT at build time (or the environment variable OPENBLAS_THREAD_TIMEOUT at runtime) which defines the number of clock cycles to wait as 1 << n where the default n is 26 (which according to the comment in Makefile.rule is equivalent to about 25ms at 3GHz).
Reacted by Jérémie du Boisberranger, Olivier Grisel, Ralf Gommers and Julien Jerphanion
Activity
martin-frbg commentedon Apr 22, 2021
most likely you get too many threads running in parallel in the non-openmp case
jeremiedbb commentedon Apr 22, 2021
omp_get_max_threads()returns 4 (I have 2 physical cores + hyperthreading). Do you think OpenBLAS is using too many threads ?It only occurs when I use an OpenMP loop, so it means there's an interaction between OpenBLAS built with pthreads and OpenMP.
jeremiedbb commentedon Apr 22, 2021
For what it's wotrh the number of running threads showed by htop only increases by 4 when I run this
jeremiedbb commentedon Apr 22, 2021
a profiling with linux perf shows that most of the time comes from
do_waitfrom libgompogrisel commentedon Apr 23, 2021
Would be worth checking this:
from https://conda-forge.org/docs/user/announcements.html
isuruf commentedon Apr 23, 2021
Is it really
libgomp? Note that we are usinglibomp(from LLVM) with openblas openmp build becauselibgompis not fork safe. If you are loadinglibgomp.so.3from conda-forge, it's a symlink tolibomp, but if you are loadinglibgomp.so.3from the system it'll be the real libgomp and therefore you'll have two instances of openmp loaded which is not advisable.jeremiedbb commentedon Apr 23, 2021
My main concern is that the issue happens with OpenBLAS built with pthreads (not when built with OpenMP) !
When I execute my snippet above, libopenblas is loaded because numpy is imported and libgomp is loaded because of the prange loop. It can be confirmed with
If I replace prange by range, libgomp disappears from the list and if I don't import numpy libopenblas disappears.
It also confirms that I have only 1 OpenMP runtime loaded (threadpoolctl takes symlinks into account).
Also, for this specific environment (
conda create -n tmp -c conda-forge python numpy cython ipython) it's libgomp and not libomp which is installed by conda-forgeisuruf commentedon Apr 23, 2021
Thanks for the info. That's to be expected though. When running openmp, you create a few threads and then each thread calls blas which in turn creates more threads. In a multi-threaded environment, it's safest to just do
openblas_set_num_threads(1)before calling blas.jeremiedbb commentedon Apr 23, 2021
It's not an oversubscription issue here. They are not nested. I first call gemm and then call a function which executes a parallel loop (with no blas inside).
jeremiedbb commentedon Apr 23, 2021
Also I ran
ps -o nlwp <pid>on the process running my python script and it returns 8 which is what I'd expect: 4 from openblas and 4 from openmpisuruf commentedon Apr 23, 2021
I see the same issue. 30x slowdown with libgomp and 2x slowdown with libomp.
isuruf commentedon Apr 23, 2021
OMP_PROC_BIND=TRUEreduces the slowdown in libgomp.brada4 commentedon Apr 23, 2021
OpenBLAS assumes each thread has CPU at its hands with all outermost caches. 30x slowdoen means multiple threads are stuck on same CPU and actually get to spill to the main memory instead of cache.
You can set OPENBLAS_NUM_THREADS=1 running samples right now, before building OMP version to address oversubscription issue. pthread version has no idea of OpenMP spinnnging full room of threads, and spins up own in each thread.
EDIT: hyperthreads do not add any value to HPC.You can disable them to get single-threaded stuff end faster, parallel will have half double-speed cores essentially, i.e same timing as with HT
jeremiedbb commentedon Apr 26, 2021
Sorry I'm not sure to understand your answer. I agree that HT is useless in HPC most of the time but it does not seem to be the only issue here since the program is fast when OpenBLAS is built with pthreads and I run the loop in sequential mode. The issue only appears when I also run the loop in parallel with OpenMP. I recall that they are not nested but run one after the other.
I post a pure C reproducible code here, hope it will make my concerns clearer.
Here a the timings on a 40 cores machine (20 physical + HT).
Focusing on the pthreads + parallel loop case:
OPENBLAS_NUM_THREADS=20andOMP_NUM_THREADS=20the result becomes 0.34s which confirms that HT is detrimental but I don't understand what is the difference with the openblas openmp + sequential loop case.OPENBLAS_NUM_THREADS=21andOMP_NUM_THREADS=21, the result is 0.5s, i.e. 50% slower than above, which I would not expect since it's only 1 more thread.OPENBLAS_NUM_THREADS=22andOMP_NUM_THREADS=22, the result is 5s ! I don't think the only issue here is basic oversubscription.isuruf commentedon Apr 26, 2021
@jeremiedbb, can you also try with
OMP_PROC_BIND=TRUE?jeremiedbb commentedon Apr 26, 2021
@isuruf it reduces the time from 21s to 2.6s. It's better but still much slower than expected
brada4 commentedon Apr 26, 2021
That just forces each new unnecessary OpenBLAS pthread swarm onto a single core,slightly better at data locality than completely unbound but still bad.
40-cores machine might be 2 sockets with CoD each, i.e 4 sockets. Fitting task into one CoD node (10 threads) will make it fall on a single cache (uncore) complex and perfrom optimally, you can manage 4 of those via OMP, a bit of a hack but works most of the time.
PS you think 50x slowdown is bad.... 8-socket systems with subpar data locality do like 500x down....
isuruf commentedon Apr 26, 2021
OpenBLAS pthread behaviour should not be affected by what openmp does since the openblas calls are from the main thread and an unrelated work happens in the openmp threads.
brada4 commentedon Apr 27, 2021
OMP placement policy actually sets CPU affinity for OMP threads, so all the following pthreads cannot escape that. There is no hidden side-step API that nobody else uses.
ogrisel commentedon Apr 27, 2021
@jeremiedbb maybe there is a way to introspect the CPU affinity of new pthreads started before and after calling the OMP
f2function in your minimal reproducer? That would help us confirm whether or not this is the cause of the problem.https://man7.org/linux/man-pages/man3/pthread_getaffinity_np.3.html
brada4 commentedon Apr 28, 2021
cat /proc/self/status | grep _allowedjeremiedbb commentedon May 5, 2021
here's the output of
cat /proc/self/status | grep _allowedSorry but I've no idea how to interpret this :/
brada4 commentedon May 5, 2021
Put a one-minute sleep between OMP loops and check thread binding to CPU cores. It is actually documented in GOMP manual pages on any Linux system.
jeremiedbb commentedon Oct 12, 2021
I introspected the affinities for both OpenMP and OpenBLAS threadpools and it turns out that no affinity constraint is set (openblas is built with NO_AFFINITY). Here's the output of
sched_getaffinity(I also checked the /proc/thread-self/status file and the results are the same) from the snippet:So the affinity seems to not be the reason of the bad interaction between openblas-pthreads and openmp. However I found that when the openmp loop ends, the threads are still waiting for computation in an active way (OMP_WAIT_POLICY), which consumes resources and prevent openblas to start computations right away. By default, openmp makes waiting threads spin for a while.
Unfortunately, setting OMP_WAIT_POLICY=passive does not really improve the performances on a machine with many cores for some reason that I don't understand yet. The best solution I found so far is to set the num threads for both to half the number of threads, besides building openblas with openmp of course.
I guess this is a wont fix from the OpenBLAS side. OpenMP programs do not interact well with other libraries managing their own threadpool. Feel free to close the issue if you think there's no more to add about that.
Still I wonder if there is the same kind of wait policy in openblas.
jeremiedbb commentedon Oct 12, 2021
@isuruf I think this issue is a good reason to always try to use an openblas built with openmp for the scikit-learn builds on conda-forge (I noticed it was not always the case).
martin-frbg commentedon Oct 12, 2021
@jeremiedbb OpenBLAS does have a similar wait policy for its threads, governed by the value of THREAD_TIMEOUT at build time (or the environment variable OPENBLAS_THREAD_TIMEOUT at runtime) which defines the number of clock cycles to wait as
1 << nwhere the default n is 26 (which according to the comment in Makefile.rule is equivalent to about 25ms at 3GHz).martin-frbg commentedon Jan 12, 2024
Closing after copying the relevant information to the FAQ in the wiki