MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using the compiled libraries (dll) outside mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117232] Re: Using the compiled libraries (dll) outside mathematica
  • From: Joel Klein <jfklein at wolfram.com>
  • Date: Sat, 12 Mar 2011 05:09:01 -0500 (EST)

On 3/5/2011 5:07 AM, Fonseca wrote:
> Hi,
>
> Is there a way to easily use the libraries compiled by mathematica, in
> another environment (I mean, not inside mathematica).
>
> I toke the simple example from the help (the one that squares a
> number), and compiled it to a dll. But I don't know how to call it's
> function, from, for example, a vb.net code (that I would latter
> transform into a xll code for integration into excel). Well, I know
> how I would do it with a "standard" dll (one created by me, with plain
> code), but with the mathematica generated one, I can't figure out the
> data types, and if there are any dependencies, etc.
>
> Thank you,
> P. Fonseca

There is a difference between libraries intended to be loaded into the 
Mathematica kernel ("Wolfram libraries") and libraries intended to be 
used standalone, that is apart from the Mathematica kernel.  It sounds 
like you're interested in standalone.

One trick that might help you is to use CCodeGenerator to view the C 
header and source generated from the compiled function.

First get a compiled function:
cf=Compile[{x},x^2];

Now ask CCodeGenerator to view the C header:
Needs["CCodeGenerator`"]
CCodeStringGenerate[cf, "f", "CodeTarget" -> "WolframRTLHeader"]

The signature in this case is:
EXTERN_C DLLEXPORT int f(WolframLibraryData libData, mreal A1, mreal  *Res);

You can also view the source code:
CCodeStringGenerate[cf, "f"]

To understand the types such as WolframLibraryData and mreal, refer to 
WolframLibrary.h, located in SystemFiles/IncludeFiles/C in your 
Mathematica layout.

While it's not specific to your case of vb.net, take a look at the 
Low-pass Filter example in the CCodeGenerator tutorial:

http://reference.wolfram.com/mathematica/CCodeGenerator/tutorial/Examples.html#509267359

(In the Mathematica Documentation center, the same example is found at
CCodeGenerator/tutorial/Examples#333777088.)

This example walks through the process of starting with a Compile 
expression and ending up with a standalone executable that calls it. It 
also includes possibly the part you're most interested in, the C main() 
function that calls the generated function.

You asked about dependences.  A standalone function like f above can be 
compiled into a shared or dynamic library, but it needs to be linked 
with the Wolfram Runtime Library (Wolfram RTL).  Mathematica provides 
both static and dynamic versions of the WolframRTL.  I would recommend 
using the static version of the WolframRTL since then you have no 
external dependencies.  For the best performance in general, if your end 
user has access to the Intel MKL, use the dynamic version of the 
WolframRTL.  This is the one without the word "Minimal" in its name. 
The version with the word "Minimal" does not depend on MKL.

-- 
Joel Klein
Kernel Developer, Wolfram Research, Inc.


  • Prev by Date: Re: what's new in 8.0.1?
  • Next by Date: $MaxNumber
  • Previous by thread: Using the compiled libraries (dll) outside mathematica
  • Next by thread: Re: Using the compiled libraries (dll) outside mathematica