Below is my C wrapper for a Fortran ZHEEVR routine from well-known LAPACK numerical library:
void zheevr(char jobz, char range, char uplo, int n, doublecomplex* a, int lda, double vl, double vu, int il, int iu, double abstol, double* w, doublecomplex* z, int ldz, int* info)
{
int m;
int lwork = -1;
int liwork = -1;
int lrwork = -1;
int* isuppz = alloc_memory(sizeof(int) * 2 * n);
zheevr_(&jobz, &range, &uplo, &n, a, &lda, &vl, &vu, &il, &iu, &abstol, &m, w, z, &ldz, isuppz, small_work_doublecomplex, &lwork, small_work_double, &lrwork, small_work_int, &liwork, &info);
lwork = (int) small_work_doublecomplex[0].real;
liwork = small_work_int[0];
lrwork = (int) small_work_double[0];
doublecomplex* work = alloc_memory(sizeof(doublecomplex) * lwork);
double* rwork = alloc_memory(sizeof(double) * lrwork);
int* iwork = alloc_memory(sizeof(int) * liwork);
zheevr_(&jobz, &range, &uplo, &n, a, &lda, &vl, &vu, &il, &iu, &abstol, &m, w, z, &ldz, isuppz, work, &lwork, rwork, &lrwork, iwork, &liwork, info);
free(iwork);
free(rwork);
free(work);
free(isuppz);
}
This function is called hundreds of thousands of times in my application, to diagonalize the complex matrix "a" (parameter names follow the Fortran convention for this function) for the same matrix size. I think that the work arrays sizes will be the same most of the time, as the diagonalized matrices will be of the same structure. My questions are:
- Can the repeated alloc/free ("alloc_memory" is a simple wrapper around glibc's malloc) calls hurt performance, and how badly?
- Does the order of free's matter? Should I free the last allocated array first, or last?