【发布时间】:2018-11-25 13:57:54
【问题描述】:
CUDA 在此处找到了一些文档:https://docs.nvidia.com/cuda/thrust/index.html#vectors,它允许在设备内存/代码中使用向量。我正在尝试创建一个结构类型的向量以用于一般处理。下面是示例代码:
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>
struct Data
{
double first, second, total;
};
__global__
void add(thrust::device_vector<Data> *d_matrix)
{
&d_matrix[1].total = &d_matrix[1].first + &d_matrix[1].second;
}
int main()
{
thrust::host_vector<Data> matrix;
thrust::device_vector<Data> *d_matrix;
int size = sizeof(thrust::host_vector<Data>);
matrix[1].first = 2100;
matrix[1].second = 100;
cudaMalloc(&d_matrix, size);
cudaMemcpy(d_matrix, &matrix, size, cudaMemcpyHostToDevice);
add<<<1,1>>>(d_matrix);
cudaMemcpy(&matrix, d_matrix, size, cudaMemcpyDeviceToHost);
cudaFree(d_matrix);
std::cout << "The sum is: " << matrix[1].total;
return 0;
}
我收到以下错误:
gpuAnalysis.cu(13):错误:类“thrust::device_vector>”没有成员“total”
gpuAnalysis.cu(13):错误:类“thrust::device_vector>”没有成员“first”
gpuAnalysis.cu(13): error: class "thrust::device_vector>" has no member "second"
在编译过程中检测到 3 个错误 “/tmp/tmpxft_000013c9_00000000-8_gpuAnalysis.cpp1.ii”。
根据 nvidia 网站上提供的文档,这些向量能够将所有数据类型存储为 std::vector。有没有办法修复这个错误以访问每个向量元素的结构成员?
【问题讨论】:
-
这里有很多错误。即使你修复了代码中的分配和运算符优先级问题,它也不起作用,因为你不能在设备代码中使用推力设备向量,
-
我有什么办法可以在设备代码中使用向量吗?我假设来自 Nvidia 的文档是说推力设备向量允许在设备代码中使用向量。
-
文档中没有任何地方说明或暗示这一点。您可以在设备代码中访问支持设备向量的内存,但不能使用类本身。
-
好的。我是 CUDA 的新手,而不是最有经验的程序员。你介意提供一个例子来回答吗?
-
谢谢我的朋友。副本是我一直在寻找的。我搜索了一段时间,但找不到对我的问题的参考。