title: cudaMalloc/cudaMemcpy with embedded objects/structures
body: <p>I am working on quite a big parallel application using OpenMPI to distribute data among MPI Processses. Using MPI with some serialization library, such as "cereal" makes it very comfortable to pass huge-multi embedded objects around. To give a hint of what I mean by multi-embedded structure, I am currently working with simplified version, such as :</p>
  
  <pre><code>// structures for CUDA - this is inside std::vector&lt;struct_multi_data&gt; multi_data_vector
  struct struct_multi_data{
      int intended_kernel_block;
      int intended_kernel_thread;
      std::vector&lt;float&gt; data_float;
      std::vector&lt;float&gt; data_int;
      float result;
  };
  
  struct struct_unique_data{
      // this structure is shared among all blocks/threads
      float x;
      float y;
      float z;
  };
  
  
  class Data_object{
  
  // functions
      public:
          Data_object();
          ~Data_object();
  
          int resize(int multi_data_vector_len, int data_float_len, int data_int_len);
          void set_id(int id);
  
          int clean(void);
          int get_multi_data_len();
          int get_multi_data(struct_multi_data * data, int vector_element);
          int set_multi_data(struct_multi_data * data, int vector_element);
  
  
  // variables
      private:
          std::vector&lt;struct_multi_data&gt; multi_data_vector;
          struct_unique_data unique_data;
          int data_id;
  };
  </code></pre>
  
  <p>* the above code is simplified, I have removed serialization functions and some other basic stuff, but the overall structure holds</p>
  
  <p>To put it simply, I am moving around the <strong>Data_object</strong>, containing <strong>vector{struct_multi_data}</strong>, which is a vector of structures, where every structure <strong>struct_multi_data</strong> contains some <strong>vector{float}</strong>.</p>
  
  <p>I have a good reason to embed all the data into 1 Data_object, as it simplifies the MPI sending and receiving.</p>
  
  <hr>
  
  <p><strong>QUESTION</strong></p>
  
  <p>Is there some comfortable way to move the Data_object to GPU memory using cudaMalloc/cudaMemcpy functions ? </p>
  
  <p>There seems to be problem with regular std::vector. I dont want to rely on <em>Thrust</em> library, because I am not sure whether it would work with my MPI serialization solution.</p>
  
  <p><strong>EDIT QUESTION</strong>
  Can I use <em>managed</em> for my Data_object, or cudaMallocManaged() to make the data accessible to GPU ?</p>
  
  <hr>
  
  <p><strong>PLEASE READ</strong></p>
  
  <p>The size of the Data_object is well defined at the beginning of the program execution. None of the vectors changes size anywhere else, but the beginning of the execution. So why am I using vectors ? this way I can set the vectors size by passing parameters, instead of re-compiling the program to change the data size (such as when the data are defined as arrays).</p>
  
  <hr>
  
  <p><strong>RESPONSE TO COMMENTS</strong>
  1) I think can replace all the vectors with pointers to arrays.</p>