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

标签 c++ cuda nvidia thrust

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

相关文章:

c++ - Windows 窗体 - 按钮点击

cuda - nvcc 致命 : '--ptxas-options=-v' : expected a number

不同场景下的CUDA原子操作性能

c++ - 使用 NvPipe 编码桌面复制 API ID3D11Texture2D 框架

c++ - 调用哪个函数? (委托(delegate)给姐妹类)

c++ - 什么是 C++ 中的 instanceof 的等价物?

android - 无法在 Android 8 Oreo 上执行 "system"命令

python - 用 cython 包装 c++ 和 CUDA 代码

CUDA原子导致分支发散

java - 是否可以在窗口模式下在 Nvidia 3D Vision 硬件上运行 Java3D 应用程序?