c++ BLAS替换导致Linux中的矩阵乘法失败,但在Windows中不会

jjhzyzn0  于 2023-02-26  发布在  Linux
关注(0)|答案(1)|浏览(180)

我已经下载并安装了qpOASES包。
考虑以下代码:

#define ARMA_DONT_USE_CXX11 /* In Cygwin */
#include <armadillo>

int main()
{

    arma::mat A;
    A
        <<0.0119<<0<<arma::endr
        <<0.0237<<0.0119<<arma::endr
        <<0.0354<<0.0437<<arma::endr
        <<0.0469<<0.0354;

    arma::mat B;
    B
    <<1.0<<0<<0<<0<<arma::endr
    <<0<<1.0<<0<<0<<arma::endr
    <<0<<0<<1.0<<0<<arma::endr
    <<0<<0<<0<<1.0;

    std::cout<<"A^T*B:"<<std::endl<<A.t()*B<<std::endl;

    return 0;
}

当我乘两个矩阵时,它在windows中运行正常:

g++ test.cpp -std=c++11 -o bin/example1 -I/cygdrive/d/tmp/qpoases/qpOASES/include -I'C:\cygwin\usr\local\include\' -I'C:\cygwin\usr\include' -Wall -Wconversion -O3 -lqpOASES -larmadillo -L'C:\cygwin\usr\local\lib\' /cygdrive/d/tmp/qpoases/qpOASES/src/BLASReplacement.o -L/cygdrive/d/tmp/qpoases/qpOASES/bin /cygdrive/d/tmp/qpoases/qpOASES/src/LAPACKReplacement.o -Wfatal-errors -Wconversion

./bin/example1
A^T*B:
   0.0119   0.0237   0.0354   0.0469
        0   0.0119   0.0437   0.0354

但在Linux(Ubuntu)下失败了:

g++ test.cpp -std=c++11 -g -o bin/example1  -Wall -Wconversion -O3 -lqpOASES -larmadillo  -L'qpoases' qpOASES/LAPACKReplacement.o qpOASES/BLASReplacement.o -Wfatal-errors -Wconversion

./bin/example1
A^T*B:
Segmentation fault (core dumped)

我已经意识到导致错误的是BLASReplacement。所以删除BLASReplacement程序工作正常:

g++ test.cpp -std=c++11 -g -o bin/example1  -Wall -Wconversion -O3 -lqpOASES -larmadillo  -L'qpoases' qpOASES/LAPACKReplacement.o -Wfatal-errors -Wconversion

./bin/example1
A^T*B:
   0.0119   0.0237   0.0354   0.0469
        0   0.0119   0.0437   0.0354

下面是对BLASReplacement.cpp的快速访问。
看起来一起使用qpOASESarmadillo会给我使用Linux带来问题。是什么导致了这个问题,为什么这个问题在我的Windows中不存在?

    • 更新**

我使用代码块作为调试器,发现最后一行导致错误:

typedef double T;
arma_fortran(arma_dgemm)(transA, transB, m, n, k, (const T*)alpha, (const T*)A, ldA, (const T*)B, ldB, (const T*)beta, (T*)C, ldC);

这个 Package 器的定义在def_blas.hpp

extern "C"
  {
     ...
     void arma_fortran(arma_dgemm)(const char* transA, const char* transB, const blas_int* m, const blas_int* n, const blas_int* k, const double* alpha, const double* A, const blas_int* ldA, const double* B, const blas_int* ldB, const double* beta, double* C, const blas_int* ldC);
     ...
  }

github对Armadillo库的一个分支是here
它在blas_wrapper.hpp中,在armadillo中,似乎armadillodgemmqpOASESBLASReplacement.o发生了冲突。解决它的最佳方法是什么?

66bbxpm5

66bbxpm51#

我知道这个老,但qpOASES修复这个问题在2021:https://github.com/coin-or/qpOASES/pull/108
回到第一次提出这个问题的时候,最好的选择可能是在BLASReplacement.hpp/cpp中为所有导出的函数添加前缀,使其与BLASAPI不兼容。

相关问题