title: declare cuda __constant__ memory in a class
body: <p>I am new to CUDA programming. Currently, I am trying to construct an OO-framework that uses CUDA to parallel process the data. I am currently using CUDA 8.0.</p>
  
  <p>There are some key parameters <code>__constant__ int foo[3]</code> that require frequent access by all of the threads, and I think putting them into the constant memory can help improve the memory read process. I would like to have a  declared in the class header and later initializing using <code>cudaMemcpyToSymbol</code>. </p>
  
  <p>From other examples (which is not in the OO-context) I understand that the constant memory symbol can be defined as if it is a global variable and initialized using <code>cudaMemcpyToSymbol</code>. However, if I define the constant memory symbol in the header of the class, the compiler complains by :</p>
  
  <pre><code>error : attribute "__constant__" does not apply here
  </code></pre>
  
  <p>I cannot specify the <code>__constant__</code> anywhere within the class definition either. It looks like it is unable to define constant memory in the context of object-orientated framework? Please help if there are any workaround. Thank you very much!</p>
  
  <hr>
  
  <p>myClass.cuh</p>
  
  <pre><code>class myClass
  {
  private:
      __constant__ int foo[3];
      void initialize();
      void compute(); // required repeated and heavy access of foo from every thread
  }
  </code></pre>
  
  <p>myClass.cu    </p>
  
  <pre><code>void myClass::initialize()
  {
      int bar[3] ={1,2,3};
      //attempt to copy the data in bar to foo
      cudaMemcpyToSymbol(foo, bar, 3*sizeof(int), 0, cudaMemcpyHostToDevice);
  }
  </code></pre>