I need to perform both single precision and double precision arithmetics on a variable in different parts of my code. So basically, I declare the variable as single precision first. Then I call subroutine sub_a
which makes use of double precision version of the variable and performs double precision operations on that:
program main
implicit none
integer,parameter :: single = selected_real_kind(p=6,r=37)
integer,parameter :: double = selected_real_kind(p=15,r=307)
real(single),allocatable,dimension(:) :: A
real(double),allocatable,dimension(:) :: B
allocate(A(3),B(3))
A=2 ! single precision
A=A+3 ! single precision
print '(a,1x,3(f20.15))','sqrt(A),single:',sqrt(A)
print '(a,1x,I15)','mem_address of A before sub_a:',loc(A)
call sub_a(real(A,kind=double),B) ! double precision
print '(a,1x,3(f20.15))','sqrt(A),double:',B
contains
subroutine sub_a(a,b)
real(double),dimension(:),intent(in) :: a
real(double),dimension(:),intent(inout) :: b
print '(a,1x,I15)','mem_address of A in sub_a:',loc(a)
b=sqrt(a)
end subroutine sub_a
end program main
As seen in the code, I also obtained the memory address of A
prior to calling sub_a
and the version of A
inside sub_a
and they are expectedly different.
My questions are:
- Is the version of
A
inside thesub_a
allocated in the heap memory so I should not be worried about the size limitation? - is there any potential issue/bug in writing this example?
- is there a better way of capturing the purpose described in this example, specially for larger size arrays?
Many Thanks
Update: I haven't experienced any Memory issue for very large arrays, when using gfortran4.6 / ifort13.1 as compiler. I plan to use suggestion by @innoSPG as an alternative approach.