Bug 5847 – Threads started by core.thread should have same floating point state as main thread

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-04-16T11:28:00Z
Last change time
2015-06-09T05:15:05Z
Assigned to
nobody
Creator
dsimcha

Comments

Comment #0 by dsimcha — 2011-04-16T11:28:58Z
The following example code runs the same floating point function in two threads (though not concurrently). The answer produced by each thread is different in the low order bits on Windows: import std.algorithm, core.thread, std.stdio, core.stdc.fenv; real sumRange(const(real)[] range) { writeln("Rounding mode: ", fegetround); // 0 from both threads. return reduce!"a + b"(range); } void main() { immutable n = 1_000_000; immutable delta = 1.0 / n; auto terms = new real[1_000_000]; foreach(i, ref term; terms) { immutable x = ( i - 0.5 ) * delta; term = delta / ( 1.0 + x * x ) * 1; } immutable res1 = sumRange(terms); writefln("%.19f", res1); real res2; auto t = new Thread( { res2 = sumRange(terms); } ); t.start(); t.join(); writefln("%.19f", res2); } Output: Rounding mode: 0 0.7853986633972191094 Rounding mode: 0 0.7853986633972437348 If I change the new Thread statement to the following: auto t = new Thread( { asm { fninit; } res2 = sumRange(terms); } ); then both threads print the same answer. This needs fixing because, when performing floating point operations in parallel, it can lead to results that are non-deterministic and depend on how the work is scheduled.
Comment #1 by dsimcha — 2011-08-12T20:30:46Z
This was fixed for 2.053.