2014-01-30 49 views
16

當我編譯包含設計C++ 11,在Windows7x64(MSVS2012 + Nsight 2.0 + CUDA5.5),那麼我不明白的錯誤,一切編譯和下面的代碼工作得很好:我可以在Windows7x64(MSVC)和Linux64(GCC4.8.2)的.cu-files(CUDA5.5)中使用C++ 11嗎?

#include <thrust/device_vector.h> 

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

    return 0; 
} 

但是,當我嘗試編譯它下LINUX64(Debian的7 Wheezey + Nsight Eclipse的從CUDA5.5),我得到的錯誤:

../src/CudaCpp11.cu(5): error: explicit type is missing ("int" assumed)

../src/CudaCpp11.cu(5): error: no suitable conversion function from

"thrust::detail::normal_iterator>" to "int" exists

2 errors detected in the compilation of "/tmp/tmpxft_00001520_00000000-6_CudaCpp11.cpp1.ii". make: * [src/CudaCpp11.o] Error 2

當我加入一行:-stdC++ 11

在Properties-> Build中 - >設置 - >工具設置 - >構建滑臺>預處理選項(-Xcompiler)

我得到更多的錯誤:

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected a ";"

...

/usr/include/c++/4.8/bits/cpp_type_traits.h(314): error: namespace "std::__gnu_cxx" has no member

"__normal_iterator"

/usr/include/c++/4.8/bits/cpp_type_traits.h(314): error: expected a ">"

nvcc error : 'cudafe' died due to signal 11 (Invalid memory reference) make: * [src/CudaCpp11.o] Error 11

只有當我在Linux-GCC中使用thrust::device_vector<int>::iterator iter = dv.begin();,那麼我不會收到錯誤。但在Windows MSVS2012中,所有的C++ 11功能都能正常工作!

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

+3

gcc 4.8.x不是linux下的[官方支持的編譯器](http://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#linux-5-5)。 –

+0

@Robert Crovella GCC 4.7.2具有相同的效果 - 無法在nvcc + gcc中編譯C++ 11。或者,我怎麼能這樣做呢,還是我可以在nvcc + icc(Intel編譯器)中使用C++ 11? – Alex

+0

從我的頭頂開始:最後我可以參加關於GPU編程的講座。只需1.5小時。從那裏我記得我們在幻燈片上看到的所有代碼都是基於C99的GPU的特殊語言。不知道它是否是CUDA,我不記得它。但是,如果是這樣,這意味着該語言甚至不是C或C++,它只是基於C99的。也許新功能將從C或C++的新標準中獲得,可能不會。你將不得不跟蹤特定語言的發展。但如果是別的東西,請忽略此評論。我會稍後嘗試在這個主題上找到更多。 – Palec

回答

9

你可能要分裂others.cu這樣的main.cpp

others.hpp:

void others(); 

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++ 
} 

main.cpp中:

#include "others.hpp" 

int main() { 
    others(); 

    return 0; 
} 

This particular answer表明,與官方支持的gcc版本編譯(羅伯特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 

(在Debian 8測試使用nvcc 5.5和gcc 4.7.3)。

回答您的根本問題:我不知道可以在Linux中使用帶有CUDA 5.5的.cu文件中的C++ 11(並且我不知道主機端C++ 11顯示的示例正確得到在MSVC下非常混亂)。我甚至提交了一份constexpr的功能請求,該功能仍然是開放的。

CUDA programming CUDA指南5。5種狀態:

For the host code, nvcc supports whatever part of the C++ ISO/IEC 14882:2003 specification the host c++ compiler supports.

For the device code, nvcc supports the features illustrated in Code Samples with some restrictions described in Restrictions; it does not support run time type information (RTTI), exception handling, and the C++ Standard Library.

無論如何,它是能夠使用的C++ 11的特徵的一些自動在內核中,例如與boost :: auto。 作爲一個前景,其他C++ 11功能如線程可能不太可能最終在CUDA中,我聽不到有關它們的官方計劃(截至supercomputing 2013)。

無恥的插件:如果您對更多這些tweeks感興趣,請隨時查看我們的庫libPMacc,它爲模擬提供了多GPU網格和粒子抽象。我們實現了lambda,這是一種類似於1-3D矩陣的STL類訪問概念以及其他有用的東西。

一切順利, 阿克塞爾


更新:由於CUDA 7.0 C++11支持在內核已正式加入。正如BenC所指出的那樣,這個特性的一部分已經在CUDA 6.5中悄悄添加了。

3

According to Jared HoberockThrust開發人員),似乎C++ 11支持已被添加到CUDA 6.5(雖然它仍然是實驗和未記錄)。當開始在非常大的C++/CUDA項目中使用C++ 11時,這可能會使事情變得更容易,因爲當您使用CMake時,分割所有對於大型項目來說可能非常麻煩。

+1

非常感謝!好消息是:「這個想法是,nvcc將支持**主機編譯器支持的任何C++ 11構造**,他們將在未來發布描述注意事項的文檔。 最大的限制是lambda不能從主機傳遞到__global__函數啓動 根據我的經驗,**您提到的所有構造都支持g ++作爲主機編譯器。**「 – Alex

+0

更新:CUDA 7.0在內核* C++中添加* 11支持。我更新了我的答案以反映這一點。 – Ax3l

相關問題