【发布时间】:2017-04-26 20:10:12
【问题描述】:
我在 MATLAB MEX 代码中使用 CUDA Thrust 库时遇到问题。
我有一个在外部运行良好的示例,但如果我将其作为 MEX 文件编译并运行,它会在运行时产生“缺少符号”错误。
它似乎是 Thrust 库特有的。如果我使用 cudaMalloc 和 cudaMemcpy 或 cublasSetVector 而不是 thrust::device_vector,那么一切都很好。
最小示例
thrustDemo.cu:
#ifdef MATLAB_MEX_FILE
#include "mex.h"
#include "gpu/mxGPUArray.h"
#endif
#include <thrust/device_vector.h>
#include <vector>
void thrustDemo() {
std::vector<double> foo(65536, 3.14);
thrust::device_vector<double> device_foo(foo);
}
#ifdef MATLAB_MEX_FILE
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray const *prhs[]) {
thrustDemo();
}
#else
int main(void) { thrustDemo(); }
#endif
问题
我可以从命令行 (nvcc thrustDemo.cu) 编译它并正常运行生成的可执行文件。
当我尝试将其构建为 MATLAB MEX 文件(来自 MATLAB R2017a 中的mexcuda thrustDemo.cu)时,它可以正常编译和链接:
>> mexcuda thrustDemo.cu
Building with 'nvcc'.
MEX completed successfully.
但是当我尝试运行它时,我收到以下错误:
>> thrustDemo()
Invalid MEX-file '/home/kqs/thrustDemo.mexa64':
Missing symbol '_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5emptyEv' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt12length_errorC1EPKc' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt13runtime_errorC2EPKc' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEaSEPKc' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEPKc' required by '/home/kqs/thrustDemo.mexa64'
Missing symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLERKS4_' required by '/home/kqs/thrustDemo.mexa64'.
这对我来说很陌生;有人能告诉我这是什么意思吗?这些看起来像链接器错误,但它们是在运行时生成的。另外,我还以为 Thrust 是一个模板库,那有什么可以链接的呢?
最后,用cudaMalloc 和cudaMemcpy 或cublasSetVector 替换thrust::device_vector 就可以了。所以现在我在我的代码中被一堆cudaMalloc 困住了,这似乎......令人反感。我真的很想能够使用 Thrust。
版本
MATLAB R2017a
nvcc V8.0.61、gcc 5.4.0、Ubuntu 16.04.2
NVidia 驱动程序 375.39,GTX 1060 显卡(计算能力 6.1)
更新:ldd 输出
每个 cmets,我使用 ldd thrustDemo.mexa64 检查了 MEX 文件的依赖关系:
linux-vdso.so.1 => (0x00007ffdd35ea000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f097eccf000)
libcudart.so.8.0 => /usr/local/cuda-8.0/targets/x86_64-linux/lib/libcudart.so.8.0 (0x00007f097ea69000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f097e852000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f097e489000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f097e180000)
/lib64/ld-linux-x86-64.so.2 (0x0000562df178c000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f097df7b000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f097dd5e000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f097db56000)
我尝试寻找这些缺失的符号之一,并且能够找到它:
$ nm -D /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep "_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv"
0000000000120be0 W _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv
所以看来 MATLAB 一定是找错地方了。
【问题讨论】:
-
这不是运行时错误。正在加载 mex 文件时发生错误。我不知道错误的原因。但是您应该能够使用 linux 中的 ldd 等工具检查您的 mex 文件以检查依赖关系。
-
这是某种损坏的 C++/stdlib 问题或主机编译器不匹配。涉及的函数是
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const,与CUDA无关 -
我明白了;错误输出现在更有意义了。我认为 MATLAB 更喜欢使用自己的
libstdc++版本,这可能是根本原因。谢谢你们的cmets。