Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Bug segfault, pointer, matmul

pat97269
Beginner
927 Views
Hi,
I report a bug with matmul using a pointer on an array of size >= 1024, and if you add two matmul the limit size is 724 ! ifort v12.1
Pat!
Here is the example:

[fortran]program test
implicit none

      double precision,dimension(:,:),allocatable :: mat
      double precision,dimension(:),allocatable,target :: tp
      double precision,dimension(:,:),pointer :: tp1
      integer:: n

      do n=1,10000
        allocate(mat(n,n),tp(n))
        call random_number(tp)
        tp1(1:n,1:1)=>tp(1:n)
        mat=matmul(tp1,transpose(tp1)) ! segfault at n=1024
        mat=matmul(tp1,transpose(tp1))+matmul(tp1,transpose(tp1)) ! segfault at n=724
        deallocate(mat,tp)
        print *,'n',n
      end do
end program test
[/fortran]
0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
927 Views
Enable heap arrays .OR. increase your stack size.

The compiler (as you have configured) is producing code that creates temporary objects (arrays) on the stack. With the heap arrays option you can specify a size threashold where temp objects larger than the threashold are allocated from heap as opposed to stack.

Jim Dempsey
0 Kudos
pat97269
Beginner
927 Views
Ok thanks, i forgot to check that.
But i'm confused, when i compile with the check all option, the compiler doesn't throw any warning that he creates temporary arrays, is that normal ? I suppose it's the pointer that creates the temp array ?
Is there any way to code that, in a way it doesn't create temp array ?
Thanks
Pat
0 Kudos
mecej4
Honored Contributor III
927 Views
If the compiler threw warnings for every statement that caused it to generate temporaries, the output would be voluminous with large input source codes.

It is not the use of a pointer that induces creation of temporaries, but expressions such as

matmul(tp1,transpose(tp1))

Space for the transpose and space for the product may be needed, although the compiler-optimizer can analyze the situation and decide if one or more temporary arrays can be dispensed with.
0 Kudos
pat97269
Beginner
927 Views
I don't agree with you , doing a matmul with two large 2D array don't exceed stack limit .But when using a pointer to that array it does, which mean when you pass a pointer it create a temporary array (only with allocatable),is that a good behavior ? why not using the pointer directly ? If you use the transpose intrisic ,it will create the temp array to store the result , that i understand.So using a pointer for matmul and result from function not store into variable create temp array in matmul Pat

[fortran]program test  
implicit none  
  
      double precision,dimension(:,:),allocatable,target :: mat,mat2,mat3  
      double precision,dimension(:,:),pointer :: tp1  
      integer:: n  
  
      n=1024
        allocate(mat(n,n),tp(n),mat2(n,n),mat3(n,n) )   
        call random_number(mat2)  
        call random_number(mat3)  
        tp1=>mat3
        mat=matmul(mat2,tp1) ! segfault at n=1024  
        deallocate(mat,tp,mat2,mat3)  
        print *,'n',n  
     
end program test[/fortran]
0 Kudos
Reply