title: How to perform deep copying of struct with CUDA?
body: <p>Programming with CUDA I am facing a problem trying to copy some data from host to gpu.</p>
  
  <p>I have 3 nested struct like these:</p>
  
  <pre><code>typedef struct {
      char data[128];
      short length;
  } Cell;
  
  typedef struct {
      Cell* elements;
      int height;
      int width;
  } Matrix;
  
  typedef struct {
      Matrix* tables;
      int count;
  } Container;
  </code></pre>
  
  <p>So <code>Container</code> "includes" some <code>Matrix</code> elements, which in turn includes some <code>Cell</code> elements.</p>
  
  <p>Let's suppose I dynamically allocate the host memory in this way:</p>
  
  <pre><code>Container c;
  c.tables = malloc(20 * sizeof(Matrix));
  
  for(int i = 0;i&lt;20;i++){
      Matrix m;
      m.elements = malloc(100 * sizeof(Cell));
      c.tables[i] = m;
  }
  </code></pre>
  
  <p>That is, a Container of 20 Matrix of 100 Cells each.</p>
  
  <ul>
  <li>How could I now copy this data to the device memory using cudaMemCpy()?</li>
  <li>Is there any good way to perform a deep copy of "struct of struct" from host to device?</li>
  </ul>
  
  <p>Thanks for your time.</p>
  
  <p>Andrea</p>