gpt4 book ai didi

intel-mkl - 无法使用 g++ 将数学库与 intel mkl 链接

转载 作者:行者123 更新时间:2023-12-01 06:29:28 28 4
gpt4 key购买 nike

我尝试使用 g++ 用 intel mkl 11.1 进行编译:

g++ -m32 test.c -lmkl_intel -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm

错误说:
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `logf'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `atan2'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `sin'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `fabs'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `exp'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `cos'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `sqrt'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_intel_thread.so: undefined reference to `log'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `pow'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `log10'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `ceil'
/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/ia32/libmkl_core.so: undefined reference to `expf'

但是,如果我编译
g++ -m32 test.c -lmkl_intel -lmkl_intel_thread -lmkl_core -lm -liomp5 -lpthread -lm

不会有错误。顺便说一下,我不明白为什么在 -lmkl_core 之后添加 -lm 会有所帮助
gcc -m32 test.c -lmkl_intel -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm

结果没问题,我也不明白这里有什么区别。
下面是我的测试代码。
/* C source code is found in dgemm_example.c */

#define min(x,y) (((x) < (y)) ? (x) : (y))

#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"

int main()
{
double *A, *B, *C;
int m, n, k, i, j;
double alpha, beta;

printf ("\n This example computes real matrix C=alpha*A*B+beta*C using \n"
" Intel(R) MKL function dgemm, where A, B, and C are matrices and \n"
" alpha and beta are double precision scalars\n\n");

m = 2000, k = 200, n = 1000;
printf (" Initializing data for matrix multiplication C=A*B for matrix \n"
" A(%ix%i) and matrix B(%ix%i)\n\n", m, k, k, n);
alpha = 1.0; beta = 0.0;

printf (" Allocating memory for matrices aligned on 64-byte boundary for better \n"
" performance \n\n");
A = (double *)mkl_malloc( m*k*sizeof( double ), 64 );
B = (double *)mkl_malloc( k*n*sizeof( double ), 64 );
C = (double *)mkl_malloc( m*n*sizeof( double ), 64 );
if (A == NULL || B == NULL || C == NULL) {
printf( "\n ERROR: Can't allocate memory for matrices. Aborting... \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 1;
}

printf (" Intializing matrix data \n\n");
for (i = 0; i < (m*k); i++) {
A[i] = (double)(i+1);
}

for (i = 0; i < (k*n); i++) {
B[i] = (double)(-i-1);
}

for (i = 0; i < (m*n); i++) {
C[i] = 0.0;
}

printf (" Computing matrix product using Intel(R) MKL dgemm function via CBLAS interface \n\n");
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, k, alpha, A, k, B, n, beta, C, n);
printf ("\n Computations completed.\n\n");

printf (" Top left corner of matrix A: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(k,6); j++) {
printf ("%12.0f", A[j+i*k]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix B: \n");
for (i=0; i<min(k,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.0f", B[j+i*n]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix C: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.5G", C[j+i*n]);
}
printf ("\n");
}

printf ("\n Deallocating memory \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);

printf (" Example completed. \n\n");
return 0;
}

最佳答案

我正在 Debian 上工作,我按照以下步骤成功运行和编译 mkl 中的示例程序之一:

  • 我用 script.sh自动安装必要的包(需要使用 sudo )
  • 运行示例程序之一(例如 this one),如下所示:
    ( ref )
  • $ export MKLROOT=/opt/intel/compilers_and_libraries_2018.2.199/linux/mkl
  • $ cc -fopenmp -m64 -I${MKLROOT}/include test.c -Wl,--no-as-needed -L${MKLROOT}/lib/intel64 -lmkl_intel_lp64 -lmkl_core -lmkl_gnu_thread -lpthread -lm -ldl
  • $ ./a.out
  • 关于intel-mkl - 无法使用 g++ 将数学库与 intel mkl 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24199216/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com