我可以在Windows7x64(MSVC)和Linux64(GCC4.8.2)的.cu文件(CUDA5.5)中使用C++ 11吗?

Ale*_*lex 16 gcc cuda nvidia nvcc c++11

当我在Windows7x64(MSVS2012 + Nsight 2.0 + CUDA5.5)中编译包含设计C++ 11的以下代码时,我没有得到错误,并且所有内容都编译并运行良好:

#include <thrust/device_vector.h>

int main() {
    thrust::device_vector<int> dv(10);
    auto iter = dv.begin();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在Linux64(Debian 7 Wheezey + Nsight Eclipse,来自CUDA5.5)下编译它时,我得到错误:

../src/CudaCpp11.cu(5):错误:缺少显式类型(假设为"int")

../src/CudaCpp11.cu(5):错误:没有合适的转换函数

"thrust :: detail :: normal_iterator>"到"int"存在

在编译"/tmp/tmpxft_00001520_00000000-6_CudaCpp11.cpp1.ii"中检测到2个错误.make:* [src/CudaCpp11.o]错误2

当我添加行:-stdc ++ 11

在Properties-> Build-> Settings-> Tool Settings-> Build Stages-> Preprocessor options(-Xcompiler)中

我收到更多错误:

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432):错误:标识符"nullptr"未定义

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432):错误:预期";"

...

/usr/include/c++/4.8/bits/cpp_type_traits.h(314):错误:命名空间"std :: __ gnu_cxx"没有成员

"__normal_iterator"

/usr/include/c++/4.8/bits/cpp_type_traits.h(314):错误:预期">"

nvcc错误:'cudafe'因信号11(无效内存引用)而死亡:* [src/CudaCpp11.o]错误11

只有当我thrust::device_vector<int>::iterator iter = dv.begin();在Linux-GCC中使用时,我才会收到错误.但在Windows MSVS2012中,所有c ++ 11功能都可以正常工作!

我可以在Windows7x64(MSVC)和Linux64(GCC4.8.2)的.cu文件(CUDA5.5)中使用C++ 11吗?

Ax3*_*x3l 9

你可能要的main.cppothers.cu是这样的:

others.hpp:

void others();
Run Code Online (Sandbox Code Playgroud)

others.cu:

#include "others.hpp"
#include <boost/typeof/std/utility.hpp>
#include <thrust/device_vector.h>

void others() {
    thrust::device_vector<int> dv(10);
    BOOST_AUTO(iter, dv.begin()); // regular C++
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include "others.hpp"

int main() {
    others();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个特殊的答案表明,使用官方支持的gcc版本进行编译(正如Robert Crovella所说的那样)应该至少可以解决main.cpp文件中的c ++ 11代码:

g++ -std=c++0x -c main.cpp
nvcc -arch=sm_20 -c others.cu 
nvcc -lcudart -o test main.o others.o
Run Code Online (Sandbox Code Playgroud)

(在Debian 8上用nvcc 5.5和gcc 4.7.3测试).

回答你的基本问题:我不知道可以在Linux中使用CUDA 5.5的.cu文件中使用C++ 11(我不知道主机端C++ 11的显示示例是否正确地解决了MSVC).我甚至提交了一份关于constexpr支持的功能请求,该请求仍然是开放的.

CUDA 5.5 的CUDA编程指南指出:

对于主机代码,nvcc支持主机c ++编译器支持的C++ ISO/IEC 14882:2003规范的任何部分.

对于设备代码,nvcc支持代码示例中说明的功能,但限制条件中描述了一些限制; 它不支持运行时类型信息(RTTI),异常处理和C++标准库.

无论如何,可以使用一些 C++ 11功能,例如内核中的auto,例如使用boost :: auto.作为展望,其他C++ 11功能如线程可能不太可能最终进入CUDA,我听说还没有关于它们的官方计划(截至2013年超级计算).

无耻的插件:如果您对更多这些tweek感兴趣,请随时查看我们的库libPMacc,它提供用于模拟的多GPU网格和粒子抽象.我们实现了lambda,一种类似STL的访问概念,用于1-3D矩阵和其他有用的东西.

一切顺利,Axel


更新:由于正式添加了内核中的CUDA 7.0 C++ 11支持.正如BenC正确指出的那样,此功能的部分内容已经在CUDA 6.5中静默添加.