将SuperLU稀疏求解器与RcppArmadillo一起使用 [英] Using SuperLU sparse solver with RcppArmadillo

查看:108
本文介绍了将SuperLU稀疏求解器与RcppArmadillo一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用犰狳的SparseLU解算器( http://arma.sourceforge.net/docs.html#spsolve )通过RcppArmadillo:

I am trying to use the SparseLU solver from armadillo (http://arma.sourceforge.net/docs.html#spsolve) through RcppArmadillo:

#define ARMA_USE_SUPERLU
// [Rcpp::depends(RcppArmadillo)]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::vec sp_solver(arma::sp_mat K, arma::vec x) {
  arma::superlu_opts opts;
  opts.symmetric = true;
  arma::vec res;
  arma::spsolve(res, K, x, "superlu", opts);
  return res;
}

/*** R
library(Matrix)
K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)
x <- runif(2)
sp_solver(K, x)
*/

我收到错误的对'superlu_free'的未定义引用.我想我缺少一些库链接.知道如何解决这个问题吗?

I get the error undefined reference to 'superlu_free'. I guess I'm missing some library linking. Any idea how to solve this issue?

我在Windows 10上.

I'm on Windows 10.

推荐答案

RcppArmadillo是超级方便,而且我自己一直一直使用它.由于将要从R中调用 all 个Rcpp *代码,因此我们可以假定存在 some 个功能.

RcppArmadillo is super-convenient and I use it myself all the time. Because all Rcpp* code is going to be called from R, we can assume some functionality to be present.

其中包括LAPACK和BLAS,并解释了即使Armadillo文档明确明确指出您需要LAPACK和BLAS的情况下,为什么也可以使用无链接"的RcppArmadillo的原因.为什么?好吧,因为R已经有了LAPACK和BLAS.

This includes LAPACK and BLAS, and explains why we can use RcppArmadillo "without linking" even when the Armadillo documentation clearly states you need LAPACK and BLAS. Why? Well because R already has LAPACK and BLAS.

(顺便说一句,当且仅当R是使用其自己的LAPACK子集构建的时,这会导致相当大的早期问题,因为某些复杂值例程无法使用.),这些年来,Brian Ripley在将那些缺少的例程添加到R的LAPACK中最有帮助.当R用外部LAPACK和BLAS构建时,从来没有问题,这在Deem中很常见./我维护的Ubuntu软件包.)

(As an aside, this lead to considerable early issues if and only if R was built with its own subset of LAPACK as certain complex valued routines were not available. Baptiste was hit quite a bit by this as his packages need(ed) these. Over the years, Brian Ripley was most helpful in adding those missing routines to R's LAPACK. And one never had issues when R was built with an external LAPACK and BLAS as is common for e.g. the Debian/Ubuntu package I maintain.)

在这里您需要SuperLU.这是可选的,并且确保您的链接是您的工作.简而言之,它不是神奇地工作.而且,由于涉及链接,因此很难实现自动化,这使我们变得难以控制平台依赖和安装要求.

Here you want SuperLU. This is optional, and it is your job to ensure this gets linked. In short, it does not just work magically. And it is hard to automate as it involves linking which gets us platform-dependendy and installation requirements we cannot control for easily.

但是问题并不新鲜,实际上有整个Rcpp Gallery帖子主题.

But the question is not new, and there is in fact an entire Rcpp Gallery post on the topic.

而且该帖子中有适合我的系统的单行代码,您的代码也可以正常工作(一旦我将 Rcpp :: depends 更正为有它需要的双括号:

And with one-liner from that post that is suitable for my system, your code works just fine too (once I correct the Rcpp::depends to have the double brackets it needs:

R> Sys.setenv("PKG_LIBS"="-lsuperlu")
R> sourceCpp("answer.cpp")

R> library(Matrix)

R> K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)

R> x <- runif(2)

R> sp_solver(K, x)
         [,1]
[1,] 0.264929
[2,] 0.546050
R> 

更正的代码

#define ARMA_USE_SUPERLU
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::vec sp_solver(arma::sp_mat K, arma::vec x) {
  arma::superlu_opts opts;
  opts.symmetric = true;
  arma::vec res;
  arma::spsolve(res, K, x, "superlu", opts);
  return res;
}

/*** R
library(Matrix)
K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)
x <- runif(2)
sp_solver(K, x)
*/

这篇关于将SuperLU稀疏求解器与RcppArmadillo一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