JNA with Fortran assumed size array

后端 未结 1 1378
渐次进展
渐次进展 2021-01-19 08:27

I have a Fortran subroutine taking an assumed size array:

subroutine sub(arr)
  implicit none
  double precision arr(*)
end subroutine

I ma

相关标签:
1条回答
  • 2021-01-19 08:54

    You will need to pass the length as an additional parameter. Using an assumed-shape array will not work, here's why:

    In the ABI employed by most Fortran compilers, arrays as parameters ("dummy arguments") can take one of two representations, depending on the interface used in the subroutine/function:

    • Those passed with known sizes or assumed sizes, like arr(n) or arr(*), usually receive just a pointer to the first element, with elements assumed to be contiguous.
    • Those passed with assumed shapes, like arr(:) receive an array descriptor structure. This is completely implementation dependent, but usually such a structure contains the pointer to the first element of the data plus information on the bounds of each dimension, the stride, etc.

    That is the reason why you can directly pass a single row, or only the elements in even indices, of an array if the function receives it as an assumed shape array: the descriptor structure encodes the information that the data is not necessarily contiguous and so the Fortran compiler does not need to copy arr(5:2:) to a temporary location in memory.

    The reason why you cannot use such facilities to communicate with Java is that the descriptor structure is completely non-standard, a part of the particular ABI of each compiler. Thus, even if you somehow managed to understand how to build it (and it would be non trivial) the next version of your compiler could bring a total change.

    0 讨论(0)
提交回复
热议问题