This topic contains 4 replies, has 3 voices, and was last updated by Alessandro Di Nola October 20, 2019 at 4:19 pm.
May 14, 2019 at 6:26 pm #1650
Hi. I’m learning Fortran and OpenMP at the same time because I’ve recognized that parallelization of the code is really important to reduce computational time. I’ve succeeded in writing a parallel code for solving standard optimal growth model using value function iteration with grid search method in one main program. And now I’m struggling to do that with interpolation method. As a example, in
prog08_05.f90, I’ve tried to modify the code to parallelize the part
! calculate optimal decision for every gridpointsuch that,
!$OMP PARALLEL DO PRIVATE( ia ) do ia = 1, NA ! initialize starting value and communicate resources x_in = a(ia) - c(ia) ia_com = ia call fminsearch(x_in, fret, 0d0, a(ia), utility) ! get optimal consumption and value function c(ia) = a(ia) - x_in V_new(ia) = -fret enddo !$OMP END PARALLEL DO
However the distance between
V(:)does not converge, and adding
ia_cominto the list of PRIVATE clause or
prog08_05m.f90cannot fix the problem. I think I have to make some more variables PRIVATE in each thread but got stuck. Would you please give me some advice?
DongminMay 19, 2019 at 9:46 pm #1655
it seems that at least
fretneed to be declared as private in your code, as they are given different values in each iteration of the loop and would get messed up in a parallel loop.
fminsearchshould work in parallel mode. At least, I haven’t encountered any problems with it, yet.
Try to set these three into the
PRIVATEstatement and see what happens. It would be great, if you could let men know whether it worked.
FabianMay 20, 2019 at 4:58 pm #1657
PRIVATEcannot fix the problem. The distance between
V(:)still does not converge. Should I modify external function in
prog08_05m.f90?August 18, 2019 at 10:07 am #1686
are you changing any global variables within the function
utility. This might also cause a problem.
FabianOctober 20, 2019 at 4:19 pm #1702
Alessandro Di Nola
I guess you have to declare
fretin the private statement and
ia_comas threadprivate in the module. The following code works on my PC:
!$omp parallel default(shared) private(ia,x_in,fret) !$omp do do ia = 1, NA ! initialize starting value and communicate resources x_in = a(ia) - c(ia) ia_com = ia call fminsearch(x_in, fret, 0d0, a(ia), utility) ! get optimal consumption and value function c(ia) = a(ia) - x_in V_new(ia) = -fret enddo !$omp end do !$omp end parallel
Then in the module globals you need to add the following line at the end of the declarations of variables and before the
I hope this helps!
Also, thanks to Fabian for this book and the forum, they are both a very useful resource!
You must be logged in to reply to this topic.