1

I am writing some code, in which I need to use some modules given to me by my guide. Now, in these modules, there are subroutines which take dummy arrays, which have a fixed dimension. However, I need to pass some allocatable arrays to these functions.

What I want to know is whether there is a way to pass allocatable arrays as arguments to subroutines or functions whose dummy arguments are of a fixed dimension.

Here is the actual subroutine which is to be used:

function crosscorr(x, y, n, k)
! Cross-correlation of array x and y for a given time shift k.
implicit none
integer :: n, k
real(8), dimension(0:n-1) :: x, y
real(8) crosscorr
if (k.ge.0) then
    crosscorr = dot_product(x(0:n-1-k), y(k:n-1))
else
    crosscorr = dot_product(x(-k:n-1), y(0:n-1+k))
endif
end function crosscorr
!!!---------------------------------------------------------------------!!!


!!!---------------------------------------------------------------------!!!
subroutine xcorr_full(x, y, n, shift, delay, ccmax, ccpol)
! Cross-correlation of array x and y.
! Return time shift, correlation coefficient, and polarity at maximum correlation.
implicit none
integer :: n, shift, delay, ccpol
real(8), dimension(0:n-1) :: x, y
real(8) :: cc, ccmax, ccmin
integer :: k, kmin
!f2py intent(in)  :: x, y, n, shift
!f2py intent(out) :: delay, ccmax, ccpol
!f2py intent(hide):: c, k, kmin, ccmin

shift = 1
ccmax = 0
ccmin = 0
kmin = 0
do k = -n+1,n-1,shift
    cc = crosscorr(x,y,n,k)   
    if (cc.gt.ccmax) then
        ccmax = cc
        delay = k
    endif
    if (cc.lt.ccmin) then
        ccmin = cc
        kmin = k
    endif
enddo
if (ccmax.gt.-ccmin) then
    ccpol = 1
else
    ccmax = -ccmin
    delay = kmin
    ccpol = -1
endif
ccmax = ccmax/sqrt( dot_product(x,x) * dot_product(y,y) )

end subroutine xcorr_full

And here is the code from the program:

real(8), allocatable :: v(:,:),vref_win(:),v_win(:)

allocate(vref_win(0:ending-starting))
allocate(v_win(0:ending-starting))
vref_win(0:ending-starting)=v(starting-1:ending-1,ref_t)


do i=0,n_stn-1
    v_win(0:ending-starting)=v(starting-1:ending-1,i)
    call xcorr_full(vref_win,v_win,ending-starting+1,1,t_corr,r,sign)
enddo

The whole program is a bit long, so I am not posting it here, but all the variables are properly defined, the array v has data, and is indexed correctly

This is the error I am getting if I just try to pass the arrays normally:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

I am using the -fcheck=all flag while compiling using gfortran(v7.3.0)

My question is however not specifically dependent on this program. Any explanation for this, as well as any reading resources, would be very helpful since I could not find any proper resources which could help answer this exact question.

Yuki.kuroshita
  • 759
  • 9
  • 29

0 Answers0