C++ calling f90 function returning array 
Author Message
 C++ calling f90 function returning array

Hello,
I am trying to call a function (in f90) returning arrays from C++.
How do I do it? (Using Visual C++/fortran 6.6c).
It would be easier if the f90 was coded as a subroutine vice function, but I
would rather not modify an older routine.
I was able to write an f90 subroutine gateway that called the function and
returned the array in the argument. That worked fine.
But, I would like to call the original function (which was written as a
function to make it a pure function).
Here is a simplified example:

--------------------------
f90 function to be called
--------------------------
module temp  ! takes care of the explicit interface
  contains

  function test( x ) result( y )
     real(8), dimension(3) :: x
     real(8), dimension(3) :: y
     y = x
     return
  end function test

end module temp

----------------------------------
C++ caller:
-----------------------------------
#include <iostream>

// Prototype for TEST: note test is actually coded as a subroutine
extern "C" void __stdcall TEST( double* b, double* a );

int main()
{

   double a[3] = {1,2,3};
   double b[3];

   TEST( b, a);
   cout << "b = (" << b[0] << ", " << b[1] << ", " << b[2] << ");" << endl;

   return 0;

Quote:
}

-----------------------------------
f90 caller:
-----------------------------------
program f90_test
using temp

  real(8) :: c(3) = (/  1, 2, 3 /)
  real(8)  :: d(3)
  d = test(c)
  write(6,*) "d = ", d

end program f90_test

---------------------------------------------------------------
The problem is: (after working out all the linkage problems):
1. I noticed that the function returning array is actually coded like
    "subroutine test( y, x )", hence the declaration in
    C++ as "void( double*, double* )"
2. When   extern "C" void __stdcall TEST( double* b, double* a );
 I step into the f90 routine (with the de{*filter*}), the values
   of x are correct (i.e., equal to a). However, y has location 0;
   so writing to it causes the program to crash.
3. But, works fine when called from f90. Interestingly, the location of y
   is different from location of d. When it returns from test, however,
   the values for d are correctly set. Is this set by the caller or the
   callee? I don't know what goes on the stack, and who clears stuff from
   the stack.

So, how do I word the C++ declaration?

Thanks for any help!
ujs



Tue, 18 Jul 2006 22:27:54 GMT  
 C++ calling f90 function returning array
| Hello,
| I am trying to call a function (in f90) returning arrays from C++.
| How do I do it? (Using Visual C++/Fortran 6.6c).
| It would be easier if the f90 was coded as a subroutine vice function, but I
| would rather not modify an older routine.
| I was able to write an f90 subroutine gateway that called the function and
| returned the array in the argument. That worked fine.
| But, I would like to call the original function (which was written as a
| function to make it a pure function).
| Here is a simplified example:
<snip>
| The problem is: (after working out all the linkage problems):
| 1. I noticed that the function returning array is actually coded like
|     "subroutine test( y, x )", hence the declaration in
|     C++ as "void( double*, double* )"
| 2. When   extern "C" void __stdcall TEST( double* b, double* a );
|  I step into the f90 routine (with the de{*filter*}), the values
|    of x are correct (i.e., equal to a). However, y has location 0;
|    so writing to it causes the program to crash.
| 3. But, works fine when called from f90. Interestingly, the location of y
|    is different from location of d. When it returns from test, however,
|    the values for d are correctly set. Is this set by the caller or the
|    callee? I don't know what goes on the stack, and who clears stuff from
|    the stack.
|
| So, how do I word the C++ declaration?

I'm not surprised that it's implemented as void with two arguments --
that's a common trick to return complex return values. Now, the answer
how to mimic that in C++ really depends on how F90 compiler does the stuff.
There are three ways that come to my mind:

1) Caller allocates the space; the correct prototype is double*, but
   you have to allocate it yourself
2) Callee allocates the space; in this case, the correct prototype is double**.
   (But who releases the space in this case?)
3) Callee allocates the space, but copy-in/copy-out occurs somewhere in
   the function prolog/epilog.

You should probably ask that in Intel Forum at:

http://www.*-*-*.com/

as people who designed it will certainly know best.

--
 Jugoslav
___________
www.geocities.com/jdujic



