PS: As an afterthought I think a more productive and optimal alternative is to write 2-3 high level f90 shell SUBROUTINES calling the QT functions and performing the several operations needed to construct the resulting Ta and TPT' and those will then also act as interface with C++.
Best regards
George artilogica@btconnect.com
----- Original Message ----- From: "G. Perendia" george@perendia.orangehome.co.uk To: "List for Dynare developers" dev@dynare.org Sent: Thursday, June 18, 2009 8:06 PM Subject: returning arrays from fortran function to C
Dear Andrea and Michel (et al)
I have encountered an unexpected problem integrating KalmanFilter with the f90 QT library - passing the QT result arrays back to C++.
QT Fortran routines have been written in standard Fortran FUNCTION format, (i.e., not SUBROUTINE), so that they are returning double or single dimensional array (they are named by), by value ( not reference) and, as
it
appears, only simple, single variables seems can be passed from Fortran FUNCTIONs back to C++ (e.g. INT or REAL).
On the other hand, NAG, BLAS and LAPACK routines have all been written as Fortran SUBROUTINEs and the subroutines are the equivalent of "C"
functions
returning "(void)".
SUBROUTINE can be integrated with C more easily as they receive parameters and return their results through the variables passed as calling
parameters
by references.
For example, dgemv.f from BLAS library gets Y by reference and returns modified Y passed as calling parameter reference.
SUBROUTINE DGEMV(TRANS,M,N,ALPHA,A,LDA,X,INCX,BETA,Y,INCY)
....
- Y - DOUBLE PRECISION array of DIMENSION at least
...
Before entry .... the incremented array Y
must contain the vector y. On exit, Y is overwritten by the
updated vector y.
....
I could not find any references on how to get arrays from Fortran FUCTION
as
return value back to C - does anyone know how to do it or, if at all possible?
One way I can think of is less explored option of returning Fortran
pointer
to the resulting array from the QT functions instead of the array by
value
and I think I can work one of that out.
However, even if there is another way to pass function result arrays back
to
C++, I expect it is bound to be less efficient than passing it by
reference
(or Fortran pointer), especially for larger matrices. And, if there is no efficient alternative, I can either easily rewrite QT library as
SUBROUTINE
routines instead FUCTION or try to use Fortran pointers and, if ok, I can then also rewrite QT library to return pointers..
Best regards
George