<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic BUG: c_f_pointer and hidden copies on stack in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772179#M23149</link>
    <description>Hi Tim, Hi Steve,&lt;BR /&gt;&lt;BR /&gt;I am happy to hear, that Intel is aware of this unnecessary temporaries.&lt;BR /&gt;We figured, that the use of pointers in high performance codes is not a good idea if compiled with ifort.&lt;BR /&gt;&lt;BR /&gt;If we have the pointers a,b,c to 3d arrays of the shape array(1024,1024,1024) and do the calculation:&lt;BR /&gt; a=a+b**2+c**2&lt;BR /&gt;this is _much slower_ (because of unnesseary temporaries) than using allocatables instead of pointers.&lt;BR /&gt;&lt;BR /&gt;We were not aware of that.&lt;BR /&gt;Can we fix this with some more moden Fortran technics ?&lt;BR /&gt;&lt;BR /&gt;So we have to change code pieces to something like this: (?)&lt;BR /&gt;-----------&lt;BR /&gt;do k=lbound(a,3), ubound(a,3)&lt;BR /&gt; do j=lbound(a,2), ubound(a,2)&lt;BR /&gt; do i=lbound(a,1), ubound(a,1)&lt;BR /&gt; a(i,j,k) = a(i,j,k) + b(i,j,k)**2 + c(i,j,k)**2&lt;BR /&gt; end do&lt;BR /&gt; end do&lt;BR /&gt;end do&lt;BR /&gt;-----------&lt;BR /&gt;Would this avoid temp arrays?&lt;BR /&gt;&lt;BR /&gt;I found this thread in &lt;A href="http://compgroups.net/comp.lang.fortran/c_f_pointer-aliasing-and-performance/598845" target="_blank"&gt;http://compgroups.net/comp.lang.fortran/c_f_pointer-aliasing-and-performance/598845&lt;/A&gt;&lt;BR /&gt;where you Steve also participated.&lt;BR /&gt;&lt;BR /&gt;From there the easiest solutions seems to me to do what is posted as:&lt;BR /&gt;"Pass the data to a subroutine, and "forget" to tell the subroutine&lt;BR /&gt;that the data is a TARGET.  Do all your computations in the subroutine."&lt;BR /&gt;&lt;BR /&gt;Does that work?&lt;BR /&gt;&lt;BR /&gt;Another solution seems to be cray pointers and the compile option "safe_cray_ptr". &lt;BR /&gt;But this makes the code non-portable.&lt;BR /&gt;&lt;BR /&gt;IBMs xlf has a compiler option called "&lt;B&gt;-qalias=noaryovrlp"&lt;/B&gt;.&lt;BR /&gt;(&lt;A href="http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlf121.aix.doc/compiler_ref/alias.html"&gt;http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlf121.aix.doc/compiler_ref/alias.html&lt;/A&gt;)&lt;BR /&gt;I would like to see something simular for the intel fortran compiler.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik</description>
    <pubDate>Wed, 06 Jun 2012 12:36:31 GMT</pubDate>
    <dc:creator>jhgoebbert</dc:creator>
    <dc:date>2012-06-06T12:36:31Z</dc:date>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772172#M23142</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;We came across a strange behaviour of intel 12.1 (which cannot be seen with gfortran)&lt;BR /&gt;while using c_f_pointer().&lt;BR /&gt;&lt;BR /&gt;The program below gives a segmentation fault, if "n" ist large (eg. 512).&lt;BR /&gt;because an unnessesary user-hidden copy of the array is made on the stack in the line "var2=var1".&lt;BR /&gt;If "n" is small (eg. 4) no segmantation fault happens, because the stack size is suffcient.&lt;BR /&gt;&lt;BR /&gt;c_f_pointer always returns a coherent memory-segment.&lt;BR /&gt;Therefore the compiler can be sure, that the returned fortran pointer is also coherent.&lt;BR /&gt;Doing a user-hidden copy is not nesessary and should be avoided.&lt;BR /&gt;&lt;BR /&gt;The segmentation fault can be avoided using the compiler-flag "-heap-arrays".&lt;BR /&gt;But a copy is also time and memory consuming and therefore slows down the code and increased the memory footprint. Specially performace critical codes are affected by these hidden copies.&lt;A href="http://dict.leo.org/ende?lp=ende&amp;amp;p=DOKJAA&amp;amp;search=affected&amp;amp;trestr=0x8004"&gt;&lt;/A&gt;&lt;BR /&gt;So, "-heap-arrays" is no solution.&lt;BR /&gt;&lt;BR /&gt;I this a bug? Can it be fixed?&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik&lt;BR /&gt;&lt;BR /&gt;program cfpointer
