我是新手使用推力库。我有我的CUDA C代码,它使用全局2D数组。我在我的代码中使用内核函数初始化它。
我必须知道是否可以使用thrust::device_vector
或thrust::fill
来初始化和填充2D数组。
例如:
// initialize 1D array with ten numbers in a device_vector
thrust::device_vector<int> D(10);
是否可以给..
thrust::device_vector<int> D[5][10];
如果可能的话我将如何使用thrust::fill
函数。
我的目标是使用推力库优化代码。
答案 0 :(得分:3)
在STL和推力中,vector是数据元素的容器,遵循严格的线性序列,因此它本质上基本上是1-D。在推力方面,这些数据元素可以是普通类型,甚至是结构和对象,但它们不能是其他向量(与STL不同)。
你可以创建一个向量数组,但是对它们的推力操作通常需要在数组中的每个向量上逐个进行。
关于语法,你不能这样做:
thrust::device_vector D[5][10];
您可以这样做:
thrust::device_vector<int> D[5][10];
然而,这将创建一个向量的二维数组,这不是你想要的,我不认为。
在许多情况下,2-D阵列可以“扁平化”以便像处理1维一样处理,并且在不了解您的情况的情况下,这是我建议调查的内容。如果您可以将二维数组视为1-D,可能使用指针索引,那么您可以使用单个thrust :: fill调用填充整个事物。
我还建议熟悉推力quick start guide。
这是一个工作示例,显示主机上的2D阵列具有基本展平:
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#define H 5
#define W 10
__global__ void kernel(int *data, int row, int col) {
printf("Element (%d, %d) = %d\n", row, col, data[(row*W)+col]);
}
int main(void)
{
int h[H][W];
thrust::device_vector<int> d(H*W);
thrust::copy(&(h[0][0]), &(h[H-1][W-1]), d.begin());
thrust::sequence(d.begin(), d.end());
kernel<<<1,1>>>(thrust::raw_pointer_cast(d.data()), 2, 3);
cudaDeviceSynchronize();
return 0;
}