Thread overview
Call C (or C++) with pointer to static function
Oct 10, 2010
Michael Stover
Oct 10, 2010
Simen kjaeraas
Oct 11, 2010
Michael Stover
Oct 11, 2010
Simen kjaeraas
Oct 11, 2010
Michael Stover
Oct 11, 2010
Michael Stover
Oct 11, 2010
Daniel Gibson
October 10, 2010
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


October 10, 2010
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
October 11, 2010
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
>


October 11, 2010
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
October 11, 2010
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
>


October 11, 2010
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 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
>

See
http://www.digitalmars.com/d/archives/digitalmars/D/Is_there_anybody_working_on_a_linear_algebra_library_for_D2_118357.html
:-)
October 11, 2010
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
October 11, 2010
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
>