www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Debugging D shared libraries

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Calling D from Python. I have two functions in D, compiled to a shared
object on Linux using LDC (but I get same problem using DMD).

The sequential code:

    extern(C)
    double sequential(const int n, const double delta) {
      Runtime.initialize();
      const pi =3D 4.0 * delta * reduce!(
            (double t, int i){ immutable x =3D (i - 0.5) * delta; return t =
+ 1.0 / (1.0 + x * x); })(
            0.0, iota(1, n + 1));
      Runtime.terminate();
      return pi;
    }

works entirely fine. However the "parallel" code:

    extern(C)
    double parallel(const int n, const double delta) {
      Runtime.initialize();
      const pi =3D 4.0 * delta * taskPool.reduce!"a + b"(
          map!((int i){ immutable x =3D (i - 0.5) * delta; return 1.0 / (1.=
0 + x * x); })(iota(1, n + 1)));
      Runtime.terminate();
      return pi;
    }

causes an immediate segfault (with LDC and DMD.  I am assuming that the
problem is the lack of initialization of the std.parallelism module and
hence the use of taskPool is causing a problem. I am betting I am
missing something very simple about module initialization, and that
this is not actually a bug.

Anyone any proposals?

--=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
Sep 19 2015
next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
wrote:
 Calling D from Python. I have two functions in D, compiled to a 
 shared object on Linux using LDC (but I get same problem using 
 DMD).

 [...]
I heard it crashed during the talk. Bummer. I should really be there, seeing as I live about 15 mins away. If you get a chance to talk to Alex Bishop, don't be too harsh on D to him, I'm trying to convince him to make the switch :)
Sep 19 2015
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 11:07 +0000, John Colvin via Digitalmars-d-learn
wrote:
=20
[=E2=80=A6]
 I heard it crashed during the talk. Bummer. I should really be=20
 there, seeing as I live about 15 mins away. If you get a chance=20
 to talk to Alex Bishop, don't be too harsh on D to him, I'm=20
 trying to convince him to make the switch :)
With the input from ponce (*) I now have the code working. Now all is need is a time machine so as to redo the demo successfully. Doctor, where are you? Oh you are committed to be on BBC1 in two hours time. Get Alex to have a chat with me and I can show the demonstration working. (*) ponce is arguably not the most positive or constructive name to go by. =20 --=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
Sep 19 2015
parent reply ponce <contact gam3sfrommars.fr> writes:
On Saturday, 19 September 2015 at 16:32:18 UTC, Russel Winder 
wrote:
 (*) ponce is arguably not the most positive or constructive 
 name to go
 by.
Friend call me like this IRL since forever. It seems to be a swear word in english?
Sep 19 2015
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 16:41 +0000, ponce via Digitalmars-d-learn wrote:
=20
[=E2=80=A6]
 Friend call me like this IRL since forever.
=20
 It seems to be a swear word in english?
English and Spanish meanings of the word are very different. In UK (not sure about Canada, USA, Australia, New Zealand, South Africa,=E2=80=A6) it = is generally a somewhat derogatory term. --=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
Sep 19 2015
parent ponce <contact gam3sfrommars.fr> writes:
On Saturday, 19 September 2015 at 17:02:37 UTC, Russel Winder 
wrote:
 English and Spanish meanings of the word are very different. In 
 UK (not sure about Canada, USA, Australia, New Zealand, South 
 Africa,…) it is generally a somewhat derogatory term.
