www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Call C (or C++) with pointer to static function

reply Michael Stover <michael.r.stover gmail.com> writes:
--001636c933da214bb2049248bec8
Content-Type: text/plain; charset=ISO-8859-1

I am wondering if D would be a good choice for our differential equation
modeling app.  What it needs to be able to do is dynamically compile D code
into a static function that can then be used to call C-code that expects a
pointer to a static function.

Longer description:
It is a desktop app.  The user writes equations.  I'd like to be translate
the equations into D code, compile it, and then call C code that represents
optimizing or solver procedures.  These functions expect a pointer to a
static function that calculates the values of the equations, which would be
the D code just compiled.

Is this feasible with D2?

Thanks,
Michael Stover

--001636c933da214bb2049248bec8
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I am wondering if D would be a good choice for our differential equation mo=
deling app. =A0What it needs to be able to do is dynamically compile D code=
 into a static function that can then be used to call C-code that expects a=
 pointer to a static function. =A0<br>
<div><br></div><div>Longer description:</div><div>It is a desktop app. =A0T=
he user writes equations. =A0I&#39;d like to be translate the equations int=
o D code, compile it, and then call C code that represents optimizing or so=
lver procedures. =A0These functions expect a pointer to a static function t=
hat calculates the values of the equations, which would be the D code just =
compiled.=A0</div>
<div><br></div><div>Is this feasible with D2?</div><div><br></div><div>Than=
ks,</div><div>Michael Stover</div>

--001636c933da214bb2049248bec8--
Oct 10 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 10 Oct 2010 22:03:51 +0200, Michael Stover  
<michael.r.stover gmail.com> wrote:

 I am wondering if D would be a good choice for our differential equation
 modeling app.  What it needs to be able to do is dynamically compile D  
 code
 into a static function that can then be used to call C-code that expects  
 a
 pointer to a static function.

 Longer description:
 It is a desktop app.  The user writes equations.  I'd like to be  
 translate
 the equations into D code, compile it, and then call C code that  
 represents
 optimizing or solver procedures.  These functions expect a pointer to a
 static function that calculates the values of the equations, which would  
 be
 the D code just compiled.

 Is this feasible with D2?

Absolutely. The dynamic compilation part is not directly supported, but possible. There are several ways of accomplishing this, from Burton Radon's (outdated, proof-of-concept only) dynamic compilation[1], to calling the C functions in the generated D code, and pipe the output where you need it. Interfacing to C is detailed on this page: http://digitalmars.com/d/2.0/interfaceToC.html And can mostly be summarized like this: Create a D function that is callable from C: extern( C ) returnType myFunc( /* args */ ) {} Refer to a C function that is callable from D: extern( C ) returnType myFunc( /* args */ ); [1]: http://members.shaw.ca/burton-radons/The%20Joy%20and%20Gibbering%20Terror%20of%20Custom-Loading%20Executables.html -- Simen
Oct 10 2010
prev sibling next sibling parent Michael Stover <michael.r.stover gmail.com> writes:
--0022150488a7006fcd04924c7907
Content-Type: text/plain; charset=ISO-8859-1

I am under the impression that passing a pointer to a static function (ie,
passing a pointer to a memory address that contains the starting point of a
function) is not the same as defining a method that can be called by name
from C code.  I am not a C expert though.

Can one call fortran libraries from D?  Ie, say one wanted to use LAPACK
from D?

Also, know of any mathematical libraries written in D (like implementations
of runge-kutta, linear algebra routines and data structures, optimization or
curve-fitting algorithms)?

-Mike



