Calling Fortran from Julia (ii)

Now to call savgol() from Julia requires some information, namely what the interface to the subroutine looks like. The Fortran 90 code for the subroutine header looks something like this:

subroutine savgol(c,np,nl,nr,ld,m)
integer ld,m,nl,np,nr
real c(np)

It specifies 6 parameters, the first of which is an array of real numbers, and the remaining five of which are integer‘s. The variable c returns an array of coefficients (to be used for further processing). So the trick is to associate the parameters in savgol() with their associated input values in ccall(). This is what the call the ccall() looks like:

ccall((:savgol_, ""), Int32, (Ptr{Cfloat},Ptr{Cint},
       c, &d, &nl, &nr, &0, &m)

Recall that a call to ccall() has four arguments:

  1. A “function” – library pair. This is savgol_ and “”.
  2. A return typeInt32 in this case, but the subroutine does not return anything.
  3. A tuple of input values. In this case Ptr{Float}, etc.
  4. The arguments actually passed to savgol().

Note that unlike C, all Fortran arguments must be passed by reference, hence the use of Ptr{} on all the arguments. The tricky part was again linking the datatypes, but this really just means tracing the required Julia type which can be found here. The Fortran integer type meshes well with Cint, and the Fortran real type associates with Julia’s Cfloat.

There is a cornucopia of existing libraries out there in Fortran, especially for math… why not use them?


2 thoughts on “Calling Fortran from Julia (ii)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s