thread-safe issue (gmp 6.0.0)

paul zimmermann Paul.Zimmermann at inria.fr
Tue Mar 24 10:30:21 UTC 2015


       Hi,

I hit a thread-safe issue in a code performing several large multiplications
at the same time, where valgrind --tool=helgrind reports:

==103189== Possible data race during write of size 8 at 0x52C2E90 by thread #4
==103189== Locks held: none
==103189==    at 0x507B293: __gmpn_fft_best_k (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x509B488: __gmpn_mulmod_bnm1 (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x507D706: __gmpn_nussbaumer_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x507CBEA: __gmpn_mul_n (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x50795AF: __gmpn_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x506CA9D: __gmpz_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x409F0B: calculateSqrtRat (sqrt.c:55)
==103189==    by 0x40C082: one_thread (sqrt.c:1219)
==103189==    by 0x4C2B5AD: mythread_wrapper (hg_intercepts.c:219)
==103189==    by 0x4E38B4F: start_thread (pthread_create.c:304)
==103189==    by 0x5B3E0EC: clone (clone.S:112)
==103189== 
==103189== This conflicts with a previous write of size 8 by thread #5
==103189== Locks held: none
==103189==    at 0x507B293: __gmpn_fft_best_k (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x509B99F: __gmpn_mulmod_bnm1_next_size (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x507D6AC: __gmpn_nussbaumer_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x5079AAE: __gmpn_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x506CA9D: __gmpz_mul (in /home/pzimmermann/lib/libgmp.so.10.2.0)
==103189==    by 0x409F0B: calculateSqrtRat (sqrt.c:55)
==103189==    by 0x40C082: one_thread (sqrt.c:1219)
==103189==    by 0x4C2B5AD: mythread_wrapper (hg_intercepts.c:219)

I suspect the following code, where FFT_TABLE_ATTRS is "static const":

int
mpn_fft_best_k (mp_size_t n, int sqr)
{
  FFT_TABLE_ATTRS struct fft_table_nk *fft_tab, *tab;
  mp_size_t tab_n, thres;
  int last_k;

  fft_tab = mpn_fft_table3[sqr];
  last_k = fft_tab->k;
  for (tab = fft_tab + 1; ; tab++)
    {
      tab_n = tab->n;
      thres = tab_n << last_k;
      if (n <= thres)
        break;
      last_k = tab->k;
    }
  return last_k;
}

If I replace FFT_TABLE_ATTRS by simply "const", valgrind --tool=helgrind
becomes happy.

Paul



More information about the gmp-bugs mailing list