-
Notifications
You must be signed in to change notification settings - Fork 171
Support allocatable in lpython #1815
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
Conversation
Looks good so far. Tests need to be added. |
Thanks. We need to figure out a syntax to support allocating the Allocatable arrays equivalent to |
I think the syntax you chose is fine. This: a: Allocatable[f64[:]] = empty((n,), dtype=float64) is equivalent to: real(dp), allocatable :: a(:)
allocate(a(n)) And this: b: Allocatable[i32[:]]
n = 10
b = empty((n,), dtype=int32) is equivalent to:
I think the deallocation is also equivalent, when the array goes out of scope it gets deallocated (both in Python and Fortran). The alternative design is to do something like: a: Allocatable[f64[:]] = allocate((n,), dtype=float64) And somehow mark the array as "allocatable". In the design of this PR, the array in CPython looks exactly like any other numpy array. In ASR it is "allocatable". Let me ask this: do we currently have any way to do the following: n = 10
a: f64[n] = empty((n,), dtype=float64) Here Another common use case is: integer, allocatable :: x(:)
do i = 1, 10
allocate(x(i))
x = ...
print *, x
deallocate(x)
end do which in Python would become: for i in range(1, 11):
x: Allocatable[i32[:]] = empty((i,), dtype=int32)
x[:] = ...
print(x) When it goes out of scope, it gets deallocated, so we can allocate it again in the next iteration. Although here one issue is that for local variables in a loop there might be slightly different semantics. This however should be equivalent: integer, allocatable :: x(:)
allocate(x(10))
x = ...
print *, x
deallocate(x)
do i = 1, 10
allocate(x(i))
x = ...
print *, x
deallocate(x)
end do which in Python would become: x: Allocatable[i32[:]] = empty((10,), dtype=int32)
x[:] = ...
print(x)
for i in range(1, 11):
x = empty((i,), dtype=int32)
x[:] = ...
print(x) It's a bit confusing to me, since in Fortran you need to explicitly deallocate if you want to allocate the same array again. It gets automatically deallocated when it goes out of scope. In Python I guess we would insert a deallocate before |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good as a start.
No, we don't support that at present and will raise the following error:
Yes, that's right. |
I'm adding this to auto-merge and will add the allocatable support for C backend in a new PR. |
Thanks! |
#1785