# Clarification on DO CONCURRENT

**URL:** <https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647>\
**Category:** Uncategorized\
**Created:** [August 7, 2021, 6:18am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647 "2021-08-07T06:18:26Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [May 18, 2023, 7:50am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/21 "2023-05-18T07:50:44Z")

</div>

There is non mention on the standard of threads, even though both **ifort** and **ifx** need the -qopenmp option, and clearly **ifort** uses (wrongly) multiple threads.

But, as the phrasing is similar to that used in openmp, who wrote the standard was thinking about threads and on how to make easier for the compiler to parallelize a `do concurrent`.

`do concurrent` is not related to coarrays, that is the way to achieve parallelism in Fortran with multiple images and _share_ memory between these images (not really share, as the share concept is not present in the standard).

---

<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:** [May 18, 2023, 10:56am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/22 "2023-05-18T10:56:29Z")

</div>

> [@JohnCampbell](#):
>
> > [@egio](#):
> >
> > ```auto
> > do concurrent(i = 1:NP) local_init(a)
> > a = a*2
> > b(i) = a + i
> > end do 
> > 
> > ```
> 
> In this example, does “do concurrent” imply a multi-thread activity ?

It implies nothing. The compiler is free to execute a classical do loop, a classical do loop but out of order, to launch thread with OpenMP or any other API, to launch new processes with MPI or whatever…

The equivalent OpenMP code would rather be with `aa = a*2`, not `aa = a*2**i`, as in the do concurrent version `a` is reset to the global value at the begining of each iteration.

---

<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:** [May 18, 2023, 11:15am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/23 "2023-05-18T11:15:58Z")

</div>

> [@PierU](#):
>
> do concurrent version `a` is reset to the global value at the begining of each iteration

My apologies for not understanding the new standard, although I do find this approach a bit baffling.  
Why introduce this “local\_init(a)” approach when using “aa = a\*2” would be less confusing to Fortran users.  
Is this an unnecessary change to Fortran ?

---

<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:** [May 18, 2023, 11:41am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/24 "2023-05-18T11:41:37Z")

</div>

> [@JohnCampbell](#):
>
> Why introduce this “local\_init(a)” approach when using “aa = a\*2” would be less confusing to Fortran users.

This avoids declaring an new variable, but the point is also that this is just an example to illustrate how it works, and which is well simpler than many usage cases.

You could also argue that the OpenMP `firstprivate` is useless for the very same reason.

---

<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:** [May 19, 2023, 2:20am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/25 "2023-05-19T02:20:52Z")

</div>

@PierU  
Firstprivate does have a use in OMP PARALLEL DO for initialising private copies.

If I understand your explanation, I am struggling to understand why “local\_init(a)” is defined as re-setting the value of “a” for each itteration of the DO CONCURRENT.  
There is no other construct in Fortran, so makes a very significant change to the way DO loops are approached. I think this is a very unnecessary change to the concept of a Fortran loop, with so little gain. Using aa = a\*2 is a much easier and Fortran compatible approach.

I just wonder how this concept was introduced into the Fortran standard.

Or have I misunderstood your description ?

---

<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:** [May 19, 2023, 6:42am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/26 "2023-05-19T06:42:51Z")

</div>

> [@JohnCampbell](#):
>
> Firstprivate does have a use in OMP PARALLEL DO for initialising private copies.

Indeed, but `firstprivate(a)` could be replaced by

```auto
!$OMP PARALLEL PRIVATE(aa)
aa = 2*a
...

```

---

<div class="post-metadata">

**Author:** ![Ali](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ali/32/751_2.png) [@Ali](https://fortran-lang.discourse.group/u/Ali)\
**Post date:** [December 14, 2023, 12:01pm UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/27 "2023-12-14T12:01:09Z")

</div>

Does gfortran support SHARED, LOCAL\_INIT, LOCAL within the do concurrent construct? I’ve tested do concurrent using gfortran, ifx, ifort, and nvfortran, and it seems that they might not be implemented in gfortran.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 17, 2023, 7:24pm UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/28 "2023-12-17T19:24:20Z")

</div>

As of `gfortran 13.2`, it does not:

```plaintext
$ gfortran do_concurr.f90
do_concurr.f90:6:28:

    6 | do concurrent(i = 1:NP) local_init(a)
      | 1
Error: Syntax error in DO statement at (1)

```

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 17, 2023, 8:15pm UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/29 "2023-12-17T20:15:54Z")

</div>

Just as a followup to my March 2022 reply, Intel’s LLVM-based ifx compiler does not support -parallel at all. It **will** (attempt to) parallelize `DO CONCURRENT` if you enable OpenMP, even if you don’t use OpenMP otherwise.

---

<div class="post-metadata">

**Author:** ![mklemm](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@mklemm](https://fortran-lang.discourse.group/u/mklemm)\
**Post date:** [December 20, 2023, 9:59am UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/30 "2023-12-20T09:59:59Z")

</div>

> [@Ali](#):
>
> s gfortran support SHARED, LOCAL\_INIT, LOCAL within the do concurrent construct? I’ve tested do concurrent using gfortran, ifx, ifort, and nvfortran, and it seems that they might not be implemented in gfortran.

GFortran does not support the locality specifiers. There are two feature requests for this in the GCC database: [101602 – [F2018] local and local\_init are not supported in DO CONCURRENT](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101602) and [106005 – (F2023) Support for REDUCE clause in DO CONCURRENT loop](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106005).

> [@JohnCampbell](#):
>
> If I understand your explanation, I am struggling to understand why “local\_init(a)” is defined as re-setting the value of “a” for each itteration of the DO CONCURRENT.

OpenMP PRIVATE and FIRSTPRIVATE are different from DO CONCURRENT LOCAL and LOCAL\_INIT. Like mentioned before OpenMP data-sharing attributes privatize per thread, while DO CONCURRENT privatizes per iteration. OpenMP’s specification is clear about that a thread receives such a private copy (with initialization in case of FIRSTPRIVATE). DO CONCURRENT on the other had does not have the notion of threads. It just says the loop can be executed in any order. So, you cannot assume that there are even threads (and how many of them) or some other form of concurrency (like SIMD). So, LOCAL\_INIT has to re-init the variable for every iteration as this is the only way to have a well-defined definition of the variable in the loop body for all different concurrent execution mechanisms.

As side note: when implementing this on top of OpenMP, one can actually map LOCAL init to PRIVATE, as the a value from a previously executed iteration can viewed as some random, uninitialized value. For LOCAL\_INIT, the closest OpenMP correspondence would be LINEAR(variable:0).

---

<div class="post-metadata">

**Author:** ![wyphan](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wyphan/32/632_2.png) [@wyphan](https://fortran-lang.discourse.group/u/wyphan)\
**Post date:** [December 24, 2023, 7:12pm UTC](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647/31 "2023-12-24T19:12:24Z")

</div>

To be honest, adding the locality specifiers was supposed to be part of my GSOC project last year 😂

[Previous page](https://fortran-lang.discourse.group/t/clarification-on-do-concurrent/1647.md?page=1)
