如何将我现有的 .cpp 代码与 cuda 一起使用
我用 c++ 编写了代码,想将它与 cuda 一起使用。有人可以帮助我吗?我应该提供我的代码吗?实际上我尝试这样做,但我需要一些起始代码来继续我的代码。我知道如何为 Windows(Visual Studio)做简单的方形程序(使用 cuda 和 c++)。为我的程序做这些事情是否足够?
I hv code in c++ and wanted to use it along with cuda.Can anyone please help me? Should I provide my code?? Actually I tried doing so but I need some starting code to proceed for my code.I know how to do simple square program (using cuda and c++)for windows(visual studio) .Is it sufficient to do the things for my program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下都是很好的起点。 CUDA by Example 是一个很好的教程,可以让您快速入门并运行。 《大规模并行处理器编程》包含更多背景知识,例如有关 GPU 架构历史的章节,并且通常更深入。
CUDA 示例:通用 GPU 编程简介
编程大规模并行处理器:实践方法
这些都讨论了 CUDA 3.x,因此您在某个时候会想看看 CUDA 4.x 中的新功能。
如果您的问题很好地映射到它,Thrust 绝对值得一看(请参阅上面的评论)。它是一个类似 STL 的容器、迭代器和算法库,在 CUDA 之上实现数据并行算法。
以下是有关 CUDA 和 Visual C++ 2010 入门的两个教程:
http://www.ademiller.com/blogs/tech/2011/03/using-cuda-and-thrust-with-visual-studio-2010/
< a href="http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/" rel="nofollow noreferrer">http://blog. cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/
NVIDIA 论坛上还有一个帖子:
http://forums.nvidia.com/index.php?showtopic=184539
非常笼统地询问我如何开始使用 . .. 在 Stack Overflow 上通常不是最好的方法。通常,您得到的最佳答复是“去读一本书或手册”。最好在这里提出具体问题。请不要创建重复的问题,这没有帮助。
The following are both good places to start. CUDA by Example is a good tutorial that gets you up and running pretty fast. Programming Massively Parallel Processors includes more background, e.g. chapters on the history of GPU architecture, and generally more depth.
CUDA by Example: An Introduction to General-Purpose GPU Programming
Programming Massively Parallel Processors: A Hands-on Approach
These both talk about CUDA 3.x so you'll want to look at the new features in CUDA 4.x at some point.
Thrust is definitely worth a look if your problem maps onto it well (see comment above). It's an STL-like library of containers, iterators and algorithms that implements data-parallel algorithms on top of CUDA.
Here are two tutorials on getting started with CUDA and Visual C++ 2010:
http://www.ademiller.com/blogs/tech/2011/03/using-cuda-and-thrust-with-visual-studio-2010/
http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/
There's also a post on the NVIDIA forum:
http://forums.nvidia.com/index.php?showtopic=184539
Asking very general how do I get started on ... on Stack Overflow generally isn't the best approach. Typically the best reply you'll get is "go read a book or the manual". It's much better to ask specific questions here. Please don't create duplicate questions, it isn't helpful.
将程序从直接 C(++) 转换为 CUDA 是一项艰巨的任务。据我所知,可以在 CUDA 中使用类似 C++ 的东西(尤其是已发布的 CUDA 4.0),但我认为仅从 C 东西开始(即结构、指针、基本数据类型)更容易。
首先阅读 CUDA 编程指南,然后检查 CUDA SDK 附带的示例或此处提供。我个人发现矢量加法示例非常有启发性。可以在此处找到它。
我无法告诉您如何为您的特定程序编写
global
和shared
,但是在阅读了介绍性材料之后,您至少会对如何编写有一个模糊的概念去做。问题是(据我所知)不可能说出将纯 C(++) 转换为适合 CUDA 的代码的通用方法。但这里有一些基础知识供您参考:
It's a non-trivial task to convert a program from straight C(++) to CUDA. As far as I know, it is possible to use C++ like stuff within CUDA (esp. with the announced CUDA 4.0), but I think it's easier to start with only C stuff (i.e. structs, pointers, elementary data types).
Start by reading the CUDA programming guide and by examining the examples coming with the CUDA SDK or available here. I personally found the vector addition sample quite enlightening. It can be found over here.
I can not tell you how to write your
global
s andshared
s for your specific program, but after reading the introductory material, you will have at least a vague idea of how to do.The problem is that it is (as far as I know) not possible to tell a generic way of transforming pure C(++) into code suitable for CUDA. But here are some corner stones for you:
您的项目中可以有多个 .cpp 和 .cu 文件。除非您希望 .cu 文件仅包含设备代码,否则这应该相当容易。
对于 .cu 文件,您指定一个头文件,其中包含主机函数。然后,将该头文件包含在其他 .cu 或 .cpp 文件中。链接器将完成其余的工作。这与项目中拥有多个纯 C++ .cpp 文件没有什么不同。
我假设您已经有 Visual Studio 的 CUDA 规则文件。
You can have multiple .cpp and .cu files in your project. Unless you want your .cu files to contain only device code, it should be fairly easy.
For your .cu files you specify a header file, containing host functions in it. Then, include that header file in other .cu or .cpp files. The linker will do the rest. It is nothing different than having multiple plain C++ .cpp files in your project.
I assume you already have CUDA rule files for your Visual Studio.