Question

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.

The code is the following:

#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <cstdlib>

#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->events_left = events_left;
    this->num_layers = num_layers;
    this->dz = dz;
    this->dr = dr;
    this->ndz = ndz;
    this->ndr = ndr;
}
__device__ int Simulation::getNumLayers() { return this->num_layers;}
__device__ float Simulation::getDZ() { return this->dz;}
__device__ float Simulation::getDR() { return this->dr;}
__device__ int Simulation::getNDZ() { return this->ndz;}
__device__ int Simulation::getNDR() { return this->ndr;}
__device__ int Simulation::getEventsLeft() { return this->events_left;}

__device__ void Simulation::setNumLayers(int num_layers) {this->num_layers = num_layers;}
__device__ void Simulation::setDZ(float dz) {this->dz = dz;}
__device__ void Simulation::setDR(float dr) {this->dr = dr;}
__device__ void Simulation::setNDZ(int ndz) {this->ndz = ndz;}
__device__ void Simulation::setNDR(int ndr) {this->ndr = ndr;}
__device__ void Simulation::setEventsLeft(int events_left) {this->events_left = events_left;}

__global__ void Foo(Simulation* sim){
    int i = threadIdx.x + blockIdx.x * blockDim.x;

    printf("TH <%d>, num_layers <%d>\n", i, sim[0].num_layers);
    printf("TH <%d>, dz         <%f>\n", i, sim[0].dz);
    printf("TH <%d>, dr         <%f>\n", i, sim[0].dr);
    printf("TH <%d>, ndr        <%d>\n", i, sim[0].ndr);
    printf("TH <%d>, ndz        <%d>\n", i, sim[0].ndz);
}

int main(void) {

    // Number of simulations
    int num_simulations = 1;

    // Simulations host vector
    thrust::host_vector<Simulation> 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<Simulation> dv_simulations = hv_simulations;

    // Get raw pointer to device simulations
    Simulation* d_simulations = thrust::raw_pointer_cast(dv_simulations.data());

    // Call Foo kernel
    Foo<<<1, 2>>>(d_simulations);

    return 0;
}

I expect to obtain the values passed as parameters, instead the console throws this:

TH <0>, num_layers <1428486120>
TH <1>, num_layers <1428486120>
TH <0>, dz         <0.000000>
TH <1>, dz         <0.000000>
TH <0>, dr         <0.000000>
TH <1>, dr         <0.000000>
TH <0>, ndr        <0>
TH <1>, ndr        <0>
TH <0>, ndz        <0>
TH <1>, ndz        <0>

Why? Thanks.

Was it helpful?

Solution

I think you have a basic vector manipulation error, having nothing to do with CUDA or Thrust.

This creates a vector of length num_simulations:

thrust::host_vector<Simulation> hv_simulations(num_simulations);

This then appends another element to the end of the existing vector:

hv_simulations.push_back(sim1);

You can fix this by creating an empty vector:

thrust::host_vector<Simulation> hv_simulations;

Or by copying explicitly to the first element of the vector:

hv_simulations[0] = sim1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top