Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Passing assumed shape to Fortran from JAVA JNA

Chaitra_R_
Beginner
824 Views

Hello,

My Fortran program contains the following:

REAL(KIND = R8), DIMENSION(:), ALLOCATABLE :: L, U
INTEGER :: N
N = 2
L(1:N) = (/0.0, 0.0/)

I modified the program and it looks like this now

INTERFACE
   SUBROUTINE inputs(L_array, arr_shape) BIND(C,name='inputs')
     IMPORT c_double,c_int,c_ptr
     type(c_ptr),value :: L_array
     integer(c_int), intent(in), dimension(2) :: arr_shape
   END SUBROUTINE inputs
END INTERFACE

SUBROUTINE inputs(L_array, arr_shape) BIND(C)
     USE ISO_C_BINDING
     USE VTdirect_MOD
     IMPLICIT NONE
       type(c_ptr),value :: L_array
       integer, intent(in), dimension(1) :: arr_shape_L
       real, dimension(:), pointer :: L
       integer :: n

       call c_f_pointer(L_array, L, arr_shape_L)
       print *, "L in fortran:", L
       do j = 1, n
            print *,L(j)
       end do
END SUBROUTINE

My JAVA program contains the following lines

double[] l = new double[]{0.0,0.0};
    int[] y = new int[]{2};

lib.inputs(l,x);

But, when I run this program,

I get the following output

L in fortran:   0.0000000000000000        0.0000000000000000

6.9518704107813286E-310
   1.3339772437713657E-322


What am I doing wrong?

0 Kudos
5 Replies
Steven_L_Intel1
Employee
824 Views

Since you declared the array "double" in Java, you should use real(C_DOUBLE) in the Fortran declaration of L.

0 Kudos
mecej4
Honored Contributor III
824 Views

What am I doing wrong?

You posted code fragments that the compiler rejects, because there are a number of syntax errors. Obviously, the code that you posted is not the code that you ran. I cannot make sense of your claim that you modified a program such that it became a much bigger subroutine.

The interface block is of little use -- it is meant for use by callers of the subprogram concerned. Therefore, in Subroutine Inputs()  there are variables of undeclared type.

Please post the actual code. 

0 Kudos
FortranFan
Honored Contributor III
824 Views

mecej4 wrote:

... I cannot make sense of your claim that you modified a program such that it became a much bigger subroutine.

..

Where is this claim?

0 Kudos
Steven_L_Intel1
Employee
824 Views

It's in the original post. Basically, a small Fortran program was turned into a Java program that called Fortran code. The Fortran code is larger than it needs to be.

0 Kudos
Chaitra_R_
Beginner
824 Views

I looked into this post here - https://software.intel.com/en-us/forums/topic/282360 and I changed my SUBROUTINE inputs to look like this:

SUBROUTINE inputs(array_ptr, array_size) bind(c)
USE ISO_C_BINDING
IMPLICIT NONE

TYPE(C_PTR), VALUE :: array_ptr
INTEGER(C_INT), INTENT(IN), VALUE :: array_size
INTEGER(C_INT) :: i
REAL(C_DOUBLE), DIMENSION(:), POINTER :: array

call c_f_pointer(array_ptr, array, [array_size])

DO i =1, SIZE(array)
PRINT *, array(i)
END DO

END SUBROUTINE inputs

My Java program now contains the following lines of code:

double[] arr = new double[]{0.0,0.0};
int n = 2;
lib.inputs(arr,n);

The output is given below and as expected.

   0.0000000000000000     
   0.0000000000000000   

Thank you so much for all your help. Next time, I'll make it a point to include the full code and format my code properly.

 

0 Kudos
Reply