ylikx / forpy

Forpy - use Python from Fortran
GNU Lesser General Public License v3.0
212 stars 51 forks source link

Change ndarray_create default to create a copy of the array that is passed? #4

Closed ylikx closed 5 years ago

ylikx commented 5 years ago

So far ndarray_create creates a wrapper of the Fortran array that is passed to the function and uses the Fortran array as memory buffer for the numpy ndarray. This has the advantage that ndarray creation is fast and saves memory. However, there are many caveats (see Working with arrays) for the user:

Especially for new or casual users this is not ideal. Solutions to this issue could be:

1) Let ndarray_create make a copy of the passed array. Offer previous functionality by providing a new function ndarray_create_nocopy for users who want efficiency Advantages: safer for the reasons stated above, list_create(a_list, some_object) etc. also create copies, the safe alternative would be default (could pass temporaries, no need to manage lifetime of buffer), option for efficiency for experienced users Disadvantages: existing code that relies on changes to the Fortran array affecting the ndarray and vice versa breaks, existing code runs slower/uses more memory due to additional copies

2) OR: keep the existing ndarray_create, introduce ndarray_create_copy with the safer behaviour Advantages: no api change, existing code works as usual (but might be affected by issue #3) Disadvantages: not as intuitive, unsafe variant is default, list_create etc. creates copy but ndarray_create does not

Comments on this issue to let me know, what you prefer

ylikx commented 5 years ago

Will change API to variant 1) In existing code, change ndarray_create to ndarray_create_nocopy, when copies should be avoided. When using ndarray_create_nocopy, add the Fortran asynchronous attribute to the declaration of the underlying buffer. (see #3)

ylikx commented 5 years ago

Changed in 5a14ca6c