Dear all,
I am new to programming with Fortran's openMP application (using Intel Compiler version 18). I recently have run into a problem to which I could not find an answer (after 1 month of digging around). The issue is (see below for the code) that I would like to solve a function where I assign some limiting conditions in the main body of the code, which also should be used in the function. For example, I specify that my function should allow for a current value A_cur=a0(i,j) where i and j are my do-loop running variables.
The issue is that in my main body of code, this assignment works fine. But as soon as I "call" my function f(x) where x not equal A_cur (but A_cur is a constant used in the function), my code reads A_cur=0 instead of A_cur=a0(i,j).
Hence, my question is how to solve this issue without defining the function f(x,a_in) where I set, a_in=A_cur? I would be really grateful for some insights on this issue as well as some recommendations.
This code is supposed to illustrate my issue with a very simple assignment (which however captures the issue). In this code, I specify that a_com=i, whereby i is the running variable in my OMP loop. In the main code it correctly takes the assigned value but it does not do so in the function utility. (Note that this is a simplified version of the assignment problem I mention above where the assignment issue between main body of the code and the function does not affect the outcome, while in my example above it would).
---------------------------------------------------------------------------------------------------------------------------------
module constants
integer, parameter:: i4b=selected_int_kind(15)
integer, parameter:: ndp=selected_real_kind(15)
real(ndp), parameter:: zero=0.0d0
real(ndp), parameter:: one=1.0d0
real(ndp), parameter:: two=2.0d0
integer(i4b):: t_in, a_com
real(ndp):: y_cur,a_cur
contains
double precision function utility(in)
real(ndp),intent(in):: in
real(ndp):: v1, cons
if((t_in)<two)then
cons=1+in
write(*,*) 't_in<2'
else
cons=2+in
write(*,*) 't_in>2'
endif
write(*,*) 'a_com', a_com
v1=sqrt(cons)
utility=v1
end function utility
end module constants
program test_openMP
use constants
!# Define some variables
integer(i4b):: i,t,j
integer(i4b), parameter:: i_max=10, t_max=3, j_max=3
real(ndp):: result(0:i_max,0:t_max,0:j_max), a_pr(0:i_max,0:t_max), &
income(0:j_max)
real(ndp):: a_in,a_max
a_max=100
do i=0,i_max
do t=0,t_max
a_pr(i,t)=(a_max)/((1+0.05*t)**i)
enddo
enddo
do j=0,j_max
income(j)=(1.07)**(1+j)
enddo
do t=0,t_max
t_in=t
write(*,*)'Run for t', t, 'of', t_max
!$OMP PARALLEL DO PRIVATE (a_in,y_cur,a_cur,a_com)
do i=0,i_max
write(*,*)'Run for i', i, 'of', i_max
a_in=a_pr(i,t)
a_com=i
do j=0,j_max
y_cur=income(j)
a_cur=a_in+y_cur
result(i,t,j)=utility(a_cur)
enddo
enddo
!$OMP END PARALLEL DO
!a_com=o
enddo
do i=0,i_max
t=2
j=1
write(*,*) 'i', '|', 'a_pr','|', 'Result','|', 'Utility'
write(*,*)'###############################################'
write(*,*) i,'|', a_pr(i,t),'|', result(i,t,j),'|', utility(a_pr(i,t)+income(j))
enddo
end program test_openMP
--------------------------------------------------------------------------------------------------------------------
Best,
Sebastian