I have the following problem:
I need to create an datasets array which size is not known at first. It is an composition of different subarrays.
For example the first subdataset has 4 elements, the second 10, and a third 8. Because this sizes are not known at the start of the programm I can not do somethin like:
INTEGER(4), ALLOCATE :: datasets(:) ALLOCATE(datasets(1:4+10+8))
The sizes of each subdataset is evaluated one after each other. Because there is no such thing like appending an array to an array I would have to reallocate the datasets size after each subdataset to manually expend the array. But this always involves a copy process which I definetly have to avoid.
So I tried to use an array of pointers in which every pointer points to an allocatable array. This works.
P1 | P2 | P3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
| 2 | 3
| 2 | 3
| 2 | 3
| 2 | 3
| 2 |
| 2 | Its getting tricky now, because I have a subroutine (sub1) which needs one array. Is there any possibility to pass the data inside the subroutine without copying it?
1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
MODULE TEST_MOD IMPLICIT NONE PRIVATE ! Type TYPE :: TYPE_TESTDATA INTEGER(4), ALLOCATABLE :: data(:) END TYPE TYPE_TESTDATA TYPE :: TYPE_TEST_PTR TYPE(TYPE_TESTDATA), POINTER :: p END TYPE TYPE_TEST_PTR CONTAINS ! Subroutines SUBROUTINE sub1(array) IMPLICIT NONE INTEGER(4), ALLOCATABLE, INTENT(IN) :: array( : ) ! do smth with array END SUBROUTINE sub1 SUBROUTINE sub2() IMPLICIT NONE INTEGER(4) :: size_data1, size_data2, size_data3 INTEGER(4) :: num_datasets TYPE(TYPE_TEST_PTR), ALLOCATABLE :: datasets(:) num_datasets = 3 size_data1 = 4 size_data2 = 10 size_data3 = 8 ! allocate ALLOCATE(datasets(1:num_datasets)) ALLOCATE(datasets(1)%p%data(1:size_data1)) ALLOCATE(datasets(2)%p%data(1:size_data2)) ALLOCATE(datasets(3)%p%data(1:size_data3)) datasets(1)%p%data = 1 datasets(2)%p%data = 2 datasets(3)%p%data = 3 CALL sub1(datasets(:)%p%data) END SUBROUTINE sub2 END MODULE TEST_MOD
I hope I have described the problem quite understandably. Thanks for your help in advance.