public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/36842]  New: Fortran: Minimize heap allocation of temporary arrays.
@ 2008-07-15 18:15 rajiv dot adhikary at amd dot com
  2008-07-23  9:44 ` [Bug fortran/36842] " rguenth at gcc dot gnu dot org
  2010-05-10 18:26 ` mikael at gcc dot gnu dot org
  0 siblings, 2 replies; 4+ messages in thread
From: rajiv dot adhikary at amd dot com @ 2008-07-15 18:15 UTC (permalink / raw)
  To: gcc-bugs

Instead of automatically allocating the temporary array in heap, it would be
wise to perform a few checks to determine a temporary array is actually
required, whether to reserve memory in stack instead etc.

   The code produced by GCC for the following subroutine does the following.
   i.   Start the loop.
   ii.  Malloc memory required to hold Ry(:,n) * Rx(:)
   iii. Perform Ry(:,n)* Rx(:), store result in malloced memory
   iv.  Copy result from malloced memory to Ry(:,n)
   v.   Free malloced memory
   vi.  go to loop start.
   This is very inefficient.

    subroutine malloc_test(Ry, Rx, ny)
    implicit none
      integer(kind=kind(1)), intent(in) :: ny
      real(kind=kind(1.0d0)), dimension(:,:), pointer :: Ry
      real(kind=kind(1.0d0)), dimension(:),  pointer :: Rx
      integer(kind=kind(1)) :: n

      do n = 1,ny
        Ry(:,n) = Ry(:,n) * Rx(:)
      end do
    end subroutine malloc_test

Other relevant information:
1. Compile flags: -O3 -ffast-math -m64 -march=amdfam10

2. gfortran version: gfortran -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: /tmp/src/gcc-4.3.0/configure --prefix=/opt/amd/gcc-4.3.0
--enable-languages=c,c++,fortran --enable-stage1-checking
--with-as=/opt/amd/gcc-4.3.0/bin/as --with-ld=/opt/amd/gcc-4.3.0/bin/ld
--with-mpfr=/tmp/install/mpfr-2.3.0 --with-gmp=/tmp/install/gmp-4.2.2
Thread model: posix
gcc version 4.3.1 20080312 (prerelease) (GCC)

3. model name: AMD Phenom(tm) 8650 Triple-Core Processor

4. flags     : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm
3dnowext 3dnow constant_tsc pni cx16 popcnt lahf_lm cmp_legacy svm extapic
cr8_legacy altmovcr8 abm sse4a misalignsse 3dnowprefetch osvw


-- 
           Summary: Fortran: Minimize heap allocation of temporary arrays.
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rajiv dot adhikary at amd dot com


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


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

* [Bug fortran/36842] Fortran: Minimize heap allocation of temporary arrays.
  2008-07-15 18:15 [Bug fortran/36842] New: Fortran: Minimize heap allocation of temporary arrays rajiv dot adhikary at amd dot com
@ 2008-07-23  9:44 ` rguenth at gcc dot gnu dot org
  2010-05-10 18:26 ` mikael at gcc dot gnu dot org
  1 sibling, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-07-23  9:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-07-23 09:43 -------
Confirmed.  Dependency analysis should see that no temporary is required here.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-07-23 09:43:44
               date|                            |


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


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

* [Bug fortran/36842] Fortran: Minimize heap allocation of temporary arrays.
  2008-07-15 18:15 [Bug fortran/36842] New: Fortran: Minimize heap allocation of temporary arrays rajiv dot adhikary at amd dot com
  2008-07-23  9:44 ` [Bug fortran/36842] " rguenth at gcc dot gnu dot org
@ 2010-05-10 18:26 ` mikael at gcc dot gnu dot org
  1 sibling, 0 replies; 4+ messages in thread
From: mikael at gcc dot gnu dot org @ 2010-05-10 18:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from mikael at gcc dot gnu dot org  2010-05-10 18:25 -------
(In reply to comment #1)
> Confirmed.  Dependency analysis should see that no temporary is required here.
> 
As both Rx and Ry have the pointer attribute, I'm not so sure about it. 
Note that if one removes the pointer attribute, there is no temporary any more. 


-- 


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


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

* [Bug fortran/36842] Fortran: Minimize heap allocation of temporary arrays.
       [not found] <bug-36842-4@http.gcc.gnu.org/bugzilla/>
@ 2011-09-07  9:31 ` burnus at gcc dot gnu.org
  0 siblings, 0 replies; 4+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-09-07  9:31 UTC (permalink / raw)
  To: gcc-bugs

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

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burnus at gcc dot gnu.org

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-09-07 09:13:07 UTC ---
At least I fail how the compiler can know whether the arguments alias or not.
For an illustration, use:
   real(kind=kind(1.0d0)), target :: data(5,5)
   data = Reshape([((i*10+j, i = 1, 5), j=1,5)], [5,5])
   Rx => data(1,:)
   Ry => data
for the actual argument. You will get different results with the current code,
   Ry(:,n) = Ry(:,n) * Rx(:)
and with a loop which simply assigns it without a temporary,
   do m = 1, size(Rx)
     Ry(m,n) = Ry(m,n) * Rx(m)
   end do

Thus, I see no other possibility than having a temporary which stores first the
evaluated right-hand side before it assigns it to the left-hand side.

As noted by Mikael, if the compiler knows that the two variables cannot alias,
no temporary is generated. That's the case if one of the variables is neither a
target nor a pointer - or if both variables are nonpointers (also with target)
but not dummy arguments - or both are nonpointers (also with target and dummy
argument) but allocatable. (Cf. gcc/fortran/dependency.c's
gfc_check_dependency).

 * * *

It would help a lot if the temporary generation (malloc/free) could be moved
out of the loop; however, that's nontrivial to do for both the front end and
for the middle end. Cf. PR 19831 and PR 21046. 

 * * *

Regarding the heap usage: Since GCC 4.7 the flag -fstack-arrays exists (implied
by -Ofast), which uses the stack instead of the heap for the temporary array.


Thus, unless someone has a better idea or comes up with an optimizable test
case, I vote for closing this PR.


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

end of thread, other threads:[~2011-09-07  9:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-15 18:15 [Bug fortran/36842] New: Fortran: Minimize heap allocation of temporary arrays rajiv dot adhikary at amd dot com
2008-07-23  9:44 ` [Bug fortran/36842] " rguenth at gcc dot gnu dot org
2010-05-10 18:26 ` mikael at gcc dot gnu dot org
     [not found] <bug-36842-4@http.gcc.gnu.org/bugzilla/>
2011-09-07  9:31 ` burnus 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).