Totally different results on Intel and Nvidia FORTRAN compilers for DO CONCURRENT loop

Hello,

I’m trying to parallelize the following code with do concurrent construct (NP = 4, IC = 5) ,

DO I = 1, NI ! NI = NP*IC
TMP = TMP + FLOAT(I)
A(I) = TMP
ENDDO

Parallel version,

SUBROUTINE PARLDO(A)
USE CONST !NP = 4, IC = 5
IMPLICIT NONE
REAL, DIMENSION(NI), INTENT(OUT) :: A
INTEGER :: I,K,J=0, SHIFT=0
REAL :: TMP=0.0, FLTI
DO CONCURRENT(I = 1 : NP)
DO K = I+IC*(I-1)-J , IC*I
TMP = TMP + FLOAT(K)
A(K) = TMP
ENDDO
J = I
ENDDO
END SUBROUTINE

Intel results (first data for the sequence loop above),

Nvidia results,

ONLY NVIDIA IS OK!

What is this? My bug or Intel ( :) )

All was compiled with -fopenmp option for latest version of compiler.

THANKS FOR ANY SUGGESTION!

Hi @mmaxutov,

Thanks for the report. This question concerns NVFORTRAN and Fortran behavior rather than Nsight Graphics. I’m moving the topic to a more proper one.

Thanks
An

Ok, thanks!

BTW, the difference remains when this code is modified to this one,

DO CONCURRENT( I = 1: NI )REDUCE(+:TMP) ! NI = NP*IC
TMP = TMP + FLOAT(I)
A(I) = TMP
ENDDO

Hi mmaxutov,

I haven’t used the Intel compiler with DO CONCURRENT, but my best guess is that they aren’t implicitly privatizing “TMP”. Try adding a “LOCAL” clause to see if this fixes is the issue.

DO CONCURRENT(I = 1 : NP) LOCAL(TMP)
DO K = I+IC\*(I-1)-J , IC\*I

For “REDUCE”, a reduction variable’s value can’t reliably be taken within the body of the loop. Each thread will have it’s own private partial reduction variable (after the loop, the compiler inserts code for the final reduction from the partial reduction variables), so the value of TMP won’t be correct when compared to serial execution.

-Mat

Thank you Mat for your reply!

When the LOCAL clause is used, then the variable is not parallelized (every thread has an own copy of this variable). When REDUCE operator is used, the variable will be parallelized but “race condition” will run for the conflicts avoid. Do I get it right?

Unfortunately, it doesn’t work in both cases.

First, colorized text of the full program,

Second, the results:

  1. Intel

 2. Nvidia

UBUNTU 25.04, latest compilers. ONLY NVIDIA(and GFORTRAN!) gives a correct results!

On second look, the code does have a loop carried dependency, so what could be happening is that NV is detecting the dependency so running serially while Intel is still running in parallel.

If you add the flag “-Minfo=accel” the compiler will tell you if the loop was parallelized.

The loop isn’t parallelizable since the value of TMP is dependent on the previous loop iteration’s value.

You could do something like this which accumulates the values of “I”:

DO CONCURRENT (I = 1:N) REDUCE(TMP)
  TMP = TMP + FLOAT(I)
ENDDO

But trying to capture the intermediate value into A(I) in a parallel reduction is a problem since each thread will have it’s own private partial reduction variable. Hence the value of TMP will vary by the number of threads that are used and not match the serial execution.

This really isn’t a race condition, which is when threads read/write into the same memory, but rather a dependency issue.

If you could post the code in a code block rather than a screen shot so I can copy it, I can look at it in more depth. Also, letting know the compiler versions you’re using as well as the compiler flags for each, that would be helpful.

-Mat

Thanks again Mat!

I don’t know ho to paste here a code block, maybe a simply text of the program will be useful,

