Parallelization using OpenMP

Forums General questions about Fortran programming Parallelization using OpenMP

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by Alessandro Di Nola October 20, 2019 at 4:19 pm.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1650

    DM Chun
    User

    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 gridpoint such 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_new(:) and V(:) does not converge, and adding x_in, ia_com into the list of PRIVATE clause or !$OMP threadprivate(ia_com) into prog08_05m.f90 cannot 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?

    Thanks.

    Dongmin

    #1655

    Fabian Kindermann
    Moderator

    Dear Dongmin,

    it seems that at least ia_com, x_in and fret need 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. fminsearch should work in parallel mode. At least, I haven’t encountered any problems with it, yet.

    Try to set these three into the PRIVATE statement and see what happens. It would be great, if you could let men know whether it worked.

    Best,
    Fabian

    #1657

    DM Chun
    User

    Dear Fabian,

    Including ia_com, x_in and fret into the PRIVATE cannot fix the problem. The distance between V_new(:) and V(:) still does not converge. Should I modify external function in prog08_05m.f90?

    #1686

    Fabian Kindermann
    Moderator

    Dear Dongmin,

    are you changing any global variables within the function utility. This might also cause a problem.

    Best,
    Fabian

    #1702

    Alessandro Di Nola
    User

    Dear Dongmin,

    I guess you have to declare x_in and fret in the private statement and ia_com as 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 contains statement:

    !$omp threadprivate(ia_com)

    I hope this helps!
    Also, thanks to Fabian for this book and the forum, they are both a very useful resource!

    Best,
    Alessandro

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.