# MAGMA dsyevd segmentation fault

Hello I solve an eigenvalue problem with this code:

``````subroutine jdiag(lda,n,A,W)
! A: input matrix, and eigenvectors will be stored here
! W: vector for eigenvalues
! n: matrix size   (4*NCC, 4*az atomok szama)
! lda: leading dimension
implicit none
integer :: lda, n
real*8 :: A(lda,n), W(n)
!call jdiagLAPACK(lda,n,A,W)  ! CPU serial version (LAPACK)
call jdiagMAGMA(lda,n,A,W) ! GPU version (MAGMA)
return
end
``````

Lapack function is very simple and works well:

``````subroutine jdiagLAPACK(lda,n,A,W)
implicit none
integer :: info, lda, n
real*8 :: A(lda,n), W(n), WORK(lwork)
call dsyev ('v','L', n, A, lda, W, WORK, lwork, info)
return
end
``````

But it runs only 1 CPU. A want it to move to GPU. My only idea is MAGMA:

``````subroutine jdiagMAGMA(lda,n,A,W)

implicit none

integer :: info, lda, n
real*8 :: A(lda,n), W(n)
real*8, allocatable :: WORK(:)
integer*4 :: lwork = -1
integer :: ngpu = 1
integer, allocatable :: IWORK(:)
integer :: liwork = -1
integer ::status

call magmaf_init()

allocate (work(10000), IWORK(10000) )    !, stat=status)
call magmaf_dsyevd_m (ngpu, 'V', 'L', n, A, lda, W, WORK, lwork, IWORK, liwork, info)
deallocate (work, iwork)      !, stat=status)

lwork = int(work(1))
liwork = int(iwork(1))

allocate (work(lwork), IWORK(liwork))        !, stat=status)
call magmaf_dsyevd_m (ngpu, 'V', 'L', n, A, lda, W, WORK, lwork, IWORK, liwork, info)
call magmaf_finalize()
return
end
``````

In the 2nd dsyev call it gives Segmentation fault and I am unable to find its reason. Can anyone help me?

I am having trouble understanding what you are doing.
You deallocate arrays and then read the first element of them?

allocate (work(10000), IWORK(10000) ) !, stat=status)

! allocate memory from the stack (ie not initialized chunk of memory,
! full of any sort of garbage )
call magmaf_dsyevd_m (ngpu, ‘V’, ‘L’, n, A, lda, W, WORK, lwork, IWORK, liwork, info)

! call magma_dysevd_m with the garbage arrays - I hope they are intent(out)

deallocate (work, iwork) !, stat=status)

! no longer associate the symbols work and iwork with the starting location
of a chunk of memory

lwork = int(work(1))
liwork = int(iwork(1))

! set lwork and liwork to the first value in the deallocated and uniitialized
! work() and iwork()

allocate (work(lwork), IWORK(liwork)) !, stat=status)

!
! allocate a new chunk of uninitialized memory equal to the size of the
! first element of array no longer associated with a chunk of memory.

call magmaf_dsyevd_m (ngpu, ‘V’, ‘L’, n, A, lda, W, WORK, lwork, IWORK, liwork, info)

You are completely true, I noticed this mistake before but could not chang it in the forum. If you see the code, this mistake cannot be the root of the problem, it effected only writing out two numbers.

The corrected code is:

``````subroutine jdiagMAGMA(lda,n,A,W)

implicit none

integer :: info, lda, n
real*8 :: A(lda,n), W(n)
real*8, allocatable :: WORK(:)
integer*4 :: lwork = -1
integer :: ngpu = 1
integer, allocatable :: IWORK(:)
integer :: liwork = -1
integer ::status

call magmaf_init()

allocate (work(10000), IWORK(10000) )    !, stat=status)
call magmaf_dsyevd_m (ngpu, 'V', 'L', n, A, lda, W, WORK, lwork, IWORK, liwork, info)

lwork = int(work(1))
liwork = int(iwork(1))
deallocate (work, iwork)

! allocate with the new, proper size:
allocate (work(lwork), IWORK(liwork))        !, stat=status)
call magmaf_dsyevd_m (ngpu, 'V', 'L', n, A, lda, W, WORK, lwork, IWORK, liwork, info)
call magmaf_finalize()
return
end
``````