1

I am working in Python calling a Fortran function bound by f2py. When I explicitly dimension the array my sum call returns the desired result, but when I use assumed-shape it returns 0

! foo.f95
function sum_test(arr)
  IMPLICIT NONE
  integer(8), dimension(:), intent(in) :: arr
  integer(8) :: sum_test
  sum_test = sum(arr)
end function sum_test

Python side:

import foo
foo.sum_test([1,2,3])
0L

if I dimension explicitly in Fortran:

! foo.f95
function sum_test(arr)
  IMPLICIT NONE
  integer(8), dimension(3), intent(in) :: arr
  integer(8) :: sum_test
  sum_test = sum(arr)
end function sum_test

Python side:

import foo
foo.sum_test([1,2,3])
6L

Note that if I print out my values on the assumed-shape version like so:

write(*,*) arr

I can see the values in the array.

I'm clearly missing some key piece here!

2
  • Hi, welcome. Try to keep your posts consise, avoid lengthy introductions, thanks and greetings. Your name is already under the post with the icon, no need to repeat it. Use tag fortran and add a specific version if you have a reason to do so (not here). Many more people follow the more generic tag so you are more likely to get an answer. Also it is good to use capital letters where they should be (I, Fortran, Python) otherwise you may appear "lazy" to some. Commented Nov 20, 2016 at 19:56
  • It is also good to report the versions of the software you used and the compiler flags you used. Commented Nov 20, 2016 at 20:02

1 Answer 1

1

Assumed-shape arrays require the caller to pass information about the array bounds along with the data address. The mechanism for doing this is implementation-dependent and not all implementations document their method. Your Python code is just passing the data address, but Fortran expects (usually) a "descriptor" data structure. (Fortran 2015 specifies a standard way of doing this as part of "Further C Interoperability".)

What I suggest instead is what Fortran calls "adjustable arrays", where you pass the extent as a separate argument.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the explanation!
What was called adjustable array in f77 is called assumed size since f90. As Steve said, this is probably want you want for inter-language calls, so that f2003 style C interoperability may be used, not depending on supplying a descriptor.
I am not sure about your answer. I think assumed shape arrays should be supported by f2py. Of course the f2py must use the correct calling convention of the right compiler, but it should be supported.
when i use a subroutine instead of a function, everything worked well.. when using a function i had to also pass the dimension as well for it to work
I disagree with @tim18's reply. Adjustable arrays and assumed-size are two different things. An adjustable array is a dummy argument array where one or more dimensions are variables that are also dummy arguments (or in COMMON or module variables or host-associated). Assumed-size arrays have * for the last upper bound and have no defined upper bound. Adjustable arrays aren't called that in current Fortran, they're a subset of "Explicit-shape arrays" where the bounds are "specification expressions".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.