Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28988 Discussions

Exchange operators, custom datatypes and constants between FORTRAN and C++

grasemannka
Beginner
320 Views

Hello,

I have a very difficult problem to solve. I have to write a library for FORTRAN and C++ Programmers. The dll has to be programmed in FORTRAN. For the Fortran side, I want to provide a module containing a custom type and some operators working on this new type. Take a look at this ...

module
resistormodule
type resistor
real(8) value
integer :: id
character(len=10) :: name
end type resistor
interface operator(+)
module procedure serial
end interface
interface operator(//)
module procedure parallel
end interface
private :: serial
private :: parallel
contains
function serial(a,b) result(c)
!DEC$ ATTRIBUTES C, REFERENCE, DLLEXPORT, ALIAS : '_serial' :: serial
type(resistor), intent(in) :: a[value]
type(resistor), intent(in) :: b[value]
type(resistor) :: c
c%v = a%value + b%value
end function serial
function parallel(a,b) result(c)
!DEC$ ATTRIBUTES C, REFERENCE, DLLEXPORT, ALIAS : '_para' :: parallel
type(resistor), intent(in) :: a[value]
type(resistor), intent(in) :: b[value]
type(resistor) :: c
c%value = 1.0 / ( 1.0/a%value + 1.0/b%value )
end function parallel
end module
resistormodule
This m
odule can be used like the following by the Fortran crew:
use resistormodule
type(resistor) :: R1
type(resistor) :: R2
type(resistor) :: RGES
R1%value = 15.0
R2%value = 35.0
RGES = R1 + R2 ! R1 and R2 are connected in serial
print *,"RGES = ",RGES%value
RGES = R1 // R2 ! R1 and R2 are connected in parallel
print *,"RGES = ",RGES%value
Now the C++ crew should use the resistor datatype with the operators as well . 
So I want to provide a propriate header file with the dll, looking like this:

struct

resistor {

double v;

int id;

char n[10];

char h[10];

};

extern

"C" {

resistor para( resistor, resistor );

resistor serial( resistor, resistor );

}

resistor operator&&(const resistor &a, const resistor &b)

{

resistor res;

res = serial(a,b);

return res;

}

resistor operator||(const resistor &a, const resistor &b)

{

resistor res;

res = para(a,b);

return res;

}

Everything works fine 'till here, but now I have some problems:
  • The operator overloading in C++is redundant a can lead to differencesin the operator definition andnaming from C++to Fortran side.
    -> How can I import the Fortran definedoperators?
  • I want to provide a small collection of predefined resistors and constantsmaintained in FORTRAN to both development teams.
    -> How can Iexport global and constant datatypes( e.g. a predefined resistor like
    type(resistor) :: RSTD( 12.34, 0, 'Internal Standard Resistor' )
    ) to both the Fortran developing team and the C++ crew?
    -> How can I make this dataobject constant?

I will be very glad for your help and hints ....

0 Kudos
1 Reply
joerg_kuthe
Novice
320 Views

I hope I have understood your problem correctly.

1. you are askin hwo to import the Fortran defined operators into C++.
To my understanding of Fortran this is not possible -at least not to import an operator. To me, defining an operator bya MODULE PROCEDURE is nothing else than teaching the compiler to use a specific procedure, if this operator occurs. I.e.v, teaching the FORTRAN compiler to replace that operator by that function. If you want to export the functionality, you have to export the functions that you name in the MODULE PROCEDURE statement, exactly as you do it. Not more is possible.

2. Since DEC$ ATTRIBUTES DLLEXPORT exports any symbol, you may also export global variables.

type(resistor) :: RSTD = resistor( 12.34, 0, 'Internal Standard Resistor' )
DEC$ ATTRIBUTES DLLEXPORT :: RSTD

should do the job. I am not sure if this works with PARAMETERs also (haven't tested that). The description of ATTRIBUTES DLLEXPORT doesn't mention constants and I guess PARAMETERs are usually replaced by their values when compiler encounters them, thus they probably aren't global symbols and so cannot be exported.

Jrg Kuthe
www.qtsoftware.de


0 Kudos
Reply