[Issue 5847] New: Threads started by core.thread should have same floating point state as main thread

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Apr 16 11:32:33 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5847

           Summary: Threads started by core.thread should have same
                    floating point state as main thread
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: nobody at puremagic.com
        ReportedBy: dsimcha at yahoo.com


--- Comment #0 from David Simcha <dsimcha at yahoo.com> 2011-04-16 11:28:58 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list