Hi,
Ok, so my example was too abrieviated w.r.t. what I am actually doing in my code. Below is a better stripped down version. The pointer is actually not returned but stored internally.
This test program compiles without warnings and runs fine on Cray, PGI, GNU and Intel and gives the correct output.
Cheers,
Oli
========== mod_test.f90 =================
MODULE mod_test
IMPLICIT NONE
PRIVATE
TYPE :: p3d
REAL, POINTER, DIMENSION(:,:,:) :: p
END TYPE p3d
TYPE (p3d) :: p_var(2)
INTEGER :: i=0
PUBLIC :: test_addjob, test_do
CONTAINS
SUBROUTINE test_addjob( var, nx, ny, nz )
IMPLICIT NONE
INTEGER, INTENT(IN) :: nx, ny, nz
REAL, INTENT(IN), TARGET :: var(nx, ny, nz)
i = i + 1
IF (i>2) STOP
p_var(i)%p => var
END SUBROUTINE test_addjob
SUBROUTINE test_do( )
IMPLICIT NONE
WRITE(*,*) p_var(1)%p(1,1,1)
WRITE(*,*) p_var(2)%p(1,1,1)
END SUBROUTINE test_do
END MODULE mod_test
========== test.f90 =================
PROGRAM test
USE mod_test
IMPLICIT NONE
REAL, ALLOCATABLE, TARGET :: a(:,:,:),b(:,:)
INTEGER :: i,j,k
ALLOCATE(a(2,3,4), b(2,3))
FORALL (i=1:2, j=1:3, k=1:4) a(i,j,k) = i + j*10 + k*100
FORALL (i=1:2, j=1:3 ) b(i,j ) = i + j*10
CALL test_addjob(a,10,20,30)
CALL test_addjob(b,10,20,1)
CALL test_do()
DEALLOCATE(a,b)
END PROGRAM test
===============================================
> -----Original Message-----
> From: Fortran 90 List [mailto:[log in to unmask]] On
> Behalf Of W.J. Metzger
> Sent: Freitag, 13. April 2012 15:30
> To: [log in to unmask]
> Subject: Re: 3d pointer to 2d target
>
> It may compile with no error message, but did you actually run it?
> I tried with gfortran and ifort. Both compile with no error message, but
> both give segmentation faults when you run the program. Bill Long has told
> you why.
>
> An easy change to the program makes actually writing the interface block
> unnecessary:
>
> program test
> real, target :: a(3,4,5), b(3,4)
> real, pointer :: p(:,:,:)
> ForAll (i=1:3, j=1:4, k=1:5) a(i,j,k) = i*100 + j*10 + k
> ForAll (i=1:3, j=1:4 ) b(i,j ) = i*10 + j
> print *, a(2, 3, 1)
> call get_ptr(p,a,3,4,5)
> print *, p(2, 3, 1)
> print *, b(2, 3 )
> call get_ptr(p,b,3,4,1)
> print *, p(2, 3, 1)
>
> contains
> subroutine get_ptr(p,var,nx,ny,nz)
> real, pointer :: p(:,:,:)
> integer :: nx,ny,nz
> real, target :: var(nx,ny,nz)
> p => var
> end subroutine get_ptr
> end program test
>
>
> Cheers, Wes
>
> On Fri, 13 Apr 2012, Bill Long wrote:
>
> > On 4/13/12 2:52 AM, Oliver Fuhrer wrote:
> >> Dear all,
> >>
> >> Thanks for all your replies! I think I found another solution to problem.
> >> I am by no means a Fortran standards expert, but it seems to run with
> PGI,
> >> Cray and GNU compilers...
> >>
> >> subroutine get_ptr(p,var,nx,ny,nz)
> >> real, pointer :: p(:,:,:)
> >> integer :: nx,ny,nz
> >> real, target :: var(nx,ny,nz)
> >> p => var
> >> end subroutine get_ptr
> >>
> >> program test
> >> real, target :: a(3,4,5), b(3,4)
> >> real, pointer :: p(:,:,:)
> >> call get_ptr(p,a,3,4,5)
> >> call get_ptr(p,b,3,4,1)
> >> end program test
>
> Dr. W. J. Metzger Experimental High Energy Physics Group
> tel. +31-24-3653127 Faculty of Science
> +31-24-3652099 (secr.) Radboud University Nijmegen
> fax. +31-24-3652191 Heyendaalseweg 135
> 6525 AJ Nijmegen, The Netherlands
> e-mail: [log in to unmask] or [log in to unmask]
> http://home.cern.ch/metzger/ or http://www.hef.ru.nl/~wes
|