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

Problem: Looping over a subroutine

Harry_G_
Principiante
1.949 Vistas

I try to draw samples and calculate something by writing a subroutine.

! main file
program mymain
use mymodule
implicit none
real,dimension(10)::inputdata
real,dimension(:,:),allocatable::popmat
integer :: snr,pnr,i,nsimc
real :: sdot
integer, parameter :: nsim=100
real, dimension(nsim) :: sdotmat
inputdata = (/1,2,3,4,5,6,7,8,9,10/)
allocate(popmat(5,2))
popmat = transpose(reshape(inputdata,(/2,5/)))
snr=3
pnr=size(popmat,1)
do i=1,nsimc
 call resample(snr,pnr,popmat,sdot)
 sdotmat(nsimc) = sdot
end do
deallocate(popmat)
print*, sdotmat
end program mymain

The subroutine for simulation looks like:

module mymodule
contains
subroutine resample(snr,pnr,popmat,sdot)
implicit none
integer, intent(in) :: snr,pnr
real, dimension(pnr,2), intent(in) :: popmat
real, intent(out) :: sdot
real, dimension(snr,2) :: smat
integer :: m,i,l
real :: r
m=0
do while (m < snr)
 do i=1,pnr
  call random_number(r)
  l = int((real(pnr)-i+1)*r)+1
  if (l .GT. (snr-m)) then
   cycle
  end if
  m=m+1
  smat(m,1:2)=popmat(i,1:2)
 end do
end do
sdot = dot_product(smat(:,1),smat(:,2))
end subroutine resample
end module mymodule

If I write a code to draw random samples INSIDE the subroutine, it works properly. But when I loop over the subroutine from the main, it is compiled, but execution produces an error. Would you let me know what problems are in the codes above? Thanks!!

0 kudos
1 Solución
mecej4
Colaborador Distinguido III
1.949 Vistas

With the main program corrected as follows, the program runs with no errors.

program mymain
use mymodule
implicit none
real,dimension(10)::inputdata
real,dimension(:,:),allocatable::popmat
integer :: snr,pnr,i
real :: sdot
integer, parameter :: nsimc=100
real, dimension(nsimc) :: sdotmat
inputdata = (/1,2,3,4,5,6,7,8,9,10/)
allocate(popmat(5,2))
popmat = transpose(reshape(inputdata,(/2,5/)))
snr=3
pnr=size(popmat,1)
do i=1,nsimc
 call resample(snr,pnr,popmat,sdot)
 sdotmat(i) = sdot
end do
deallocate(popmat)
print*, sdotmat
end program mymain

 

Ver la solución en mensaje original publicado

3 Respuestas
mecej4
Colaborador Distinguido III
1.949 Vistas

The variable nsimc is used before its value has been set. On Line-18 of the main program, the subscript should probably be the loop variable i rather than nsimc. Perhaps, you meant "nsimc" and "nsim" to be one and the same.

 

Harry_G_
Principiante
1.949 Vistas

@mecej4: Thanks. I fixed the small error for the nsimc.

do nsimc=1,nsim
 call resample(snr,pnr,popmat,sdot)
 sdotmat(nsimc) = sdot
end do

Now it works. Thanks!

mecej4
Colaborador Distinguido III
1.950 Vistas

With the main program corrected as follows, the program runs with no errors.

program mymain
use mymodule
implicit none
real,dimension(10)::inputdata
real,dimension(:,:),allocatable::popmat
integer :: snr,pnr,i
real :: sdot
integer, parameter :: nsimc=100
real, dimension(nsimc) :: sdotmat
inputdata = (/1,2,3,4,5,6,7,8,9,10/)
allocate(popmat(5,2))
popmat = transpose(reshape(inputdata,(/2,5/)))
snr=3
pnr=size(popmat,1)
do i=1,nsimc
 call resample(snr,pnr,popmat,sdot)
 sdotmat(i) = sdot
end do
deallocate(popmat)
print*, sdotmat
end program mymain

 

Responder