# Gfortran with do concurrent for windows 10

**URL:** <https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392>\
**Category:** Help\
**Created:** [August 24, 2023, 12:08pm UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392 "2023-08-24T12:08:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 24, 2023, 12:08pm UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/1 "2023-08-24T12:08:40Z")

</div>

For `gfortran (Windows OS)` which compiler flag or options should be used to enforce **do concurrent**? I find it is `/Qparallel` for intel and should be used with O2 or O3. It is not active by default there.

> **[parallel, Qparallel](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-2/parallel-qparallel.html)**
>
> Tells the auto-parallelizer to generate multithreaded code for loops that can be safely executed in parallel. This feature is only available for ifort.

Is it `-O3` for gfortran ( Windows 10 )?

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [August 26, 2023, 5:15pm UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/2 "2023-08-26T17:15:37Z")

</div>

This previous thread has some info, but not sure about gfortran + windows… (I myself have no experience with do concurrent yet 🚴‍♂️ )

> [@DO CONCURRENT: compiler flags to enable parallelization](https://fortran-lang.discourse.group/t/do-concurrent-compiler-flags-to-enable-parallelization/4300):
>
> If I understand correctly, a do concurrent construct does not necessarily imply that the code inside the block will run in parallel, because (for instance) the compiler might estimate that the compute task does not justify the overhead of parallelization. On the other hand, I have doubts about what must be done to allow the compiler to consider a possible parallelization. More specifically, my questions are: Is it correct that parallelization of do and do concurrent loops is deactivated by de…

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 26, 2023, 11:55pm UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/3 "2023-08-26T23:55:55Z")

</div>

I got the best performance using `-O3` together with `-march=native`. Have not gone deeper yet but it looks like a good area.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 27, 2023, 8:37am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/4 "2023-08-27T08:37:13Z")

</div>

> [@Shahid](#):
>
> `-O3` together with `-march=native`

Sure, but these flags are not related to loop parallelization.

According to the other discussion thread, I would give a try to `-ftree-parallelize-loops=N`

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [August 27, 2023, 8:44am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/5 "2023-08-27T08:44:28Z")

</div>

**I do not know how DO CONCURRENT can be multi-threaded with Gfortran on Windows !**

I am using Gfortran 11.1.0 on Windows 10, but do not get parallel operation with DO CONCURRENT.

The following program example does a simple use of MATMUL in an !$OMP PARALLEL DO and a DO CONCURRENT loop.  
I compiled with and without -fopenmp

By reporting both elapsed time and CPU time, it clearly shows if multiple threads are being used from the ratio of CPU time to Elapsed (system\_clock), expressed as %

I am not an experienced user of DO CONCURRENT, to know if it can be mult-threaded or just the next generation of FORALL.

I am sceptical on the use of auto-parallelizer, as with !$OMP I recommend explicit use of SHARED and PRIVATE with DEFAULT(NONE)

```auto
! The following started out as a short example, but expanced with more documentation. 
! test of do concurrent to check operation
   real*8, allocatable :: A(:,:,:), C(:,:,:)
   integer :: n = 1000, P=50, i
   real*8 :: times(2)

   call report_compiler_settings (6) 

   call delta_time ('START', times)
   allocate ( A(n,n,P), C(n,n,P) )

   call random_number ( a )
   call delta_time ('INITIALISE', times)

   do concurrent ( i = 1:P )
     c(:,:,i) = MATMUL ( A(:,:,i), A(:,:,i) )
   end do
   call delta_time ('Do concurrent MATMUL', times)

   !$OMP PARALLEL DO SHARED ( A,C,P )
   do i = 1,P
     c(:,:,i) = MATMUL ( A(:,:,i), A(:,:,i) )
   end do
   !$OMP END PARALLEL DO
   call delta_time ('OMP DO MATMUL', times)

   End

   subroutine delta_time ( desc, times )
     character desc*(*)
     real*8 :: times(2)

     real*8 :: last_cpu = 0, cpu, per
     integer*8 :: last_tick = -1, tick, rate
     
     call system_clock ( tick, rate )
     call cpu_time (cpu)
     if ( last_tick < 0 ) then
       last_tick = tick
       last_cpu = cpu
     end if

     times(1) = dble(tick-last_tick) / dble (rate)
     times(2) = cpu-last_cpu
     per = times(2)/max (times(1),1.d-6)*100.
     last_tick = tick
     last_cpu = cpu
     write (*,11) desc, times, per
 11 format (a,2f10.4,f10.4,'%')
   end subroutine delta_time
   
   subroutine report_compiler_settings (lu) 
 use iso_fortran_env 
! 
     character*128 run_date_string 
     character*128 command_string 
     integer*4 lu 
! 
! build time variables ( when compiled ) 
! 
     write (lu,*) ' ' 
     write (lu,*) '==== Build Information ========== ' 
     write (lu,*) 'Compiler Options : ', compiler_options () 
     write (lu,*) 'Compiler Version : ', compiler_version () 
! 
! run time variables ( when run ) 
! 
     call date_and_time_string (run_date_string) 
     call get_command (command_string) 
! 
     write (lu,*) ' ' 
     write (lu,*) '==== Run Time Information ========== ' 
     write (lu,11) ' Run Date', trim(run_date_string) 
     write (lu,11) ' Run Command', trim(command_string) 
! 
  11 format (a,t22,': ',a)
   end subroutine report_compiler_settings
  
   subroutine date_and_time_string (string) 
     character string*(*) 
     character :: months(12)*3 = (/ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' /) 
     integer value(8) 
  
     call DATE_AND_TIME (values=value) 
     write (string,11) value(3),months(value(2)),value(1), value(5:8) 
  11 format ('Run on ',i0,'-',a,'-',i4,' at ',i0,':',i2.2,':',i2.2,'.',i3) 
   end subroutine date_and_time_string

```

My .bat file to test 2 compile options is:

```auto
del %1.exe
del %1.o

time /T >> %1.log

set vec=-march=native
set options=-O3 %vec% -ffast-math
 
gfortran %1.f90 %options% -o %1.exe
 
dir %1.* /od >> %1.log
echo ================================================================ >> %1.log
time /T >> %1.log
set options >> %1.log

%1 >> %1.log

set options=-O3 %vec% -ffast-math -fopenmp
 
gfortran %1.f90 %options% -o %1.exe
 
dir %1.* /od >> %1.log
echo ================================================================ >> %1.log
time /T >> %1.log
set options >> %1.log

%1 >> %1.log

notepad %1.log

```

I also tried options=-O3 -ffast-math -ftree-parallelize-loops=8, with no apparent change ?

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 27, 2023, 9:21am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/6 "2023-08-27T09:21:06Z")

