title: Helgrind (Valgrind) and OpenMP (C): avoiding false positives?
body: <p>The documentation for the Valgrind thread error detection tool Helgrind, found <a href="http://valgrind.org/docs/manual/hg-manual.html" rel="noreferrer">here</a></p>
  
  <p>warns that, if you use GCC to compile your OpenMP code, GCC's OpenMP runtime library (<strong>libgomp.so</strong>) will cause a chaos of false positive reports of data races, because of its use of atomic machine instructions and Linux futex system calls instead of POSIX pthreads primitives.  It tells you that you can solve this problem, however, by recompiling GCC with the <code>--disable-linux-futex</code> configuration option.</p>
  
  <p>So I tried this.  I compiled and installed to a local directory (<strong>~/GCC_Valgrind/gcc_install</strong>) a new GCC version 4.7.0 (the latest release as of this writing) with the <code>--disable-linux-futex</code> configuration option.  I then created a small OpenMP test program (<strong>test1.c</strong>) that has no visible data races:</p>
  
  <pre><code>/* test1.c */
  
  #include &lt;omp.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define NUM_THREADS 2
  
  int a[NUM_THREADS];
  
  int main(void) {
          int i;
  #pragma omp parallel num_threads(NUM_THREADS)
          {
                  int tid = omp_get_thread_num();
                  a[tid] = tid + 1;
          }
          for (i = 0; i &lt; NUM_THREADS; i++)
                  printf("%d ", a[i]);
          printf("\n");
          return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I compiled this program as follows</p>
  
  <pre><code>~/GCC_Valgrind/gcc_install/bin/gcc -Wall -fopenmp  -static -L~/GCC_Valgrind/gcc_install/lib64 -L~/GCC_Valgrind/gcc_install/lib -o test1 test1.c
  </code></pre>
  
  <p>However, I got 30 false positive data race reports!--all occurring in <strong>libgomp</strong> code.  I then compiled <strong>test1.c</strong> without the <code>-static</code> flag, and ran Helgrind on it again.  This time, I got only 9 false positive data race reports, but that is still too many--and, without the <code>-static</code> flag, I cannot trace the supposed race in the <strong>libgomp</strong> code.</p>
  
  <p>Has anybody found a way to reduce, if not eliminate, the number of false positive data race reports from Helgrind applied to an OpenMP program compiled with GCC?  Thanks!</p>