In French it means "to rub down with abrasive paper".
Sep 20 2015
prev sibling next sibling parent reply ponce <contact gam3sfrommars.fr> writes:
On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
wrote:
 Calling D from Python. I have two functions in D, compiled to a 
 shared object on Linux using LDC (but I get same problem using 
 DMD).

 The sequential code:

     extern(C)
     double sequential(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * reduce!(
             (double t, int i){ immutable x = (i - 0.5) * delta; 
 return t + 1.0 / (1.0 + x * x); })(
             0.0, iota(1, n + 1));
       Runtime.terminate();
       return pi;
     }

 works entirely fine. However the "parallel" code:

     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x = (i - 0.5) * delta; return 
 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }

 causes an immediate segfault (with LDC and DMD.  I am assuming 
 that the problem is the lack of initialization of the 
 std.parallelism module and hence the use of taskPool is causing 
 a problem. I am betting I am missing something very simple 
 about module initialization, and that this is not actually a 
 bug.

 Anyone any proposals?
Try using an explicit TaskPool and destroying it with scope(exit). Also if using LDC, you can use global ctor/dtor to deal with the runtime. ----------------------->8--------------------- extern (C) { pragma(LDC_global_crt_ctor, 0) void initRuntime() { import core.runtime; Runtime.initialize(); } pragma(LDC_global_crt_dtor, 0) void deinitRuntime() { import core.runtime; Runtime.terminate(); } } ----------------------->8---------------------
Sep 19 2015
next sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 12:21 +0000, ponce via Digitalmars-d-learn wrote:
 [=E2=80=A6]
=20
 Try using an explicit TaskPool and destroying it with scope(exit).
=20
=20
 Also if using LDC, you can use global ctor/dtor to deal with the=20
 runtime.
=20
=20
 ----------------------->8---------------------
=20
         extern (C) {
              pragma(LDC_global_crt_ctor, 0)
              void initRuntime()
              {
                  import core.runtime;
                  Runtime.initialize();
              }
              pragma(LDC_global_crt_dtor, 0)
              void deinitRuntime()
              {
                  import core.runtime;
                  Runtime.terminate();
              }
          }
=20
 ----------------------->8---------------------
=20
Hummm=E2=80=A6 I now do not get a segfault, and the code runs as expected : -))))) but the program never terminates. :-( Also, what would I need to cover the DMD and the GDC situations? --=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
Sep 19 2015
parent reply ponce <contact gam3sfrommars.fr> writes:
On Saturday, 19 September 2015 at 15:42:15 UTC, Russel Winder 
wrote:
 Hummm… I now do not get a segfault, and the code runs as 
 expected :
 -))))) but the program never terminates. :-(
Where is it stuck?
 Also, what would I need to cover the DMD and the GDC situations?
I don't know. :(
Sep 19 2015
next sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 15:58 +0000, ponce via Digitalmars-d-learn wrote:
 On Saturday, 19 September 2015 at 15:42:15 UTC, Russel Winder=20
 wrote:
=20
 Hummm=E2=80=A6 I now do not get a segfault, and the code runs as=20
 expected :
 -))))) but the program never terminates. :-(
=20 Where is it stuck?
I commented out the : //pragma(LDC_global_crt_dtor, 0) //void deinitRuntime() { //import core.runtime: Runtime; //Runtime.terminate(); //} and it now works fine :-)))))
=20
 Also, what would I need to cover the DMD and the GDC situations?
=20 I don't know. :(
Let's hope DMD and GDC folk chip in :-) Sadly the: pragma(LDC_global_crt_ctor, 0) void initRuntime() { import core.runtime: Runtime; Runtime.initialize(); } will not compile under DMD :-( --=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
Sep 19 2015
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 19 September 2015 at 16:15:45 UTC, Russel Winder 
wrote:
 Sadly the:

  pragma(LDC_global_crt_ctor, 0)
     void initRuntime() {
       import core.runtime: Runtime;
       Runtime.initialize();
    }

 will not compile under DMD :-(
version(LDC){ /* ... */ } not that it helps make things work correctly, but at least they'll compile :)
Sep 19 2015
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 16:33 +0000, John Colvin via Digitalmars-d-learn
wrote:
 On Saturday, 19 September 2015 at 16:15:45 UTC, Russel Winder=20
 wrote:
 Sadly the:
=20
  pragma(LDC_global_crt_ctor, 0)
     void initRuntime() {
       import core.runtime: Runtime;
       Runtime.initialize();
    }
=20
 will not compile under DMD :-(
=20 version(LDC){ /* ... */ } =20 not that it helps make things work correctly, but at least=20 they'll compile :)
Indeed, it works well. Well for LDC. DMD and GDC are still broken. My GDC problems are deeper that this code: Debian packages seem to have weird problems and Fedora do not package GDC. --=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
Sep 19 2015
next sibling parent Martin Krejcirik <mk-junk i-line.cz> writes:
Dne 19. 9. 2015 v 18:41 Russel Winder via Digitalmars-d-learn napsal(a):
 Indeed, it works well. Well for LDC. DMD and GDC are still broken. My
 GDC problems are deeper that this code: Debian packages seem to have
 weird problems and Fedora do not package GDC.
All I need to do to make your example work, is to move the calls of Runtime.initialize and Runtime.terminate to a wrapper functions called at the start and end of the main program. -- mk
Sep 20 2015
prev sibling parent reply Johannes Pfau <nospam example.com> writes:
Am Sat, 19 Sep 2015 17:41:41 +0100
schrieb Russel Winder via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com>:

 On Sat, 2015-09-19 at 16:33 +0000, John Colvin via Digitalmars-d-learn
 wrote:
 On Saturday, 19 September 2015 at 16:15:45 UTC, Russel Winder 
 wrote:
 Sadly the:
 
  pragma(LDC_global_crt_ctor, 0)
     void initRuntime() {
       import core.runtime: Runtime;
       Runtime.initialize();
    }
 
 will not compile under DMD :-(
version(LDC){ /* ... */ } not that it helps make things work correctly, but at least they'll compile :)
Indeed, it works well. Well for LDC. DMD and GDC are still broken. My GDC problems are deeper that this code: Debian packages seem to have weird problems and Fedora do not package GDC.
Have you tried using a newer GDC version? The debian jessie version probably uses the 2.064.2 frontend. I wanted to add attribute(cctor/cdtor) support for some time now, I even wrote the code some time but didn't push it to the main repo for some reason. I'll put it on the TODO list but I can't work on this for the next 2-3 weeks.
Sep 20 2015
next sibling parent reply Johannes Pfau <nospam example.com> writes:
Am Sun, 20 Sep 2015 17:47:00 +0200
schrieb Johannes Pfau <nospam example.com>:

 Am Sat, 19 Sep 2015 17:41:41 +0100
 schrieb Russel Winder via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com>:
 
 On Sat, 2015-09-19 at 16:33 +0000, John Colvin via
 Digitalmars-d-learn wrote:
 On Saturday, 19 September 2015 at 16:15:45 UTC, Russel Winder 
 wrote:
 Sadly the:
 
  pragma(LDC_global_crt_ctor, 0)
     void initRuntime() {
       import core.runtime: Runtime;
       Runtime.initialize();
    }
 
 will not compile under DMD :-(
version(LDC){ /* ... */ } not that it helps make things work correctly, but at least they'll compile :)
Indeed, it works well. Well for LDC. DMD and GDC are still broken. My GDC problems are deeper that this code: Debian packages seem to have weird problems and Fedora do not package GDC.
Have you tried using a newer GDC version? The debian jessie version probably uses the 2.064.2 frontend. I wanted to add attribute(cctor/cdtor) support for some time now, I even wrote the code some time but didn't push it to the main repo for some reason. I'll put it on the TODO list but I can't work on this for the next 2-3 weeks.
Just realized this thread is titled "Debugging D shared libraries" ;-) GDC does not yet support shared libraries.
Sep 20 2015
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 2015-09-20 at 17:49 +0200, Johannes Pfau via Digitalmars-d
-learn wrote:
 [=E2=80=A6]
=20
 Just realized this thread is titled "Debugging D shared libraries" ;
 -)
 GDC does not yet support shared libraries.
Conversely I thought it did due to the GCC toolchain thingy. I'm using DMD and LDC just now so I have no actual data. --=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
Sep 22 2015
prev sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via Digitalmars-d
-learn wrote:
 Am Sat, 19 Sep 2015 17:41:41 +0100
=20
[=E2=80=A6]
=20
 Have you tried using a newer GDC version? The debian jessie version
 probably uses the 2.064.2 frontend.
Debian Jessie is far too out of date to be useful. I'm on Debian Sid (still quite old), and Fedora Rawhide (not quite so old). Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well tell Iain which DMD is being used, but I haven't a clue. :-) [=E2=80=A6] The real problem using GDC is: gdc -I. -O3 -fPIC -c -o processAll_library_d.o processAll_library_d.d /usr/include/d/core/stdc/config.d:28:3: error: static if conditional cannot be at global scope static if( (void*).sizeof > int.sizeof ) ^ I haven't had chance to sit down and see if this is reasonable or not. --=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
Sep 22 2015
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 22 September 2015 at 14:37:11 UTC, Russel Winder 
wrote:
 On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via 
 Digitalmars-d -learn wrote:
 [...]
[…]
 [...]
Debian Jessie is far too out of date to be useful. I'm on Debian Sid (still quite old), and Fedora Rawhide (not quite so old). Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well tell Iain which DMD is being used, but I haven't a clue. :-) […] The real problem using GDC is: gdc -I. -O3 -fPIC -c -o processAll_library_d.o processAll_library_d.d /usr/include/d/core/stdc/config.d:28:3: error: static if conditional cannot be at global scope static if( (void*).sizeof > int.sizeof ) ^ I haven't had chance to sit down and see if this is reasonable or not.
seeing as it's an error in core.stdc.config, I'd say it's definitely not reasonable
Sep 22 2015
parent Johannes Pfau <nospam example.com> writes:
Am Tue, 22 Sep 2015 14:40:43 +0000
schrieb John Colvin <john.loughran.colvin gmail.com>:

 On Tuesday, 22 September 2015 at 14:37:11 UTC, Russel Winder=20
 wrote:
 On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via=20
 Digitalmars-d -learn wrote:
 [...]
[=E2=80=A6]
 [...]
Debian Jessie is far too out of date to be useful. I'm on=20 Debian Sid (still quite old), and Fedora Rawhide (not quite so old). Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well=20 tell Iain which DMD is being used, but I haven't a clue. :-) [=E2=80=A6] The real problem using GDC is: gdc -I. -O3 -fPIC -c -o processAll_library_d.o=20 processAll_library_d.d /usr/include/d/core/stdc/config.d:28:3: error: static if=20 conditional cannot be at global scope static if( (void*).sizeof > int.sizeof ) ^ I haven't had chance to sit down and see if this is reasonable=20 or not.
=20 seeing as it's an error in core.stdc.config, I'd say it's=20 definitely not reasonable
It's indeed strange. =20 Russel Winder if you can reproduce this with the latest GDC* please file a bug report. Even the error message doesn't make sense: static if works fine at global scope, AFAIK. * you could use the latest binaries from http://gdcproject.org/downloads
Sep 22 2015
prev sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 17:15 +0100, Russel Winder wrote:
=20
[=E2=80=A6]
 Sadly the:
