Compiling lapacke example code?

 ̄綄美尐妖づ 提交于 2019-12-11 21:40:52

问题


I'm trying to compile the examples found here:

http://www.netlib.org/lapack/lapacke.html#_examples

Specifically, I'm trying to get the "Calling CGEQRF and the CBLAS" example to work. The code is like so:

#include <stdio.h>
#include <stdlib.h>
#include <lapacke.h>
#include <cblas.h>

int main (int argc, const char * argv[])
{
 lapack_complex_float *a,*tau,*r,one,zero;
 lapack_int info,m,n,lda;
 int i,j;
 float err=0.0;
 m = 10;   n = 5;   lda = m;
 one = lapack_make_complex_float(1.0,0.0);
 zero= lapack_make_complex_float(0.0,0.0);
 a = calloc(m*n,sizeof(lapack_complex_float));
 r = calloc(n*n,sizeof(lapack_complex_float));
 tau = calloc(m,sizeof(lapack_complex_float));
 for(j=0;j<n;j++)
    for(i=0;i<m;i++)
       a[i+j*m] = lapack_make_complex_float(i+1,j+1);
 info = LAPACKE_cgeqrf(LAPACK_COL_MAJOR,m,n,a,lda,tau);
 info = LAPACKE_cungqr(LAPACK_COL_MAJOR,m,n,n,a,lda,tau);
 for(j=0;j<n;j++)
    for(i=0;i<n;i++)
       r[i+j*n]=(i==j)?-one:zero;
 cblas_cgemm(CblasColMajor,CblasConjTrans,CblasNoTrans,
           n,n,m,&one,a,lda,a,lda,&one,r,n );
 for(i=0;i<n;i++)
    for(j=0;j<n;j++)
       err=MAX(err,cabs(r[i+j*n]));
 printf("error=%e\n",err);
 free(tau);
 free(r);
 free(a);
 return(info);
} 

If I save the file as a .cpp (perhaps this is my first mistake?) and compile using

g++ lapacketest.cpp -llapack 

I get the following compile errors:

3_20_2.cpp:14:7: error: assigning to '_Complex float *' from incompatible type 'void *' a = calloc(m*n,sizeof(lapack_complex_float)); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3_20_2.cpp:15:7: error: assigning to '_Complex float *' from incompatible type 'void *' r = calloc(n*n,sizeof(lapack_complex_float)); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3_20_2.cpp:16:9: error: assigning to '_Complex float *' from incompatible type 'void *' tau = calloc(m,sizeof(lapack_complex_float)); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3_20_2.cpp:29:25: error: use of undeclared identifier 'cabs' err=MAX(err,cabs(r[i+j*n]));

I tried changing to .c and compiling with gcc, but I got other strange errors. Any thoughts? I'm slowly trying transition from Matlab to c++ for scientific computing and so far it's just been a giant headache.


回答1:


It looks like calloc(m*n,sizeof(lapack_complex_float)); is not returning a pointer to an object of type lapack_complex_float.




回答2:


I could compile it but you have to change the line

#include <lapacke.h> 

to

#include <lapacke_utils.h>

You find at http://www.netlib.org/lapack/explore-html/da/d8e/lapacke__utils_8h_source.html. After you download it and put in the same directory of your program or use the tags -L -I to locate the library on your system during the compilation.

To compile use:

gcc CGEQRF_CUNGQR.c -llapacke -lblas -lm


来源:https://stackoverflow.com/questions/22541401/compiling-lapacke-example-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!