Search code examples
pythoncudapycuda

Implementing a new class in PyCuda


I'm a begginer in using pycuda, so far, I've learned some basic stuff how to write the kernels from book "Cuda by example", and my next task is to use a class, that is already written in C++, inside of kernels. How can I import this .h file to pycuda? Do I have to use boost python for transfering this .h so it can be imported like any other module in python? The thing is that I only need this special variables inside of a kernel. This class is used for computing the derivatives and I will write you a couple of lines so you can see how it's constructed:

adoublecuda.h:

namespace adtl {
class adouble {
public:
    // ctors
    __device__ __host__ inline adouble();
    __device__ __host__ inline adouble(const double v);
    __device__ __host__ inline adouble(const adouble& a);

    __device__ __host__ inline adouble operator - () const;
    __device__ __host__ inline adouble operator + () const;

...etc.

I use this class inside of C-CUDA just typing the #include "adoublecuda.h" and now I want to include the same thing in PyCUDA. I'm only using this class inside of kernels (I don't need adouble variables inside of main). So will I have to use boost python in order to include this header file to PyCuda?

Any advice would be appreciated, thank you for helping me!


Solution

  • If it is only used inside device code, and you are not intending to do any metaprogramming, then you actually don't need to do anything with Python or PyCUDA to make it work. Just compile your device code to a CUBIN object (so use nvcc -cubin) and then load that CUBIN file using driver.module_from_file().

    You can see a complete example of this in this older answer of mine.