DO CONCURRENT: compiler flags to enable parallelization

What is the probability that cons<=0?

The pattern is deterministic, so I could add an early exit clause:

! Initialize v_new
            v_max = large_negative
            ap_ind = 0
            ! Choose a' optimally by stepping through all possible values
            do ap_c=1,n_a
                aprime_val = ap_grid(ap_c)
                cons = R*a_val + z_val - aprime_val
                if (cons>0.0d0) then
                    v_temp = f_util(cons) + beta*EV(ap_c,z_c)
                    !v_temp = f_util(cons) + beta*sum(v(ap_c,:)*z_tran(z_c,:))
                    if (v_temp>v_max) then
                        v_max = v_temp
                        ap_ind = ap_c
                    end if
                else
                    exit
                endif
            enddo !end a'

I did not do it in the initial version because I read somewhere that introducing exit clauses within nested loops can be bad for performance. Of course the exit clauses saves on the number of iterations on the ap_c loop: it may prevent some optimizations however so the net effect on performance is not clear a priori.

I recommend adding default(none) to all do concurrent constructs.That will force the addition of locality specifiers that are very helpful in preventing race conditions. I would go so far as to say don’t use a compiler that doesn’t support locality specifiers unless you have sufficient experience with parallel programming to avoid race conditions without the aid of the compiler. Recent versions of the GCC, HPE, Intel, Lfortran, LLVM, NAG and NVIDIA compilers support locality specifiers. I haven’t verified the NVIDIA support, I would be surprised if it doesn’t support locality specifiers.

Bill Long, retired from HPE, refers to default none as good code hygiene — much like implicit none. Bill led the Fortran standard committee’s high-performance computing (HPC) subgroup. I believe Bill was on the standard committee when do concurrent was developed and I know he led the HPC subgroup when locality specifiers were developed.