我可以在.h文件中定义CUDA内核吗? [英] Can i just define CUDA kernels in .h files?

查看:199
本文介绍了我可以在.h文件中定义CUDA内核吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解我应该如何处理CUDA程序中的不同文件:

I have difficulties understanding the way i should handle different files in a CUDA program:

我试图重构一个CUDA程序,我已经工作了一会儿。到目前为止它或多或少是一个单文件程序。我有1 .cu文件,其中包含所有的CUDA代码以及main函数。我有几个头文件,包括,但它们只包含非CUDA函数。该程序越来越大,更麻烦,我想把内核结构为不同的文件,以便于阅读。

I am trying to restructure a CUDA program i have been working on for a while. So far it was more or less a one-file-program. I had 1 .cu file which contained all the CUDA code as well as the main function. I had several header files that were included, but they contained only non-CUDA functions. The program is getting bigger and messier and i want to structure the kernels into different files for readability.

最初我想这样做的方法是有.cuh文件。我没有得到工作,所以我试图让我的头,它建议一个.h文件和一个.cu文件。然而,程序将不再构建,包括其他.cu文件后。它通常不会识别CUDA关键字,如__global__,否则会在外部包含中抛出错误,这似乎是不相关的。

Initially i thought the way to do this is to have .cuh files. I didn't get that to work, so i tried to get my head around this, which suggests a .h file and a .cu file. However the program would not build anymore after including other .cu files in it. It would typically either not recognize CUDA keywords such als "__global__" or it would throw errors in external includes, which seemed unrelated.

然而,我注意到,当我定义一个.h文件中的内核。我有感觉这不是一个好主意,但不知道它的问题是什么。
让我感到烦恼的是,从我的理解,.h文件不应该由nvcc编译,那么它仍然工作?

I noticed however, that it builds when i define the kernel in a .h file. I have the feeling this is not a good idea, but don't know what the problem with it is. What bothers me, is that from my understanding the .h files should not even be compiled by nvcc, so how does it still work? I have great trouble understanding what the best way to go about this is.

我使用的是Visual Studio 2012和CUDA 5.5

I am using Visual Studio 2012 and CUDA 5.5

推荐答案

这里的规则和行为在概念上与C或C ++编码中允许的不同。

The rules and behavior here aren't really any different conceptually than what is permissible in C or C++ coding.

对于通过 #include 指令明确包含在另一个文件中的文件,文件名,文件扩展名 - .cu .h .cuh .hpp 或者你有什么,真的没有关系。这只是一个指令,编译器拿起该文件,并插入它在源的这一点,就像它是在那里输入。

For a file that is explicitly included in another file via an #include directive, the file name, and indeed the file extension - .cu, .h, .cuh. .hpp or what have you, really doesn't matter. That is just a directive to the compiler to pick up that file, and insert it at this point in the source, just as if it had been typed there.

所以a语句像我不能得到 .cuh 工作,但我可以得到 .h 工作真的不使感。编译器不关心文件名。 .cuh .h 只是命名约定,以帮助我们组织大型代码库。

So a statement like I couldn't get .cuh to work but I could get .h to work really doesn't make sense. The compiler doesn't care about the filename. Things like .cuh and .h are just naming conventions to help us organize large code bases.

文件不会被编译,除非它们在源模块中或包含在源模块中(例如 .cu .c .cpp 等)编译器不单独编译头文件(预编译头是另一个主题,与本讨论无关)。

Files don't get compiled unless they are in or included in a source module (e.g. .cu or .c or .cpp, etc.) The compiler doesn't separately compile header files (precompiled headers is another subject, not relevant to this discussion). It only compiles them if they are included in a source module.

在头文件中定义一个函数的危险是,如果你将头文件包含在多个头文件中源模块,该函数将被定义(即编译)多个源模块。通常你不想要这个,因为它往往导致多个定义错误。

The danger of defining a function in a header file is that if you include the header file in more than one source module, the funcion will be defined (i.e. compiled in) more than one source module. Usually you don't want this, as it tends to lead to multiple definition errors.

如果您打算将头文件包含在一个且只有一个源模块中,那么将一些代码(即定义)放置在该头中并没有什么问题文件。但是头文件的典型用法是声明,而不是定义

If you intend a header file to be included in one and only one source module, there is no real problem with placing some of your code (i.e. definitions) in that header file. But the typical usage for header files is declarations, not definitions.

A __ global__ 这个讨论的函数真的没有任何其他C / C ++函数没有区别。如果将内核定义包含在多个源模块中,则在头文件中放置内核定义会面临多重定义错误的风险。如果你只将它包含在一个源代码模块中,那么这是你想做的。

A __global__ function for this discussion is really no different than any other C/C++ function. Putting a kernel definition in a header file runs the risk of multiple definition errors, if you include it in more than one source module. If you only include it in one source module, it's fine if that is what you want to do.

这篇关于我可以在.h文件中定义CUDA内核吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