MODULE CONST
INTEGER, PARAMETER :: NP = 4, IC = 5
INTEGER            :: NI
END MODULE
!
PROGRAM MAIN
USE CONST
USE OMP_LIB
IMPLICIT NONE
INTEGER                         :: I, COUNT=0
REAL                            :: TMP=0.0,FLTI
REAL, DIMENSION(:), ALLOCATABLE :: A
INTERFACE
SUBROUTINE PARLDO(A)
USE CONST
REAL, DIMENSION(NI), INTENT(OUT) :: A
END SUBROUTINE
END INTERFACE
!
NI = NP*IC; ALLOCATE(A(NI))
CALL OMP_SET_NUM_THREADS(NP)
!
DO I = 1, NI ! NI = NP*IC
TMP = TMP + FLOAT(I)
A(I) = TMP
ENDDO
!
PRINT*,A
PRINT*,""
A = 0.0
CALL PARLDO(A)
PRINT*,A
DEALLOCATE(A)
END PROGRAM
!
SUBROUTINE PARLDO(A)
USE CONST !NP = 4, IC = 5
IMPLICIT NONE
REAL, DIMENSION(NI), INTENT(OUT) :: A
INTEGER                          :: I,K,J=0, SHIFT=0
REAL                             :: TMP=0.0, FLTI
! DO CONCURRENT(I = 1 : NP)(TMP)
!     DO K =  I+IC\*(I-1)-J , IC\*I
!       TMP  = TMP + FLOAT(K)
!       A(K) = TMP
!     ENDDO
!     J = I
! ENDDO
DO CONCURRENT( I = 1: NI )LOCAL(TMP) !REDUCE(+:TMP)
TMP = TMP + FLOAT(I)
A(I) = TMP
ENDDO
END SUBROUTINE

Another issue regarding the compiler version and flags:

INTEL: ifx -fopenmp -O2, compiler version : ifx (IFX) 2026.1.0 20260617

NVIDIA: nvfortran -fopenmp -O2, compiler version: nvfortran 26.5-0

CPU: Intel Core 9 Ultra 285HX

IDE: NetBeans 21

Thanks, but it looks like I need the dependent “const” module source file as well.

For nvfortran, the flag to enable DO CONCURRENT parallelization is “-stdpar=multicore”, targeting multicore CPU and “-stdpar=gpu” to offload to GPUs. So for us, you weren’t actually running in parallel. Though I think we’d detect the dependency and run serially anyway, but can check that if you post the module.

Mat, its already corrected on the post above :)

However another copy,

MODULE CONST
INTEGER, PARAMETER :: NP = 4, IC = 5
INTEGER :: NI
END MODULE

PS I use your recommendations for compiler options, thank you!

Using offload will be my next task.

PS: hte code string on the code block above is incorrect! Must be

 DO K =  I+IC*(I-1)-J , IC*I

instead this one,

DO K =  I+IC\*(I-1)-J , IC\*I

Oops, sorry my cut and paste error. I updated your post to put it a code block (the “</>” icon) for easier cutting.

Sure enough, when adding the -stdpar flag, it gets the expected same “wrong” results as Intel:

% nvfortran test.F90 -Minfo=accel -stdpar=multicore; a.out
parldo:
     49, Generating Multicore code
         49, Loop parallelized across CPU threads
    1.000000        3.000000        6.000000        10.00000
    15.00000        21.00000        28.00000        36.00000
    45.00000        55.00000        66.00000        78.00000
    91.00000        105.0000        120.0000        136.0000
    153.0000        171.0000        190.0000        210.0000

    1.000000        2.000000        3.000000        4.000000
    5.000000        6.000000        7.000000        8.000000
    9.000000        10.00000        11.00000        12.00000
    13.00000        14.00000        15.00000        16.00000
    17.00000        18.00000        19.00000        20.00000

Removing the local cause makes “TMP” a shared variable (which is a race condition), so the results are random depending on the order of execution of the threads.

% nvfortran test.F90 -Minfo=accel -stdpar=multicore ; a.out
parldo:
     49, Generating Multicore code
         49, Loop parallelized across CPU threads
    1.000000        3.000000        6.000000        10.00000
    15.00000        21.00000        28.00000        36.00000
    45.00000        55.00000        66.00000        78.00000
    91.00000        105.0000        120.0000        136.0000
    153.0000        171.0000        190.0000        210.0000

    129.0000        65.00000        31.00000        133.0000
    5.000000        37.00000        44.00000        63.00000
    14.00000        182.0000        55.00000        77.00000
    27.00000        28.00000        92.00000        93.00000
    110.0000        128.0000        152.0000        172.0000
% a.out
    1.000000        3.000000        6.000000        10.00000
    15.00000        21.00000        28.00000        36.00000
    45.00000        55.00000        66.00000        78.00000
    91.00000        105.0000        120.0000        136.0000
    153.0000        171.0000        190.0000        210.0000

    75.00000        30.00000        3.000000        99.00000
    16.00000        42.00000        105.0000        36.00000
    20.00000        85.00000        11.00000        48.00000
    98.00000        74.00000        51.00000        91.00000
    28.00000        60.00000        55.00000        95.00000

Bottom line is that the loop is not parallelizable.

Ok, Mat. I understand now. Many thanks for your great work!

Sincerely,

-Malik.

Getting back to the blocked version, the problem here is that the initial value of TMP needs to be set the last value computed in the previous block’s last step. Though since this is a simple arithmetic series, we can pre-compute the value thus removing the dependency.

