0

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?

3
  • WHat exactly do you want? Edit your code so some calculations were processed with cuda? Mar 18, 2011 at 6:34
  • 9
    Please, in the future consider capitalization, spaces, grammar, using full words instead of text message style abbreviations and editing the preface of your question once you have supplied additional information. If you can't help yourself, we can't help you. When you post on SO, you are talking to thousands of your peers at once, please consider that next time. Most of us prefer helping people who put some effort into their questions.
    – user50049
    Mar 18, 2011 at 12:58
  • +1 Ask specific questions, provide as much detail as you can and don't create duplicates.
    – Ade Miller
    Mar 19, 2011 at 1:39

3 Answers 3

7

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.

1
4

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 globals and shareds 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:

  • Central idea for CUDA: Loops can be transformed into different threads executed multiple times in parallel on the GPU.
  • Therefore, the single iterations optimally are independent of other iterations.
  • For optimal execution, the single execution branches of the threads should be (almost) the same, i.e. the single threads sould do almost the same.
2
  • 3
    I can assure you that it’s easier to use the C++ CUDA library Thrust from the start instead of relying on C features only. Mar 18, 2011 at 13:34
  • +1 for the suggestion to use Thrust. If your problem maps onto the containers and algorithms it provides this is a much quicker way to get started. I'd abstracts you from the underlying CUDA impementation somewhat which is both good (you're more productive) and bad (if you want to learn/understand it.
    – Ade Miller
    Mar 18, 2011 at 19:03
2

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.

1
  • Thanks. I hv rules file for VS2008.
    – Sneha
    Mar 18, 2011 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.