</div>

I am trying it but do not understand what is `N` here and how it should be adjusted.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 27, 2023, 9:34am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/7 "2023-08-27T09:34:42Z")

</div>

`N` is supposed to be the number of threads used for the parallelization.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 27, 2023, 11:00am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/8 "2023-08-27T11:00:35Z")

</div>

Here is my code:

( I am trying to understand normal nested loop vs do concurrent here )

[main.f90](https://fortran-lang.discourse.group/uploads/short-url/6pcHYtsVFsVm93pJi4SS7dfZrlg.f90) (1.8 KB)

_ **For do concurrent** _

1. _The first command I use:_

> gfortran main.f90

gives output time

> 41.953 seconds.

1. _The next command:_

> gfortran main.f90 -ftree-parallelize-loops=8

gives output time

> 41.594 seconds.

1. _The next command:_

> gfortran main.f90 -O3 -march=native

gives output time

> 7.188 seconds.

**Note:** gcc version is 11.3.0

_ **For normal do loop** _

1. _The first command I use:_

> gfortran main.f90

gives output time

> 57.859 seconds.

1. _The next command:_

> gfortran main.f90 -O3 -march=native

gives output time

> 20.391 seconds.

So the question remains: How is do concurrent functioning here? Why the performance is better when using do concurrent (without enforcing parallel instructions )than a normal loop?

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 27, 2023, 11:53am UTC](https://fortran-lang.discourse.group/t/gfortran-with-do-concurrent-for-windows-10/6392/9 "2023-08-27T11:53:35Z")

</div>

Thanks for sharing the files. I did some testing

**1) with OpenMP**

 ![OpenMP](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/7/78040efadf742fb5a961fdcdd106c00224230887.jpeg)

**2) Without OpenMP**

 ![no_openMP](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/d/dbfdf7271da7737ac0b281a6587e62211f4cfcb1.jpeg)

**3) Enforcing parallel**

 ![parallel](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/9a5e8c007e76e495173cce30671b9cb56c360acf.jpeg)

> [@JohnCampbell](#):
>
> with no apparent change

It seems like that.
