1

我正在尝试从 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 安装)。

4

1 回答 1

2

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

除了那dgemm_是 BLAS 的一部分并且接口没有定义在lapacke.h... LAPACKE 相比 LAPACKC 可能臃肿,但它还不够大!

摆脱麻烦的一种(丑陋?)方法是在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 函数的许多接口都在 LAPACKlapacke.h的其他头文件中定义。例如,要使用 构建代码dgetrf_(&n, &n, A, &n,ipvs, &info);,代码将由gcc main.c -o main -llapack -lblas -I /path/to/lapacke.h

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

再见,

弗朗西斯

于 2014-02-28T22:34:40.203 回答