public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask
@ 2014-03-25 22:52 burnus at gcc dot gnu.org
  2014-03-25 23:00 ` [Bug fortran/60661] " burnus at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-03-25 22:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60661

            Bug ID: 60661
           Summary: DO CONCURRENT with MASK: Avoid using a temporary for
                    the mask
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
                CC: tkoenig at gcc dot gnu.org

Currently, gfortran generates a temporary as shown below. However, the question
is whether one cannot do without a temporary by moving the mask expression into
the loop.

I think that usually works - but not always. It works when:
a) The variable in the mask does not occur on the LHS of an assignment or as
intent([in]out) argument of a pure subroutine
b) If the variable only occurs with the same array index as later in the body
of the DO CONCURRENT loop

I am not sure whether something with FORALL prevents this optimization.

I think the simplest fix would be to transform
  DO CONCURRENT(i=1:n, mask(i))
     ...
to
  DO CONCURRENT(i=1:n)
    IF (.not. mask(i)) CYCLE
in the FE optimization


"7.2.4.2.3 Evaluation of the mask expression
The scalar-mask-expr, if any, is evaluated for each combination of index-name
values. If there is no scalar-mask-expr, it is as if it appeared with the value
true. The index-name variables may be primaries in the
scalar-mask-expr. The set of active combinations of index-name values is the
subset of all possible combinations (7.2.4.2.2) for which
the scalar-mask-expr has the value true."

C736 (R752) The scalar-mask-expr shall be scalar and of type logical.
C737 (R752) Any procedure referenced in the scalar-mask-expr , including one
referenced by a defined operation,
shall be a pure procedure (12.7).


    forall (i=start:end:stride; maskexpr)
      e<i> = f<i>
      g<i> = h<i>
    end forall
   (where e,f,g,h<i> are arbitrary expressions possibly involving i)
   Translates to:
    count = ((end + 1 - start) / stride)
    masktmp(:) = maskexpr(:)

    maskindex = 0;
    for (i = start; i <= end; i += stride)
      {
        if (masktmp[maskindex++])
          e<i> = f<i>
      }
    maskindex = 0;
    for (i = start; i <= end; i += stride)
      {
        if (masktmp[maskindex++])
          g<i> = h<i>
      }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/60661] DO CONCURRENT with MASK: Avoid using a temporary for the mask
  2014-03-25 22:52 [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask burnus at gcc dot gnu.org
@ 2014-03-25 23:00 ` burnus at gcc dot gnu.org
  2014-03-27  6:54 ` burnus at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-03-25 23:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60661

--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> ---
Note that one needs to be careful to handle OpenACC/OpenMP correctly to make
sure that, e.g., "!$acc loop" remains attached to the loop it belongs to.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/60661] DO CONCURRENT with MASK: Avoid using a temporary for the mask
  2014-03-25 22:52 [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask burnus at gcc dot gnu.org
  2014-03-25 23:00 ` [Bug fortran/60661] " burnus at gcc dot gnu.org
@ 2014-03-27  6:54 ` burnus at gcc dot gnu.org
  2014-03-30 22:20 ` tkoenig at gcc dot gnu.org
  2014-08-10 15:50 ` tkoenig at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-03-27  6:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60661

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> ---
Quote from the standard:
http://mailman.j3-fortran.org/pipermail/j3/2014-March/007259.html

The key paragraph is [176:22]:

"At the completion of the execution of the DO statement, the execution cycle
begins."

Figuring out the list of index values is part of the execution of the DO
CONCURRENT statement [176:20-21].


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/60661] DO CONCURRENT with MASK: Avoid using a temporary for the mask
  2014-03-25 22:52 [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask burnus at gcc dot gnu.org
  2014-03-25 23:00 ` [Bug fortran/60661] " burnus at gcc dot gnu.org
  2014-03-27  6:54 ` burnus at gcc dot gnu.org
@ 2014-03-30 22:20 ` tkoenig at gcc dot gnu.org
  2014-08-10 15:50 ` tkoenig at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2014-03-30 22:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60661

--- Comment #3 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
We have to be a bit careful about statement like

  do concurrent(i=1:n, a(i)>sum(a)/n)
    a(i) = a(i) * 0.5
  end do

which really have to be before the execution
of the loop body itself.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/60661] DO CONCURRENT with MASK: Avoid using a temporary for the mask
  2014-03-25 22:52 [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask burnus at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2014-03-30 22:20 ` tkoenig at gcc dot gnu.org
@ 2014-08-10 15:50 ` tkoenig at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2014-08-10 15:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60661

Thomas Koenig <tkoenig at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-08-10
     Ever confirmed|0                           |1

--- Comment #4 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
For

do concurrent(i=1:n, a(i)>sum(a)/n)

we currently evaluate the sum every time.  This can
definitely be improved, by taking out expressions which
do not depend on the index variable out of the mask.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-10 15:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-25 22:52 [Bug fortran/60661] New: DO CONCURRENT with MASK: Avoid using a temporary for the mask burnus at gcc dot gnu.org
2014-03-25 23:00 ` [Bug fortran/60661] " burnus at gcc dot gnu.org
2014-03-27  6:54 ` burnus at gcc dot gnu.org
2014-03-30 22:20 ` tkoenig at gcc dot gnu.org
2014-08-10 15:50 ` tkoenig at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).