On Sun, Oct 10, 2010 at 5:10 PM, Simen kjaeraas <simen.kjaras gmail.com>wrote:

 On Sun, 10 Oct 2010 22:03:51 +0200, Michael Stover <
 michael.r.stover gmail.com> wrote:

  I am wondering if D would be a good choice for our differential equation
 modeling app.  What it needs to be able to do is dynamically compile D
 code
 into a static function that can then be used to call C-code that expects a
 pointer to a static function.

 Longer description:
 It is a desktop app.  The user writes equations.  I'd like to be translate
 the equations into D code, compile it, and then call C code that
 represents
 optimizing or solver procedures.  These functions expect a pointer to a
 static function that calculates the values of the equations, which would
 be
 the D code just compiled.

 Is this feasible with D2?

Absolutely. The dynamic compilation part is not directly supported, but possible. There are several ways of accomplishing this, from Burton Radon's (outdated, proof-of-concept only) dynamic compilation[1], to calling the C functions in the generated D code, and pipe the output where you need it. Interfacing to C is detailed on this page: http://digitalmars.com/d/2.0/interfaceToC.html And can mostly be summarized like this: Create a D function that is callable from C: extern( C ) returnType myFunc( /* args */ ) {} Refer to a C function that is callable from D: extern( C ) returnType myFunc( /* args */ ); [1]: http://members.shaw.ca/burton-radons/The%20Joy%20and%20Gibbering%20Terror%20of%20Custom-Loading%20Executables.html -- Simen

--0022150488a7006fcd04924c7907 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I am under the impression that passing a pointer to a static function (ie, = passing a pointer to a memory address that contains the starting point of a= function) is not the same as defining a method that can be called by name = from C code. =A0I am not a C expert though.<div> <br></div><div>Can one call fortran libraries from D? =A0Ie, say one wanted= to use LAPACK from D?</div><div><br></div><div>Also, know of any mathemati= cal libraries written in D (like implementations of runge-kutta, linear alg= ebra routines and data structures, optimization or curve-fitting algorithms= )?</div> <div><br></div><div>-Mike<br><div><div> <div><font face=3D"&#39;courier new&#39;, monospace"><br></font></div><div>= <font face=3D"&#39;courier new&#39;, monospace"><br></font></div><br><div c= lass=3D"gmail_quote">On Sun, Oct 10, 2010 at 5:10 PM, Simen kjaeraas <span = dir=3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com" target=3D"_blank"=
simen.kjaras gmail.com</a>&gt;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div><div></div><div>On Sun, 10 Oct 2010 22:= 03:51 +0200, Michael Stover &lt;<a href=3D"mailto:michael.r.stover gmail.co= m" target=3D"_blank">michael.r.stover gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I am wondering if D would be a good choice for our differential equation<br=

de<br> into a static function that can then be used to call C-code that expects a<= br> pointer to a static function.<br> <br> Longer description:<br> It is a desktop app. =A0The user writes equations. =A0I&#39;d like to be tr= anslate<br> the equations into D code, compile it, and then call C code that represents= <br> optimizing or solver procedures. =A0These functions expect a pointer to a<b= r> static function that calculates the values of the equations, which would be= <br> the D code just compiled.<br> <br> Is this feasible with D2?<br> </blockquote> <br></div></div> Absolutely. The dynamic compilation part is not directly supported, but<br> possible. There are several ways of accomplishing this, from Burton<br> Radon&#39;s (outdated, proof-of-concept only) dynamic compilation[1], to<br=

where you need it.<br> <br> Interfacing to C is detailed on this page:<br> <a href=3D"http://digitalmars.com/d/2.0/interfaceToC.html" target=3D"_blank= ">http://digitalmars.com/d/2.0/interfaceToC.html</a><br> <br> And can mostly be summarized like this:<br> <br> Create a D function that is callable from C:<br> extern( C ) returnType myFunc( /* args */ ) {}<br> <br> Refer to a C function that is callable from D:<br> extern( C ) returnType myFunc( /* args */ );<br> <br> <br> [1]: <a href=3D"http://members.shaw.ca/burton-radons/The%20Joy%20and%20Gibb= ering%20Terror%20of%20Custom-Loading%20Executables.html" target=3D"_blank">= http://members.shaw.ca/burton-radons/The%20Joy%20and%20Gibbering%20Terror%2= 0of%20Custom-Loading%20Executables.html</a><br> <br> -- <br><font color=3D"#888888"> Simen<br> </font></blockquote></div><br></div> </div></div> --0022150488a7006fcd04924c7907--
Oct 10 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Michael Stover <michael.r.stover gmail.com> wrote:

 I am under the impression that passing a pointer to a static function  
 (ie,
 passing a pointer to a memory address that contains the starting point  
 of a
 function) is not the same as defining a method that can be called by name
 from C code.  I am not a C expert though.

