Slow pi when using timer

The following code is my code for calculating pi = 3.1415 ... approximately using this formula:

use Time;
var timer = new Timer();

config const n = 10**9;
var x = 0.0, s = 0.0;

// timer.start();                                     // [1]_____

for k in 0 .. n {
    s = ( if k % 2 == 0 then 1.0 else -1.0 );  // (-1)^k
    x += s / ( 2.0 * k + 1.0 );
}

// timer.stop();                                      // [2]_____
// writeln( "time = ", timer.elapsed() );             // [3]_____

   writef( "pi (approx) = %30.20dr\n", x * 4 );
// writef( "pi (exact)  = %30.20dr\n", pi );          // [4]_____

When the above code is compiled as chpl --fast test.chpland executed as time ./a.out, it works with ~ 4 seconds as

pi (approx) =         3.14159265458805059268

real    0m4.334s
user    0m4.333s
sys     0m0.006s

On the other hand, if I uncomment the lines [1--3](for use Timer), the program runs much slower with ~ 10 seconds as

time = 10.2284
pi (approx) =         3.14159265458805059268

real    0m10.238s
user    0m10.219s
sys     0m0.018s

The same slowdown happens when I uncomment only the line [4](to print the built-in pi value when the lines [1-3]stayed in the know):

pi (approx) =         3.14159265458805059268
pi (exact)  =         3.14159265358979311600

real    0m10.144s
user    0m10.141s
sys     0m0.009s

So, I wonder why this slowdown happens ...

- (, Timer)?

- OSX10.11 + chapel-1.16, . :

$ printchplenv --anonymize
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: clang
CHPL_TARGET_ARCH: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_MAKE: make
CHPL_ATOMICS: intrinsics
CHPL_GMP: gmp
CHPL_HWLOC: hwloc
CHPL_REGEXP: re2
CHPL_WIDE_POINTERS: struct
CHPL_AUX_FILESYS: none

$ clang --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

, , this CHPL_TARGET_COMPILER=gnu ~/.chplconfig ( make). ~ 4 . , clang OSX10.11. , OSX ( >= 10.12) , OSX/clang ( >= 9.0). FYI, ( GNU) :

$ printchplenv --anonymize
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: gnu +
CHPL_TARGET_ARCH: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_MAKE: make
CHPL_ATOMICS: intrinsics
CHPL_GMP: none
CHPL_HWLOC: hwloc
CHPL_REGEXP: none
CHPL_WIDE_POINTERS: struct
CHPL_AUX_FILESYS: none
+4
2

- (, )?

, Timer ( Chapel) . ( ) , .

+4

[--fast] ,

, /,

( ), forall, Atomics .add(), , , [PAR] - (ref. thin [PAR] -gains v/s [SEQ] -).

.

use Time;
var timer = new Timer();

config const n = 10**9;
         var s = 0.0, x = 0.0;
         var AtomiX: atomic real;                           // [AtomiX]______
             AtomiX.write( 0.0 );                           // [AtomiX]______

timer.start();                                              // [1]_____

for k in 0 .. n {
    s  = ( if k % 2 == 0 then 1.0 else -1.0 );     // (-1)^k
    x += s / ( 2.0 * k + 1.0 );
}

/* forall k in 0..n { AtomiX.add( ( if k % 2 == 0 then 1.0 else -1.0 )
                                / ( 2.0 * k + 1.0 )
                                  ); } */                   // [AtomiX]______

timer.stop();                                               // [2]_____
writeln( "time = ", timer.elapsed() );                      // [3]_____

   writef( "pi (approx) = %30.20dr\n", 4 * x );    
// writef( "pi (approx) = %30.20dr\n", 4 * AtimiX.read() ); // [AtomiX]______
// writef( "pi (exact)  = %30.20dr\n", pi );                // [4]_____

/*
--------------------------------------------------- [--fast] // AN EMPTY RUN
time = 1e-06

Real time:  9.582 s
User time:  8.479 s
Sys. time:  0.591 s
CPU share: 94.65 %
Exit code: 0
--------------------------------------------------- [--fast] // all commented

pi (approx) =         3.14159265458805059268

Real time: 15.553 s
User time: 13.484 s ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~> Timer ~ +/- 1 second ( O/S noise )
Sys. time:  0.985 s
CPU share: 93.03 %
Exit code: 0
-------------------------------------------------- [--fast ] // Timer-un-commented
time = 5.30128
time = 5.3329
pi (approx) =         3.14159265458805059268

Real time: 14.356 s
User time: 13.047 s ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~< Timer ~ +/- 1 second ( O/S noise )
Sys. time:  0.585 s
CPU share: 94.95 %
Exit code: 0

Real time: 16.804 s
User time: 14.853 s
Sys. time:  0.925 s
CPU share: 93.89 %
Exit code: 0

-------------------------------------------------- [--fast] // Timer-un-commented + forall + Atomics

time = 14.7406
pi (approx) =         3.14159265458805680993

Real time: 28.099 s
User time: 26.246 s
Sys. time: 0.914 s
CPU share: 96.65 %
Exit code: 0
*/
+1

Source: https://habr.com/ru/post/1690041/


All Articles