犰狳 + Matlab Mex 段错误 [英] Armadillo + Matlab Mex segfault

查看:31
本文介绍了犰狳 + Matlab Mex 段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在摆弄这个,所以我想我可以让每个人都从我的经验中受益,请在下面查看我的答案.

我第一次在 Matlab 中运行已编译的 Mex 文件时遇到问题,因为 Matlab 抱怨它无法打开共享库 libarmadillo.我使用环境变量解决了这个问题LD_LIBRARY_PATHLD_RUN_PATH(DYLD_LIBRARY_PATHLYLD_RUN_PATH 在 osx 中).

但问题仍然存在,即使完全相同的代码在 Matlab 之外(不是 Mex'd)编译和运行良好,一个简单的测试文件也会在运行时出现段错误.

解决方案

segfault 似乎是由于 Matlab 使用 64 位整数(long longint64_t) 在其捆绑的 LAPACK 和 BLAS 库中.另一方面,犰狳默认使用 32 位整数(64 位平台上的常规 int,或 int32_t).

有两种解决方案;第一个涉及强制 Matlab 链接到系统的库(使用 ints),第二个涉及更改 Armadillo 的配置文件以使用 BLAS 启用 long longs.我倾向于认为第一个更可靠,因为没有黑盒效果,但也比较麻烦,因为你需要手动安装并记住你的 BLAS 和 LAPACK 库的路径.

这两种解决方案都要求我停止使用 Armadillo 的共享库并手动链接/包含源代码.为此,您必须简单地在您的系统上安装 LAPACK 和 BLAS(如果它们还没有,在 Ubuntu 中是 libblas-devliblapack-dev),然后复制整个 includes 目录,例如在 $HOME/.local/arma 中.

<小时>

解决方案 1:链接到系统的库

在 matlab 控制台中,将环境变量 BLAS_VERSIONLAPACK_VERSION 设置为指向您系统的库.就我而言(Ubuntu 14.04,Matlab R2014b):

setenv('BLAS_VERSION','/usr/lib/libblas.so');setenv('LAPACK_VERSION','/usr/lib/liblapack.so');

然后就可以正常编译了:

mex -compatibleArrayDims -outdir +mx -L/home/john/.local/arma -llapack -lblas -I/home/john/.local/arma test_arma.cpp

或者如果您在 includes/armadillo_bits/config.hpp 中定义标志 ARMA_64BIT_WORD,则可以删除选项 -compatibleArrayDims.p><小时>

解决方案 2:更改 Armadillo 的配置

第二种解决方案是取消注释 Armadillo 的配置文件 includes/armadillo_bits/config.hpp 中的标志 ARMA_BLAS_LONG_LONG.Matlab 将链接到其捆绑的 LAPACK 和 BLAS 库,但这次 Armadillo 不会出现段错误,因为它使用了正确的字长.和以前一样,如果你想删除 -compatibleArrayDims,你也可以取消注释 ARMA_64BIT_WORD.

I fiddled with this the whole day, so I thought I might make everyone benefit from my experience, please see my answer below.

I first had a problem with running a compiled Mex file within Matlab, because Matlab complained that it couldn't open the shared library libarmadillo. I solved this using the environment variables LD_LIBRARY_PATH and LD_RUN_PATH (DYLD_LIBRARY_PATH and LYLD_RUN_PATH in osx).

The problem remained however, that a simple test file would segfault at runtime even though the exact same code would compile and run fine outside Matlab (not Mex'd).

解决方案

The segfault seems to be caused by the fact that Matlab uses 64bits integers (long long or int64_t) in its bundled LAPACK and BLAS libraries. Armadillo on the other hand, uses 32bits integers (regular int on a 64bits platform, or int32_t) by default.

There are two solutions; the first one involves forcing Matlab to link to the system's libraries instead (which use ints), the second involves changing Armadillo's config file to enable long longs with BLAS. I tend to think that the first is more reliable, because there is no black-box effect, but it's also more troublesome, because you need to manually install and remember the path of your BLAS and LAPACK libs.

Both solutions required that I stopped using Armadillo's shared libraries and linked/included manually the sources. To do this, you must simply install LAPACK and BLAS on your system (if they are not already there, in Ubuntu that's libblas-dev and liblapack-dev), and copy the entire includes directory somewhere sensible like in $HOME/.local/arma for example.


Solution 1: linking to system's libraries

From the matlab console, set the environment variables BLAS_VERSION and LAPACK_VERSION to point to your system's libraries. In my case (Ubuntu 14.04, Matlab R2014b):

setenv('BLAS_VERSION','/usr/lib/libblas.so');
setenv('LAPACK_VERSION','/usr/lib/liblapack.so');

You can then compile normally:

mex -compatibleArrayDims -outdir +mx -L/home/john/.local/arma -llapack -lblas -I/home/john/.local/arma test_arma.cpp

or if you define the flag ARMA_64BIT_WORD in includes/armadillo_bits/config.hpp, you can drop the option -compatibleArrayDims.


Solution 2: changing Armadillo's config

The second solution involves uncommenting the flag ARMA_BLAS_LONG_LONG in Armadillo's config file includes/armadillo_bits/config.hpp. Matlab will link to its bundled LAPACK and BLAS libraries, but this time Armadillo won't segfault because it's using the right word-size. Same than before, you can also uncomment ARMA_64BIT_WORD if you want to drop the -compatibleArrayDims.

这篇关于犰狳 + Matlab Mex 段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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