Re: calling Fortran code from Lisp

Raymond Toy <[email protected]>
Newsgroups gmane.lisp.cmucl.general
Message-ID <[email protected]>
Tamas K Papp wrote:
> On Fri, Apr 06, 2007 at 05:11:27PM -0400, Raymond Toy wrote:
>   
>>>>>>> "Tamas" == Tamas K Papp <Tamas> writes:
>>>>>>>               
>>     Tamas> Hi,
>>     Tamas> Is it possible to call a Fortran subroutine from Lisp?  I have seen
>>     Tamas> the C language FFI in the docs, but I wonder if it would be possible
>>     Tamas> to call Fortran somehow.  There is a subroutine for banded sparse
>>     Tamas> matrices in LAPACK I would like to use.
>>
>> As mentioned by others, there are already libraries that can do this.
>> If you want to cook your own, you do need to exercise some care.  All
>> Fortran compilers I know of have, essentially, a C-compatible
>> interface.  The only thing you need to know about is how Fortran
>> handles strings.  
>>
>> If the existing libraries don't do what you want, please ask again
>> here, and we can help with setting up a call to a Fortran routine.
>>     
>
> Hi Raymond,
>
> First, a short explanation why I am not using the existing libraries:
> I need the ability to reshape a more than 2 dimensional array into a
> matrix, do some matrix manipulation (eg matrix product, solving linear
> system), reshape the result into an array, etc.  I don't know how to
> do this in either Matlisp or rfi's libraries.
>
>   
Matlisp only handles 2-D matrices.  You can mix and match Lisp arrays 
and Matlisp matrices, with some care.
> I tried calling dgemm to do a simple matrix multiplication first.
> Fortunately Atlas has a C interface, so I can use that.  But I got
> stuck because I don't know how to extract the pointer of a simple
> array, the sys:vector-sap mentioned in the user's guide doesn't work
> here.  What should I use instead?
>
>   
vector-sap only works on specialized arrays.  For other array types, you 
need to get to the actual storage area that hold the data.  
(%array-data? or with-array-data?)  Once you have that object, 
vector-sap will do what you want.
> If you have any advice on what to do, or how to improve this code in
> other ways, please tell me.
>
>
>   
[snip code]

What happens when you run this code?  I can't easily run this myself 
right now.
One note, however.  LAPACK matrices are in column-major order but Lisp 
is row-major order.  You might need to transpose your matrices before 
calling LAPACK routines.  This is why Matlisp stores its matrices as a 
1D vector internally.  Perhaps the C interface doesn't need this.

Ray
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.