=20
  pragma(LDC_global_crt_ctor, 0)
     void initRuntime() {
       import core.runtime: Runtime;
       Runtime.initialize();
    }
=20
 will not compile under DMD :-(
On the otherhand using a: version(LDC) { =E2=80=A6 } solves the problem. --=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
Sep 19 2015
prev sibling parent reply Laeeth Isharc <laeethnospam nospam.laeeth.com> writes:
On Saturday, 19 September 2015 at 12:21:02 UTC, ponce wrote:
 On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
 wrote:
 Calling D from Python. I have two functions in D, compiled to 
 a shared object on Linux using LDC (but I get same problem 
 using DMD).

 The sequential code:

     extern(C)
     double sequential(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * reduce!(
             (double t, int i){ immutable x = (i - 0.5) * 
 delta; return t + 1.0 / (1.0 + x * x); })(
             0.0, iota(1, n + 1));
       Runtime.terminate();
       return pi;
     }

 works entirely fine. However the "parallel" code:

     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x = (i - 0.5) * delta; 
 return 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }

 causes an immediate segfault (with LDC and DMD.  I am assuming 
 that the problem is the lack of initialization of the 
 std.parallelism module and hence the use of taskPool is 
 causing a problem. I am betting I am missing something very 
 simple about module initialization, and that this is not 
 actually a bug.

 Anyone any proposals?
Try using an explicit TaskPool and destroying it with scope(exit). Also if using LDC, you can use global ctor/dtor to deal with the runtime. ----------------------->8--------------------- extern (C) { pragma(LDC_global_crt_ctor, 0) void initRuntime() { import core.runtime; Runtime.initialize(); } pragma(LDC_global_crt_dtor, 0) void deinitRuntime() { import core.runtime; Runtime.terminate(); } } ----------------------->8---------------------
What is the difference between shared static this and the global constructor ? Russell, if you use shared static this for dmd does it work ? Laeeth.
Sep 19 2015
next sibling parent ponce <contact gam3sfrommars.fr> writes:
On Saturday, 19 September 2015 at 16:25:28 UTC, Laeeth Isharc 
wrote:
 What is the difference between shared static this and the 
 global constructor ?  Russell, if you use shared static this 
 for dmd does it work ?  Laeeth.
Would like to know too. On OSX I've found that shared static this() was called by Runtime.initialize(), maybe it's different on Linux.
Sep 19 2015
prev sibling next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 19 September 2015 at 16:25:28 UTC, Laeeth Isharc 
wrote:
 On Saturday, 19 September 2015 at 12:21:02 UTC, ponce wrote:
 [...]
What is the difference between shared static this and the global constructor ? Russell, if you use shared static this for dmd does it work ? Laeeth.
IIRC shared static this is only called by the runtime, so obviously you can't use it to initialise the runtime itself.
Sep 19 2015
parent Laeeth Isharc <laeethnospam nospam.laeeth.com> writes:
On Saturday, 19 September 2015 at 16:34:05 UTC, John Colvin wrote:
 On Saturday, 19 September 2015 at 16:25:28 UTC, Laeeth Isharc 
 wrote:
 On Saturday, 19 September 2015 at 12:21:02 UTC, ponce wrote:
 [...]
What is the difference between shared static this and the global constructor ? Russell, if you use shared static this for dmd does it work ? Laeeth.
IIRC shared static this is only called by the runtime, so obviously you can't use it to initialise the runtime itself.
Aha... Is there an equivalent in dmd to the global constructor ?
Sep 19 2015
prev sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-09-19 at 16:25 +0000, Laeeth Isharc via Digitalmars-d
-learn wrote:
=20
[=E2=80=A6]
 What is the difference between shared static this and the global=20
 constructor ?  Russell, if you use shared static this for dmd=20
 does it work ?  Laeeth.
I had no idea what to put in a: shared static this { =E2=80=A6 } so I don't have one. --=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
Sep 19 2015
prev sibling next sibling parent reply Laeeth Isharc <laeethnospam nospam.laeeth.com> writes:
On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
wrote:
 Calling D from Python. I have two functions in D, compiled to a 
 shared object on Linux using LDC (but I get same problem using 
 DMD).

 The sequential code:

     extern(C)
     double sequential(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * reduce!(
             (double t, int i){ immutable x = (i - 0.5) * delta; 
 return t + 1.0 / (1.0 + x * x); })(
             0.0, iota(1, n + 1));
       Runtime.terminate();
       return pi;
     }

 works entirely fine. However the "parallel" code:

     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x = (i - 0.5) * delta; return 
 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }

 causes an immediate segfault (with LDC and DMD.  I am assuming 
 that the problem is the lack of initialization of the 
 std.parallelism module and hence the use of taskPool is causing 
 a problem. I am betting I am missing something very simple 
 about module initialization, and that this is not actually a 
 bug.

 Anyone any proposals?
Btw have you looked at Colvin's prettypyd ? It's a nicer way to wrap things. Just pdef!() before functions, aggregates and fields to wrap them. For demos, I should also think that showing Python code in one Jupyter cell calling D code in another is a pretty nice way to show interop. Just need to install the pyd Magic. Your D code can import dub libraries too.
Sep 19 2015
parent Laeeth Isharc <spamnolaeeth nospamlaeeth.com> writes:
On Saturday, 19 September 2015 at 17:41:39 UTC, Laeeth Isharc 
wrote:
 On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
 wrote:
 Calling D from Python. I have two functions in D, compiled to 
 a shared object on Linux using LDC (but I get same problem 
 using DMD).

 The sequential code:

     extern(C)
     double sequential(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * reduce!(
             (double t, int i){ immutable x = (i - 0.5) * 
 delta; return t + 1.0 / (1.0 + x * x); })(
             0.0, iota(1, n + 1));
       Runtime.terminate();
       return pi;
     }

 works entirely fine. However the "parallel" code:

     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x = (i - 0.5) * delta; 
 return 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }

 causes an immediate segfault (with LDC and DMD.  I am assuming 
 that the problem is the lack of initialization of the 
 std.parallelism module and hence the use of taskPool is 
 causing a problem. I am betting I am missing something very 
 simple about module initialization, and that this is not 
 actually a bug.

 Anyone any proposals?
Btw have you looked at Colvin's prettypyd ? It's a nicer way to wrap things. Just pdef!() before functions, aggregates and fields to wrap them. For demos, I should also think that showing Python code in one Jupyter cell calling D code in another is a pretty nice way to show interop. Just need to install the pyd Magic. Your D code can import dub libraries too.
In particular it should just work that way as PyD should deal with runtime initialization and the like. https://github.com/DlangScience/PydMagic https://github.com/DlangScience/PydMagic/blob/master/examples/test.ipynb https://github.com/John-Colvin/ppyd
Sep 19 2015
prev sibling parent reply rom <monfollet.romain hotmail.fr> writes:
On Saturday, 19 September 2015 at 10:45:22 UTC, Russel Winder 
wrote:
 Calling D from Python. I have two functions in D, compiled to a 
 shared object on Linux using LDC (but I get same problem using 
 DMD).

 The sequential code:

     extern(C)
     double sequential(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * reduce!(
             (double t, int i){ immutable x = (i - 0.5) * delta; 
 return t + 1.0 / (1.0 + x * x); })(
             0.0, iota(1, n + 1));
       Runtime.terminate();
       return pi;
     }

 works entirely fine. However the "parallel" code:

     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi = 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x = (i - 0.5) * delta; return 
 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }

 causes an immediate segfault (with LDC and DMD.  I am assuming 
 that the problem is the lack of initialization of the 
 std.parallelism module and hence the use of taskPool is causing 
 a problem. I am betting I am missing something very simple 
 about module initialization, and that this is not actually a 
 bug.

 Anyone any proposals?
Isn't it simply that when doing some work asynchronously, as with task pool, work is being done in other threads than the main. All the while, you're killing the Runtime. The fact that when removing Runtime.terminate makes the code work might support that explanation.
Sep 20 2015
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 2015-09-20 at 07:49 +0000, rom via Digitalmars-d-learn wrote:
=20
[=E2=80=A6]
 works entirely fine. However the "parallel" code:
=20
     extern(C)
     double parallel(const int n, const double delta) {
       Runtime.initialize();
       const pi =3D 4.0 * delta * taskPool.reduce!"a + b"(
           map!((int i){ immutable x =3D (i - 0.5) * delta; return=20
 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
       Runtime.terminate();
       return pi;
     }
=20
[=E2=80=A6]
=20
 Isn't it simply that when doing some work asynchronously, as with=20
 task pool, work is being done in other threads than the main. All=20
 the while, you're killing the Runtime. The fact that when=20
 removing  Runtime.terminate makes the code work might support=20
 that explanation.
I am not sure that can be the case per se as reduce only uses threads internally, the task pool operations should be complete by the time pi is initialized. On the other hand there might be a lock contention between a quiescent default task pool and the terminate code. The code is now moved on a bit from the above, and it works sufficient for the task at hand. This isn't production code so it doesn't have to be quite as good and correct as maybe it should be. =20 --=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
Sep 22 2015