/* * A D program to calculate Pi using quadrature as a parallel map algorithm. * * Copyright © 2010--2011 Russel Winder */ // std.parallelism is currently not in Phobos2, though it is being voted on for inclusion in Phobos2, so // ensure the compilation command takes care of all the factors to include the library. import std.algorithm ; import std.datetime ; import std.parallelism ; import std.range ; import std.stdio ; import std.typecons ; void execute ( immutable int numberOfTasks ) { immutable n = 1000000000 ; immutable delta = 1.0 / n ; StopWatch stopWatch ; stopWatch.start ( ) ; immutable sliceSize = n / numberOfTasks ; real getTerm(int i) { immutable x = ( i - 0.5 ) * delta; return delta / ( 1.0 + x * x ) ; } immutable pi = 4.0 * taskPool.reduce!"a + b"( std.algorithm.map!getTerm(iota(n)), sliceSize ); stopWatch.stop ( ) ; immutable elapseTime = stopWatch.peek ( ).hnsecs * 100e-9 ; writefln ( "==== D Parallel Map pi = %.18f" , pi ) ; writefln ( "==== D Parallel Map iteration count = %d" , n ) ; writefln ( "==== D Parallel Map elapse = %f" , elapseTime ) ; writefln ( "==== D Parallel Map task count = %d" , numberOfTasks ) ; } int main ( immutable string[] args ) { execute ( 1 ) ; writeln ( ) ; execute ( 2 ) ; writeln ( ) ; execute ( 8 ) ; writeln ( ) ; execute ( 32 ) ; return 0 ; }