&lt;BR /&gt; use,intrinsic :: iso_c_binding
&lt;BR /&gt; implicit none
&lt;BR /&gt; integer,parameter :: wp=8
&lt;BR /&gt; integer :: n=512&lt;BR /&gt; real(wp), allocatable, target :: mem(:,:,:,:)
&lt;BR /&gt; real(wp), pointer :: var1(:,:,:),var2(:,:,:)
&lt;BR /&gt; type(c_ptr), target :: ptr
&lt;BR /&gt;
&lt;BR /&gt; allocate(mem(n,n,n,2))
&lt;BR /&gt;
&lt;BR /&gt; ptr=c_loc(mem(1,1,1,1))
&lt;BR /&gt; call c_f_pointer(ptr,var1,[n,n,n])
&lt;BR /&gt; ptr=c_loc(mem(1,1,1,2))
&lt;BR /&gt; call c_f_pointer(ptr,var2,[n,n,n])
&lt;BR /&gt;
&lt;BR /&gt; write(*,*) 'A'
&lt;BR /&gt; var1=5.0_wp
&lt;BR /&gt; write(*,*) 'B'
&lt;BR /&gt; var2=0.0d0
&lt;BR /&gt; write(*,*) 'C'
&lt;BR /&gt; var2=var1 ! segmentation fault
&lt;BR /&gt; write(*,*) 'D'
&lt;BR /&gt;
&lt;BR /&gt;end program cfpointer&lt;BR /&gt;</description>
      <pubDate>Thu, 31 May 2012 11:09:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772172#M23142</guid>
      <dc:creator>jhgoebbert</dc:creator>
      <dc:date>2012-05-31T11:09:53Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772173#M23143</link>
      <description>Evidently, the compiler is concerned that var1 and var2 may overlap, with unknown alignment. It must decide at compile time whether to allow for this, as it doesn't analyze the specific values you supply in allocate vs. size of arrays. &lt;BR /&gt;If interested, you might try variations, such as declaring SEQUENCE, and using !dir$ IVDEP, to see if you can eliminate the temporary and copies.&lt;BR /&gt;I attempted to run under Amplifier-XE in hopes of seeing which version is actually run when copying, and I reported the resulting failure of Amplifier.</description>
      <pubDate>Thu, 31 May 2012 14:25:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772173#M23143</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2012-05-31T14:25:57Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772174#M23144</link>
      <description>Hi Tim,&lt;BR /&gt;&lt;BR /&gt;thanks for your answer.&lt;BR /&gt;&lt;BR /&gt;We understand your argument, that the compiler is concerned that var1 and var2 may overlap.&lt;BR /&gt;This is really bad news :(&lt;BR /&gt;Fortran assumes generaly, that memory must not overlap - so shouldn't intel compiler assume this by default?&lt;BR /&gt;&lt;BR /&gt;If we simplify the problem this way, fortran pointer seem to be the problem in general:&lt;BR /&gt;program test&lt;BR /&gt; implicit none&lt;BR /&gt; real(kind=8),dimension(:,:,:),pointer :: a,b&lt;BR /&gt; allocate(a(1024,1024,1024),b(1024,1024,1024))&lt;BR /&gt; a=1.0&lt;BR /&gt; b=2.0&lt;BR /&gt; b=a ! segmentation fault&lt;BR /&gt;end program test&lt;BR /&gt;&lt;BR /&gt;If you replace "pointer" by "allocatable" everything works fine.&lt;BR /&gt;Again Intel creates a hidden copy on the stack :(&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 31 May 2012 16:09:01 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772174#M23144</guid>
      <dc:creator>jhgoebbert</dc:creator>
      <dc:date>2012-05-31T16:09:01Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772175#M23145</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;to simplify the program even more, do this:&lt;BR /&gt;&lt;BR /&gt;program test&lt;BR /&gt; implicit none&lt;BR /&gt; real(kind=8),dimension(:,:,:),pointer :: a,b&lt;BR /&gt; real(kind=8),dimension(:,:,:),allocatable :: mem_a, mem_b&lt;BR /&gt; allocate(mem_a(1024,1024,1024)) &lt;BR /&gt; allocate(mem_b(1024,1024,1024))&lt;BR /&gt; a=1.0&lt;BR /&gt; b=2.0&lt;BR /&gt; b=a ! segmentation fault&lt;BR /&gt;end program test&lt;BR /&gt;&lt;BR /&gt;For the this program the compiler should be able to decide for non-overlay at compile-time.&lt;BR /&gt;
Or at least at run-time. But it does not.&lt;BR /&gt;&lt;BR /&gt;Our CFD code is doing nothing else, than working on large arrays, which 
are passed from function to function using pointers. All calculation 
time goes into constructs like ptr1=ptr2*ptr3 and similar.&lt;BR /&gt;&lt;BR /&gt;The more I think about it, the more I come to the conclusion, that &lt;SPAN style="text-decoration: underline;"&gt;this is a critical bug&lt;/SPAN&gt; - specially for high-performance codes. The intel compiler must have a flag to turn off these hidden copies.&lt;BR /&gt;&lt;BR /&gt;some more information I found here &lt;A href="http://software.intel.com/en-us/forums/showthread.php?t=85104"&gt;http://software.intel.com/en-us/forums/showthread.php?t=85104&lt;/A&gt;&lt;BR /&gt;and a nice example, with the same problem: &lt;A href="http://fftw.org/doc/Allocating-aligned-memory-in-Fortran.html#Allocating-aligned-memory-in-Fortran"&gt;http://fftw.org/doc/Allocating-aligned-memory-in-Fortran.html#Allocating-aligned-memory-in-Fortran&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik</description>
      <pubDate>Thu, 31 May 2012 17:30:30 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772175#M23145</guid>
      <dc:creator>jhgoebbert</dc:creator>
      <dc:date>2012-05-31T17:30:30Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772176#M23146</link>
      <description>In general, those pointers to 2 places in a single allocatable array could overlap in any way you choose.&lt;BR /&gt;In your new example, it does appear that the compiler should recognize separately allocated arrays as not overlapping, but the code assigns values to pointers without association, so it seems the first bug would be in not reporting that. Not that I care to pose as an expert in such things.&lt;BR /&gt;I have cases of long standing where ifort makes unnecessary temporaries but gfortran does not, making the use of f90 array assignments uncompetitive. Slow but significant progress has been made over the years. I was informed months ago there would be no more work on that issue, yet there has been additional progress since.&lt;BR /&gt;</description>
      <pubDate>Thu, 31 May 2012 17:47:52 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772176#M23146</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2012-05-31T17:47:52Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772177#M23147</link>
      <description>The compiler catches assigning values to nonassociated pointers if the option "-check pointers" or "-check all is used"</description>
      <pubDate>Sat, 02 Jun 2012 00:20:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772177#M23147</guid>
      <dc:creator>Anonymous66</dc:creator>
      <dc:date>2012-06-02T00:20:42Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772178#M23148</link>
      <description>In general, the language allows overlap between any two POINTER variables. We have seen several cases where gfortran did not consider this and delivered wrong results because it did not make a temp.</description>
      <pubDate>Mon, 04 Jun 2012 18:15:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772178#M23148</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2012-06-04T18:15:36Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772179#M23149</link>
      <description>Hi Tim, Hi Steve,&lt;BR /&gt;&lt;BR /&gt;I am happy to hear, that Intel is aware of this unnecessary temporaries.&lt;BR /&gt;We figured, that the use of pointers in high performance codes is not a good idea if compiled with ifort.&lt;BR /&gt;&lt;BR /&gt;If we have the pointers a,b,c to 3d arrays of the shape array(1024,1024,1024) and do the calculation:&lt;BR /&gt; a=a+b**2+c**2&lt;BR /&gt;this is _much slower_ (because of unnesseary temporaries) than using allocatables instead of pointers.&lt;BR /&gt;&lt;BR /&gt;We were not aware of that.&lt;BR /&gt;Can we fix this with some more moden Fortran technics ?&lt;BR /&gt;&lt;BR /&gt;So we have to change code pieces to something like this: (?)&lt;BR /&gt;-----------&lt;BR /&gt;do k=lbound(a,3), ubound(a,3)&lt;BR /&gt; do j=lbound(a,2), ubound(a,2)&lt;BR /&gt; do i=lbound(a,1), ubound(a,1)&lt;BR /&gt; a(i,j,k) = a(i,j,k) + b(i,j,k)**2 + c(i,j,k)**2&lt;BR /&gt; end do&lt;BR /&gt; end do&lt;BR /&gt;end do&lt;BR /&gt;-----------&lt;BR /&gt;Would this avoid temp arrays?&lt;BR /&gt;&lt;BR /&gt;I found this thread in &lt;A href="http://compgroups.net/comp.lang.fortran/c_f_pointer-aliasing-and-performance/598845" target="_blank"&gt;http://compgroups.net/comp.lang.fortran/c_f_pointer-aliasing-and-performance/598845&lt;/A&gt;&lt;BR /&gt;where you Steve also participated.&lt;BR /&gt;&lt;BR /&gt;From there the easiest solutions seems to me to do what is posted as:&lt;BR /&gt;"Pass the data to a subroutine, and "forget" to tell the subroutine&lt;BR /&gt;that the data is a TARGET.  Do all your computations in the subroutine."&lt;BR /&gt;&lt;BR /&gt;Does that work?&lt;BR /&gt;&lt;BR /&gt;Another solution seems to be cray pointers and the compile option "safe_cray_ptr". &lt;BR /&gt;But this makes the code non-portable.&lt;BR /&gt;&lt;BR /&gt;IBMs xlf has a compiler option called "&lt;B&gt;-qalias=noaryovrlp"&lt;/B&gt;.&lt;BR /&gt;(&lt;A href="http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlf121.aix.doc/compiler_ref/alias.html"&gt;http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlf121.aix.doc/compiler_ref/alias.html&lt;/A&gt;)&lt;BR /&gt;I would like to see something simular for the intel fortran compiler.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik</description>
      <pubDate>Wed, 06 Jun 2012 12:36:31 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772179#M23149</guid>
      <dc:creator>jhgoebbert</dc:creator>
      <dc:date>2012-06-06T12:36:31Z</dc:date>
    </item>
    <item>
      <title>BUG: c_f_pointer and hidden copies on stack</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772180#M23150</link>
      <description>Hi Annalee,&lt;BR /&gt;&lt;BR /&gt;the pointers in the example are associated (in the background) while allocating.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Jens Henrik</description>
      <pubDate>Wed, 06 Jun 2012 12:39:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/BUG-c-f-pointer-and-hidden-copies-on-stack/m-p/772180#M23150</guid>
      <dc:creator>jhgoebbert</dc:creator>
      <dc:date>2012-06-06T12:39:20Z</dc:date>
    </item>
  </channel>
</rss>

