Repository navigation
Open
Milestone
Description
Hi,
I'm trying to leverage #4577 in a project (scikit-learn) that has a mix of OpenMP and OpenBLAS built with the pthreads threading layer to make OpenBLAS use the OpenMP threadpool. Ideally we'd use an OpenBLAS built with the OpenMP threading layer but it's outside of our control because it comes from a dependency.
I naively tried the example callback presented in #4577 (comment), but I can't make it work. Here's a simple reproducer, just a gemm:
test.c
# include <stdio.h>
# include <stdlib.h>
# include <stddef.h>
# include <omp.h>
# include <cblas.h>
void omp_cb (int sync, openblas_dojob_callback dojob, int numjobs, size_t jobdata_elsize, void *jobdata, int dojob_data)
{
#pragma omp parallel for
for(int i = 0; i < numjobs; i++)
{
printf("thread: %d, i: %d\n", omp_get_thread_num(), i);
void *element_adrr = (void *) (((char *)jobdata) + ((unsigned) i)*jobdata_elsize);
dojob(i, element_adrr, dojob_data);
}
return;
}
void test()
{
int n = 100;
double *A = (double *)malloc(n * n * sizeof(double));
double *B = (double *)malloc(n * n * sizeof(double));
double *C = (double *)malloc(n * n * sizeof(double));
for(int i = 0; i < n * n; i++)
{
A[i] = 1.0;
B[i] = 1.0;
C[i] = 0.0;
}
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, A, n, B, n, 0.0, C, n);
free(A);
free(B);
free(C);
}
int main()
{
openblas_set_threads_callback_function(omp_cb);
test();
}Compile command:
gcc -o test test.c -fopenmp -I/home/jeremie/R/installs/OpenBLAS/include -Wl,-rpath,/home/jeremie/R/installs/OpenBLAS/lib -L/home/jeremie/R/installs/OpenBLAS/lib -lopenblas
It just results in a segfault at the first step of the loop. Note that it still segfaults if I remove the omp pragma and just use a sequential loop in the callback.
Any help would be greatly appreciated.
Metadata
Metadata
Assignees
Labels
No labels
Activity
martin-frbg commentedon Jun 30, 2024
This is hopefully fixed by the one-liner in #4775 (though I still need to track down the source of the spurious queue entry)
jeremiedbb commentedon Jul 1, 2024
Thanks. I just tested the fix but now the program hangs at the first step of the callback loop.
Here's the backtrace:
And some additional info
(Side question: Is it expected that even when setting a sequential callback, OpenBLAS still creates that many threads ?)
martin-frbg commentedon Jul 1, 2024
Strange, I ran several tests with and without valgrind and did not encounter any hang yesterday. Is this still with the unmodified testcase that you posted above ? I guess there is a small chance that this may be a "generic" lockup from combining OpenMP and non-OpenMP threading which don't know of each other's existence. (And my understanding is that in this iteration of the user-defined threading callback, the OpenBLAS mechanisms for obtaining the initial thread count are unchanged. @shivammonaka can you comment on this issue ?
jeremiedbb commentedon Jul 1, 2024
Yes with the exact same reproducer, with a sequential loop or with an OpenMP based parallel loop. In addition, it works if I set
OPENBLAS_NUM_THREADS=1but hangs for any value > 1.martin-frbg commentedon Jul 1, 2024
I cannot reproduce the problem (on x86_64 hardware of various sizes up to 64 cores and with multiple compiler versions) with my PR in place. Did you do a full rebuild (i.e.
make cleanbefore themake) after applying the patch ? (And are you sure your testcase is picking up the "fixed" version of the library ?)jeremiedbb commentedon Jul 2, 2024
Sorry, there was a slight difference with the original reproducer. I explicitely set the number of openmp threads for the for loop:
Then if I set
OPENBLAS_NUM_THREADSto any value less or equal to the number of openmp threads it works. If I set it to any value larger, it hangs.shivammonaka commentedon Jul 2, 2024
Hi @martin-frbg, @jeremiedbb , Sorry for the late reply. I was preoccupied with some other stuff. Let me explain my understanding of this error
Question : Why does this happens : -
I set OPENBLAS_NUM_THREADS to any value less or equal to the number of openmp threads it works. If I set it to any value larger, it hangs.Answer :
inner_threads(thread_id)function call with thread_id as a argument. So, The number of threads required by each BLAS call is pre-decided even before threading was initiated (Insidegemm_driver()inlevel3_thread.c)I am currently working on fixing this thing only. I want to architecturally change the execution of the algorithm so that
I have a solution ready for OpenMP backend but currently its in testing phase. I will try to get it out ASAP.
martin-frbg commentedon Jul 2, 2024
Seems to be related to requesting more OMP threads than there are iterations in the for loop, possibly the two independent threading models each having an idle thread and both waiting for each other to make the first move...
shivammonaka commentedon Jul 2, 2024
It actually requires Number of OMP Threads on caller side >= Number of iteration in the for loop. BLAS wants each iteration of the loop to be executed by a different thread otherwise it goes into infinite waiting. Sadly, That's how
inner_thread()currently works. I am sorry but I am not sure what the second line meant.