Skip to content
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

Resizable allocatable arrays #326

Open
PierUgit opened this issue Jan 4, 2024 · 5 comments
Open

Resizable allocatable arrays #326

PierUgit opened this issue Jan 4, 2024 · 5 comments

Comments

@PierUgit
Copy link
Contributor

PierUgit commented Jan 4, 2024

Following a discussion on https://fortran-lang.discourse.group/t/enhancements-to-allocatable-arrays/

The basic idea is to have something similar to the C++ vectors, which can be dynamically resized while minimizing the occurences of malloc/copy/free, thanks to a significant overprovisionning of the capacity.

I came up with a description how it could work in Fortran, including a demonstration code (which is non standard, but nicely illustrates the proposed features): https://github.com/PierUgit/enhanced-allocatables

In particular, iteratively appending a element to an array would be much more efficient.

a = [a, x]
! replaced with
resize(a, append=x)

I do not copy/paste the whole description here, it's all in the README of the above repository.

I believe the change would be relatively simple for the compiler writers.

@certik
Copy link
Member

certik commented Jan 4, 2024

Is the idea to add "capacity" into the array descriptor? Because you are not introducing any new types, so it would use the existing array types in Fortran, which means that only arrays that are implemented using a descriptor can be resized. The arrays that do not have a descriptor cannot be resized without a copy.

You can then reshape the existing array as long as it fits into the capacity (otherwise you copy & double the capacity), even a multidimensional one.

@PierUgit
Copy link
Contributor Author

PierUgit commented Jan 4, 2024

Is the idea to add "capacity" into the array descriptor?

Essentially, yes, this is what the compilers would have to do.

which means that only arrays that are implemented using a descriptor can be resized. The arrays that do not have a descriptor cannot be resized without a copy.

This pre-proposal is only for allocatable arrays: I guess they always have a descriptor?

@PierUgit PierUgit changed the title Resizable arrays Resizable allocatable arrays Jan 4, 2024
@certik
Copy link
Member

certik commented Jan 4, 2024

This pre-proposal is only for allocatable arrays: I guess they always have a descriptor?

I see, I think that will work. Yes, allocatable arrays usually have a descriptor in most compilers.

@PierUgit
Copy link
Contributor Author

PierUgit commented Jan 15, 2024

It's too early for a pull request, but I've prepared a draft proposal to have a basis for the discussion: https://github.com/PierUgit/enhanced-allocatables/blob/main/proposal.txt

@PierUgit
Copy link
Contributor Author

Sorry, I messed a bit the topic by opening this issue while #318 was already opened :( ... Maybe one should be closed, just adding a reference to the other one. :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants