BAdhi
BAdhi

Reputation: 510

Using Template Classes with CUDA keywords in generic classes

I have created a template Class that wraps the Kernel functions. So that when ever another class needs use the kernel, it can simply call the class function without worrying about the CUDA terms and functionality. Since the class is template class all the definition and the declaration is included in the same file as below.

Scanner.h

#include <iostream>
#include "ScanKernals.h"

class Scanner {
public :
    template<typename T>
    void ScanDevice(T * h_in, int size);

    template<typename T>
    void ScanHost();

};

template<typename T>
void Scanner::ScanHost()
{

}

template<typename T>
void Scanner::ScanDevice(T * h_in, int size)
{
  T * d_in;
  cudaMalloc(&d_in, size * sizeof(T));
  cudaMemcpy(d_in , h_in, size * sizeof(T), cudaMemcpyHostToDevice);
  // runs kernel that is required to calculate the scan 
}

ScanKernals.h

template<typename T>
__global__
void scan(T * d_in, T* d_out, int size)
{
   // keranel code
}

The above class is then used in the main function as below

main.cpp

#include <iostream>
#include "Scanner.h"

int main()
{
    Scanner scanner;

    return 0;
}

But when I compile the code, the compiler gives errors, not recognizing CUDA specific keywords.

If I separate the definition of the Scanner class to a separate .cpp file this error will not be generated but since the template classes cannot be declared and defined in two separate files, this is not an option

Am I missing something here, is there a workaround?

Upvotes: 1

Views: 798

Answers (2)

talonmies
talonmies

Reputation: 72344

If you have a code path which includes CUDA syntax containing code into a C++ compilation, there are two things you must do:

  1. Compile the code using the nvcc compiler driver
  2. Rename the file which is being compiled with a .cu extension. nvcc uses the file extension to determine the compilation trajectory of a given file, and if the file has a .cpp extension, the code will be passed directly to the host compiler and compilation will fail.

You are apparently failing to do one or perhaps both of these things.

Upvotes: 2

Florent DUGUET
Florent DUGUET

Reputation: 2916

Default behavior of nvcc on your files is not what you expected as @talonmies underlined. However, you may want to try the following compiler command-line option :

--x {c|c++|cu}                             (-x)
    Explicitly specify the language for the input files, rather than letting
    the compiler choose a default based on the file name suffix.
    Allowed values for this option:  'c','c++','cu'.

Specifying explicitly that your input code is cuda, even though named with a .cpp suffix. Here is an example main.cpp file:

__global__ void f() {}

Without the flag:

/usr/local/cuda-7.5/bin/nvcc -c main.cpp
main.cpp:1:1: error: ‘__global__’ does not name a type
 __global__ void f() {}
 ^

With the flag (no error):

/usr/local/cuda-7.5/bin/nvcc -c main.cpp -x cu

File naming freedom seems safe.

Upvotes: 0

Related Questions