Search code examples
gccclangopenmpthread-sanitizerdata-race

Is using -fopenmp necessary when using thread sanitizers in clang or gcc


I am trying to use threadsanitizer on given piece of code(in ok.c file) as:

clang -fsanitize=thread ok.c -w -I../runtime

This works fine and no data race is detected, but when I try giving -fopenmp option to sanitizer it dumps the terminal with possible location of data race in the loop.

clang -fsanitize=thread -fopenmp ok.c -w -I../runtime

Terminal output:
$
WARNING: ThreadSanitizer: data race (pid=7980)
  Atomic read of size 1 at 0x7d680001f700 by thread T2:
    #0 pthread_mutex_lock <null> (a.out+0x000000439b00)
    #1 __kmp_reap_worker <null> (libomp.so.5+0x0000000477a2)


int l_3438[10]; //shared 
int i;
            #pragma omp parallel for
            for (i = 0; i < 10; i++){
                l_3438[i] = (-10L);
            }

I tried using shared and private attributes as well to make things more clear.

int l_3438[10]; //shared 
int i;
            #pragma omp parallel for shared(l_3438) private(i)
            for (i = 0; i < 10; i++){
                l_3438[i] = (-10L);
            }

Question: Is -fopenmp flag necessary when using thread sanitizer? Thanks.


Solution

  • Unless you are concerned about false positives (compiler diagnosing data races when there are none) I find the question (as it is posted) should be reversed. It should have been: Should I use thread sanitizer for openmp programs?

    If your aim is to detect data races that might result from using openmp constructs, then you should definitely use thread sanitizer with such programs.

    And if your question is really about avoiding false positives when using thread sanitizers with openmp programs, this is covered in this post.