Skip to content

thread safety of dgemv in 0.3.6 #2164

Closed

Description

@simonnwood

This may be related to #1761. I am calling the dgemv supplied by a single threaded OpenBLAS 0.3.6 from OpenMP threads, and intermittently obtaining incorrect results. A stripped down version of the problem is provided by the following code (modified from #1761). The code multiples 2 matrices by repeatedly calling dgemv, first from a single thread, and then in parallel using OpenMP. The results are compared to see if they are identical, as expected. In this code the experiment is repeated 1000 times or until the results differ substantially.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <cblas.h>


void mult(double *A,double *B,double *C,int m,int n,int nt) {
/* pointless C=A'B code using openmp. A and B are m by n. C is n by n. */
  int i,j;
#pragma omp parallel for private(i) num_threads(nt)
  for (i=0;i<n;i++) {
    cblas_dgemv(CblasColMajor, CblasTrans,m,n,1,A,m,B+i*m,1,0,C+i*n,1);
  }
  /* NOTE: in original code producing problem replacing dgemv with dgemm 
     did not fix the problem */
}


void main() {
  double *A,*B,*C,*D,*c,*d,diff=0.0,x,max=0.0;
  int n = 200,m=300,i,j;
  A = (double *)calloc((size_t)n*m,sizeof(double));
  B = (double *)calloc((size_t)n*m,sizeof(double));
  C = (double *)calloc((size_t)n*n,sizeof(double));
  D = (double *)calloc((size_t)n*n,sizeof(double));
  for (i=0;i<n*m;i++) {A[i] = i*0.01;B[i]= n*m/(i*.04+.1);}
  mult(A,B,D,m,n,1);
  for (j=0;j<1000;j++) { /* repeat multi-threaded call until a failure encountered */
    mult(A,B,C,m,n,4);        
    for (d=D,c=C,i=0;i<n*n;i++,c++,d++) { /* compare C & D which should be identical */
      x = fabs(*d-*c); if (x>diff) diff=x;
      if (*d>max) max = *d;
    }
    if (diff>max*1e-2) break;
    printf(".");fflush(stdout);
  }
  printf("\nMax |D| = %g\nMax abs diff = %g\n",max,diff);
}

Compiling and running this as follows:

gcc problem.c -I /opt/OpenBLAS/include/ -L /opt/OpenBLAS/lib/ -lopenblas -fopenmp
LD_LIBRARY_PATH=/opt/OpenBLAS/lib ./a.out

gives (after a variable number of iterations) a result like

Max |D| = 4.48832e+09
Max abs diff = 3.48985e+09

under 0.3.6. In contrast, using 0.2.20 it always runs for the full 1000 iterations before returning

Max |D| = 4.48832e+09
Max abs diff = 0

as it should. Note that the only substantive change to the code relative to #1761 is the loop to repeat the experiment, so I don't think that the problem can lie in the test code.

