【发布时间】:2011-09-09 11:00:04
【问题描述】:
如何在 CUDA 中创建全局变量? 可以举个例子吗?
例如,如何在 CUDA 函数中创建数组
__global__ void test()
{
int *a = new int[10];
}
或者我怎样才能创建一个全局数组并通过这个函数访问它。例如
__device__ int *a;
__global__ void test()
{
a[0] = 2;
}
或者我怎样才能像下面这样使用..
__global__ void ProcessData(int img)
{
int *neighborhood = new int[8];
getNeighbourhood(img, neighbourhood);
}
我仍然有一些问题。我发现与
相比__device__
如果我定义
"__device__ __constant__" (read only)
将改善内存访问。 但我的问题是我在主机内存中有一个数组说
float *arr = new float[sizeOfTheArray];
我想将它作为设备中的变量数组,我需要在设备内存中修改它,我需要将它复制回主机。我该怎么做??
【问题讨论】:
-
我需要一个全局数组和设备上的变量。我怎样才能做到这一点??例如我要定义 PI=3.14... 并且我要在调用设备函数时使用..
-
例如,图像的宽度和高度,我正在使用主机函数读取图像并将其传递给设备函数,我想将 W 和 H 保留在设备内存中,以便我可以访问它来自其他函数而不将其作为参数传递。
-
我想这样用。 global void ProcessData(int img){int 邻域 = new int[8]; getNeighbourhood(img, neighbourhood);}
标签: cuda