Skip to content

Optional dimensioning in assumed-shape arrays #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ivan-pi opened this issue Nov 15, 2019 · 4 comments
Open

Optional dimensioning in assumed-shape arrays #89

ivan-pi opened this issue Nov 15, 2019 · 4 comments
Labels
Clause 8 Standard Clause 8: Attribute declarations and specifications

Comments

@ivan-pi
Copy link

ivan-pi commented Nov 15, 2019

Assumed-shape arrays are often used to make calling subroutines easier, i.e. by not having to explicitly pass the array size (or bounds). Say we have a function which solves a system of equations for multiple right-hand sides returning the solution in right hand size array. The subroutine might look something like this:

subroutine solve(A,b,ierr)
  real, intent(inout) :: A(:,:)
  real, intent(inout) :: b(:,:)
  integer, intent(out) :: ierr
  ! check shapes match, if not, produce an error flag
  ! perform factorization of A...
  ! perform back substitution for columns of b...
end subroutine

This interface is not optimal since, due to the rules of matrix multiplication, we actually know that the number of rows in b should be equal to the number of rows in A. Therefore one might expect it to be possible to use the following interface:

subroutine solve(A,b)
  real, intent(inout) :: A(:,:)
  real, intent(inout) :: b(size(A,1),:)
 
  ! perform factorization of A...
  ! perform back substitution for columns of b from 1 to size(b,2)...
end subroutine

This way we could avoid the need for writing assertion statements to check if the first dimensions of A and B match, and forcing the user to check an error flag. Of course this problem can be avoided completely by using explicit shape arrays, defeating the entire point of having assumed-shape ones. A second (unattractive) option would be to use an assumed-size array, and explicitly pass only the number of columns in b:

subroutine solve(A,b,nb)
  real, intent(inout) :: A(:,:)
  real, intent(inout) :: b(size(A,1),*)
 
  ! perform factorization of A...
  ! perform back substitution for columns from 1 to nb...
end subroutine

Could someone perhaps explain what is the reason that the second example above is not allowed and the compiler flags it as a "bad array specification"?

@certik
Copy link
Member

certik commented Nov 15, 2019

Thanks @ivan-pi for raising this issue. This is something I encounter very frequently myself. The explicit-shape arrays are actually more readable in this case:

subroutine solve(A, b, n, k)
  real, intent(inout) :: A(n,n)
  real, intent(inout) :: b(n,k)
  integer, intent(in) :: n, k
 
  ! perform factorization of A...
  ! perform back substitution for columns of b from 1 to k...
end subroutine

Except that one has to pass n and k as arguments. It would be nice to be able to do something like this with assumed-shape arrays.

@ivan-pi
Copy link
Author

ivan-pi commented Nov 15, 2019

I was annoyed by this today, while creating wrappers of some old least squares routines.

I suppose that if assert statements (#70) were available then the current assumed-shape syntax would be sufficient.

@certik
Copy link
Member

certik commented Nov 16, 2019

Here is how it would look like with an assert statement:

subroutine solve(A, b)
  real, intent(inout) :: A(:,:)
  real, intent(inout) :: b(:,:)
  assert(size(A,1) == size(A,2))
  assert(size(A,1) == size(b,1))

 
  ! perform factorization of A...
  ! perform back substitution for columns of b from 1 to size(b,2)...
end subroutine

@FortranFan
Copy link
Member

There have been some recent efforts to capture the array properties in Fortran like here and here,

I wonder if the ideas in such papers can be improved upon to achieve something that works with dummy argument characteristics like array shapes or parts thereof?

@certik certik added the Clause 8 Standard Clause 8: Attribute declarations and specifications label Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clause 8 Standard Clause 8: Attribute declarations and specifications
Projects
None yet
Development

No branches or pull requests

3 participants