您可以采取一些方法。这取决于你将如何使用这些数据。
- 如果你的模式访问不断和块内的线程读取相同的位置,使用__constant__内存广播读取请求。
- 如果你的模式访问与给定位置的邻居,或者随机存取(未合并),那么我会如果你需要读推荐使用纹理内存
- /写数据,知道你的数组的大小在内核中定义为__device__ blah [size]。
在例如:
__constant__ int c_blah[65536]; // constant memory
__device__ int g_blah[1048576]; // global memory
__global__ myKernel() {
// ... i want to use blah inside ...
int idx = threadIdx.x + blockIdx.x * blockDim.x;
// get data from constant memory
int c = c_blah[idx];
// get data from global memory
int g = g_blah[idx];
// get data from texture memory
int t = tex1Dfetch(ref, idx);
// operate
g_blah[idx] = c + g + t;
}
int main() {
// declare array in host
int c_h_blah[65536]; // and initialize it as you want
// copy from host to constant memory
cudaMemcpyToSymbol(c_blah, c_h_blah, 65536*sizeof(int), 0, cudaMemcpyHostToDevice);
// declare other array in host
int g_h_blah[1048576]; // and initialize it as you want
// declare one more array in host
int t_h_blah[1048576]; // and initialize it as you want
// declare a texture reference
texture<int, 1, cudaReadModeElementType> tref;
// bind the texture to the array
cudaBindTexture(0,tref,t_h_blah, 1048576*sizeof(int));
// call your kernel
mykernel<<<dimGrid, dimBlock>>>();
// copy result from GPU to CPU memory
cudaMemcpy(g_h_blah, g_blah, 1048576*sizeof(int), cudaMemcpyDeviceToHost);
}
您可以使用三种阵列内核不带任何参数传递给内核。请注意,这只是一个使用示例,并不是对存储器层次结构的优化使用,即:不建议以这种方式使用常量内存。
希望得到这个帮助。
来源
2011-10-13 07:35:24
pQB
为什么不把你的参数包装到结构中?应避免通过全局变量调用参数。 –