Hello,
I am trying to link to a fortran 77 library. So far, I can call a subroutine without parameters, but fail otherwise. Here's where I'm at:
The fortran routine is very simple: accept arguments and dump them to disk. The following version accepts argument a, and writes a,b & i to disk. The initial version had no arguments, and a was set internaly. This initial version worked.
subroutine test_link (a)!a, b, i)
real a,b integer i
b=2.0 i=1 open (10, file='call_args.txt',status='replace') write (10,*) a, b, i close (10) return end
My cffi definitions are (the routine is in the amos library):
(define-foreign-library amos (:unix "libamos.so"))
(use-foreign-library amos)
(defctype test-link :int)
;; I got the trailing underscore by using `nm' on the library. (defcfun "test_link_" test-link (a :float)) ; (b :float) (i :int))
(test-link- 2.0)) ; 2.0 2.1 1)
I get partial success. A call to test-link- returns 0 (not sure what that means), but the file contents are incorrect: -3.5797695E+21 2.000000 1 i.e, instead of 2.0 I get -3.579e21.
I will be needing to call single and double reals and integers. I would also like eventually to pass single and double complex numbers.
I hear from people that linking to fortran is doable, but I have not seen simple examples.
Right now I am using gfortran. Eventually I will try intel fortran.
Thanks for your help
Mirko
On Tue, Sep 1, 2009 at 14:28, Mirko Vukovicmirko.vukovic@gmail.com wrote:
;; I got the trailing underscore by using `nm' on the library. (defcfun "test_link_" test-link (a :float)) ; (b :float) (i :int))
Fortran passes by pointer ("by reference" is the proper way to call it, unless you're a C programmer) unless the Fortran subroutine / function specifically tags the variable as a "value" type. You're not, so a, b, and i should all be pointers in the above defcfun.
mfh
By pointer - off course.
That led me into the minefield of setting up pointers, which I avoided by looking by the cl-blapack library since it has a set-up to define fortran types and necessary pointers on top of cffi.
I managed to compile blapack and foreign-numeric-vector libraries (I had to ignore some things, and delete others, but I will report that on the lisp-matrix group where that library was discussed).
I then adapted some of the calls in blapack for my library (amos library on netlib.org for calculating bessel functions of complex arguments), and initial tests are passing.
Thanks for the cffi library :-)
Mirko
On Wed, Sep 2, 2009 at 12:40 AM, Mark Hoemmen mark.hoemmen@gmail.comwrote:
On Tue, Sep 1, 2009 at 14:28, Mirko Vukovicmirko.vukovic@gmail.com wrote:
;; I got the trailing underscore by using `nm' on the library. (defcfun "test_link_" test-link (a :float)) ; (b :float) (i :int))
Fortran passes by pointer ("by reference" is the proper way to call it, unless you're a C programmer) unless the Fortran subroutine / function specifically tags the variable as a "value" type. You're not, so a, b, and i should all be pointers in the above defcfun.
mfh
cffi-devel mailing list cffi-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
On Wed, Sep 2, 2009 at 6:16 PM, Mirko Vukovicmirko.vukovic@gmail.com wrote:
That led me into the minefield of setting up pointers, which I avoided by looking by the cl-blapack library since it has a set-up to define fortran types and necessary pointers on top of cffi.
If you are interested in adding better Fortran support for CFFI that'd be nice. E.g., a define-fortran-function function that defaults to reference types would be a start I suppose.
Cheers,
On Wed, Sep 2, 2009 at 1:28 PM, Luís Oliveira luismbo@gmail.com wrote:
On Wed, Sep 2, 2009 at 6:16 PM, Mirko Vukovicmirko.vukovic@gmail.com wrote:
That led me into the minefield of setting up pointers, which I avoided by looking by the cl-blapack library since it has a set-up to define fortran types and necessary pointers on top of cffi.
If you are interested in adding better Fortran support for CFFI that'd be nice. E.g., a define-fortran-function function that defaults to reference types would be a start I suppose.
I would love to make more permanent contributions, but not now -- I am organizing a conference, and loosing my mind over it and my regular job. In two months, if I maintain my sanity :-)
Cheers,
-- Luís Oliveira http://student.dei.uc.pt/~lmoliv/ http://student.dei.uc.pt/%7Elmoliv/