2012-01-18 41 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平臺,以前已知的驅動程序問題)。

相關問題