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

Problem of binding between fortran and java using JNA

Gubernatis__Pierre
583 Views

Hi all....

I have a strange problem while binding a fortran function for using it in java....

I can call several Fortran routines from java and it works fine. But when it comes to a given Fortran function, i have a SIGSEV fault in Java. The only way to fix this up (that I found) is to add "VALUE" on the arguments of the function in the Function. When I do this, the Java binding works well and I get the expected result. The BIG problem is that the Fortran call is not working anymore. So I have been obliged to duplicate the function in the Fortran lib, one with the "VALUE" tag (that I use from Java) and one without the tag (for Fortran call) !!    I don't understand what happens and how I could correct this...

My Fortran :

      FUNCTION XESM(M,N,T,ILCOM) BIND(C,name='xesm')
!                                                                       
      IMPLICIT NONE                                                     
      INTEGER, VALUE :: M,N,ILCOM
!                                                                       
      REAL(8), VALUE  :: T
      REAL(8) :: XESM                             
!                                                                       
      write(*,*) 'received : ',M,N,T
      RETURN                                                            
      END      

My Java :

public class TestJNA {
    
  public interface EOS extends Library {
     EOS INSTANCE = (EOS) Native.loadLibrary("eos.so", EOS.class);
     double xesm(int M, int N, double T, int IC);
} 
 
  public static void main(String[] args) {
 
   NativeLibrary.addSearchPath("eos.so", "./lib/");  
   EOS eosdll = EOS.INSTANCE;
                 
   int IC=0 ;   int M=1, N=1 ;   double T=1500.;

       double ES = eosdll.xesm(M,N,T,IC);
        
    }
}

To summarize, I need the "VALUE" tag to get java working correctly (but the regular Fortran call fails), and vice versa....I would greatly appreciate your help.. Thanks in advance...

0 Kudos
2 Replies
Gubernatis__Pierre
583 Views

 I have tried to use IntByReference and DoubleByReference types from om.sun.jna.ptr.  (instead of int and double) and it seems to work...

I don't know it this is the right way to solve the problem...

0 Kudos
Steve_Lionel
Honored Contributor III
583 Views

What you need is an "explicit interface" for the function that is visible to the Fortran caller. The easiest way to do this is to enclose function XESM in a module and then USE the module from other places in your Fortran code that call XESM. That way all the attributes of the interface will be seen by the Fortran caller.

For example:

module XESM_MOD
contains
function XESM ....
end function XESM
end module XESM_MOD

and then in your Fortran procedures:

subroutine SOMESUB ...
use XESM_MOD
...
ret = XESM(....)
...

 

0 Kudos
Reply