title: Can you include a .cu extension header in a c++ header?
body: <p>I have a .cu file that when compiled on its own, right click and select compile, it compiles just fine, but when I have another header file, a c++ header file, that calls this .cu file the build fails. The .cu file properties have been edited to build with the CUDA compiler. The errors that I am getting are 'blockIdx': undeclared identifier 'blockDim': undeclared identifier, etc.. basically errors that I would expect compiling cuda code with a c++ compiler. So is it possible to include a .cu cuda code in a c++ header? </p>
  
  <p>Here is the .cu file:</p>
  
  <p>Matrix.cu</p>
  
  <pre><code>#include &lt;cuda.h&gt;
  #include &lt;cuda_runtime.h&gt;
  #include &lt;cuda_device_runtime_api.h&gt;
  
  #define BLOCKSIZE 32
  
  using namespace std;
  
  
  
  template&lt;typename T&gt; class Matrix
  {
  public:
  typedef T value_type;
  ~Matrix();
  Matrix();
  Matrix(int rows, int columns);
  int height;
  int width;
  int stride;
  size_t size;
  
  void CreateIdentity(Matrix&lt;T&gt;&amp;I);
  private:
  vector&lt;T&gt; elements;
  T* firstElement;
  };
  
  
  template&lt;typename T&gt;
  Matrix&lt;T&gt;::~Matrix()
  {
  }
  
  template&lt;typename T&gt;
  Matrix&lt;T&gt;::Matrix()
  {
  }
  
  template&lt;typename T&gt;
  Matrix&lt;T&gt;::Matrix(int rows, int columns)
  {
  height = rows;
  width = columns;
  stride = columns; //in row major order this is equal to the # of columns
  elements.resize(rows*columns);
  firstElement = elements.data();
  size = height*width*sizeof(T);
  }
  
  
  
  __global__ void IdentityMatrixKernel(float* identity, int size)
  {
  int index_x = blockIdx.x * blockDim.x + threadIdx.x;
  int index_y = blockIdx.y * blockDim.y + threadIdx.y;
  
  // map the two 2D indices to a single linear, 1D index
  int grid_width = gridDim.x * blockDim.x;
  int index = index_y * grid_width + index_x;
  
  // map the two 2D block indices to a single linear, 1D block index
  //int result = blockIdx.y * gridDim.x + blockIdx.x;
  
  // write out the result
  
  if (index % (size+1))
  {
      identity[index] = 0;
  }
  
  else
  {
      identity[index] = 1;
  }
  }
  
  
  template&lt;typename T&gt;
  void Matrix&lt;T&gt;::CreateIdentity(Matrix&lt;T&gt;&amp;I)
  {
  float* d_I;
  int size1 = I.height;
  int size2 = I.height*I.width*sizeof(float);
  
  cudaMalloc(&amp;d_I,size2);
  
  dim3 block_size;
  block_size.x = BLOCKSIZE;
  block_size.y = BLOCKSIZE;
  
  dim3 grid_size;
  grid_size.x = size1/ block_size.x + 1;
  grid_size.y = size1/ block_size.y + 1;
  
  IdentityMatrixKernel&lt;&lt;&lt;block_size,grid_size&gt;&gt;&gt;(d_I,size1);
  
  cudaMemcpy(I.GetPointer(),d_I,size2,cudaMemcpyDeviceToHost);
  
  cudaFree(d_I);
  }
  </code></pre>
  
  <p>And here is the header file that #include "Matrix.cu"</p>
  
  <p>Element.h</p>
  
  <pre><code>#pragma once
  #include "Matrix.cu"
  #include &lt;vector&gt;
  
  using namespace std;
  
  class Element
  {
  public:
  Element(void);
  ~Element(void);
  Element(int iD, float k, vector&lt;int&gt; nodes);
  Element(int iD, vector&lt;int&gt; nodes, int pId);
  
  void SetElementType(DOF type);
  DOF GetElementType();
  int GetNodeId(int index);
  int GetNodesPerElement();
  int GetPartId();
  
  void CalculateShapeFunctions(Matrix&lt;int&gt; spaceCoordinates);
  void CalculateSShapeDerivative(Matrix&lt;int&gt; spaceCoordinates);
  void CalculateTShapeDerivative(Matrix&lt;int&gt; spaceCoordinates);
  Matrix&lt;float&gt; GetShapeFunctions();
  float GetSShapeDerivative(int row, int column);
  float GetTShapeDerivative(int row, int column);
  void SetStrainDisplacement(Matrix&lt;float&gt; B);
  Matrix&lt;float&gt; GetStrainDisplacement();
  
  private:
  
  int elementId;
  float stiffness;
  vector&lt;int&gt; nodeIds;
  DOF elementType;
  int partId;
  Matrix&lt;float&gt; shapeFunctions;
  Matrix&lt;float&gt; sShapeDerivative;
  Matrix&lt;float&gt; tShapeDerivative;
  Matrix&lt;float&gt; strainDisplacement;
  };
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>So I have been directed to try and separate the template class member functions implementing cuda into a .cu file while keeping the template class definition and any template member functions not using cuda in the original header file. This does seem on the right path, c++ compiler compiles the .h file while the cuda compiler does the .cu, but I am having trouble getting rid of link errors. I understand that I need to explicitly instantiate my template class for the types I need in the .cu file to avoid link errors, but I seem to still get them. </p>
  
  <p>I instantiated my template class at the end of the .cu file as follows:</p>
  
  <pre><code>template class Matrix&lt;float&gt;;
  template class Matrix&lt;int&gt;;
  template class Matrix&lt;string&gt;;
  </code></pre>
  
  <p>I am now getting link errors to the template member functions using cuda.</p>