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

Generic routine to accept pointers or allocatable

David2
Beginner
594 Views


Hi -

Does anyone have a suggestion about how to make generic routines in a module which can accept either an allocatable or a pointer as an argument?

At the moment I have resorted to writing a two routines, one for pointers and one for allocatables. The allocatable version makes pointers to the arguments and calls the pointer version, but I still can't use an interface statement because the type/kind/arugments all match!

So now for example I have

SUBROUTINE SETUP_INTERP_A(Xin,Yin,Xout,Yout,WEIGHTS)
IMPLICIT NONE
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Xin(:,:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Yin(:,:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Xout(:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Yout(:)

TYPE(INTERP_WEIGHTS), INTENT(OUT) :: WEIGHTS

REAL(SP), POINTER :: XinP(:,:)
REAL(SP), POINTER :: YinP(:,:)
REAL(SP), POINTER :: XoutP(:)
REAL(SP), POINTER :: YoutP(:)

NULLIFY(XinP,YinP,XoutP,YoutP)


IF(ALLOCATED(Xin)) XinP => Xin

IF(ALLOCATED(Yin)) YinP => Yin

IF(ALLOCATED(Xout)) XoutP => Xout

IF(ALLOCATED(Yout)) YoutP => Yout

CALL SETUP_INTERP_P(XinP,YinP,XoutP,YoutP,WEIGHTS)
END SUBROUTINE SETUP_INTERP_A


SUBROUTINE SETUP_INTERP_P(Xin,Yin,Xout,Yout,WEIGHTS)
IMPLICIT NONE
REAL(SP), POINTER, INTENT(IN) :: Xin(:,:)
REAL(SP), POINTER, INTENT(IN) :: Yin(:,:)
REAL(SP), POINTER, INTENT(IN) :: Xout(:)
REAL(SP), POINTER, INTENT(IN) :: Yout(:)

TYPE(INTERP_WEIGHTS), INTENT(OUT) :: WEIGHTS
...
...
...
END SETUP_INTERP_P


Another fine point of this probelm: is it bad form to pass allocatable arrays which do not have the attribute 'target' to setup_interp_a ? It seems to work?

David

0 Kudos
2 Replies
Steven_L_Intel1
Employee
594 Views
Generics cannot be distinguished by anything other than Type, Kind and Rank.

Within the routine itself, nothing you do to the allocatables is a problem. In the caller, though, lack of TARGET might cause problems if you modify the arrays through the pointers.
0 Kudos
David2
Beginner
594 Views

Using the pointer outside the routine has not caused a problem yet, but if this is not a safe method I will have to look for other options.

It is extremely useful for allowing pointers to data in defined types - which is probably also a bit sketchy...


0 Kudos
Reply