Tue, 18 Jul 2006 22:46:37 GMT  
 C++ calling f90 function returning array
UJS:  Our Web page for several mixed languae  references to this at:
http://www.*-*-*.com/ #MixedLanguag...
may be of help.

Skip Knoble, Penn State


-|Hello,
-|I am trying to call a function (in f90) returning arrays from C++.
-|How do I do it? (Using Visual C++/Fortran 6.6c).
-|It would be easier if the f90 was coded as a subroutine vice function, but I
-|would rather not modify an older routine.
-|I was able to write an f90 subroutine gateway that called the function and
-|returned the array in the argument. That worked fine.
-|But, I would like to call the original function (which was written as a
-|function to make it a pure function).
-|Here is a simplified example:
-|
-|--------------------------
-|f90 function to be called
-|--------------------------
-|module temp  ! takes care of the explicit interface
-|  contains
-|
-|  function test( x ) result( y )
-|     real(8), dimension(3) :: x
-|     real(8), dimension(3) :: y
-|     y = x
-|     return
-|  end function test
-|
-|end module temp
-|
-|----------------------------------
-|C++ caller:
-|-----------------------------------
-|#include <iostream>
-|
-|// Prototype for TEST: note test is actually coded as a subroutine
-|extern "C" void __stdcall TEST( double* b, double* a );
-|
-|int main()
-|{
-|
-|   double a[3] = {1,2,3};
-|   double b[3];
-|
-|   TEST( b, a);
-|   cout << "b = (" << b[0] << ", " << b[1] << ", " << b[2] << ");" << endl;
-|
-|   return 0;
-|}
-|
-|-----------------------------------
-|f90 caller:
-|-----------------------------------
-|program f90_test
-|using temp
-|
-|  real(8) :: c(3) = (/  1, 2, 3 /)
-|  real(8)  :: d(3)
-|  d = test(c)
-|  write(6,*) "d = ", d
-|
-|end program f90_test
-|
-|---------------------------------------------------------------
-|The problem is: (after working out all the linkage problems):
-|1. I noticed that the function returning array is actually coded like
-|    "subroutine test( y, x )", hence the declaration in
-|    C++ as "void( double*, double* )"
-|2. When   extern "C" void __stdcall TEST( double* b, double* a );
-| I step into the f90 routine (with the de{*filter*}), the values
-|   of x are correct (i.e., equal to a). However, y has location 0;
-|   so writing to it causes the program to crash.
-|3. But, works fine when called from f90. Interestingly, the location of y
-|   is different from location of d. When it returns from test, however,
-|   the values for d are correctly set. Is this set by the caller or the
-|   callee? I don't know what goes on the stack, and who clears stuff from
-|   the stack.
-|
-|So, how do I word the C++ declaration?
-|
-|Thanks for any help!
-|ujs

   Herman D. (Skip) Knoble, Research Associate
   (a computing professional for 38 years)

   Web: http://www.*-*-*.com/
   Penn State Information Technology Services
    Academic Services and Emerging Technologies
     Graduate Education and Research Services
   Penn State University
     214C Computer Building
     University Park, PA 16802-21013
   Phone:+1 814 865-0818   Fax:+1 814 863-7049



Tue, 18 Jul 2006 23:20:04 GMT  
 C++ calling f90 function returning array



Quote:
> UJS:  Our Web page for several mixed languae  references to this at:

http://www.personal.psu.edu/faculty/h/d/hdk/fortran.html#MixedLanguag...

Quote:
> may be of help.

> Skip Knoble, Penn State

A keeper, thanks Skip.

--
Ciao,
Gerry T.



Wed, 19 Jul 2006 00:57:35 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. F90 problem compiling function returning pointer array

2. F90 function returning array

3. f90 function returns structure with allocatable arrays

4. VC++ calling fortran function and fortran function calling a c++ function

5. Calling F90 function from C: assumed shape arrays

6. FORTRAN dynamic arrays used as parameters of functions called from C++

7. F95: array-returning function as argument to another function

8. fnct returning array (F90)

9. C functions calling {C++/Ada/...} functions

10. HELP F90, External function returning pointer

11. emulating F90 array sections in C++

12. returning ARRAY structures from DLL-functions in Dyalog

 

 
Powered by phpBB® Forum Software