Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29553 討論

Multi-dimensional array pointer points to a 1D array?

Yaqi_Wang
初學者
1,590 檢視
I have a a prrety large 1D array allocated. Let us say x(1:100000000). Then I want to use a 2D array pointer y(1:50000000,1:2) to point to x, so I can access array elements in a more meaningful way.

Is there an easy way to do this? I do not want to reallocate memory and copy x to y.

Thanks.
0 積分
4 回應
Steven_L_Intel1
1,590 檢視
Easy way - pass X(1) to a subroutine where the argument is declared (1:50000000,1:2) and do what you need there. This can also be done with pointer remapping in version 12 but I'll have to do some experiments first. You can also fake it by using C_F_POINTER(C_LOC(X),P,[50000000,2]) where P is a POINTER with dimension (:,:).
Yaqi_Wang
初學者
1,590 檢視
thanks Steve.

Few further questions:

Is the pointer remapping written in Fortran standard?

If I use C_F_POINTER to remap the pointer and later x becomes unaccessable, then can I deallocate memory with ponter Y?
abhimodak
新貢獻者 I
1,590 檢視
There are a few things that the program below does not test but it looks like pointer remapping is working in IVF 12 update 1.

Abhi


-----

[fxfortran]      Program Test_PointerPartition
         
            !
            ! Purpose: Test bounds remapping for pointers.
            !
         
         Implicit None

         Real(8), Target :: A(0:20), C(22:26)
         
         Real(8), Pointer :: B(:)
         
         Integer, Target  :: G(100)
         Integer, Pointer :: H(:,:)
         
         Integer :: i
         
   !
   !##########
   !

         A = 1.0d0; C = 0.0d0
        

         Nullify(B)
         B => A(11:20)
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 1, 10
         
         Nullify(B)
         B => A
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 0, 20
         
         Nullify(B)
         B => A(:)
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 1, 21

         Nullify(B)
         B => C
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 22, 26         
         
         Nullify(B)
         B => C(:)
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 1, 5         

         Nullify(B)
         B(51:60) => A(11:20)
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 51, 60

         Nullify(B)
         B(1:5) => C
         print *, LBOUND(B), UBOUND(B)
         ! correct answer: 1, 5 
         
         Nullify(H)
         H(0:24,2:5) => G
         print *, LBOUND(H), UBOUND(H)
         ! correct answer: {0, 2}, {24,5}
         
         do i=1,Size(G)
            G(i) = i
         end do
         print *, H(5,:)
         ! correct answer: {6,31,56,81}

      End Program Test_PointerPartition[/fxfortran]
Steven_L_Intel1
1,590 檢視
Pointer remapping is standard F2003. If you create a new pointer with C_F_POINTER you can still deallocate using the original pointer.
回覆