title: CUDA Thrust How to transfer objects to device using vectors
body: <p>I'm learning Cuda Thrust and I want to transfer objects from a host_vector to a device array in order to use it in a Kernel.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;thrust/host_vector.h&gt;
  #include &lt;thrust/device_vector.h&gt;
  #include &lt;thrust/copy.h&gt;
  #include &lt;cstdlib&gt;
  
  #ifndef SIMULATION_H_
  #define SIMULATION_H_
  
  class Simulation {
      public:
      int num_layers;
      float dz;
      float dr;
      int ndz;
      int ndr;
      int events_left;
  
      __host__ __device__ Simulation();
      __host__ __device__ Simulation(int events_left, int num_layers, float dz, float dr, int ndz, int ndr);
      __device__ int getNumLayers();
      __device__ float getDZ();
      __device__ float getDR();
      __device__ int getNDZ();
      __device__ int getNDR();
      __device__ int getEventsLeft();
  
      __device__ void setNumLayers(int num_layers);
      __device__ void setDZ(float dz);
      __device__ void setDR(float dr);
      __device__ void setNDZ(int ndz);
      __device__ void setNDR(int ndr);
      __device__ void setEventsLeft(int events_left);    
  };
  
  #endif /* SIMULATION_H_ */
  
  __host__ __device__ Simulation::Simulation(){}
  __host__ __device__ Simulation::Simulation(int events_left, int num_layers, float dz, float dr, int ndz, int ndr) {
      this-&gt;events_left = events_left;
      this-&gt;num_layers = num_layers;
      this-&gt;dz = dz;
      this-&gt;dr = dr;
      this-&gt;ndz = ndz;
      this-&gt;ndr = ndr;
  }
  __device__ int Simulation::getNumLayers() { return this-&gt;num_layers;}
  __device__ float Simulation::getDZ() { return this-&gt;dz;}
  __device__ float Simulation::getDR() { return this-&gt;dr;}
  __device__ int Simulation::getNDZ() { return this-&gt;ndz;}
  __device__ int Simulation::getNDR() { return this-&gt;ndr;}
  __device__ int Simulation::getEventsLeft() { return this-&gt;events_left;}
  
  __device__ void Simulation::setNumLayers(int num_layers) {this-&gt;num_layers = num_layers;}
  __device__ void Simulation::setDZ(float dz) {this-&gt;dz = dz;}
  __device__ void Simulation::setDR(float dr) {this-&gt;dr = dr;}
  __device__ void Simulation::setNDZ(int ndz) {this-&gt;ndz = ndz;}
  __device__ void Simulation::setNDR(int ndr) {this-&gt;ndr = ndr;}
  __device__ void Simulation::setEventsLeft(int events_left) {this-&gt;events_left = events_left;}
  
  __global__ void Foo(Simulation* sim){
      int i = threadIdx.x + blockIdx.x * blockDim.x;
  
      printf("TH &lt;%d&gt;, num_layers &lt;%d&gt;\n", i, sim[0].num_layers);
      printf("TH &lt;%d&gt;, dz         &lt;%f&gt;\n", i, sim[0].dz);
      printf("TH &lt;%d&gt;, dr         &lt;%f&gt;\n", i, sim[0].dr);
      printf("TH &lt;%d&gt;, ndr        &lt;%d&gt;\n", i, sim[0].ndr);
      printf("TH &lt;%d&gt;, ndz        &lt;%d&gt;\n", i, sim[0].ndz);
  }
  
  int main(void) {
  
      // Number of simulations
      int num_simulations = 1;
  
      // Simulations host vector
      thrust::host_vector&lt;Simulation&gt; hv_simulations(num_simulations);
  
      // Parameters for simulation one
      float dz = 0.01;
      float dr = 0.01;
      int ndz = 40;
      int ndr = 50;
      int events_left = 1000;
      int num_layers = 3;
  
      // Create a simulation
      Simulation sim1(events_left, num_layers, dz, dr, ndz, ndr);
  
      // Add simulation one to simulations vector
      hv_simulations.push_back(sim1);
  
      // Transfer simulations to device
      thrust::device_vector&lt;Simulation&gt; dv_simulations = hv_simulations;
  
      // Get raw pointer to device simulations
      Simulation* d_simulations = thrust::raw_pointer_cast(dv_simulations.data());
  
      // Call Foo kernel
      Foo&lt;&lt;&lt;1, 2&gt;&gt;&gt;(d_simulations);
  
      return 0;
  }
  </code></pre>
  
  <p>I expect to obtain the values passed as parameters, instead the console throws this:</p>
  
  <pre><code>TH &lt;0&gt;, num_layers &lt;1428486120&gt;
  TH &lt;1&gt;, num_layers &lt;1428486120&gt;
  TH &lt;0&gt;, dz         &lt;0.000000&gt;
  TH &lt;1&gt;, dz         &lt;0.000000&gt;
  TH &lt;0&gt;, dr         &lt;0.000000&gt;
  TH &lt;1&gt;, dr         &lt;0.000000&gt;
  TH &lt;0&gt;, ndr        &lt;0&gt;
  TH &lt;1&gt;, ndr        &lt;0&gt;
  TH &lt;0&gt;, ndz        &lt;0&gt;
  TH &lt;1&gt;, ndz        &lt;0&gt;
  </code></pre>
  
  <p>Why? Thanks.</p>