我只是 CUDA 和 Nsight 的初学者,希望利用出色的 GPU 性能进行线性代数运算(例如 CUBLAS)。我在 Eigen 的帮助下编写了很多自定义代码并且有很多矩阵乘法运算,所以我希望我的代码保持不变,只需在 GPU 上执行这些运算即可。
我使用 Visual Studio Nsight 创建了一个示例项目,它工作正常,但是当我添加时
#include <Eigen/Dense>
连接到该项目,我遇到以下错误
1>------ Build started: Project: MatrixPerformanceCompare, Configuration: Debug Win32 ------
1> Compiling CUDA source file kernel.cu...
1>
1> C:\CUDA\Progs\VS\SampleProject\MatrixPerformanceCompare>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2010 -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin" -Ic:\CUDA\Progs\VS\SampleProject\MatrixPerformanceCompare\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj "C:\CUDA\Progs\VS\SampleProject\MatrixPerformanceCompare\kernel.cu"
1>c:\cuda\progs\vs\sampleproject\matrixperformancecompare\include\eigen\src/Core/Block.h(102): error : "operator=" has already been declared in the current scope
1>c:\cuda\progs\vs\sampleproject\matrixperformancecompare\include\eigen\src/Core/Ref.h(122): error : "operator=" has already been declared in the current scope
1>c:\cuda\progs\vs\sampleproject\matrixperformancecompare\include\eigen\src/Core/products/Parallelizer.h(20): warning : variable "m_maxThreads" was set but never used
1>c:\cuda\progs\vs\sampleproject\matrixperformancecompare\include\eigen\src/Geometry/RotationBase.h(76): error : function template "Eigen::operator*(const Eigen::EigenBase<OtherDerived> &, const Eigen::Quaternion<_Scalar, _Options> &)" has already been defined
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\CUDA 5.5.targets(592,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2010 -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin" -Ic:\CUDA\Progs\VS\SampleProject\MatrixPerformanceCompare\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj "C:\CUDA\Progs\VS\SampleProject\MatrixPerformanceCompare\kernel.cu"" exited with code 2.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
我知道这是与定义保护有关的错误,但 Eigen 中的错误似乎没问题,并且在简单的 c++ 项目中,具有相同 Eigen 源的代码编译得很好。 你能帮我吗?
最佳答案
C++ 代码的 CUDA 前端解析器无法在所有情况下正确解析极其复杂的主机模板定义。它的工作是查看 .cu
文件中的代码,并尝试将必须由 GPU 工具链编译的代码与应传递到主机编译器的代码分开。已知将 Boost 和 QT header 导入 .cu
文件时会失败。我敢打赌 Eigen 模板也会导致同样的问题。
我知道的唯一解决方案是重构代码,将依赖模板的主机代码分离到具有 .cc
扩展名的不同文件中。 CUDA 前端从未在 .cc
文件中看到任何代码,问题就消失了。实际上,这种代码分割并不是真正的问题,因为无论如何,主机模板代码实际上都不能在 CUDA GPU 代码中使用,最坏的情况是您可能需要一个小的包装函数或额外的抽象级别来保持 GPU和主机代码分开。
关于c++ - CUDA 和 Eigen 出现成员 "has already been declared"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216359/