help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Include Fortran Code which Returns Arrays of Dynamic/Unknown Size


From: Jaroslav Hajek
Subject: Re: Include Fortran Code which Returns Arrays of Dynamic/Unknown Size
Date: Thu, 9 Oct 2008 07:16:26 +0200

On Wed, Oct 8, 2008 at 11:01 PM, Marco2008 <address@hidden> wrote:
>
>
> John W. Eaton wrote:
>>
>> I think we need to know more details about precisely how your function
>> does what you claim it does.  Perhaps you could post a simple example
>> that has all the relevant features (i.e., some Fortran function that
>> allocates a matrix and returns it as a parameter).  Exactly how does
>> your Fortran function allocate the data?  How would you call your
>> fucntion in a program written purely in Fortran?
>>
>> jwe
>>
>
> So here is the example that works:
>
> c++-wrapper (fun_cpp.cc):
>
>   #include <octave/oct.h>
>   #include "f77-fcn.h"
>   extern "C"
>   {
>        F77_RET_T
>        F77_FUNC (funfortran, FUNFORTRAN) (double* matrix1);
>   }
>   DEFUN_DLD (fun_cpp, args, ,"...")
>   {
>       octave_value_list retval;
>       Matrix M1 = Matrix(2,2);
>        F77_XFCN (funfortran, FUNFORTRAN,
>                        ( M1.fortran_vec() ) );
>        if (f77_exception_encountered)
>        {
>                error ("Error!");
>                return retval;
>        }
>        retval(0) = M1;
>        return retval;
>   }
>
> Fortran-Subroutine (funfortran.f90):
>
> subroutine funfortran(matrix1)
>        implicit none
>        real(8), intent(out)  ::  matrix1(2,2)
>        matrix1(1,:) = (/1,2/)
>        matrix1(2,:) = (/3,4/)
> end subroutine funfortran
>
> Call in octave:
>
> octave:1> fun_cpp
> ans =
>
>   1   2
>   3   4
>
> octave:2>
>
>
> My problem is the following fortran code:
>
> subroutine funfortrantwo(vector1)
>        implicit none
>        integer :: size1
>        real(8), allocatable :: vector1(:)
>        size1 = 3
>        ! size1 here is defined by a fixed number only to make the
>        ! example clear. Normally, it is computed inside this fortran
>        ! subroutine and not known before a call of funfortrantwo.
>        allocate( vector1(size1) )
>        vector1 = 2
> end subroutine funfortrantwo
>
> I do not know how to write the c++-wrapper for this subroutine. In the first
> example the size of the matrix is known before the fortran subroutine is
> called. But this is not the case in the second example.
>

Hi Marco,

in terms of Fortran 2003/Interoperability with C, allocatable arrays
are not interoperable. These arrays are always passed by array
descriptor (allocating descriptor and passing its address), but how
exactly the descriptor looks is completely compiler-dependent. Also,
note that it is Fortran runtime library that allocates the array, but
presumably C++ runtime library should deallocate it, which is almost
bound to cause trouble.
The easiest portable way to achieve what you need is to first make a
special "query" call to the Fortran subroutine that will only return
the array size, allocate it in C++, and then invoke a normal call.
This is, for instance, how LAPACK works.

regards


-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]