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---------------------