public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception
@ 2014-12-15 18:32 zhouyan at me dot com
  2014-12-15 18:36 ` [Bug c++/64318] " zhouyan at me dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: zhouyan at me dot com @ 2014-12-15 18:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

            Bug ID: 64318
           Summary: Using _Cilk_for with <random> cause strange floating
                    point exception
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhouyan at me dot com

Below is a minimum example,

#include <random>

int main ()
{
    std::mt19937 eng;
    std::normal_distribution<> rnorm;
    _Cilk_for (int i = 0; i != 10000; ++ i)
        rnorm(eng);
}

It is compiled with the following,

$GCC_PATH/g++ -std=c++11 -fcilkplus -g -o test test.cpp $GCC_LDFLAGS

where $GCC_PATH expands to where I installed the trunk version of GCC and
$GCC_LDFLAGS is -L /opt/GCC/devel/lib/gcc/x86_64-redhat-linux-gnu/lib64 -L
/opt/GCC/devel/lib/gcc/x86_64-redhat-linux-gnu/5.0.0 -Wl,-rpath
-Wl,/opt/GCC/devel/lib/gcc/x86_64-redhat-linux-gnu/lib64 -Wl,-rpath
-Wl,/opt/GCC/devel/lib/gcc/x86_64-redhat-linux-gnu/5.0.0
(Basically it avoids the program to find the system libstdc++ etc., at runtime)

The GCC version is trunk, output of $GCC_PATH/g++ -v is
Using built-in specs.
COLLECT_GCC=/opt/GCC/devel/bin/g++
COLLECT_LTO_WRAPPER=/opt/GCC/devel/libexec/gcc/x86_64-redhat-linux-gnu/5.0.0/lto-wrapper
Target: x86_64-redhat-linux-gnu
Configured with: ../GCC-src/configure --build=x86_64-redhat-linux-gnu
--prefix=/opt/GCC/devel --disable-multilib --disable-nls --disable-werror
--enable-checking=release --enable-languages=c,c++,fortran
--enable-libstdcxx-time=yes --enable-lto --enable-stage1-checking
--enable-version-specific-runtime-libs --with-system-zlib
Thread model: posix
gcc version 5.0.0 20141215 (experimental) (GCC)

The problem does not always shows up, but run the program enough times (or
increase the loop count), sooner or later I encounter a floating point
exception. Run the program through a debug, whenever the crash happens, it
always happen in the following frame,
thread #2: (some verbose output omitted) at random.tcc:3473
   3470        =
std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
   3471                       __bits);
   3472          const long double __r = static_cast<long double>(__urng.max())
-> 3473                    - static_cast<long double>(__urng.min()) + 1.0L;
   3474          const size_t __log2r = std::log(__r) / std::log(2.0L);
   3475          size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) /
__log2r);
   3476          _RealType __sum = _RealType(0);

It since the cast from integer to long double cause the exception, which makes
no sense. (And replace the random number generating to doing just this cast,
the problem disappears)

The problem does not occur with optimized code, and it seems that it does not
occur with other <random> distributions. For example, the frame at issue
suggest it might be a problem in std::uniform_real_distribuiton ,which is used
by normal_distribution, but replace the distribution, the problem disappears.
Also, change _Cilk_for to normal for loop, there is no issue. The only thing I
can think of is that _Cilk here somehow corrupted stack data.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/64318] Using _Cilk_for with <random> cause strange floating point exception
  2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
@ 2014-12-15 18:36 ` zhouyan at me dot com
  2014-12-15 19:19 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: zhouyan at me dot com @ 2014-12-15 18:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

--- Comment #1 from zhouyan at me dot com ---
I forgot to mention that, the system is CentOS 7 (with all updates)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/64318] Using _Cilk_for with <random> cause strange floating point exception
  2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
  2014-12-15 18:36 ` [Bug c++/64318] " zhouyan at me dot com
@ 2014-12-15 19:19 ` jakub at gcc dot gnu.org
  2014-12-15 19:27 ` zhouyan at me dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-12-15 19:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |redi at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Are the std::mt19937 and std::normal_distribution<> classes meant to be
thread-safe?  I mean, in your testcase you access (supposedly read and write)
the same objects from multiple threads, so unless the standard says it is
required to work in a thread-safe, your testcase is racy.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/64318] Using _Cilk_for with <random> cause strange floating point exception
  2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
  2014-12-15 18:36 ` [Bug c++/64318] " zhouyan at me dot com
  2014-12-15 19:19 ` jakub at gcc dot gnu.org
@ 2014-12-15 19:27 ` zhouyan at me dot com
  2014-12-15 19:31 ` zhouyan at me dot com
  2014-12-15 19:35 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: zhouyan at me dot com @ 2014-12-15 19:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

--- Comment #3 from zhouyan at me dot com ---
Here is version that shall be thread-safe, that produce the same problem,

#include <random>

int main ()
{
    _Cilk_for (int i = 0; i != 10000; ++i) {
        std::mt19937 eng(i);
        std::normal_distribution<> rnorm;
        rnorm(eng);
    }
}


The original program which I first found the issue was thread-safe. The MWE I
first submitted was created a little too hasty. In fact, neither mt19937 nor
normal_distribution is thread-safe. Anyway, I don't think race condition was
the problem.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/64318] Using _Cilk_for with <random> cause strange floating point exception
  2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
                   ` (2 preceding siblings ...)
  2014-12-15 19:27 ` zhouyan at me dot com
@ 2014-12-15 19:31 ` zhouyan at me dot com
  2014-12-15 19:35 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: zhouyan at me dot com @ 2014-12-15 19:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

--- Comment #4 from zhouyan at me dot com ---
The new example can be unsafe, if the constructor of the two classes are
unsafe. However, I went through the source of <random> before (during 4.8, 4.9
release), unless something changed, it's not the case.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/64318] Using _Cilk_for with <random> cause strange floating point exception
  2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
                   ` (3 preceding siblings ...)
  2014-12-15 19:31 ` zhouyan at me dot com
@ 2014-12-15 19:35 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2014-12-15 19:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64318

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The original testcase does indeed have a data race. The revised testcase is
valid.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-12-15 19:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-15 18:32 [Bug c++/64318] New: Using _Cilk_for with <random> cause strange floating point exception zhouyan at me dot com
2014-12-15 18:36 ` [Bug c++/64318] " zhouyan at me dot com
2014-12-15 19:19 ` jakub at gcc dot gnu.org
2014-12-15 19:27 ` zhouyan at me dot com
2014-12-15 19:31 ` zhouyan at me dot com
2014-12-15 19:35 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).