몇 가지 방법을 사용할 수 있습니다. 그것은 당신이 어떻게 그 데이터를 사용할 것인지에 달려 있습니다.
- 당신의 패턴 액세스 일정하고 블록 내에서 스레드가 같은 위치를 읽기 읽기 요청을 방송 __constant__ 메모리를 사용합니다. 당신의 패턴 액세스가 주어진 위치의 이웃 관계가, 또는 임의 액세스 (병합하지 않음), 나는 당신이 읽기를해야하는 경우/사용 텍스처 메모리
- 추천 데이터를 작성하고 알 수있는 경우
- 배열의 크기는 커널에서 __device__ blah [크기]로 정의합니다. 예에서
: 당신은 커널에 세 개의 배열을 사용하지 않고 커널에 어떤 매개 변수를 전달할 수
__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
대신 매개 변수를 구조체에 넣는 것이 어떻습니까? 전역 변수를 매개 변수로 사용하는 것은 피해야합니다. –