On 21/08/20 1:54 AM, gah4 wrote:
> On Thursday, August 19, 2021 at 2:08:04 PM UTC-7, Jos Bergervoet wrote:
>> On 21/08/16 4:24 AM, Robin Vowels wrote:
>
> (snip)
>>> It does.
>>> Try LBOUND and UBOUND.
>
>> That's only part of the array descriptor metadata (the stride still
>> missing..) And it gives the user only read access.
>
> Yes, it would be interesting to have an extension, or alternative, to
> C_F_POINTER that would allow one to specify the strides separarately,
What we want is a transposed pointer assignment, like in:
!-- Warning, dangerous hacking, tested only for gfortran 9.1.0
program swap_strides
implicit none
integer, target :: A(4,4), B(4,4), C(4,4)
integer, pointer :: P(:,:)
A(1,:) = [1 ,2 ,3 ,4]
A(2,:) = [0 ,5 ,6 ,7]
A(3,:) = [0 ,0 ,8 ,9]
A(4,:) = [0 ,0 ,0 ,10]
call printmat(A) ! The original matrix
!-- Different ways to get transpose without copying:
P => transptr(A); call printmat(P)
P => transptr(B); P = A; call printmat(B)
call printmat( transptr(A) )
transptr(C) = A; call printmat(C)
!-- More complicated mirroring and transposing:
P => transptr( A( :, 4:1:-1 ) ); call printmat(P)
call printmat(transptr( A( :, 4:1:-1 ) ))
!-- This one does not work: (optimization dependent?)
B = transptr(A); call printmat(B) ! Error !!!
contains
subroutine printmat(m)
integer, intent(in) :: m(:,:)
integer :: i
print *; do i=1,ubound(m,1); print "(19i4)", m(i,:); end do
end
!-- Pointer hacking function using descriptor wrapper type:
function transptr(m) result(p)
integer, target, intent(in) :: m(:,:)
integer, pointer :: p(:,:)
integer :: ar(22)
type data; integer, pointer :: p(:,:); end type; type(data) :: d
d%p => m; ar = transfer(d, ar) ! fetch descriptor in ar(:)
ar(11:22) = [ar(17:22), ar(11:16)] ! swap index information
d = transfer(ar, d) ! put back in the descriptor
p => d%p ! copy pointer from wrapper
end
end program
> such that one could arrange for all the different ways of addressing
> array elements.
The above could perhaps be extended, but it is of course a very
unreliable way of hacking, so the real challenge is to propose
new syntax and make it part of the language standard (after which
it will of course be perfectly reliable, I mean!)
As shown in the above, gfortran apparently uses 22 integers in
the descriptor of a matrix. At least for gcc 9.1.0.
--
Jos