My (64bit) test set up is an Intel i5-6300U CPU, Ubuntu 16.04, gcc 5.4.0. OpenBLAS was compiled with USE_THREAD=0 (and I also tried FCOMMON_OPT=-frecursive and NUM_THREADS=8 after looking at #1690).

My real application of this is the R package https://cran.r-project.org/web/packages/mgcv/index.html which offers some `big data' methods that need this functionality and are currently intermittently failing their tests.

Activity

  1. martin-frbg commented on Jun 16, 2019

    @martin-frbg
    Collaborator

    Probably related to #2126

  2. TiborGY commented on Jun 16, 2019

    @TiborGY
    Contributor

    The latest dev version with USE_LOCKING=1 might fix this.
    Also, we now have a thread safety tester integrated into the project. Could you try compiling with CPP_THREAD_SAFETY_TEST = 1? This runs a test after the build, and should catch most thread safety issues.

  3. simonnwood commented on Jun 17, 2019

    @simonnwood
    Author

    Thanks, the latest dev with make USE_THREAD=0 USE_LOCKING=1 indeed fixes the problem, both for the test code (above) and the original application. Will USE_LOCKING=1 be the default with USE_THREAD=0 in future stable releases? I need to update my package documentation to make sure users of openblas have a suitable one.

  4. martin-frbg commented on Jun 17, 2019

    @martin-frbg
    Collaborator

    Not decided yet as I am not sure what the "default" context for a USE_THREAD=0 build of OpenBLAS is supposed to be - a truely single-threaded program, where the locking would only incur unnecessary overhead and an unwanted dependency on libpthread, or a heavily multithreaded program whose use of OpenMP is (contrary to earlier assumptions) not sufficient to keep things in order.

  5. TiborGY commented on Jun 17, 2019

    @TiborGY
    Contributor
  6. simonnwood commented on Jun 17, 2019

    @simonnwood
    Author

    My example above only ever uses 4 threads (and also failed with only 2 threads), and all it is doing is calling dgemv from these threads, with no overlap of memory that is supposed to be modified between the threads. Does that really count as heavily multi-threaded, and is there a way I could have used openMP to keep things in order?

    Or am I missing something about how to build a single threaded BLAS, and there is another way to do it than USE_THREAD=0 that avoids these problems (I'm very much in recipe following mode with this, and may well be missing things)?

    My problem is that my users are often working on a machine where the BLAS is managed by someone else, especially in the cases in which some multi-threading may help (they are statistically modelling in R and selecting a number of threads to use), and usually have only the vaguest idea of what the BLAS is actually doing, let alone how to configure it. If I have to tell them to check that OpenBLAS has been compiled using USE_LOCKING=1 they are liable to read this as telling them not to use OpenBLAS (or not to multithread).

  7. TiborGY commented on Jun 17, 2019

    @TiborGY
    Contributor
  8. simonnwood commented on Jun 17, 2019

    @simonnwood
    Author

    Thanks, that's helpful. Oddly I can't ever generate a problem in 0.2.20, only in 0.3.x. The problem from my perspective is that single threaded OpenBLAS is the default on e.g. Fedora: the default BLAS not being thread safe on some platforms is quite difficult to handle. I guess all I'm really saying is that it would be great if OpenBLAS could be safe by default, allowing experienced users wanting absolute bleeding edge performance on purely single threaded applications to turn off thread safety.

  9. martin-frbg commented on Jun 17, 2019

    @martin-frbg
    Collaborator

    Yes, original GotoBLAS (and accordingly OpenBLAS until about 0.2.18) was totally not thread-safe. My theory is that this simply did not become apparent on the slower x86 cpus of the time. Around 0.2.20 locks had got put around practically everything so people started to complain about decreased performance in supposedly safe OpenMP contexts so the locks were put into #ifdef's (Mind you I do not consider myself an expert on thread safety, I only happened to be one of the first people who ran
    valgrind's helgrind on OpenBLAS and tried to do something about the surprisingly alarming reports.)

  10. TiborGY commented on Jun 17, 2019

    @TiborGY
    Contributor

    The problem from my perspective is that single threaded OpenBLAS is the default on e.g. Fedora: the default BLAS not being thread safe on some platforms is quite difficult to handle. I guess all I'm really saying is that it would be great if OpenBLAS could be safe by default

    I guess that could be done. But it might also be worthwhile to reach out to package maintainers, to see if they are receptive towards the idea of releasing multiple builds with different build configs and package names.

  11. courage17340 commented on Oct 29, 2019

    @courage17340

    I also met up with this problem, and "USE_THREAD=0 USE_LOCKING=1" solved it. Since 0.3.7 has been released, I think that it's a good idea to update the wiki page on "How can I use OpenBLAS in multi-threaded applications?" to inform users of thread safety.

  12. martin-frbg commented on Oct 29, 2019

    @martin-frbg
    Collaborator

    Wiki updated now, thanks for the reminder.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions