如何从C调用CLAPACK?

时间:2014-02-28 02:54:37

标签: c lapack blas lapacke

我正在尝试从C调用CLAPACK函数。我下载了CLAPACK-3.2.1(来自http://www.netlib.org/clapack/clapack.tgz)并按照此处的说明(http://people.sc.fsu.edu/~%20jburkardt/c_src/clapack/clapack.html)。我的CLAPACK发行版是:

$ ls
BLAS/  COPYING  F2CLIBS/  INCLUDE/  INSTALL/  Makefile  make.inc.example  my_example.c  README.install  SRC/  TESTING/

我的文件my_example.c只是:

#include <stdio.h>
#include "blaswrap.h"
#include "f2c.h"
#include "clapack.h"

int main()
{
  char ta = 'N';
  char tb = 'N';
  double a[3][3];
  a[0][0] = 1;
  a[0][1] = 2;
  a[0][2] = 3;
  a[1][0] = 4;
  a[1][1] = 5;
  a[1][2] = 6;
  a[2][0] = 7;
  a[2][1] = 8;
  a[2][2] = 9;
  double b[3][3];
  b[0][0] = 1;
  b[0][1] = 0;
  b[0][2] = 0;
  b[1][0] = 0;
  b[1][1] = 0;
  b[1][2] = 0;
  b[2][0] = 5;
  b[2][1] = 5;
  b[2][2] = 5;
  double c[3][3];
  long int m = 3;
  long int n = 3;
  long int k = 3;
  double alpha = 1.0;
  double beta = 0.0;
  long int lda = 3;
  long int ldb = 3;
  long int ldc = 3;

  f2c_dgemm(&ta, &tb, &m, &n, &k, &alpha, &a[0][0], &lda, &b[0][0], &ldb, &beta, &c[0][0], &ldc);
  printf("Resulting C[0][0]: %f\n", c[0][0]);
  return 0;
}

当我编译它时,我得到了这个:

$ gcc -I./INCLUDE -I./F2CLIBS/libf2c/ -I./BLAS/WRAP/ my_example.c BLAS/SRC/dgemm.c BLAS/SRC/xerbla.c BLAS/SRC/lsame.c -o my_example.o
How can I get this to compile and run correctly?
BLAS/SRC/xerbla.c: In function ‘xerbla_’:
BLAS/SRC/xerbla.c:69:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
BLAS/SRC/xerbla.c:70:3: warning: format ‘%i’ expects argument of type ‘int’, but argument 3 has type ‘integer’ [-Wformat]

这是调用clapack函数的正确方法吗?数组传递是否正确?

(PS我不是想动态链接到现有的clapack安装)。

1 个答案:

答案 0 :(得分:2)

我不知道LAPACKC和LAPACKE之间的区别,因为我不知道LAPACKC,但我希望LAPACKE能够做到这一点...... http://www.netlib.org/lapack/lapacke.html

除了dgemm_是BLAS的一部分且接口未在lapacke.h中定义... 与LAPACKC相比,LAPACKE可能会膨胀,但它还不够大!

(丑陋?)摆脱麻烦的方法是在c代码中声明fortran函数并链接BLAS。 http://www.netlib.org/lapack/lawnspdf/lawn270.pdf

这是一个最小的工作示例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <lapacke.h>

extern void dgemm_(char *transa, char *transb, int *m, int *n, int *k, double
        *alpha, double *a, int *lda, double *b, int *ldb, double *beta, double *c,
        int *ldc );

int main()
{
    double AA[3*3]={  2.0, -1.0,  0.0,
            0,  2.0, -1.0,
            0.0,  0,  2.0};

    double B[3*3]={  2.0, -1.0,  0.0,
            0,  2.0, -1.0,
            0.0,  0,  2.0};

    double C[3*3]={  2.0, -1.0,  0.0,
            0,  2.0, -1.0,
            0.0,  0,  2.0};
    int nn=3;
    int lda=3;
    int ldb=3;
    int ldc=3;
    double alpha=2.0;
    double beta=0.0;

    dgemm_( "N", "N", &nn, &nn, &nn,&alpha, AA, &lda,
            B,&ldb, &beta, C, &ldc );

    printf("C is : %10.4f %10.4f %10.4f\n",C[0], C[1], C[2]);
    printf("       %10.4f %10.4f %10.4f\n",C[3], C[4], C[5]);
    printf("       %10.4f %10.4f %10.4f\n",C[6], C[7], C[8]);
    return 0;
}

要构建此代码:gcc main.c -o main -lblas

LAPACK函数的许多接口在lapacke.h和LAPACKE的其他头文件中定义。例如,要使用dgetrf_(&n, &n, A, &n,ipvs, &info);构建代码,代码将由gcc main.c -o main -llapack -lblas -I /path/to/lapacke.h

构建

希望这会有所帮助,尽管我不确定会告诉你一些有价值的东西!

再见,

弗朗西斯