Calling Fortran-functions returning complex numbers from C 
Author Message
 Calling Fortran-functions returning complex numbers from C

Hello everyone!

I have a problem with calling complex fortran functions from C that I do
not know how to solve. Currently I am working on an interface enabling
calls from C to the Fortran BLAS and LAPACK-libraries (linear algebra routines).
Everything works great except for a few functions returning complex numbers.
Functions returning real numbers and integers do work. Complex numbers
have been used as both input and output to subroutines with correct results.

The complex functions work properly on Sun stations so this is probably
a specific problem with AIX (run on an IBM Power Parallel SP). Does anyone
know how to overcome these problems? I would be grateful for any kind of
information. I have used the following sources of information without any
success,
        comp.lang.fortran FAQ (yes, I have read it)
        AIX FAQ
        Brief Notes on Mixed Language Programming - FORTRAN and C (Glenn Geers)
        Mixed Language Programming part 4 (Bo Einarsson)
        cfortran.doc (Burkhard Burow)
        XL Fortran for AIX, User's guide

/Christer Andersson



Mon, 28 Feb 2000 03:00:00 GMT  
 Calling Fortran-functions returning complex numbers from C

Hi Christer,

|> I have a problem with calling complex Fortran functions from C that I do
|> not know how to solve. Currently I am working on an interface enabling
|> calls from C to the Fortran BLAS and LAPACK-libraries (linear algebra routines).
|> Everything works great except for a few functions returning complex numbers.
|> Functions returning real numbers and integers do work. Complex numbers
|> have been used as both input and output to subroutines with correct results.
|>
|> The complex functions work properly on Sun stations so this is probably
|> a specific problem with AIX (run on an IBM Power Parallel SP). Does anyone
|> know how to overcome these problems? I would be grateful for any kind of
|> information. I have used the following sources of information without any
|> success,

     I suspect that the problem lies in the way that XL Fortran returns the
results of functions of type complex; the value is returned in FPR1 (real
part) and FPR2 (imaginary part) in the case of COMPLEX(4) and COMPLEX(8),
and in FPR1-FPR4 (FPR1 and FPR2 contain the real part and FPR3 and FPR4
contain the imaginary part) in the case of COMPLEX(16).

     Functions of type real work because the value is returned in FPR1, and
the same is the case for C functions of type float and double.  Functions
of type integer work because the value is returned in GPR3 (or GPR3 and GPR4
in the case of INTEGER(8)) and the same is the case for C functions of type
int, et al.  However, there's no data type in C that will let you get at
anything beyond FPR1.

     The calling conventions on Sun must be different, and permit the
complex case to go through.

     One way around the problem might be to write a Fortran wrapper for
the functions of type complex.  For example, if you're using a struct with
two real components as the return type of the function in C, your Fortran
wrapper would be a function of derived type declared in a similar fashion.
The Fortran wrapper could then call the BLAS or LAPACK routine and store
the result back into the derived type result.  The calling conventions for
C functions returning structs are similar to those for Fortran functions of
derived type on AIX, so this approach should work.  I don't know Sun's
calling conventions, so I don't know whether that approach would be
portable or only AIX-specific.

     By the way, the section of the User's Guide to check would have been
the one entitled "Interlanguage Calls".  It gets into all of the nitty
gritty details about how arguments are passed and how results are returned.

     Please let me know if any of this isn't clear or if you encounter any
problems.

Thanks,

Henry
-------------------------------------------------------------------------------
Henry Zongaro      XL Fortran/XL HPF Compiler Development
IBM SWS Toronto Lab   Tie Line 778-6044  Phone (416) 448-6044



Tue, 29 Feb 2000 03:00:00 GMT  
 Calling Fortran-functions returning complex numbers from C

Quote:

>     One way around the problem might be to write a Fortran wrapper for
>the functions of type complex.  For example, if you're using a struct with
>two real components as the return type of the function in C, your Fortran
>wrapper would be a function of derived type declared in a similar fashion.
>The Fortran wrapper could then call the BLAS or LAPACK routine and store
>the result back into the derived type result.  The calling conventions for
>C functions returning structs are similar to those for Fortran functions of
>derived type on AIX, so this approach should work.  I don't know Sun's
>calling conventions, so I don't know whether that approach would be
>portable or only AIX-specific.

Maybe a Fortran *and* a C wrapper using an extra argument to pass the
complex value by reference across the Fortran/C barrier.  The Fortran
wrapper would simply assign the value returned by the BLAS function to
the extra argument.  The C wrapper would declare the struct to hold the
complex value, and pass a pointer to it as an argument to the Fortran
wrapper.  On return from the Fortran wrapper, return the value stored
in the struct as the value of the C wrapper.

Since in you can't do much in C with the result of a struct-valued
function other than assign its value, you could dispense with the
C wrapper entirely and call the Fortran wrapper directly.  Or you
could keep the C wrapper to handle other Fortran/C issues, plus permit
input arguments to be handled in conventional C-style (by value).

--
Jim Riley



Fri, 03 Mar 2000 03:00:00 GMT  
 Calling Fortran-functions returning complex numbers from C



Quote:
> Hello everyone!

> I have a problem with calling complex Fortran functions from C that I do
> not know how to solve. Currently I am working on an interface enabling
> calls from C to the Fortran BLAS and LAPACK-libraries (linear algebra
routines).
> Everything works great except for a few functions returning complex
numbers.
> Functions returning real numbers and integers do work. Complex numbers
> have been used as both input and output to subroutines with correct
results.

> The complex functions work properly on Sun stations so this is probably
> a specific problem with AIX (run on an IBM Power Parallel SP). Does
anyone
> know how to overcome these problems? I would be grateful for any kind of
> information. I have used the following sources of information without any
> success,
>    comp.lang.fortran FAQ (yes, I have read it)
>    AIX FAQ
>    Brief Notes on Mixed Language Programming - FORTRAN and C (Glenn Geers)
>    Mixed Language Programming part 4 (Bo Einarsson)
>    cfortran.doc (Burkhard Burow)
>    XL Fortran for AIX, User's guide

> /Christer Andersson


You might be interested in my paper "Portable mixed Language programming
using C++ and FORTRAN 77" (36 pages). section 6.2.1 is: How to call a
function returning a COMPLEX. You should be able to figure out some
solutions from that. My document uses C++ rather than C, and it shows how
C++ is actually better suited for Fortran integration that C is.

See my homepage: http://home.sol.no/~arnholm/ . You can download a ZIP-file
containing documentation in various formats, as well as a range of  C++
header files implementing COMPLEX, CHARACTER classes etc. that may be used
when calling Fortran from C++. This stuff will appear on the C/C++ User
Group CD-ROM ( see ttp://www.hal9k.com/cug/v400d.htm#cug477 for details).

Carsten Arnholm



Sun, 05 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C calling fortran:complex data type

2. Variable Numbers of Parameters in Fortran Calls

3. AIX: calling complex Fortr functions from C

4. function parameters and return values aren't changed by function call

5. Macro to check function return, calling function again?

6. system call to return current line number

7. system call to return current line number

8. system call to return current line number

9. calling C++ function from Fortran: parameter passing

10. help: calling C function from fortran

11. Calling Fortran Functions

12. Calling C functions from within Fortran.

 

 
Powered by phpBB® Forum Software