SUBROUTINE PARLDO(A)
USE CONST !NP = 4, IC = 5
IMPLICIT NONE
REAL, DIMENSION(NI), INTENT(OUT) :: A
INTEGER :: I, K, START, END
REAL    :: TMP

DO CONCURRENT (I = 1:NP) LOCAL(K, TMP, START, END)
    START   = IC*(I-1) + 1
    END   = IC*I
    TMP = REAL(START-1) * REAL(START) / 2.0
    DO K = START, END
        TMP  = TMP + FLOAT(K)
        A(K) = TMP
    ENDDO
ENDDO
END SUBROUTINE
% nvfortran -stdpar=multicore test2.F90 ; a.out
    1.000000        3.000000        6.000000        10.00000
    15.00000        21.00000        28.00000        36.00000
    45.00000        55.00000        66.00000        78.00000
    91.00000        105.0000        120.0000        136.0000
    153.0000        171.0000        190.0000        210.0000

    1.000000        3.000000        6.000000        10.00000
    15.00000        21.00000        28.00000        36.00000
    45.00000        55.00000        66.00000        78.00000
    91.00000        105.0000        120.0000        136.0000
    153.0000        171.0000        190.0000        210.0000

This only works because we can precompute the starting value of TMP for each block. A more general solution is to split this into a three phased blocked parallel prefix-sum. The first phase computes the values for each block, the second computes the prefix sum for each block (i.e. the initial value of TMP), then phase three is the same as before.

Phases 1 and 3 are parallel, 2 is sequential.

MODULE CONST
INTEGER, PARAMETER :: NP = 4, IC = 5
INTEGER            :: NI
END MODULE
!
PROGRAM MAIN
USE CONST
USE OMP_LIB
IMPLICIT NONE
INTEGER                         :: I, COUNT=0
REAL                            :: TMP=0.0,FLTI
REAL, DIMENSION(:), ALLOCATABLE :: A
INTERFACE
SUBROUTINE PARLDO(A)
USE CONST
REAL, DIMENSION(NI), INTENT(OUT) :: A
END SUBROUTINE
END INTERFACE
!
NI = NP*IC; ALLOCATE(A(NI))
CALL OMP_SET_NUM_THREADS(NP)
!
DO I = 1, NI ! NI = NP*IC
TMP = TMP + FLOAT(I)
A(I) = TMP
ENDDO
!
PRINT*,A
PRINT*,""
A = 0.0
CALL PARLDO(A)
PRINT*,A
DEALLOCATE(A)
END PROGRAM

SUBROUTINE PARLDO(A)
USE CONST !NP = 4, IC = 5
IMPLICIT NONE
REAL, DIMENSION(NI), INTENT(OUT) :: A
INTEGER :: I, K, START, END
REAL    :: BLOCKSUM(NP), OFFSET(NP), TMP

! Phase 1: local sum of each block
DO CONCURRENT (I = 1:NP) LOCAL(K, START, END, TMP)
    START  = IC*(I-1) + 1
    END  = IC*I
    TMP = 0.0
    DO K = START, END
        TMP = TMP + FLOAT(K)
    ENDDO
    BLOCKSUM(I) = TMP
ENDDO

! Pass 2: exclusive prefix sum over the NP block sums
OFFSET(1) = 0.0
DO I = 2, NP
    OFFSET(I) = OFFSET(I-1) + BLOCKSUM(I-1)
ENDDO

! Pass 3: fill each block using its precomputed starting value
DO CONCURRENT (I = 1:NP) LOCAL(K, START, END, TMP)
    START  = IC*(I-1) + 1
    END  = IC*I
    TMP = OFFSET(I)
    DO K = START, END
        TMP  = TMP + FLOAT(K)
        A(K) = TMP
    ENDDO
ENDDO
END SUBROUTINE

I was thinking of doing a something like this, Mat

But you told me, how!

BTW

I shortened the text a little while keeping your changes,

SUBROUTINE PARLDO(A)
USE CONST
IMPLICIT NONE
REAL, DIMENSION(NI), INTENT(OUT) :: A
INTEGER                          :: I, K, SHIFT
REAL                             :: TMP
DO CONCURRENT (I = 1:NP) LOCAL(TMP, SHIFT)
   SHIFT   = IC*(I-1) + 1
   TMP = FLOAT(SHIFT-1) * FLOAT(SHIFT) / 2.0
   DO K = SHIFT, IC*I
       TMP  = TMP + FLOAT(K)
       A(K) = TMP
   ENDDO
ENDDO
END SUBROUTINE

-Malik