2012-10-25 57 views
2

我想知道是否有人可以在內核中的這種行爲與新的運營商提供一些線索..以下是代碼新的運營商..奇怪的行爲

#include <stdio.h> 
#include "cuda_runtime.h" 
#include "cuComplex.h" 
using namespace std; 
__global__ void test() 
{ 

    cuComplex *store; 
    store= new cuComplex[30000]; 
    if (store==NULL) printf("Unable to allocate %i\n",blockIdx.y); 
    delete store; 
    if (threadIdx.x==10000) store->x=0.0; 
} 

int main(int argc, char *argv[]) 
{ 
    float timestamp; 
    cudaEvent_t event_start,event_stop; 
    // Initialise 


    cudaEventCreate(&event_start); 
    cudaEventCreate(&event_stop); 
    cudaEventRecord(event_start, 0); 
    dim3 threadsPerBlock; 
    dim3 blocks; 
    threadsPerBlock.x=1; 
    threadsPerBlock.y=1; 
    threadsPerBlock.z=1; 
    blocks.x=1; 
    blocks.y=500; 
    blocks.z=1; 

    cudaEventRecord(event_start); 
    test<<<blocks,threadsPerBlock,0>>>(); 
    cudaEventRecord(event_stop, 0); 
    cudaEventSynchronize(event_stop); 
    cudaEventElapsedTime(&timestamp, event_start, event_stop); 
    printf("test took %fms \n", timestamp); 
} 

在GTX680 Cuda的5運行此和調查輸出將會注意到,隨機存儲器未分配:(我在想,也許這是因爲所有的全局存儲器完成,但我有2GB的內存和積極以來塊的最高金額爲16的內存分配量此方法應該在最大爲16 * 30000 * 8 = 38.4x10e6 ..即約38MB,所以我還需要考慮什麼?

+0

分配應該是'16 * 30000 *的sizeof(cuComplex)' – pQB

+0

還要注意的是'threadIdx.x'將內存永遠不會等於'10000' – pQB

+0

這可能只是運行時堆中的內存碎片。你有沒有試過增加運行時堆大小? – talonmies

回答

2

問題與malloc()和free()設備系統調用使用的堆的大小有關。有關更多詳細信息,請參見NVIDIA CUDA C編程指南中的section 3.2.9 Call Stackappendix B.16.1 Heap Memory Allocation

,如果你設置堆大小以適合你的內核要求您的測試將工作

cudaDeviceSetLimit(cudaLimitMallocHeapSize, 500*30000*sizeof(cuComplex)); 
+0

但是,這將消耗我的全部記憶...並沒有真正需要的,因爲從6塊只分配是可能的...有沒有一種方法所必須的限度更精細的計算? – Daniel

+0

變化'500'爲'併發塊X流Multiprocessors'獲得您所需要的最小尺寸,是'SM = 8'在GTX680和依賴的核心要求,在寄存器或共享內存方面的併發塊的數量。 – pQB