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

Segmentation fault with the matrix transpose function

Phy_Soham
Novice
833 Views

 

It works for the ( nxn ) matrix when n is smaller, and the result and argument of the transpose function are the same.

For larger n, it produces a segmentation fault.

A way out is transposing through a temporary matrix, as shown in the code below.

I am using Intel Fortran Compiler Classic version: ifort (IFORT) 2021.7.0 20220726.


gfortran (v-10.4.0) does not produce the segmentation fault.


Am I missing something in the Fortran standards?

program TransposeBug

    implicit none

    integer, parameter          :: dp = selected_real_kind(15, 307)

    real(dp), dimension(:, :), allocatable      :: Mat, Mat_tmp
    integer                                     :: n


    ! ================================ Small vlaue of n works ================================ !

    n = 81
    allocate( Mat(n, n) )
    call random_number( Mat )
    Mat = transpose( Mat )

    write(*, *) "Transpose of Matrix complete"

    ! ================================ Small vlaue of n works ================================ !

    deallocate( Mat )

    ! ==================== Large vlaue of n works through temporary matrix =================== !

    n = 11838
    allocate( Mat(n, n) )
    call random_number( Mat )
    allocate( Mat_tmp(n, n) )
    Mat_tmp = transpose( Mat )
    Mat = Mat_tmp

    write(*, *) "Transpose of Matrix complete"

    ! ==================== Large vlaue of n works through temporary matrix =================== !

    deallocate( Mat_tmp )
    deallocate( Mat )

    ! ============================= Large vlaue of n do not works ============================ !

    n = 11838
    allocate( Mat(n, n) )
    call random_number( Mat )
    Mat = transpose( Mat )

    write(*, *) "Transpose of Matrix complete"

    ! ============================= Large vlaue of n do not works ============================ !

end program TransposeBug

 

Result:

 Transpose of Matrix complete
 Transpose of Matrix complete
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source 
libpthread-2.17.s  00007F47E6766630  Unknown               Unknown  Unknown
a.out              000000000040572C  Unknown               Unknown  Unknown
a.out              000000000040429D  Unknown               Unknown  Unknown
libc-2.17.so       00007F47E63AB555  __libc_start_main     Unknown  Unknown
a.out              0000000000404176  Unknown               Unknown  Unknown

 

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
825 Views

Try enabling heap-arrays to avoid stack overflow.

 

Jim Dempsey

View solution in original post

2 Replies
jimdempseyatthecove
Honored Contributor III
826 Views

Try enabling heap-arrays to avoid stack overflow.

 

Jim Dempsey

Phy_Soham
Novice
778 Views

Thanks,

 
jimdempseyatthecove  . No segmentation fault with "-heap-arrays" flag.
0 Kudos
Reply