【问题标题】:Segfault on `mkl_malloc``mkl_malloc` 上的段错误
【发布时间】:2014-06-16 14:31:06
【问题描述】:

上下文

我想使用英特尔的 MKL 库来创建一个数组,然后用它做各种事情。但是,它在mkl_malloc 上出现段错误。

问题

我正在尝试运行以下程序。在运行它时,我在指定的行出现段错误。问题在于mkl_malloc。我能做些什么来解决这个问题?怎么回事?

#include "mkl_types.h"
#include "mkl_spblas.h"
#include <stddef.h>  // For NULL
#include <stdio.h>


// Find reason for segfault
int main() {
    MKL_INT m=2000, k=1000;
    double  *A_dense;

    // Allocate memory to matrix
    A_dense = (double *)mkl_malloc( m*k*sizeof( double ), 128);  // Fails here.
    if (A_dense == NULL) {
        printf("ERROR: Can't allocate memory for matrices. Aborting... \n\n");
        mkl_free(A_dense);
        return 1;
    }

    mkl_free(A_dense);
    return 0;
}

编译:

 $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/mkl/lib/intel65
 $ gcc -m64 -I/opt/intel/mkl/include -L/opt/intel/mkl/lib/intel64 -lmkl_rt -lpthread -lm test_alloc.c -o test
test
test_alloc.c: In function 'main':
test_alloc.c:13:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

我使用 valgrind 得到的输出如下:

$ valgrind --leak-check=full ./test
==69680== Memcheck, a memory error detector
==69680== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==69680== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==69680== Command: ./test
==69680== 
==69680== Invalid read of size 8
==69680==    at 0x868D1D1: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_intel_lp64.so)
==69680==    by 0x4F56B1C: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_rt.so)
==69680==    by 0x4006B0: main (in /export/home/myuser/devel/part_distances/lib/test)
==69680==  Address 0xf42400 is not stack'd, malloc'd or (recently) free'd
==69680== 
==69680== 
==69680== Process terminating with default action of signal 11 (SIGSEGV)
==69680==  Access not within mapped region at address 0xF42400
==69680==    at 0x868D1D1: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_intel_lp64.so)
==69680==    by 0x4F56B1C: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_rt.so)
==69680==    by 0x4006B0: main (in /export/home/myuser/devel/part_distances/lib/test)
==69680==  If you believe this happened as a result of a stack
==69680==  overflow in your program's main thread (unlikely but
==69680==  possible), you can try to increase the size of the
==69680==  main thread stack using the --main-stacksize= flag.
==69680==  The main thread stack size used in this run was 8388608.
==69680== 
==69680== HEAP SUMMARY:
==69680==     in use at exit: 4,603 bytes in 19 blocks
==69680==   total heap usage: 19 allocs, 0 frees, 4,603 bytes allocated
==69680== 
==69680== LEAK SUMMARY:
==69680==    definitely lost: 0 bytes in 0 blocks
==69680==    indirectly lost: 0 bytes in 0 blocks
==69680==      possibly lost: 0 bytes in 0 blocks
==69680==    still reachable: 4,603 bytes in 19 blocks
==69680==         suppressed: 0 bytes in 0 blocks
==69680== Reachable blocks (those to which a pointer was found) are not shown.
==69680== To see them, rerun with: --leak-check=full --show-reachable=yes
==69680== 
==69680== For counts of detected and suppressed errors, rerun with: -v
==69680== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
Segmentation fault

替代方案

我正在使用单一动态链接 (SDL),因为我无法使用静态链接。

【问题讨论】:

  • 那些“Begin”、“Size is”和“Middle”打印输出不在您的代码中......
  • @unwind:修复了 valgrind 输出。我删除了打印输出以创建一个最小的工作示例。
  • 编译器警告让我敲响了警钟。看起来像是某种 32/64 位问题。您可以尝试包含mkl.h 而不是您现在包含的两个文件,看看是否可以修复编译警告。还要仔细检查您使用的是 64 位 mkl 库而不是 32 位库。
  • @KlasLindbäck 谢谢! mkl.h 修复问题。我很确定我始终使用 64 位。该库仅在/opt/intel/mkl/lib/intel64 下可用,lib/ 下没有其他内容。
  • @KlasLindbäck 你能回答一下吗?

标签: c segmentation-fault malloc valgrind intel-mkl


【解决方案1】:

文档建议包括mkl.h

所以改变:

#include "mkl_types.h"
#include "mkl_spblas.h"

#include "mkl.h"

【讨论】:

    【解决方案2】:

    你会收到一个可怕的警告:

    test_alloc.c:13:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
    

    我假设这是来自调用 mkl_malloc() 的代码行:

    A_dense = (double *)mkl_malloc( m*k*sizeof( double ), 128);
    

    这意味着您缺少标题,否则这里将没有“整数”。

    另外,你不应该像在 C 中那样使用 void *as I might have mentioned before on this site

    【讨论】:

    • "...从不投void *...":为什么不呢?对此有任何阅读吗?我启用了所有警告,并且通过修复,我没有收到任何警告。
    • @Unapiedra 我添加了一个链接。
    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2012-09-07
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多