Well, yes and no. Given extern( C ) returnType myFunc( /* args */ ) {} You still have no function pointer to it. That is however easy to procure: someCfunctionThatExpectsAfnPointer( &myFunc ); It could be that there is some hidden detail of C static functions that I don't know, but i believe this works.
 Can one call fortran libraries from D?  Ie, say one wanted to use LAPACK
 from D?

http://dsource.org/projects/scid uses LAPACK, so I guess it is possible.
 Also, know of any mathematical libraries written in D (like  
 implementations
 of runge-kutta, linear algebra routines and data structures,  
 optimization or
 curve-fitting algorithms)?

At least some of this has been mentioned time and time again on the newsgroup, you should be able to find it by searching. From dsource.org/projects: http://dsource.org/projects/scid http://dsource.org/projects/dstats -- Simen
Oct 11 2010
prev sibling next sibling parent Michael Stover <michael.r.stover gmail.com> writes:
--0016e68dec3ba644a9049256bbff
Content-Type: text/plain; charset=ISO-8859-1

Thank you for helping me understand.

Mike

On Mon, Oct 11, 2010 at 8:39 AM, Simen kjaeraas <simen.kjaras gmail.com>wrote:

 Michael Stover <michael.r.stover gmail.com> wrote:

  I am under the impression that passing a pointer to a static function (ie,
 passing a pointer to a memory address that contains the starting point of
 a
 function) is not the same as defining a method that can be called by name
 from C code.  I am not a C expert though.

Well, yes and no. Given extern( C ) returnType myFunc( /* args */ ) {} You still have no function pointer to it. That is however easy to procure: someCfunctionThatExpectsAfnPointer( &myFunc ); It could be that there is some hidden detail of C static functions that I don't know, but i believe this works. Can one call fortran libraries from D? Ie, say one wanted to use LAPACK
 from D?

http://dsource.org/projects/scid uses LAPACK, so I guess it is possible. Also, know of any mathematical libraries written in D (like
 implementations
 of runge-kutta, linear algebra routines and data structures, optimization
 or
 curve-fitting algorithms)?

At least some of this has been mentioned time and time again on the newsgroup, you should be able to find it by searching. From dsource.org/projects: http://dsource.org/projects/scid http://dsource.org/projects/dstats -- Simen

