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.


Reply via email to