我正在学习Cuda Thrust,我想将对象从host_vector传输到设备数组,以便在内核中使用它。
代码如下:
#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;
}
我希望获得作为参数传递的值,而不是控制台抛出这个:
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>
为什么呢?感谢。
答案 0 :(得分:2)
我认为您有一个基本的vector
操作错误,与CUDA或Thrust无关。
这会创建一个长度为num_simulations
的矢量:
thrust::host_vector<Simulation> hv_simulations(num_simulations);
然后将另一个元素附加到现有向量的末尾:
hv_simulations.push_back(sim1);
您可以通过创建空矢量来解决此问题:
thrust::host_vector<Simulation> hv_simulations;
或者通过显式复制到矢量的第一个元素:
hv_simulations[0] = sim1;