--0016e68dec3ba644a9049256bbff Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you for helping me understand.<div><br></div><div>Mike<br><br><div cl= ass=3D"gmail_quote">On Mon, Oct 11, 2010 at 8:39 AM, Simen kjaeraas <span d= ir=3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com">simen.kjaras gmail= .com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im">Michael Stover &lt;<a hre= f=3D"mailto:michael.r.stover gmail.com" target=3D"_blank">michael.r.stover = gmail.com</a>&gt; wrote:<br> <br> </div><div class=3D"im"><blockquote class=3D"gmail_quote" style=3D"margin:0= 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> I am under the impression that passing a pointer to a static function (ie,<= br> passing a pointer to a memory address that contains the starting point of a= <br> function) is not the same as defining a method that can be called by name<b= r> from C code. =A0I am not a C expert though.<br> </blockquote> <br></div> Well, yes and no. Given<div class=3D"im"><br> <br> extern( C ) returnType myFunc( /* args */ ) {}<br> <br></div> You still have no function pointer to it. That is however easy to procure:<= br> <br> someCfunctionThatExpectsAfnPointer( &amp;myFunc );<br> <br> It could be that there is some hidden detail of C static functions that I<b= r> don&#39;t know, but i believe this works.<div class=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Can one call fortran libraries from D? =A0Ie, say one wanted to use LAPACK<= br> from D?<br> </blockquote> <br> </div><a href=3D"http://dsource.org/projects/scid" target=3D"_blank">http:/= /dsource.org/projects/scid</a> uses LAPACK, so I guess it is possible.<div = class=3D"im"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Also, know of any mathematical libraries written in D (like implementations= <br> of runge-kutta, linear algebra routines and data structures, optimization o= r<br> curve-fitting algorithms)?<br> </blockquote> <br></div> At least some of this has been mentioned time and time again on the<br> newsgroup, you should be able to find it by searching.<br> <br>
From <a href=3D"http://dsource.org/projects" target=3D"_blank">dsource.org/=

<br> <a href=3D"http://dsource.org/projects/scid" target=3D"_blank">http://dsour= ce.org/projects/scid</a><br> <a href=3D"http://dsource.org/projects/dstats" target=3D"_blank">http://dso= urce.org/projects/dstats</a><br> <br> -- <br><font color=3D"#888888"> Simen<br> </font></blockquote></div><br></div> --0016e68dec3ba644a9049256bbff--
Oct 11 2010
prev sibling next sibling parent Daniel Gibson <metalcaedes gmail.com> writes:
On Mon, Oct 11, 2010 at 2:30 AM, Michael Stover
<michael.r.stover gmail.com> wrote:
 I am under the impression that passing a pointer to a static function (ie=

 passing a pointer to a memory address that contains the starting point of=

 function) is not the same as defining a method that can be called by name
 from C code. =A0I am not a C expert though.
 Can one call fortran libraries from D? =A0Ie, say one wanted to use LAPAC=

 from D?
 Also, know of any mathematical libraries written in D (like implementatio=

 of runge-kutta, linear algebra routines and data structures, optimization=

 curve-fitting algorithms)?
 -Mike

See http://www.digitalmars.com/d/archives/digitalmars/D/Is_there_anybody_workin= g_on_a_linear_algebra_library_for_D2_118357.html :-)
Oct 11 2010
prev sibling next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Sun, 10 Oct 2010 20:30:52 -0400, Michael Stover wrote:
 
 Can one call fortran libraries from D?  Ie, say one wanted to use LAPACK
 from D?

Since it is possible in C, it should be possible in D. A quick search turned up this page: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html When it comes to LAPACK (and BLAS), Bill Baxter has already written D1 bindings. I've converted them to D2 and published them (with his kind permission) as part of SciD (see scid.bindings.*). Which brings me to your next question...
 Also, know of any mathematical libraries written in D (like
 implementations of runge-kutta, linear algebra routines and data
 structures, optimization or curve-fitting algorithms)?

Of the things you mention, SciD only contains a few linear algebra routines (see scid.linalg and the aforementioned BLAS/LAPACK bindings). There may be other things you'll find useful in there, though: http://www.dsource.org/projects/scid Also, check out this list (may not be up to date): http://www.prowiki.org/wiki4d/wiki.cgi?ScientificLibraries -Lats
Oct 11 2010
prev sibling parent Michael Stover <michael.r.stover gmail.com> writes:
--20cf304351442682a70492582bd4
Content-Type: text/plain; charset=ISO-8859-1

Thank you, I have checked them out.  Both SciD and dstats look useful.  We
would have to supplement with our own math libraries and bindings to various
C/Fortran libraries I think.   I am just exploring possibilities because of
the endless troubles we have with compiling and linking C++ code on Mac,
Linux, and Windows and thus why we've never compiled our math models (which
makes them really slow).  For estimation problems, it's just not workable
without compiling the model at runtime to native code.  It was my thought
that D would make the step easier and faster than C++.  But is does have to
work on Mac, Windows, and Linux user desktops.

