简体   繁体   English

CUDA-nvcc如何编译__host__代码?

[英]cuda - How does nvcc compile __host__ code?

I'm trying to convert my c++ only project to cuda code to run on GPU. 我正在尝试将仅c ++项目转换为cuda代码以在GPU上运行。

I'm new to cuda programming and I don't know what to do with this circumstance: 我是cuda编程的新手,我不知道该怎么办:

If I have a very complicated class definition and now I want to pass a class instance to the device and execute some of its member functions on the device, I then should rewrite my whole .cpp file. 如果我有一个非常复杂的类定义,现在我想将一个类实例传递给设备并在设备上执行其某些成员函数,则应重写整个.cpp文件。 Do I only need to turn those functions run on device to __host__ __device__ or should I rewrite all the functions? 我是否只需要将设备上运行的那些功能转换为__host__ __device__还是应该重写所有功能?

I think nvcc treat functions with no function type qualifiers as __host__ . 我认为nvcc将没有函数类型限定符的函数视为__host__ How does it compile host code? 它如何编译主机代码? Does it compile them exactly as g++ does? 是否完全像g++一样编译它们?

If I have a very complicated class definition and now I want to pass a class instance to the device and execute some of its member functions on the device, I then should rewrite my whole .cpp file. 如果我有一个非常复杂的类定义,现在我想将一个类实例传递给设备并在设备上执行其某些成员函数,则应重写整个.cpp文件。 Do I only need to turn those functions run on device to __host__ __device__ or should I rewrite all the functions? 我是否只需要将设备上运行的那些功能转换为__host__ __device__还是应该重写所有功能?

That depends entirely on your code. 那完全取决于您的代码。 CUDA has support for a limited subset of C++ language features (fully documented here ) and almost no C++ standard library is supported. CUDA支持有限的C ++语言功能子集( 在此完整介绍),并且几乎不支持C ++标准库。 So there is no general answer, but it is likely that you will have to rewrite at least some of your class member function code if you wish to call them on the GPU. 因此,没有通用的答案,但是如果您希望在GPU上调用它们,则可能至少需要重写一些类成员函数代码。

I think nvcc treat functions with no function type qualifiers as __host__ . 我认为nvcc将没有函数类型限定符的函数视为__host__ How does it compile host code? 它如何编译主机代码? Does it compile them exactly as g++ does? 是否完全像g ++一样编译它们?

The first thing to understand is that nvcc isn't a compiler, it is a compiler driver . 首先要了解的是nvcc不是编译器,而是编译器驱动程序 Plain C++ code in a file without a .cu file extension is, by default, passed straight to the host compiler with a set of predefined compiler options without modification. 默认情况下,不带.cu文件扩展名的文件中的纯C ++代码直接传递给带有一组预定义编译器选项的主机编译器,而无需进行修改。

Host code inside a .cu extension file is parsed by the CUDA C++ front end to look for CUDA syntax and then passed to the host compiler. .cu扩展文件中的主机代码由CUDA C ++前端解析,以查找CUDA语法,然后传递给主机编译器。 This process can fail on extremely complex template definitions and bleeding edge language features. 对于极其复杂的模板定义和最先进的语言功能,此过程可能会失败。 nvcc also includes CUDA headers automatically and these headers can potentially conflict with the contents of your own code. nvcc还自动包含CUDA标头,这些标头可能与您自己的代码内容冲突。 But eventually your host code reaches the host C++ compiler, albeit by a different route. 但是最终您的主机代码到达了主机C ++编译器,尽管途径不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM