gpt4 book ai didi

c++ - CUDA Thrust 如何使用 vector 将对象传输到设备

转载 作者:行者123 更新时间:2023-11-28 02:51:03 26 4
gpt4 key购买 nike

我正在学习 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>

为什么?谢谢。

最佳答案

我认为您有一个基本的 vector 操作错误,与 CUDA 或 Thrust 无关。

这将创建一个长度为 num_simulations 的 vector :

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

然后将另一个元素附加到现有 vector 的末尾:

hv_simulations.push_back(sim1);

你可以通过创建一个空 vector 来解决这个问题:

thrust::host_vector<Simulation> hv_simulations;

或者通过显式复制到 vector 的第一个元素:

hv_simulations[0] = sim1;

关于c++ - CUDA Thrust 如何使用 vector 将对象传输到设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com