2011-04-17 78 views
0

我試圖從lapack lib調用F77_NAME(dgeqrf)函數,但程序崩潰。 您認爲這是錯誤的?從lapack庫調用F77_NAME()函數時,我的代碼崩潰

#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; 
    for(i=0;i<*nrow**ncol;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); 

F77_NAME(dgeqrf)(&m,&n,acopy,&m,tau,work,&lwork,&info); 

for(i=0;i<m*n;i++) 
printf("%f",acopy[i]); 

return 0; 
} 
+3

是不是應該執行'int lwork = max(&one,&m)'以便LAPACK知道工作區的大小?否則,這似乎是好的,你絕對應該使用調試器來查看哪一行會導致程序崩潰。 – 2011-04-17 09:36:59

+1

而不是調試器,使用幾個放置良好的'printf()'語句就足夠了。 – Philip 2011-04-17 09:44:28

+0

:))我無法相信它,它的工作原理,它需要最大(&一,&m)...需要更仔細,並在10小時的編碼後睡覺 – cfort 2011-04-17 14:53:30

回答

0

從dgeqrf.f源代碼:


* LWORK (input) INTEGER 
*   The dimension of the array WORK. LWORK >= max(1,N). 
*   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. 

所以,如果你想一個最佳的運行首先需要調用與設置爲-1「lwork」功能來獲得的「最佳規模工作'。