我正在一台 24 核机器上工作,内存约为 400GB,运行 Ubuntu。我在使用英特尔 MKL cblas_dgemm 进行简单矩阵乘法时遇到段错误:
int k=5;
int m=2E5;
int n=3E4;
double * A = (double *)mkl_malloc( m*k*sizeof( double ), 64 );
UTILITIES::check_alloc("A", A); //Check alloc just checks if the pointer is null
UTILITIES::random_matrix(m,k,A); //fills matrix with random numbers
double * B = (double *)mkl_malloc( k*n*sizeof( double ), 64 );
UTILITIES::check_alloc("B", B);
UTILITIES::random_matrix(k,n,B); //files matrix with random numbers
double * TestMatrix = (double *)mkl_malloc( m*n*sizeof( double ), 64 );
printf("Allocating a matrix of size %le\n",(double) m*n*sizeof(double)); //4.8E10
UTILITIES::check_alloc("TestMatrix", TestMatrix);//No problem
for (int i=0; i<m*n; i++) { //I added this just to make sure I could allocate the memory, of course this will all be overwritten
TestMatrix[i] = 1.0;
}
printf("TestMatrix %lf\n", TestMatrix[1]); //no problem accessing the memory
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m,n,k,
1.0,
A,k,
B,n,
0.0,
TestMatrix,n); //Segfaults at this line (without it, there is no segfault).
如果我更改矩阵的大小,使 n=1E4,则不会出现段错误。
你有什么想法吗?
请注意,我已确认我正在使用 64 位进行编译:
#if __x86_64__
printf("Running 64 bit\n");
#endif
我的编译行:
g++ -m64 main.cpp -I/opt/intel/composer_xe_2013_sp1.0.080/mkl/include -L/opt/intel/composer_xe_2013_sp1.0.080/mkl/lib/intel64 -lmkl_rt;
最佳答案
我最好的猜测是初始化 for
循环中的 m*n
导致溢出。
但是,(归功于 mch)段错误是由于 m*n*sizeof(double)
中的溢出造成的。显然,m*n
在向上转换为 size_t
之前会被评估。解决此问题的一种方法是将 m
和 n
更改为 long long
,相应地也将 i
更改为 i
。 p>
关于c++ - 大型矩阵上的 cblas_dgemm 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427892/