2012-01-18 42 views
4

我有一个特斯拉C2070应该有5636554752字节的内存。当我知道有足够的内存空间时,为什么cudaMalloc会给我一个错误?

然而,这给了我一个错误:

int *buf_d = NULL; 

err = cudaMalloc((void **)&buf_d, 1000000000*sizeof(int)); 

if(err != cudaSuccess) 
{ 
    printf("CUDA error: %s\n", cudaGetErrorString(err)); 
    return EXIT_ERROR; 
} 

这怎么可能?这是否与最大存储器音调有关?这里是GPU的规格:

Device 0: "Tesla C2070" 
CUDA Driver Version: 3.20 
CUDA Runtime Version: 3.20 
CUDA Capability Major/Minor version number: 2.0 
Total amount of global memory: 5636554752 bytes 
Multiprocessors x Cores/MP = Cores: 14 (MP) x 32 (Cores/MP) = 448 (Cores) 
Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 49152 bytes Total number of registers available per block: 32768 Warp size: 32 
Maximum number of threads per block: 1024 
Maximum sizes of each dimension of a block: 1024 x 1024 x 64 
Maximum sizes of each dimension of a grid: 65535 x 65535 x 1 
Maximum memory pitch: 2147483647 bytes 

至于我运行的机器,它有24个英特尔®至强®处理器的X565,与Linux发行岩石5.4(小牛)。

任何想法?谢谢!

+6

你在哪个平台上? – 2012-01-18 06:33:08

+6

你得到了什么错误代码? – 2012-01-18 09:03:42

+3

使用'cudaGetErrorString'打印错误代码总是有帮助的。这将针对问题 – jwdmsd 2012-01-18 16:54:26

回答

10

的基本问题是你的问题的标题 - 你实际上并不知道你有足够的内存,你是假设你怎么做。运行时API包括cudaMemGetInfo函数,该函数将返回设备上有多少空闲内存。在设备上建立上下文时,驱动程序必须为设备代码预留空间,为每个线程预留空间,支持printf的fifo缓冲区,每个线程的堆栈以及堆内核malloc/new调用(请参阅this answer细节)。所有这些都会占用相当多的内存,在假定可用于代码的ECC预留之后,远远低于最大可扩展内存。该API还包括cudaDeviceGetLimit,您可以使用它查询设备运行时支持消耗的内存量。还有一个伙伴呼叫cudaDeviceSetLimit,它可以让您更改运行时支持的每个组件将保留的内存量。

即使您将运行时内存足迹调整为适合您的口味并且具有驱动程序的实际可用内存值,仍然存在要应对的页面大小粒度和碎片考虑因素。很少有可能分配API将报告为空闲的每个字节。

const size_t Mb = 1<<20; // Assuming a 1Mb page size here 

size_t available, total; 
cudaMemGetInfo(&available, &total); 

int *buf_d = 0; 
size_t nwords = total/sizeof(int); 
size_t words_per_Mb = Mb/sizeof(int); 

while(cudaMalloc((void**)&buf_d, nwords * sizeof(int)) == cudaErrorMemoryAllocation) 
{ 
    nwords -= words_per_Mb; 
    if(nwords < words_per_Mb) 
    { 
     // signal no free memory 
     break; 
    } 
} 

// leaves int buf_d[nwords] on the device or signals no free memory 

(注从未附近的编译器,唯一安全的CUDA 3或更高版本):通常情况下,当目标是尽量和卡上分配每一个可用的字节,我会做这样的事情。隐含地假设没有一个明显的大分配问题来源适用于此(32位主机操作系统,未启用TCC模式的WDDM Windows平台,以前已知的驱动程序问题)。

相关问题