Take the 2-minute tour ×

I'm trying to call the F77_NAME(dgeqrf) function from lapack lib but the program crashes. What do you think it is wrong ?

#include <stdio.h>
#include <R.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>

double* getcopy(int* nrow, int* ncol,double* a)
    double* copy = malloc(*nrow**ncol*sizeof(double));
    int i;
        copy[i] = a[i];
    return copy;

int main() {
int m=3,n=3;
double a[] = {12,-51,4,6,167,-68,-4,24,-41};
double* acopy = getcopy(&m,&n,a);

double tau[3];
int info=0;
int i;

int one=1;
double* work = malloc(max(&one,&m)*sizeof(double));
int lwork = 0;
int lda = max(&m,&n);



return 0;
share|improve this question
Aren't you supposed to do int lwork = max(&one, &m) so that LAPACK knows the size of the workspace ? Otherwise this seems okay, you definitely should use a debugger to see which line makes the program crash. –  Alexandre C. Apr 17 '11 at 9:36
Instead of a debugger, it might suffice to use several well-placed printf() statements. –  Philip Apr 17 '11 at 9:44
:)) i can't believe it, it worked, it needed the max(&one,&m) ... need to be more carefully and sleep after 10 hours of coding –  cfort Apr 17 '11 at 14:53
add comment

1 Answer

From dgeqrf.f source code:

*  LWORK   (input) INTEGER
*          For optimum performance LWORK >= N*NB, where NB is
*          the optimal blocksize.
*          If LWORK = -1, then a workspace query is assumed; the routine
*          only calculates the optimal size of the WORK array, returns
*          this value as the first entry of the WORK array, and no error
*          message related to LWORK is issued by XERBLA.

So if you want an optimal run you first need to call the function with 'lwork' set to -1 to get the optimal size of 'work'.

share|improve this answer
add comment

Your Answer


