1

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.

Sameeran Joshi
  • 59
  • 1
  • 10

1 Answers1

1

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.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • How do you identify a false positive BTW? – Sameeran Joshi Apr 29 '19 at 07:14
  • 1
    false positive in this case is when the compiler detects that there is a data race when there is no data race. The post I have linked to contains one such example. – P.W Apr 29 '19 at 07:15
  • Some thread checkers identify race conditions when the OpenMP runtime starts the parallel region. There is usually some unprotected data exchange from the master thread to its workers that is not a true race, as the OpenMP runtime knows about this and was carefully crafted to be correct. – Michael Klemm Apr 29 '19 at 07:37
  • @SameeranJoshi: Does this answer your question? – P.W May 02 '19 at 04:38
  • Ya! Thanks, I tried writing blog based on above experience. https://medium.com/@joshisameeran/using-tsan-threadsanitizer-and-ways-to-avoid-false-sharing-in-clang-and-gcc-15fae5283ad1 – Sameeran Joshi May 02 '19 at 15:40
  • 1
    @SameeranJoshi: Nice article. BTW, you can accept/upvote this answer. See https://stackoverflow.com/help/someone-answers – P.W May 02 '19 at 16:30