-Mike

On Mon, Oct 11, 2010 at 9:56 AM, Lars T. Kyllingstad
<public kyllingen.nospamnet> wrote:

 On Sun, 10 Oct 2010 20:30:52 -0400, Michael Stover wrote:

 Can one call fortran libraries from D?  Ie, say one wanted to use LAPACK
 from D?

Since it is possible in C, it should be possible in D. A quick search turned up this page: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html When it comes to LAPACK (and BLAS), Bill Baxter has already written D1 bindings. I've converted them to D2 and published them (with his kind permission) as part of SciD (see scid.bindings.*). Which brings me to your next question...
 Also, know of any mathematical libraries written in D (like
 implementations of runge-kutta, linear algebra routines and data
 structures, optimization or curve-fitting algorithms)?

Of the things you mention, SciD only contains a few linear algebra routines (see scid.linalg and the aforementioned BLAS/LAPACK bindings). There may be other things you'll find useful in there, though: http://www.dsource.org/projects/scid Also, check out this list (may not be up to date): http://www.prowiki.org/wiki4d/wiki.cgi?ScientificLibraries -Lats

--20cf304351442682a70492582bd4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you, I have checked them out. =A0Both SciD and dstats look useful. = =A0We would have to supplement with our own math libraries and bindings to = various C/Fortran libraries I think. =A0 I am just exploring possibilities = because of the endless troubles we have with compiling and linking C++ code= on Mac, Linux, and Windows and thus why we&#39;ve never compiled our math = models (which makes them really slow). =A0For estimation problems, it&#39;s= just not workable without compiling the model at runtime to native code. = =A0It was my thought that D would make the step easier and faster than C++.= =A0But is does have to work on Mac, Windows, and Linux user desktops.<div> <br></div><div>-Mike<br><br><div class=3D"gmail_quote">On Mon, Oct 11, 2010= at 9:56 AM, Lars T. Kyllingstad <span dir=3D"ltr">&lt;public kyllingen.nos= pamnet&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"marg= in:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"im">On Sun, 10 Oct 2010 20:30:52 -0400, Michael Stover wrote:= <br> <br> &gt; Can one call fortran libraries from D? =A0Ie, say one wanted to use LA= PACK<br> &gt; from D?<br> <br> </div>Since it is possible in C, it should be possible in D. =A0A quick sea= rch<br> turned up this page:<br> <br> <a href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.= html" target=3D"_blank">http://www.yolinux.com/TUTORIALS/LinuxTutorialMixin= gFortranAndC.html</a><br> <br> When it comes to LAPACK (and BLAS), Bill Baxter has already written D1<br> bindings. =A0I&#39;ve converted them to D2 and published them (with his kin= d<br> permission) as part of SciD (see scid.bindings.*). =A0Which brings me to<br=

<div class=3D"im"><br> <br> &gt; Also, know of any mathematical libraries written in D (like<br> &gt; implementations of runge-kutta, linear algebra routines and data<br> &gt; structures, optimization or curve-fitting algorithms)?<br> <br> </div>Of the things you mention, SciD only contains a few linear algebra<br=

There may be other things you&#39;ll find useful in there, though:<br> <br> =A0<a href=3D"http://www.dsource.org/projects/scid" target=3D"_blank">http= ://www.dsource.org/projects/scid</a><br> <br> Also, check out this list (may not be up to date):<br> <br> =A0<a href=3D"http://www.prowiki.org/wiki4d/wiki.cgi?ScientificLibraries" = target=3D"_blank">http://www.prowiki.org/wiki4d/wiki.cgi?ScientificLibrarie= s</a><br> <br> <br> -Lats<br> </blockquote></div><br></div> --20cf304351442682a70492582bd4--
Oct 11 2010