我在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線程的本地內存量
我嘗試使用CUDA 6.5在Linux上檢查此限制失敗。
下面是我使用的代碼(其唯一目的是測試本地內存的限制,它沒有做任何有用的計算):
#include <iostream>
#include <stdio.h>
#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<<<dimGrid, dimBlock>>>(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]
我在運行時就得到了一個「內存不足」的錯誤GTX 580,當我達到每線程65000字節。下面是該程序的控制檯的確切輸出:
MEMSIZE=65000 bytes.
GPUassert: out of memory cuda_test_private_memory.cu 48
我也做了測試與GTX 770 GPU(Linux上,CUDA 6.5)。它在沒有錯誤的情況下運行於MEMSIZE = 200000,但是在運行時出現「內存不足錯誤」,出現在MEMSIZE = 250000的位置。
如何解釋這種行爲?難道我做錯了什麼 ?
其CUDA版本之間的最大堆棧大小您使用的?這是Linux還是Windows?當你編譯**代碼或者當你運行**代碼時,你會得到「內存不足錯誤」嗎? (將精確的錯誤文本粘貼到問題中)您用於編譯代碼的命令行是什麼?我的猜測是你正在編譯一個pre-cc2.0架構。如果我爲cc1.1體系結構編譯此代碼,編譯時會出現「內存不足錯誤」,因爲cc1.x設備對每個線程(16KB)的本地內存有更小的限制。如果我爲cc2.0體系結構進行編譯,您的代碼將爲我編譯並正常運行。 – 2015-03-02 14:54:21
您的問題也可能來自以下代碼行:'char output [MEMSIZE];'This(host code)創建基於堆棧的分配,並且這些類型的分配可能會受限於平臺。將確切的錯誤文本粘貼到問題中會有所幫助。 (你可以編輯你自己的問題。) – 2015-03-02 15:00:38
@RobertCrovella謝謝你對我的問題感興趣。我編輯了我的問題以添加缺少的信息。運行時由cudaGetErrorString()報告的確切錯誤文本是「內存不足」。 – devel484 2015-03-03 09:14:03