The LAPACK forum has moved to https://github.com/Reference-LAPACK/lapack/discussions.

zheev segfault

Open discussion regarding features, bugs, issues, vendors, etc.

zheev segfault

Postby jfkimberly » Wed Apr 16, 2008 12:59 pm

Hi,

I'm trying to obtain the eigenvectors and eigenvalues of
a Hermitian matrix (complex nxn matrix) using zheev_ of LAPACK (in C).
Everything works fine up to a certain matrix size, n <= 626, but when
n > 626, a segmentation fault keeps popping up. I'm pretty sure I don't need any more RAM. Here is the part of the code

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include "ran3.c" //random number generator

int main(void)
{
//parameters for zheev_()
  double *HT;
  int LWORK,LRWORK;
  double *w;
  complex double *WORK;
  double *RWORK;

  long idum = time(0);
  ///////////////////////////////////////////////////////
  //Change this according to the dimension of your matrix
  //H: Matrix (nxn), U: Eigenvector (column vectors)
  // H[n][n]
  complex double H[627][627],U[627][627];
  ///////////////////////////////////////////////////////
  int i,j,k,n,s,ok,size;
  char c4,c5;
  c4='V';
  c5='L';
  ///////////////////////////////////////////////////////
  // Change this according to the dimension of the matrix
  n = 627;
  ///////////////////////////////////////////////////////
 
// Random nxn Hermitian matrix
  for(i=0;i<n;i++) {
    for(j=0;j<=i;j++) {
      H[i][j]=ran3(&idum)+I*ran3(&idum);
      H[j][i]=conj(H[i][j]);
    }
  }

///////////////////////////////////////////////////////////////

  HT = (double *)malloc(sizeof (double)*n*n*2);

  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      {
        HT[2*(n*i+j)]=creal(H[j][i]);
        HT[2*(n*i+j)+1]=cimag(H[j][i]);
      }

  w=(double *)malloc(sizeof(double)*n);
  LWORK=5*n;
  WORK=(complex double *)malloc(sizeof(complex double)*LWORK);

  LRWORK=3*n;
  RWORK= (double *)malloc(sizeof (double)*LRWORK);

  // Solving the linear system
  zheev_(&c4,&c5,&n,HT,&n,w,WORK,&LWORK,RWORK,&ok);

  if(ok!=0)printf("diagonalization failed\n");

  // Eigenvalues in ascending order
  for(i=0;i<n;i++)
    printf("%lf\t",w[i]);
  printf("\n");

  // copying eigenvectors
  for(i=0;i<size;i++)
    for(j=0;j<size;j++)
      U[j][i]=HT[2*(n*i+j)]+I*HT[2*(n*i+j)+1];

  for(i=0;i<n;i++)  {
      for(j=0;j<n;j++)
        {printf("%lf+I*%lf\t",creal(U[i][j]),cimag(U[i][j]));}
      printf("\n");
    }
    printf("Okay!");
 
}


Here H is the nxn Hermitian matrix, w is the array containing the eigenvalues, and U is the array containing the eigenvectors. I'm a beginner in C and LAPACK so any suggestions or solutions would be greatly appreciated. Thank you.
jfkimberly
 
Posts: 2
Joined: Tue Apr 15, 2008 2:47 am

Postby Julien Langou » Wed Apr 16, 2008 2:26 pm

I can not find an obvious mistake in the LAPACK portion of your code.
Can you comment the LAPACK call and confirm us that without the LAPACK
ZHEEV call the code does not SEGFAULT while it does with the LAPACK call.
-j
Julien Langou
 
Posts: 835
Joined: Thu Dec 09, 2004 12:32 pm
Location: Denver, CO, USA

Postby Julie » Wed Apr 16, 2008 2:47 pm

Hi,
You are assigning a lot of temporary arrays that are not deallocated after you do not need them.
You may want to optimized your code and thus your memory.
If you try with a smaller size, you code works, so it is just a problem of allocating/deallocating correctly
Julie
Julie
 
Posts: 299
Joined: Wed Feb 23, 2005 12:32 am
Location: ICL, Denver. Colorado

Postby jfkimberly » Wed Apr 16, 2008 11:23 pm

Thank you to the both of you for your comments. I was kind of
hoping to hear from both of you, as I have found your suggestions posted on this site the most helpful.

Julien, I tried your suggestion, I commented out everything from the LAPACK portion of the code down, but a segfault still appears.

Julie, I am just starting to use C and I don't know the proper way of allocating/deallocating memory. If you could give any further suggestions I would be much obliged.

Thank you once again to the both of you.
jfkimberly
 
Posts: 2
Joined: Tue Apr 15, 2008 2:47 am


Return to User Discussion

Who is online

Users browsing this forum: No registered users and 6 guests