www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Parallel example fron documentation does not compile

reply Ish <ratta1i cmich.edu> writes:
This code snippet is from: 
http://dlang.org/phobos/std_parallelism.html
---
import std.algorithm, std.parallelism, std.range;

void main() {
     // Parallel reduce can be combined with
     // std.algorithm.map to interesting effect.
     // The following example (thanks to Russel Winder)
     // calculates pi by quadrature  using
     // std.algorithm.map and TaskPool.reduce.
     // getTerm is evaluated in parallel as needed by
     // TaskPool.reduce.
     //
     // Timings on an Athlon 64 X2 dual core machine:
     //
     // TaskPool.reduce:       12.170 s
     // std.algorithm.reduce:  24.065 s

     immutable n = 1_000_000_000;
     immutable delta = 1.0 / n;

     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))
     );
}

dmd compiler gives error:
/usr/include/dmd/phobos/std/parallelism.d(2624): Error: function 
std.parallelism.TaskPool.reduce!"a + 
b".reduce!(MapResult!(getTerm, Result)).reduce cannot get frame 
pointer to D main

Is there way to compile it?

-Ish
Feb 19 2016
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Sadly the answer is no.

This is due to=C2=A0https://issues.dlang.org/show_bug.cgi?id=3D5710=C2=A0wh=
ich
apparently no-one is either willing or able to fix. The problem
afflicts dmd, l;dc2 and I suspect gdc.

cf.=C2=A0https://github.com/russel/Pi_Quadrature/tree/master/D

for some extra detail and some workarounds.

It appears I had not added this original version, but I am now fixing
this.

On Fri, 2016-02-19 at 21:57 +0000, Ish via Digitalmars-d-learn wrote:
 This code snippet is from:=C2=A0
 http://dlang.org/phobos/std_parallelism.html
 ---
 import std.algorithm, std.parallelism, std.range;
=20
 void main() {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// Parallel reduce can be combined with
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// std.algorithm.map to interesting effect.
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// The following example (thanks to Russel =
Winder)
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// calculates pi by quadrature=C2=A0=C2=A0u=
sing
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// std.algorithm.map and TaskPool.reduce.
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// getTerm is evaluated in parallel as need=
ed by
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// TaskPool.reduce.
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0//
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// Timings on an Athlon 64 X2 dual core mac=
hine:
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0//
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// TaskPool.reduce:=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A012.170 s
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// std.algorithm.reduce:=C2=A0=C2=A024.065 =
s
=20
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0immutable n =3D 1_000_000_000;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0immutable delta =3D 1.0 / n;
=20
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0real getTerm(int i)
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0{
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0immutable x =3D ( i=
- 0.5 ) * delta;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return delta / ( 1.=
0 + x * x ) ;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}
=20
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0immutable pi =3D 4.0 * taskPool.reduce!"a +=
b"(
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0std.algorithm.map!g=
etTerm(iota(n))
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0);
 }
=20
 dmd compiler gives error:
 /usr/include/dmd/phobos/std/parallelism.d(2624): Error: function=C2=A0
 std.parallelism.TaskPool.reduce!"a +=C2=A0
 b".reduce!(MapResult!(getTerm, Result)).reduce cannot get frame=C2=A0
 pointer to D main
=20
 Is there way to compile it?
=20
 -Ish
--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Feb 19 2016