【发布时间】:2018-06-04 00:29:29
【问题描述】:
我在 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 <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]
当我达到每个线程 65000 字节时,我在 GTX 580 上运行时遇到“内存不足”错误。这是控制台中程序的确切输出:
MEMSIZE=65000 bytes.
GPUassert: out of memory cuda_test_private_memory.cu 48
我还使用 GTX 770 GPU(在具有 CUDA 6.5 的 Linux 上)进行了测试。 MEMSIZE=200000 运行时没有错误,但 MEMSIZE=250000 在运行时出现“内存不足错误”。
如何解释这种行为?难道我做错了什么 ?
【问题讨论】:
-
您使用的是哪个 CUDA 版本?这是linux还是windows?您是否在编译代码或运行代码时遇到“内存不足错误”? (将确切的错误文本粘贴到问题中)您用于编译代码的命令行是什么?我的猜测是您正在为 pre-cc2.0 架构进行编译。如果我为 cc1.1 架构编译此代码,我会在编译时收到“内存不足错误”,因为 cc1.x 设备对每个线程的本地内存 (16KB) 的限制更小。如果我为 cc2.0 架构编译,你的代码对我来说可以正常编译和运行。
-
您的问题也可能来自这行代码:
char output[MEMSIZE];这(主机代码)创建了一个基于堆栈的分配,这些类型的分配可能会因平台而受到限制。将确切的错误文本粘贴到问题中会有所帮助。 (您可以编辑自己的问题。) -
@RobertCrovella 感谢您对我的问题感兴趣。我已经编辑了我的问题以添加缺失的信息。 cudaGetErrorString() 在运行时报告的确切错误文本是“内存不足”。
-
这是一个非常棒的问答,几乎可以肯定是 cuda 程序员(包括我自己)的常见问题。我希望它能得到更多的关注!
标签: memory cuda limit gpu-local-memory