从 lapack 库调用 F77_NAME() 函数时,我的代码崩溃了

发布于 2024-11-02 01:31:27 字数 887 浏览 6 评论 0原文

我试图从 lapack lib 调用 F77_NAME(dgeqrf) 函数,但程序崩溃了。

你觉得有什么不对吗?

#include <R.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
#include <stdio.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;
}

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 <R.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
#include <stdio.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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-11-09 01:31:27

来自 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.

因此,如果您想要最佳运行,您首先需要调用该函数并将“lwork”设置为 -1 以获得“work”的最佳大小。

From dgeqrf.f source code:


*  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.

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文