question about calling CUDA kernels using a class

I have a C++ solution with several projects in C++ and I want to create a class to call my CUDA kernels. I have added my project CUDA to the solution. Can you help me with the next steps?

Thanks a lot,

Diego

Double posting in multiple forums won’t get your question answered any faster.

Have a look at the cppIntegration sample in the SDK. There are also several threads on the forums that discuss different issues. The gist is that you need to declare your cuda functions extern “C” and then link the object code to your c++ library.

If you have a particular question or problem, I’d be glad to help.

Hi Mr. Anderson,

Sorry about the double posting but I posted that first in the Programming forum and I was wondering if people could answer the question in relation to Linux. So that was my mistake. After I found a thread but it is not complety clear for me. I have seen the cppIntegration but it is just a really simple example and actually is not based on the use of classes.

Going into more detail about my question. I have a solution in C++. This solution contains 3 different projects. I want to call several CUDA kernels from several methods in my C++ programs. So, I’m really confuse about how the CUDA code will be compiled. How will the CUDA code compiled?

Thus, based on your answer I should compile my CUDA code using nvcc in the command prompt and then link the object code to my c++ code.

Is it correct?

Thanks a lot for your help,

Diego

If you have 3 different solutions, you can always compile your CUDA code into a shared library and link that to all of your current projects. Or even a dynamic one, but that brings added complications.

You are limited to a C-style interface for this library, but any function declared extern “C” should be callable from within any of your classes. I keep a fairly object oriented approach even in my CUDA code.

In particular, I define a struct that stores the host and device pointers to data, along with information like width and height (say, if it were an image). Then I create separate functions for allocating memory, copying dtoh and htodh, and running kernels on the data. These low level functions are then called from within a C++ class code which orchestrates the whole operation

I will also point out that you don’t have to take the same approach I did. Most of the CUDA routines, like cudaMemcpy, cudaMalloc, etc… can be called straight from the c++ code, so you can have your class manage your data without the wrappers. The only limitations are that you cannot call kernels from your C++ code or bind texture references. So you could just create extern “C” functions compiled via nvcc with thin wrappers for these tasks and call them from C++.

Thanks a lot, this has really helped me.

Diego

Hi!

I have a problem, with my cuda project. I make a class, with cuda functions and own variables. So I made a header file with class definition. This header is called from the main.cpp and I made the functions in a .cu file, witch started including .h file.
I use visual studio2008. The simple cu files included the main functions worked good.
Maybe the “not main” cu files are not builded? Or there is a special way to do this?