我在NVIDIA文檔(
http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications,表#12)中讀到,我的GPU(GTX 580,計算能力2.0)每個線程的本地記憶體量為512 Ko.
我嘗試用CUDA 6.5檢查Linux上的這個限制是不成功的.
這是我使用的代碼(它的唯一目的是測試本地記憶體限制,它不會進行任何有用的計算):
#include
#include
#define MEMSIZE 65000 // 65000 -> out of memory, 60000 -> ok
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=false)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if( abort )
exit(code);
}
}
inline void gpuCheckKernelExecutionError( const char *file, int line)
{
gpuAssert( cudaPeekAtLastError(), file, line);
gpuAssert( cudaDeviceSynchronize(), file, line);
}
__global__ void kernel_test_private(char *output)
{
int c = blockIdx.x*blockDim.x + threadIdx.x; // absolute col
int r = blockIdx.y*blockDim.y + threadIdx.y; // absolute row
char tmp[MEMSIZE];
for( int i = 0; i < MEMSIZE; i++)
tmp[i] = 4*r + c; // dummy computation in local mem
for( int i = 0; i < MEMSIZE; i++)
output[i] = tmp[i];
}
int main( void)
{
printf( "MEMSIZE=%d bytes.\n", MEMSIZE);
// allocate memory
char output[MEMSIZE];
char *gpuOutput;
cudaMalloc( (void**) &gpuOutput, MEMSIZE);
// run kernel
dim3 dimBlock( 1, 1);
dim3 dimGrid( 1, 1);
kernel_test_private<<>>(gpuOutput);
gpuCheckKernelExecutionError( __FILE__, __LINE__);
// transfer data from GPU memory to CPU memory
cudaMemcpy( output, gpuOutput, MEMSIZE, cudaMemcpyDeviceToHost);
// release resources
cudaFree(gpuOutput);
cudaDeviceReset();
return 0;
}
和編譯指令行:
nvcc -o cuda_test_private_memory -Xptxas -v -O2 --compiler-options -Wall cuda_test_private_memory.cu
彙編沒問題,并報告:
ptxas info : 0 bytes gmem
ptxas info : Compiling entry function '_Z19kernel_test_privatePc' for 'sm_20'
ptxas info : Function properties for _Z19kernel_test_privatePc
65000 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 21 registers, 40 bytes cmem[0]
當我在每個線程達到65000位元組時,我在GTX 580上運作時出現“記憶體不足”錯誤.以下是控制台中程式的确切輸出:
MEMSIZE=65000 bytes.
GPUassert: out of memory cuda_test_private_memory.cu 48
我還使用GTX 770 GPU進行了測試(在Linux上使用CUDA 6.5).對于MEMSIZE = 200000,它沒有錯誤地運作,但是在MEMSIZE = 250000時,在運作時發生了“記憶體不足錯誤”.
如何解釋這種行為?難道我做錯了什麼 ?